Example usage for com.mongodb DBObject get

List of usage examples for com.mongodb DBObject get

Introduction

In this page you can find the example usage for com.mongodb DBObject get.

Prototype

Object get(String key);

Source Link

Document

Gets a field from this object by a given name.

Usage

From source file:com.deafgoat.ml.prognosticator.MongoResult.java

License:Apache License

/**
 * Reads a stored WEKA classifier model from the database
 * //w  w w.j  a v a 2  s.c o m
 * @param modelName
 *            The name of the model to read from the database
 * @throws IOException
 * @throws ClassNotFoundException
 */
public Classifier readModel(String modelName) throws IOException, ClassNotFoundException {
    DBObject query = new BasicDBObject();
    query.put("name", modelName);
    DBObject dbObj = _collection.findOne(query);
    if (dbObj == null) {
        return null;
    }
    ByteArrayInputStream bis = new ByteArrayInputStream((byte[]) dbObj.get("serializedModelBytes"));
    ObjectInputStream ois = new ObjectInputStream(bis);
    return (Classifier) ois.readObject();
}

From source file:com.deliveronthego.DbConnection.java

public boolean login(String emailId, String password, String userType) {
    mongoclient = getConnection();//from ww  w  .ja  v  a 2  s  .c o  m
    @SuppressWarnings("deprecation")
    DB db = mongoclient.getDB("deliveronthego");
    DBCollection login = db.getCollection("login");
    if (emailId.contains("@")) {
        BasicDBObject logInObj = new BasicDBObject();
        logInObj.put("emailId", emailId);
        DBCursor logInCursor = login.find(logInObj);

        while (logInCursor.hasNext()) {
            logInCursor.next();
            DBObject userDetailObj = logInCursor.curr();
            if (userDetailObj != null) {
                String logInPassword = userDetailObj.get("password").toString();
                String loginUserType = userDetailObj.get("userType").toString();
                System.out.println(logInPassword);
                if ((logInPassword != null) && (logInPassword.equalsIgnoreCase(password))
                        && (loginUserType != null) && (loginUserType.equalsIgnoreCase(userType))) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }

        }
        return true;
    } else {
        return false;
    }
}

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 a2 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   ww  w  . j av a  2  s .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.dhamacher.addressbook.DBController.java

License:Open Source License

/**
 * This methods provides the data that is then published in the JList
 * component of the user view [Main.class]
 *
 * @return An object array for the JList component in the user view
 *///from  ww  w .j av  a  2s . c o m
protected Object[] getContacts() {
    if (collection.count() == 0) {
        Object[] data = { "" };
        return data;
    } else {
        /* Use the database cursor to iterate trough the collection */
        DBCursor cursor = collection.find();

        /* New array based on the size of the collection */
        Object[] data = new Object[(int) collection.count()];
        int i = 0;
        while (cursor.hasNext()) {
            DBObject document = cursor.next();
            data[i] = document.get("first_name") + " " + document.get("last_name");
            i++;
        }
        cursor.close();
        return data;
    }
}

From source file:com.dhamacher.addressbook.DBController.java

License:Open Source License

/**
 * This methods is used for contact lookup in the database. The parameter 
 * comes from the selected item from the JList component
 * @param fullname The full name of the contact.
 * @return Contact instance/*from  ww w .  ja v a2  s .c om*/
 */
protected Contact getContact(String fullname) {
    String[] name = fullname.split(" ");
    BasicDBObject query = new BasicDBObject();
    query.put("first_name", name[0]);
    query.put("last_name", name[1]);
    DBObject document = collection.findOne(query);
    Contact contact = new Contact.Builder((String) document.get("first_name"),
            (String) document.get("last_name")).homePhone((String) document.get("home_phone"))
                    .cellPhone((String) document.get("cell_phone")).email((String) document.get("email"))
                    .relationship((String) document.get("relationship")).notes((String) document.get("notes"))
                    .build();
    return contact;
}

From source file:com.dilmus.dilshad.scabi.deprecated.DDBOld.java

License:Open Source License

