Example usage for com.mongodb BasicDBObject BasicDBObject

List of usage examples for com.mongodb BasicDBObject BasicDBObject

Introduction

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

Prototype

public BasicDBObject() 

Source Link

Document

Creates an empty object.

Usage

From source file:act.server.MongoDB.java

License:Open Source License

public void updateMetadata(Seq seq) {
    BasicDBObject query = new BasicDBObject().append("_id", seq.getUUID());
    DBObject obj = this.dbSeq.findOne(query);
    obj.put("metadata", MongoDBToJSON.conv(seq.getMetadata()));
    this.dbSeq.update(query, obj);
}

From source file:act.server.MongoDB.java

License:Open Source License

public void updateReferences(Seq seq) {
    BasicDBObject query = new BasicDBObject().append("_id", seq.getUUID());
    DBObject obj = this.dbSeq.findOne(query);
    BasicDBList refs = new BasicDBList();

    List<DBObject> newReferences = new ArrayList<>();
    for (JSONObject ref : seq.getReferences()) {
        newReferences.add(MongoDBToJSON.conv(ref));
    }//from  ww  w.  j a  v  a 2  s .  co  m

    refs.addAll(newReferences);
    obj.put("references", refs);
    this.dbSeq.update(query, obj);
}

From source file:act.server.MongoDB.java

License:Open Source License

public void updateRxnRefs(Seq seq) {
    BasicDBObject query = new BasicDBObject().append("_id", seq.getUUID());
    DBObject obj = this.dbSeq.findOne(query);
    obj.put("rxn_refs", seq.getReactionsCatalyzed());
    this.dbSeq.update(query, obj);
}

From source file:act.server.MongoDB.java

License:Open Source License

/**
 * graphByOrganism() returns a list of all reactionIDs containing the given organismID.
 *
 * @param organismID/*  w w  w .  j a  v a2s. co m*/
 * @return List<Long> List of reaction IDs for given organismID
 */
public List<Long> graphByOrganism(Long organismID) {

    DBObject query = new BasicDBObject();
    if (organismID == null || organismID > -1)
        query.put("organisms.id", organismID);
    List<Long> graphList = new ArrayList<Long>();

    DBCursor reactionCursor = this.dbReactions.find(query);
    for (DBObject i : reactionCursor) {
        graphList.add(((Integer) i.get("_id")).longValue()); // checked: db type IS int
    }
    return graphList;
}

From source file:act.server.MongoDB.java

License:Open Source License

/**
 * getOrganisms() returns a list of all unique species IDs in database
 * mapped to itself, its parents, and descendants
 *
 * @return//w w  w. java 2s. co  m
 */
@SuppressWarnings("unchecked")
public Map<Long, Set<Long>> getOrganisms() {
    List<Long> ids = (List<Long>) this.dbReactions.distinct("organisms.id");
    //map species id to all ids associated with it
    Map<Long, Set<Long>> speciesIDs = new HashMap<Long, Set<Long>>();
    for (Long organismID : ids) {
        //check if organism id on species level
        List<Long> idsToAdd = new ArrayList<Long>();
        Long speciesID;
        DBObject orgQuery = new BasicDBObject();
        orgQuery.put("_id", organismID);
        DBObject org = dbOrganisms.findOne(orgQuery);
        String rank = (String) org.get("rank");
        Long parent = (Long) org.get("parent_id"); // checked: db type IS long
        speciesID = null;
        while (organismID != 1) {
            idsToAdd.add(organismID);
            if (rank.equals("species")) {
                speciesID = organismID;
                //break;
            }
            orgQuery.put("_id", parent);
            org = dbOrganisms.findOne(orgQuery);
            organismID = parent;
            rank = (String) org.get("rank");
            parent = (Long) org.get("parent_id"); // checked: db type IS long
        }
        if (speciesID == null)
            continue;
        if (!speciesIDs.containsKey(speciesID)) {
            speciesIDs.put(speciesID, new HashSet<Long>());
        }
        speciesIDs.get(speciesID).addAll(idsToAdd);
    }
    return speciesIDs;
}

