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

private static DBObject compare(DBObject doc, DBObject docref, boolean listsAreSet) {
    boolean different = false;

    BasicDBObject diff = new BasicDBObject();
    Set<String> refKeys = new HashSet<String>();
    refKeys.addAll(docref.keySet());/*from   www .  j  a  va 2 s.co  m*/
    for (String k : doc.keySet()) {
        // as numerical calculations are improved, some computed fields are
        // bound to change: e.g., rarity and estimateEnergy
        // so make a special exception for those and ignore its val field...
        // but compare any other key recursively for differences...
        if (k.equals("rarity") || k.equals("estimateEnergy") || k.equals("coefficient"))
            continue;

        Object val = doc.get(k);

        if (!docref.containsKey(k)) {
            // this field is new
            diff.put("+" + k, val);
            different = true;
        } else {
            // field exists in old doc, recursively compare
            Object refval = docref.get(k);
            refKeys.remove(k);

            Object d;
            if ((d = compare(val, refval, listsAreSet)) != null) {
                // keys identical but values differ, add without the + or - to key
                different = true;
                diff.put(k, d);
            } else {
                // values identical and keys same too, do not put in diff.
            }
        }
    }

    // all remaining fields were deleted from old doc
    for (String kref : refKeys) {
        if (kref.equals("rarity") || kref.equals("estimateEnergy") || kref.equals("coefficient")) // see why in loop above
            continue;

        diff.put("-" + kref, docref.get(kref));
        different = true;
    }

    return different ? diff : null;

    // the following is not order invariant and therefore problematic:
    // return org.apache.commons.lang.StringUtils.difference(doc.toString(), docref.toString());
}

From source file:act.server.MongoDB.java

License:Open Source License

public BasicDBObject createCofactorDoc(Cofactor c, Long ID) {
    BasicDBObject doc = new BasicDBObject();

    doc.put("_id", ID);
    doc.put("InChI", c.getInChI());

    BasicDBList names = new BasicDBList();
    names.addAll(c.getNames());//from  www .ja va  2 s .  c o  m
    doc.put("names", names);

    return doc;
}

From source file:act.server.MongoDB.java

License:Open Source License

public void updateActChemical(Chemical c, Long id) {
    // See comment in updateActReaction about
    // db.collection.update, and $set

    BasicDBObject doc = createChemicalDoc(c, id);
    DBObject query = new BasicDBObject();
    query.put("_id", id);
    this.dbChemicals.update(query, doc);
}

From source file:act.server.MongoDB.java

License:Open Source License

public static BasicDBObject createChemicalDoc(Chemical c, Long ID) {
    BasicDBObject doc = new BasicDBObject();

    doc.put("_id", ID);

    doc.put("canonical", c.getCanon());

    doc.put("SMILES", c.getSmiles());
    doc.put("InChI", c.getInChI());
    doc.put("InChIKey", c.getInChIKey());

    doc.put("isCofactor", c.isCofactor());
    doc.put("isNative", c.isNative());

    BasicDBObject names = new BasicDBObject();
    BasicDBList synonyms = new BasicDBList();
    synonyms.addAll(c.getSynonyms());/*w  ww  .j  ava2s  .co m*/
    names.put("synonyms", synonyms);

    BasicDBList pubchemNames = new BasicDBList();

    for (String type : c.getPubchemNameTypes()) {
        String[] temp = c.getPubchemNames(type);
        BasicDBList dbNames = new BasicDBList();
        for (String t : temp) {
            dbNames.add(t);
        }
        BasicDBObject dbNameObj = new BasicDBObject();
        dbNameObj.put("type", type);
        dbNameObj.put("values", dbNames);
        pubchemNames.add(dbNameObj);
    }
    names.put("pubchem", pubchemNames);
    BasicDBList brendaNames = new BasicDBList(); // will really get its fields later if initial install
    brendaNames.addAll(c.getBrendaNames()); // but for cases where we call it post install, we construct full chem entry
    names.put("brenda", brendaNames);

    doc.put("names", names);

    BasicDBObject xrefs = new BasicDBObject();
    xrefs.put("pubchem", c.getPubchemID());
    int cnt = 0;
    for (REFS xrefTyp : Chemical.REFS.values()) {
        if (c.getRef(xrefTyp) != null) {
            xrefs.put(xrefTyp.name(), MongoDBToJSON.conv((JSONObject) c.getRef(xrefTyp)));
            cnt++;
        }
    }
    doc.put("xref", xrefs);

    doc.put("estimateEnergy", c.getEstimatedEnergy());

    doc.put("keywords", c.getKeywords());
    doc.put("keywords_case_insensitive", c.getCaseInsensitiveKeywords());

    doc.put("csid", c.getChemSpiderID());
    doc.put("num_vendors", c.getChemSpiderNumUniqueVendors());
    doc.put("vendors", MongoDBToJSON.conv(c.getChemSpiderVendorXrefs()));

    return doc;
}

