Example usage for com.mongodb DBCollection insert

List of usage examples for com.mongodb DBCollection insert

Introduction

In this page you can find the example usage for com.mongodb DBCollection insert.

Prototype

public WriteResult insert(final List<? extends DBObject> documents) 

Source Link

Document

Insert documents into a collection.

Usage

From source file:com.deliveronthego.CommonAPI.java

@POST
@Path("/findDrivers")
@Consumes(MediaType.APPLICATION_JSON)/*from   w w  w .  j ava  2  s.com*/
@Produces(MediaType.APPLICATION_JSON)
public Response doDeliveryDetails(String deliver) {
    System.out.println("inside delivery details");

    String message;
    GraphTraversal g = new GraphTraversal();
    try {
        System.out.println("inside delivery details");
        JSONObject deliverJson = new JSONObject(deliver);
        boolean var = new DbConnection().deliver(deliverJson.getString("emailId"),
                Double.valueOf(deliverJson.getString("pickupLatitude")),
                Double.valueOf(deliverJson.getString("pickupLongitude")),
                Double.valueOf(deliverJson.getString("dropOffLatitude")),
                Double.valueOf(deliverJson.getString("dropOffLongitude")), deliverJson.getInt("length"),
                deliverJson.getInt("breadth"), deliverJson.getInt("width"));
        System.out.println("after delivery details");

        if (var) {
            System.out.println("in success");
            message = "Delivery Details Inserted successfully";
            JSONObject statusMessage = new JSONObject();
            statusMessage.put("message", message);
            String res = g.findDriver(deliverJson.getString("emailId"), deliverJson.getDouble("pickupLatitude"),
                    deliverJson.getDouble("pickupLongitude"), deliverJson.getDouble("dropOffLatitude"),
                    deliverJson.getDouble("dropOffLongitude"));
            System.out.println(res);
            JSONArray jarr = new JSONArray(res);
            for (int j = 0; j < jarr.length(); j++) {
                JSONObject json = new JSONObject(jarr.get(j).toString());
                String driverId = json.get("driverId").toString();
                System.out.println(driverId);
                mongoclient = getConnection();
                DB db = mongoclient.getDB("deliveronthego");
                DBCollection driverSourceDetails = db.getCollection("login");
                BasicDBObject whereQuery = new BasicDBObject();
                whereQuery.put("emailId", driverId);
                whereQuery.put("userType", "Driver");
                DBCursor cursor = driverSourceDetails.find(whereQuery);
                if (cursor.hasNext()) {
                    BasicDBObject obj = (BasicDBObject) cursor.next();
                    String regId = obj.getString("regId");
                    String[] gcmIds = regId.split("\\=");
                    System.out.println(gcmIds[1]);
                    String apiKey = "AIzaSyDzXEIMFY3EGbJ4mjc9xBYyeakjggxuTC0";
                    Content content = createContent(gcmIds[1], deliverJson.getString("emailId"));

                    DotgGCMPost.post(apiKey, content);
                    DBCollection selection = db.getCollection("selection");
                    BasicDBObject sel = new BasicDBObject()
                            .append("userEmailId", deliverJson.getString("emailId"))
                            .append("driverEmailId", driverId).append("Accepted", "No");
                    selection.insert(sel);

                    throw new JSONException("gi");

                } else {
                    //System.out.println("cursor=="+cursor.toString());
                    throw new JSONException("No email found");
                }
            }
            return Response.status(200).entity(statusMessage).build();
        } else {
            System.out.println("in fail");

            message = "Delivery Details Insertion Failed";
            JSONObject statusMessage = new JSONObject();
            statusMessage.put("message", message);
            return Response.status(404).entity(statusMessage).build();
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    message = "Delivery Details Insertion Failed";
    return Response.status(404).entity(message).build();
}

From source file:com.deliveronthego.DbConnection.java

public String customerSignup(String firstName, String lastName, String emailId, String password,
        int phoneNumber, String regId) {
    mongoclient = getConnection();/*from  w  w  w  .  ja  v a2  s. c o m*/
    @SuppressWarnings("deprecation")
    DB database = mongoclient.getDB("deliveronthego");
    DBCollection customerSignUpInfo = database.getCollection("login");
    if (emailId.contains("@") && (password.length() <= 8) && (String.valueOf(phoneNumber).length() == 10)) {
        BasicDBObject customerSignUpInfoObj = new BasicDBObject("emailId", emailId).append("password",
                password);

        DBCursor customerSignUpInfoCur = customerSignUpInfo.find(customerSignUpInfoObj);
        if (customerSignUpInfoCur.hasNext()) {
            return "Customer Sign Up Info Already Exists";
        } else {
            customerSignUpInfoObj.append("firstName", firstName).append("lastName", lastName)
                    .append("phoneNumber", phoneNumber).append("userType", "User").append("regId", regId);
            customerSignUpInfo.insert(customerSignUpInfoObj);
            return "Customer Signup Info inserted successfully";
        }
    } else {
        return "Customer SignUp Info failed to insert";
    }
}

From source file:com.deliveronthego.DbConnection.java

public String driverSignup(String firstName, String lastName, String driverLicense, String emailId,
        String password, int phoneNumber, String regId) {
    mongoclient = getConnection();/*from  ww  w.j a va2  s .c  o m*/
    DB db = mongoclient.getDB("deliveronthego");
    DBCollection driverSignUpInfo = db.getCollection("login");
    if (emailId.contains("@") && (password.length() <= 8) && (String.valueOf(phoneNumber).length() == 10)
            && !driverLicense.isEmpty()) {
        BasicDBObject driverSignUpInfoObj = new BasicDBObject("driverLicense", driverLicense)
                .append("emailId", emailId).append("password", password);

        DBCursor driverSignUpInfoCur = driverSignUpInfo.find(driverSignUpInfoObj);
        if (driverSignUpInfoCur.hasNext()) {
            return "Driver Sign Up Info Already Exists";
        } else {
            driverSignUpInfoObj.append("firstName", firstName).append("lastName", lastName)
                    .append("phoneNumber", phoneNumber).append("userType", "Driver").append("regId", regId);
            driverSignUpInfo.insert(driverSignUpInfoObj);
            return "Driver Sign Up Info Inserted Successfully";
        }
    } else {
        return "Driver SignUp Info failed to insert";
    }
}

From source file:com.deliveronthego.DbConnection.java

public boolean deliver(String emailId, Double pickupLatitude, Double pickupLongitude, Double dropOffLatitude,
        Double dropOffLongitude, int length, int breadth, int width) {
    mongoclient = getConnection();// w  w w. ja va  2  s  .c  o  m
    @SuppressWarnings("deprecation")
    DB db = mongoclient.getDB("deliveronthego");
    DBCollection delivery = db.getCollection("delivery");
    BasicDBObject logObj = new BasicDBObject("emailId", emailId).append("pickupLatitude", pickupLatitude)
            .append("pickupLongitude", pickupLongitude).append("dropOffLatitude", dropOffLatitude)
            .append("dropOffLongitude", dropOffLongitude).append("length", length).append("breadth", breadth)
            .append("width", width);

    delivery.insert(logObj);

    return true;
}

From source file:com.deliveronthego.DbConnection.java

public String location(String date, double transitionLatitude, double transitionLongitude, double stopLatitude,
        double stopLongitude, String driverId) {
    mongoclient = getConnection();//from   w w w . j av a  2 s.  c om
    @SuppressWarnings("deprecation")
    DB db = mongoclient.getDB("deliveronthego");
    DBCollection location = db.getCollection("location");
    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("driverID", driverId);
    DBCursor locationCursor = location.find(whereQuery);
    boolean updateFlag = false;

    while (locationCursor.hasNext()) {
        locationCursor.next();
        DBObject locDB = locationCursor.curr();
        System.out.println(locDB);
        String dateStr = locDB.get("Date").toString();
        if (dateStr.equals(date)) {
            location.update(new BasicDBObject("driverID", driverId),
                    new BasicDBObject("$set", new BasicDBObject("transitionLatitude", transitionLatitude)
                            .append("transitionLongitude", transitionLongitude)));
            Double previousStopLatitude = (Double) locDB.get("stopLatitude");
            Double previousStopLongitude = (Double) locDB.get("stopLongitude");
            System.out.println("previousStopLatitude: " + previousStopLatitude);
            System.out.println("previousStopLongitude: " + previousStopLongitude);
            /*if((previousStopLatitude==0.0) && (previousStopLongitude==0.0))
            {
               location.update(new BasicDBObject("driverID", driverId), new BasicDBObject("$set", new BasicDBObject("stopLatitude", stopLatitude).append("stopLongitude", stopLongitude)));
            }*/
        }
        updateFlag = true;
    }

    if (!updateFlag) {
        BasicDBObject locationObj = new BasicDBObject("Date", date.toString())
                .append("transitionLatitude", transitionLatitude)
                .append("transitionLongitude", transitionLongitude).append("stopLatitude", stopLatitude)
                .append("stopLongitude", stopLongitude).append("driverID", driverId);
        location.insert(locationObj);
        return "Location Details Inserted Sucessfully";
    } else {
        return "Location Details Updated";
    }

}

From source file:com.deliveronthego.DbConnection.java

public String transcationNotification(String driverID, Boolean pickedUp, Boolean delivered) {
    mongoclient = getConnection();/*from   www .  j ava 2s.  c o m*/
    @SuppressWarnings("deprecation")
    DB db = mongoclient.getDB("deliveronthego");
    DBCollection notification = db.getCollection("notification");

    BasicDBObject notificationObj = new BasicDBObject();
    notificationObj.append("driverID", driverID);
    Date date = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    DBCursor notificationCursor = notification.find(notificationObj);

    if (pickedUp && !delivered) {
        notificationObj.append("pickedUp", pickedUp.toString()).append("delivered", delivered.toString())
                .append("date", cal.toString());
        notification.insert(notificationObj);

        return "New Transaction Data inserted";
    } else {
        if (notificationCursor.hasNext()) {
            notificationCursor.next();
            DBObject notifyObj = notificationCursor.curr();
            Date currentDateInDatabase = (Date) notifyObj.get("date");
            if (!(boolean) notifyObj.get("delivered") && currentDateInDatabase.before(date)) {
                notification.update(new BasicDBObject("driverID", driverID),
                        new BasicDBObject("$set", new BasicDBObject("delivered", delivered.toString())));
                return "Transaction Completed";
            } else {
                return "Transaction failed to update";
            }
        } else {
            return "Transaction failed";
        }
    }
}

From source file:com.ebay.cloud.cms.config.CMSProperties.java

License:Apache License

public void updateConfig(Map<String, Object> configs) {
    DBCollection coll = getPropertiesCollection(ds.getMongoInstance());
    DBCursor cursor = coll.find();//from w ww.j a va2 s.co  m
    // update existing
    while (cursor.hasNext()) {
        BasicDBObject dbo = (BasicDBObject) cursor.next();
        String key = getKey(dbo);
        if (!configs.containsKey(key)) {
            continue;
        }

        BasicDBObject qObject = new BasicDBObject();
        BasicDBObject vObject = new BasicDBObject();
        qObject.append("_id", dbo.get("_id"));
        vObject.append(key, configs.get(key));

        coll.update(qObject, vObject);
        configs.remove(key);
    }

    // insert new config
    if (!configs.isEmpty()) {
        List<DBObject> list = new ArrayList<DBObject>();
        for (Entry<String, Object> entry : configs.entrySet()) {
            DBObject dbo = new BasicDBObject();
            dbo.put(entry.getKey(), entry.getValue());

            list.add(dbo);
        }
        coll.insert(list);
    }

    loadProperties(ds.getMongoInstance());
}

From source file:com.ebay.cloud.cms.dal.persistence.MongoExecutor.java

License:Apache License

public static WriteResult insert(PersistenceContext context, MetaClass metadata, DBObject insertObject) {
    long start = System.currentTimeMillis();
    WriteResult writeResult = null;/*from w  ww  .  j a  v a  2s.  c om*/
    String msg = "success";
    DBCollection dbCollection = context.getDBCollection(metadata);
    try {
        writeResult = dbCollection.insert(insertObject);
    } catch (Throwable t) {
        msg = t.getMessage();
        handleMongoException(t);
    } finally {
        logMongoAction(context, "insert", start, dbCollection, null, insertObject, null, null, msg);
    }

    return writeResult;
}

From source file:com.ebay.cloud.cms.metadata.dataloader.MetadataDataLoader.java

License:Apache License

public void loadProperties(InputStream is) {
    DBCollection propertiesCollection = mongo.getDB(CMSConsts.SYS_DB)
            .getCollection(CMSConsts.PROPERTIES_COLLECTION);

    //load properties collection
    BasicDBObject cmsProperties = (BasicDBObject) loadBasonFromFile(is);
    for (String key : cmsProperties.keySet()) {
        DBObject obj = new BasicDBObject().append(key, cmsProperties.get(key));
        propertiesCollection.insert(obj);
    }/*from w  ww . j a  v a 2  s  .  co m*/
}

From source file:com.ebay.jetstream.config.mongo.MongoDAO.java

License:MIT License

/**
 * UPLOAD TO DB/*  w  ww  .j a  va  2s  . c  om*/
 */
public static void insertJetStreamConfiguration(BasicDBObject dbObject, MongoConnection mongoConnection) {
    JetStreamBeanConfigurationDo beanConfig = null;
    DBCollection dbCol = mongoConnection.getDBCollection();

    if (dbCol == null) {
        throw new MongoConfigRuntimeException("jetstreamconfig collection is unknown");
    }

    WriteResult result = dbCol.insert(dbObject);
    if (result.getError() != null) {
        throw new MongoConfigRuntimeException(result.getError());
    }
}