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:act.shared.helpers.XMLToImportantChemicals.java

License:Open Source License

private void putElemOrList(DBObject json, String tag, Object add) {
    // if tag already present then make it a list
    if (json.containsField(tag)) {
        BasicDBList l;//from ww w  . java2  s.  c o m
        Object already = json.get(tag);
        if (already instanceof BasicDBList) {
            l = (BasicDBList) already;
        } else {
            l = new BasicDBList();
            l.add(already);
        }
        l.add(add);
        json.removeField(tag);
        json.put(tag, l);
        return;
    }

    // else, just add as is
    json.put(tag, add);
    return;
}

From source file:act.shared.helpers.XMLToImportantChemicals.java

License:Open Source License

private void printEntry(DBObject json) {
    Object ido = json.get(idTag);
    Object caso = json.get(chemTag);
    String id = ido instanceof String ? (String) ido : "";
    String cas = caso instanceof String ? (String) caso : "";
    String inchi = getInchi(json);
    if (inchi != null)
        System.out.format("%s\t%s\t%s\t%s\n", this.DBS, id, inchi, JSON.serialize(json));
    else//  w w w.  j  av a 2  s  .  co m
        System.out.format("%s\t%s\t%s\t%s\n", this.DBS, id, cas, JSON.serialize(json));
}

From source file:act.shared.helpers.XMLToImportantChemicals.java

License:Open Source License

private String getInchi(DBObject json) {
    // under calculated-properties.property.[{kind=InChI, value=<inchi>}]
    DBObject o;// w  ww  .  j  a  va  2s  .  c o m
    if (json.containsField("calculated-properties")) {
        o = (DBObject) json.get("calculated-properties");
        if (o.containsField("property")) {
            o = (DBObject) o.get("property");
            if (o instanceof BasicDBList) {
                for (Object kv : (BasicDBList) o) {
                    o = (DBObject) kv;
                    if (o.containsField("kind") && o.get("kind").equals("InChI") && o.containsField("value")) {
                        return (String) o.get("value");
                    }
                }
            }
        }
    }
    return null;
}

From source file:AdminServer.ServiceS.java

private ArrayList getData(DBCollection collection, methodS type, String name) {
    ArrayList data = new ArrayList<>();
    DBCursor cursor;//from ww w .  j  a  va  2 s .co  m

    if (type == methodS.ATM) {
        cursor = collection.find();
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            String no = obj.get("SerialNum").toString();
            String ip = obj.get("ipAddr").toString();
            String money = obj.get("money").toString();
            String usr = obj.get("currentUser").toString();
            data.add(new tellerD(no, ip, money, usr));
        }
    } else if (type == methodS.USER) {
        cursor = collection.find(new BasicDBObject("name", name));
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            String usr = obj.get("name").toString();
            boolean ip = (boolean) obj.get("locked");
            data.add(new userD(usr, ip));
        }
    } else if (type == methodS.TRADE) {
        cursor = collection.find(new BasicDBObject("ipAddr", name));
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            String no = obj.get("serial").toString();
            String ip = obj.get("ipAddr").toString();
            String method = obj.get("type").toString();
            String usr = obj.get("name").toString();
            String delta = obj.get("delta").toString();
            String remains = obj.get("remains").toString();
            String date = obj.get("time").toString();
            data.add(new tradeD(no, ip, usr, method, delta, remains, date));
        }
    }
    return data;
}

From source file:AdminServer.ServiceS.java

private void check_query(queryS order) {
    try {/*w  w  w .  j a v a2  s .co m*/
        if (order.type == methodS.UNLOCK) {
            BasicDBObject user = new BasicDBObject().append("name", order.getName());
            DBObject obj = users.findOne(user);
            if (obj == null || (!(boolean) obj.get("locked"))) {
                output.writeObject(new queryS(false, "WTF!"));
                output.flush();
            } else {
                users.update(user,
                        new BasicDBObject().append("$set", new BasicDBObject().append("locked", false)));
                output.writeObject(new queryS(true, "UNLOCKED"));
                output.flush();
            }
        }

        else if (order.type == methodS.RESET) {
            DBObject admin = admins.findOne(currentUser);
            String pwd = admin.get("pwd").toString();
            if (order.getPwd0().equals(pwd) && (order.getPwd1() != null)) {
                admins.update(currentUser,
                        new BasicDBObject().append("$set", new BasicDBObject().append("pwd", order.getPwd1())));
                output.writeObject(new queryS(true, "Success"));
                output.flush();
            } else {
                output.writeObject(new queryS(false, "wrongPwd"));
                output.flush();
            }
        }

        else if (order.type == methodS.UPDATE) {

        }

        else {
            ArrayList tmp = new ArrayList<>();
            if (order.type == methodS.ATM) {
                tmp = (ArrayList<tellerD>) getData(clients, order.type, order.getName());
            } else if (order.type == methodS.USER) {
                tmp = (ArrayList<userD>) getData(users, order.type, order.getName());
            } else if (order.type == methodS.TRADE) {
                tmp = (ArrayList<tradeD>) getData(records, order.type, order.getName());
            }

            if (tmp.isEmpty()) {
                output.writeObject(new queryS(false, order.type, null));
                output.flush();
            } else {
                output.writeObject(new queryS(true, order.type, tmp));
                output.flush();
            }

        }
    } catch (IOException ex) {
        Logger.getLogger(ServiceS.class.getName()).log(Level.SEVERE, null, ex);
        this.DBShutdown();
    }
}

From source file:AdminServer.ServiceS.java