public ArrayList<String> fieldNames(DTableOld table) throws DScabiException {
    if (null == table) {
        throw new DScabiException("Table is null", "DBD.FNS.1");
    }//from w w  w .j a v a 2 s  .c  o  m
    log.debug("fieldNamesUsingFindOne() table.count() is {}", table.count());
    if (table.count() <= 0) {
        return null;
    }
    String map = "function() { for (var key in this) { emit(key, null); } }";
    String reduce = "function(key, stuff) { return null; }";

    MapReduceCommand cmd = new MapReduceCommand(table.getCollection(), map, reduce, null,
            MapReduceCommand.OutputType.INLINE, null);
    MapReduceOutput out = table.getCollection().mapReduce(cmd);
    //if 4th param output collection name is used above, String s = out.getOutputCollection().distinct("_id").toString();
    //if 4th param output collection name is used above, System.out.println("out.getOutputCollection().distinct " + s);
    ArrayList<String> fieldNames = new ArrayList<String>();
    for (DBObject o : out.results()) {
        log.debug("fieldNames() Key, value is : {}", o.toString());
        log.debug("fieldNames() Key name is : {}", o.get("_id").toString());
        if (false == o.get("_id").toString().equals("_id"))
            fieldNames.add(o.get("_id").toString());
    }
    return fieldNames;

}

From source file:com.dilmus.dilshad.scabi.deprecated.DDBOld.java

License:Open Source License

public ArrayList<String> fieldNamesUsingFindOne(DTableOld table) throws DScabiException {
    if (null == table) {
        throw new DScabiException("Table is null", "DBD.FNU.1");
    }//from  w w  w .j a  va 2  s .c o  m
    log.debug("fieldNamesUsingFindOne() table.count() is {}", table.count());
    if (table.count() <= 0) {
        return null;
    }
    DBObject out = table.getCollection().findOne();
    ArrayList<String> fieldNames = new ArrayList<String>();
    Set<String> st = out.keySet();
    for (String s : st) {
        log.debug("fieldNamesUsingFindOne() value is : {}", out.get(s));
        log.debug("fieldNamesUsingFindOne() Key name is : {}", s);
        if (false == s.equals("_id"))
            fieldNames.add(s);
    }
    return fieldNames;
}

From source file:com.dilmus.dilshad.scabi.deprecated.DTableOld.java

License:Open Source License

public ArrayList<String> fieldNames() throws DScabiException {
    if (null == m_tableName) {
        throw new DScabiException("Table name is null", "DBT.FNS.1");
    }/*  ww w  .j  av a  2 s . c  o m*/
    if (null == m_table) {
        throw new DScabiException("Table is null", "DBT.FNS.2");
    }
    log.debug("fieldNames() firstTime is {}", m_firstTime);
    log.debug("fieldNames() table.count() is {}", m_table.count());
    if (m_table.count() <= 0) {
        return null;
    }
    if (m_firstTime) {
        String map = "function() { for (var key in this) { emit(key, null); } }";
        String reduce = "function(key, s) { return null; }";

        MapReduceCommand cmd = new MapReduceCommand(m_table, map, reduce, null,
                MapReduceCommand.OutputType.INLINE, null);
        MapReduceOutput out = m_table.mapReduce(cmd);
        //if 4th param output collection name is used above, String s = out.getOutputCollection().distinct("_id").toString();
        //if 4th param output collection name is used above, System.out.println("out.getOutputCollection().distinct " + s);
        m_fieldNames = new ArrayList<String>();
        for (DBObject o : out.results()) {
            log.debug("fieldNames() Key, value is : {}", o.toString());
            log.debug("fieldNames() Key name is : {}", o.get("_id").toString());
            if (false == o.get("_id").toString().equals("_id"))
                m_fieldNames.add(o.get("_id").toString());
        }
        m_firstTime = false;
        return m_fieldNames;
    }
    return m_fieldNames;
}

From source file:com.dilmus.dilshad.scabi.deprecated.DTableOld.java

License:Open Source License

public ArrayList<String> fieldNamesUsingFindOne() throws DScabiException {
    if (null == m_tableName) {
        throw new DScabiException("Table name is null", "DBT.FNU.1");
    }//from w  w w. j av a 2  s .c  o  m
    if (null == m_table) {
        throw new DScabiException("Table is null", "DBT.FNU.2");
    }
    log.debug("fieldNamesUsingFindOne() firstTime is {}", m_firstTime);
    log.debug("fieldNamesUsingFindOne() table.count() is {}", m_table.count());
    if (m_table.count() <= 0) {
        return null;
    }
    if (m_firstTime) {
        DBObject out = m_table.findOne();
        m_fieldNames = new ArrayList<String>();
        Set<String> st = out.keySet();
        for (String s : st) {
            log.debug("fieldNamesUsingFindOne() value is : {}", out.get(s));
            log.debug("fieldNamesUsingFindOne() Key name is : {}", s);
            if (false == s.equals("_id"))
                m_fieldNames.add(s);
        }
        m_firstTime = false;
        return m_fieldNames;
    }
    return m_fieldNames;
}