From source file:act.server.MongoDB.java

License:Open Source License

private void mergeIntoDB(long id, Chemical c) {
    Chemical oldc = getChemicalFromChemicalUUID(id);
    Chemical mergedc = c.createNewByMerge(oldc);

    if (mergedc == null) {
        // whoa! inconsistent values on unmergables, so recover

        System.err.println("\n\n\n\n\n\n\n\n\n\n");
        System.err.println("---- Conflicting uuid or name or smiles or inchi or inchikey or pubchem_id:");
        System.err.println("---- NEW\t " + c);
        System.err.println("---- OLD\t " + oldc);
        System.err.println("---- Keeping OLD entry");
        System.err.println("\n\n\n\n\n\n\n\n\n\n");

        return;// w ww .  ja  v a  2  s .c o  m
    }

    BasicDBObject withID = new BasicDBObject();
    withID.put("_id", id);
    this.dbChemicals.remove(withID, WriteConcern.SAFE); // remove the old entry oldc from the collection
    submitToActChemicalDB(mergedc, id); // now that the old entry is removed, we can simply add
}

From source file:act.server.MongoDB.java

License:Open Source License

public void updateChemicalWithBrenda(Chemical c, String brendaName) {
    long id = alreadyEntered(c);

    if (id < 0) {
        System.err.println("Update chemical with brenda: " + brendaName + " can't find matching inchi");
        return;//from   w ww.j  a v a 2  s  . co m
    }
    BasicDBObject query = new BasicDBObject();
    query.put("_id", id);
    BasicDBObject update = new BasicDBObject();
    update.put("$push", new BasicDBObject("names.brenda", brendaName.toLowerCase()));
    this.dbChemicals.update(query, update);

}

From source file:act.server.MongoDB.java

License:Open Source License

public void updateChemicalAsNative(String inchi) {
    Chemical c = this.getChemicalFromInChI(inchi);
    if (c == null) {
        System.err.println("Can't find native in DB: " + inchi);
        return;//from  w w w . ja  v a 2 s. co  m
    }
    long id = c.getUuid();

    BasicDBObject query = new BasicDBObject();
    query.put("_id", id);
    BasicDBObject update = new BasicDBObject();
    update.put("$set", new BasicDBObject("isNative", true));
    this.dbChemicals.update(query, update);
}

From source file:act.server.MongoDB.java

License:Open Source License

public void updateChemicalWithPatents(String inchi, Integer num_patents, DBObject patents) {
    Chemical c = this.getChemicalFromInChI(inchi);
    if (c == null) {
        System.err.println("Attempting to add patent. Can't find chem in DB: " + inchi);
        return;/* w w  w .  j  a v a2 s  .c  om*/
    }
    long id = c.getUuid();

    BasicDBObject query = new BasicDBObject();
    query.put("_id", id);
    BasicDBObject update = new BasicDBObject();
    BasicDBObject set = new BasicDBObject();

    // TODO!!!!!!!
    //     patents is Array of {  patent_num: Int, patent_txt: String, patent_score: Int }
    //                     ie  {   patent ID, full text of patent, relevance to biosynthesis }
    //
    // put the patents DBObject (all elements of Array) in db.patents.
    // put the references to the entries within it in db.chemicals
    //     i.e., only an array { patent ID }
    // TODO!!!!!!!

    // TODO!!!!!!!
    //
    // Need to update functions that serialize and deserialize from the db :
    //          createChemicalDoc and convertDBObjectToChemical
    // to recreate vendors, patents etc fields....
    //
    // TODO!!!!!!!
    System.out.println("Installing patents needs to go into separate collections.. see code.");
    System.exit(-1);

    set.put("patents", patents);
    set.put("num_patents", num_patents);
    update.put("$set", set);
    this.dbChemicals.update(query, update);
}