From source file:act.server.MongoDB.java

License:Open Source License

/**
 * Following methods are related to Bing cross-references installation in the Installer DB along with various
 * queries to obtain names (aka synonyms)
 *///from w w w  . jav a  2  s .  c o  m

public BasicDBObject createBingMetadataDoc(Set<UsageTermUrlSet> usageTerms, Long totalCountSearchResults,
        String bestName) {
    BasicDBObject metadata = new BasicDBObject();
    if (usageTerms != null) {
        BasicDBList usageTermsDBObject = new BasicDBList();
        for (UsageTermUrlSet usageTerm : usageTerms) {
            // What happens if you don't translate to basic db obj in the next line?
            usageTermsDBObject.add(usageTerm.getBasicDBObject());
        }
        metadata.put("usage_terms", usageTermsDBObject);
    }
    if (totalCountSearchResults >= 0) {
        metadata.put("total_count_search_results", totalCountSearchResults);
    }
    if (!bestName.equals("")) {
        metadata.put("best_name", bestName);
    }
    return metadata;
}

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

License:Open Source License

public static DBObject conv(JSONObject o) {
    BasicDBObject result = new BasicDBObject();
    try {//www.jav  a2 s. c  o m
        Iterator i = o.keys();
        while (i.hasNext()) {
            String k = (String) i.next();
            Object v = o.get(k);
            if (v instanceof JSONArray) {
                result.put(k, conv((JSONArray) v));
            } else if (v instanceof JSONObject) {
                result.put(k, conv((JSONObject) v));
            } else {
                result.put(k, v);
            }
        }
        return result;
    } catch (JSONException je) {
        return null;
    }
}

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

License:Open Source License

private void process(XMLStreamReader xml) throws XMLStreamException {
    String tag;/*from  w  w  w  .j  a v a  2 s  . c om*/
    String root = null;
    Stack<DBObject> json = new Stack<DBObject>();
    DBObject js;
    while (xml.hasNext()) {
        int eventType = xml.next();
        while (xml.isWhiteSpace() || eventType == XMLEvent.SPACE)
            eventType = xml.next();

        switch (eventType) {
        case XMLEvent.START_ELEMENT:
            tag = xml.getLocalName();
            if (root == null) {
                root = tag;
            } else {
                json.push(new BasicDBObject());
            }
            break;
        case XMLEvent.END_ELEMENT:
            tag = xml.getLocalName();
            if (tag.equals(root)) {
                // will terminate in next iteration
            } else {
                js = json.pop();
                if (json.size() == 0) {
                    if (tag.equals(rowTag))
                        printEntry(js);
                    else
                        printUnwantedEntry(js);
                } else {
                    putListStrOrJSON(json.peek(), tag, js);
                }
            }
            break;

        case XMLEvent.CHARACTERS:
            String txt = xml.getText();
            js = json.peek();
            if (js.containsField(strTag)) {
                txt = js.get(strTag) + txt;
                js.removeField(strTag);
            }
            js.put(strTag, txt);
            break;

        case XMLEvent.START_DOCUMENT:
            break;
        case XMLEvent.END_DOCUMENT:
            break;
        case XMLEvent.COMMENT:
        case XMLEvent.ENTITY_REFERENCE:
        case XMLEvent.ATTRIBUTE:
        case XMLEvent.PROCESSING_INSTRUCTION:
        case XMLEvent.DTD:
        case XMLEvent.CDATA:
        case XMLEvent.SPACE:
            System.out.format("%s --\n", eventType);
            break;
        }
    }
}

From source file:AdminServer.ServiceS.java

private void check_query(queryS order) {
    try {/*  w w w  .  j  a v  a  2  s  .c o  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   w  w w. j  a  va 2 s .c om*/
    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);
    }
}