private void check_login(LoginS order) {
    String name = order.getName();
    String pwd = order.getPwd();//from  ww  w  . j  a  va  2 s. c  o m
    BasicDBObject user = new BasicDBObject().append("name", name);
    DBObject obj = admins.findOne(user);

    try {
        if (obj == null) {
            output.writeObject(new LoginS(false, "NO_SUCH_USER"));
            output.flush();
        } else if ((boolean) obj.get("checked")) {
            output.writeObject(new LoginS(false, "USER_CHECKED"));
            output.flush();
        } else if ((boolean) obj.get("locked")) {
            output.writeObject(new LoginS(false, "USER_LOCKED"));
            output.flush();
        } else if (!pwd.equals(obj.get("pwd").toString())) {
            output.writeObject(new LoginS(false, "WRONG_PASSWORD"));
            output.flush();
        } else {
            BasicDBObject setting = new BasicDBObject().append("checked", true).append("Last_Time",
                    this.formatter.format(new Date()));
            this.currentUser = user;
            admins.update(user, new BasicDBObject().append("$set", setting));

            output.writeObject(new LoginS(true, obj.get("Last_Time").toString()));
            output.flush();
            System.out.println(user.get("name") + " logs @" + this.serialNum);
        }
    } catch (IOException ex) {
        Logger.getLogger(ServiceS.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:analyzers.DebugAnalyzer.java

License:Apache License

/**
* This method extracts tweets from the JSON file
*
* @param    n             the number of tweets to analyze
* @param    fis           stream for tweets
* @param    jsonFile      JSON file/*from   w w  w  .  j  ava2  s .  c o m*/
* @param    analyzer      the analyzer 
* @throws   IOException   cannot open file
*/
public static void extractTweets(int n, FileInputStream fis, File jsonFile, Analyzer analyzer)
        throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8));
    TwitterTextValueReader reader = new TwitterTextValueReader(br);

    while (true) {
        // read the next tweet from the file
        int tweetLen = reader.read(cbuf, 0, MAX_CHARS);
        if (-1 == tweetLen)
            break;

        DBObject tweetJSON = (DBObject) JSON.parse(new String(cbuf, 0, tweetLen));
        String tweet = tweetJSON.get("text").toString();

        System.out.printf("[%d]\t%s\n", (counter + 1), tweet);

        showAnalysisFromStream(new StringReader(tweet), analyzer);

        System.out.println();

        ++counter;
        if (counter >= n)
            break;
    }
}

From source file:app.model.Model.java

public void setTable(JTable table, String collection) {
    DBCollection col = this.getCollection(collection);
    DBCursor cur = col.find();/*from   ww  w.  j a  v  a 2s.  c  o  m*/

    String[] columnNames = { "id", "CIDADE", "UF" };
    DefaultTableModel model = new DefaultTableModel(columnNames, 0);

    while (cur.hasNext()) {
        DBObject obj = cur.next();
        String cidade = (String) obj.get("CIDADE");
        String uf = (String) obj.get("UF");
        ObjectId id = (ObjectId) obj.get("_id");
        model.addRow(new Object[] { id, cidade, uf });
    }
    cur.close();
    table.setModel(model);
}

From source file:app.ui.Init.java

public void setTable() {
    String collection = cbDelta.getSelectedItem().toString().trim() + "_formated_"
            + cbOL.getSelectedItem().toString().toLowerCase().replace("-", "");
    collection = collection.replace("rollup", "dice_rollup");
    collection = collection.replace("drilldown", "dice_drilldown_formated");
    DBCollection col = model.getCollection(collection);
    DBCursor cur = col.find();/*from ww w  .  j a v  a  2  s  . co  m*/

    String[] columnNames = Util.getAttsNames(collection);
    String[] pseudoColumns = new String[columnNames.length + 1];
    String[] values = new String[columnNames.length + 1];

    for (int i = 0; i < columnNames.length; i++) {
        pseudoColumns[i] = columnNames[i];
    }
    pseudoColumns[pseudoColumns.length - 1] = lsM.getSelectedValue().toString().trim();

    DefaultTableModel model = new DefaultTableModel(pseudoColumns, 0);

    for (int i = 0; i < pseudoColumns.length; i++) {
        pseudoColumns[i] = "_key" + (1 + i);
    }
    pseudoColumns[pseudoColumns.length - 1] = "_value";

    while (cur.hasNext()) {
        DBObject obj = cur.next();

        for (int i = 0; i < values.length - 1; i++) {
            values[i] = (String) obj.get(pseudoColumns[i]);
        }
        values[values.length - 1] = obj.get(pseudoColumns[values.length - 1]).toString();
        /*String cidade = (String) obj.get("CIDADE");
        String uf = (String) obj.get("UF");
        ObjectId id = (ObjectId) obj.get("_id");
        */

        //model.addRow(new Object[]{id, cidade, uf});
        //model.addRow(new Object[]{values});
        Object[] newObj = new Object[values.length];
        //{values}
        for (int i = 0; i < values.length; i++) {
            newObj[i] = values[i];
        }
        model.addRow(newObj);
    }
    cur.close();
    tbCR.setModel(model);
}

From source file:applango.common.services.DB.mongo.mongoDB.java

public static int parseAppRankValueFromRollupRecord(
        DBObject rollupRecordAfterRollupActivitiesBeforeSettingNewWeight) {
    return Integer.parseInt(rollupRecordAfterRollupActivitiesBeforeSettingNewWeight.get("appRank").toString()
            .substring(0, rollupRecordAfterRollupActivitiesBeforeSettingNewWeight.get("appRank").toString()
                    .indexOf(".")));
}