From source file:act.server.MongoDB.java

License:Open Source License

public void updateChemicalWithVendors(String inchi, Integer csid, Integer num_vendors, JSONArray vendors) {
    Chemical c = this.getChemicalFromInChI(inchi);
    if (c == null) {
        System.err.println("Attempting to add vendor. Can't find chem in DB: " + inchi);
        return;//  w  ww  .j  a va 2 s  .  co  m
    }
    long id = c.getUuid();

    BasicDBObject query = new BasicDBObject();
    query.put("_id", id);
    BasicDBObject update = new BasicDBObject();
    BasicDBObject set = new BasicDBObject();
    DBObject vendors_dbobject = MongoDBToJSON.conv(vendors);
    set.put("vendors", vendors_dbobject);
    set.put("csid", csid);
    set.put("num_vendors", num_vendors);
    update.put("$set", set);
    this.dbChemicals.update(query, update);
}

From source file:act.server.MongoDB.java

License:Open Source License

public void updateStoichiometry(Reaction r) {
    BasicDBObject query = new BasicDBObject().append("_id", r.getUUID());
    DBObject obj = this.dbReactions.findOne(query);
    DBObject enz_summary = (DBObject) obj.get("enz_summary");
    BasicDBList substrates = (BasicDBList) enz_summary.get("substrates");
    BasicDBList newSubstrates = new BasicDBList();
    Set<Long> originalSubstrateIDs = new HashSet<Long>();
    for (int i = 0; i < substrates.size(); i++) {
        DBObject substrate = (DBObject) substrates.get(i);
        Long substrateID = (Long) substrate.get("pubchem");
        Boolean isForBalance = (Boolean) substrate.get("balance");
        if (isForBalance != null && isForBalance)
            continue;
        originalSubstrateIDs.add(substrateID);
        substrate.put("coefficient", r.getSubstrateCoefficient(substrateID));
        newSubstrates.add(substrate);/*w ww .  j a  v a  2  s .c om*/
    }
    Set<Long> substratesNew = r.getSubstrateIdsOfSubstrateCoefficients();
    for (Long s : substratesNew) {
        if (originalSubstrateIDs.contains(s))
            continue;
        if (r.getSubstrateCoefficient(s) == null)
            continue;
        DBObject substrate = new BasicDBObject();
        substrate.put("pubchem", s);
        substrate.put("coefficient", r.getSubstrateCoefficient(s));
        substrate.put("balance", true);
        newSubstrates.add(substrate);
    }

    BasicDBList products = (BasicDBList) enz_summary.get("products");
    BasicDBList newProducts = new BasicDBList();
    Set<Long> originalProductIDs = new HashSet<Long>();
    for (int i = 0; i < products.size(); i++) {
        DBObject product = (DBObject) products.get(i);
        Long productID = (Long) product.get("pubchem");
        Boolean isForBalance = (Boolean) product.get("balance");
        if (isForBalance != null && isForBalance)
            continue;
        originalProductIDs.add(productID);
        product.put("coefficient", r.getProductCoefficient(productID));
        newProducts.add(product);
    }
    Set<Long> productsNew = r.getProductIdsOfProductCoefficients();
    for (Long p : productsNew) {
        if (originalProductIDs.contains(p))
            continue;
        if (r.getProductCoefficient(p) == null)
            continue;
        DBObject product = new BasicDBObject();
        product.put("pubchem", p);
        product.put("coefficient", r.getProductCoefficient(p));
        product.put("balance", true);
        newProducts.add(product);
    }
    enz_summary.put("substrates", newSubstrates);
    enz_summary.put("products", newProducts);
    this.dbReactions.update(query, obj);
}