Example usage for com.mongodb DBObject keySet

List of usage examples for com.mongodb DBObject keySet

Introduction

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

Prototype

Set<String> keySet();

Source Link

Document

Returns this object's fields' names

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());
    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;/*from  w ww  .  ja v a2  s.c o m*/
        } 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 String getChemicalDBJSON(Long uuid) {
    BasicDBObject query = new BasicDBObject();
    query.put("_id", uuid);

    DBObject o = this.dbChemicals.findOne(query);
    if (o == null)
        return null;

    Set<String> keys = o.keySet();
    String json = "{\n";
    for (String key : keys) {
        json += "\t" + key + " : " + o.get(key) + ",\n";
    }//w w w  .  j a  v a  2s.  c o  m
    json += "}";
    return json;
}

From source file:act.server.MongoDB.java

License:Open Source License

public Chemical convertDBObjectToChemical(DBObject o) {
    long uuid;//from   w ww  .  ja v a  2  s.c  o m
    // WTF!? Are some chemicals ids int and some long?
    // this code below should not be needed, unless our db is mucked up
    try {
        uuid = (Long) o.get("_id"); // checked: db type IS long
    } catch (ClassCastException e) {
        System.err.println("WARNING: MongoDB.convertDBObjectToChemical ClassCast db.chemicals.id is not Long?");
        uuid = ((Integer) o.get("_id")).longValue(); // this should be dead code
    }

    String chemName = (String) o.get("canonical");
    DBObject xrefs = (DBObject) o.get("xref");
    Long pcid = null;
    try {
        pcid = (Long) (xrefs.get("pubchem"));
    } catch (Exception e) {

    }
    if (pcid == null) {
        pcid = (long) -1;
    }
    String inchi = (String) o.get("InChI");
    String inchiKey = (String) o.get("InChIKey");
    String smiles = (String) o.get("SMILES");
    Chemical c = new Chemical(uuid, pcid, chemName, smiles);
    c.setInchi(inchi);
    c.setCanon((String) o.get("canonical"));
    try {
        for (String typ : xrefs.keySet()) {
            if (typ.equals("pubchem"))
                continue;
            c.putRef(Chemical.REFS.valueOf(typ), MongoDBToJSON.conv((DBObject) xrefs.get(typ)));
        }
    } catch (Exception e) {

    }

    if (o.get("derived_data") != null) {
        BasicDBList matchedRos = (BasicDBList) ((DBObject) o.get("derived_data")).get("matched_ros");
        if (matchedRos != null) {
            for (Object roId : matchedRos) {
                c.addSubstructureRoId((Integer) roId);
            }
        }
    }

    BasicDBList names = (BasicDBList) ((DBObject) o.get("names")).get("brenda");
    if (names != null) {
        for (Object n : names) {
            c.addBrendaNames((String) n);
        }
    }
    if (names != null) {
        names = (BasicDBList) ((DBObject) o.get("names")).get("synonyms");
        for (Object n : names) {
            c.addSynonym((String) n);
        }
    }
    if (names != null) {
        names = (BasicDBList) ((DBObject) o.get("names")).get("pubchem");
        for (Object n : names) {
            String typ = (String) ((DBObject) n).get("type");
            BasicDBList pnames = (BasicDBList) ((DBObject) n).get("values");
            List<String> s = new ArrayList<String>();
            for (Object os : pnames)
                s.add((String) os);
            c.addNames(typ, s.toArray(new String[0]));
        }
    }
    if ((Boolean) o.get("isCofactor"))
        c.setAsCofactor();
    if ((Boolean) o.get("isNative"))
        c.setAsNative();
    if ((Double) o.get("estimateEnergy") != null)
        c.setEstimatedEnergy((Double) o.get("estimateEnergy"));
    BasicDBList keywords = (BasicDBList) o.get("keywords");
    if (keywords != null)
        for (Object k : keywords)
            c.addKeyword((String) k);
    BasicDBList cikeywords = (BasicDBList) o.get("keywords_case_insensitive");
    if (cikeywords != null)
        for (Object k : cikeywords)
            c.addCaseInsensitiveKeyword((String) k);

    BasicDBList vendors = (BasicDBList) o.get("vendors");
    Integer num_vendors = (Integer) o.get("num_vendors");
    Integer chemspiderid = (Integer) o.get("csid");

    c.setChemSpiderVendorXrefs(vendors == null ? null : MongoDBToJSON.conv(vendors));
    c.setChemSpiderNumUniqueVendors(num_vendors);
    c.setChemSpiderID(chemspiderid);

    /**
     * Shortest name  is most useful so just use that.
     */
    //TODO: what are we doing with shortest name here?
    String shortestName = c.getCanon();

    for (String name : c.getBrendaNames()) {
        if (shortestName == null || name.length() < shortestName.length())
            shortestName = name;
    }
    for (String name : c.getSynonyms()) {
        if (shortestName == null || name.length() < shortestName.length())
            shortestName = name;
    }

    return c;
}

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

License:Open Source License

public static JSONObject conv(DBObject o) {
    JSONObject result = new JSONObject();
    try {/*from  www. ja  v  a 2s  . c om*/
        for (String k : o.keySet()) {
            Object v = o.get(k);
            if (v instanceof BasicDBList) {
                result.put(k, conv((BasicDBList) v));
            } else if (v instanceof DBObject) {
                result.put(k, conv((DBObject) 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 putListStrOrJSON(DBObject json, String tag, DBObject toAdd) {
    // if it is a string add it unencapsulated
    if (toAdd.keySet().size() == 1 && toAdd.containsField(strTag))
        putElemOrList(json, tag, toAdd.get(strTag));
    else/*from  w w w .j av a2 s.com*/
        putElemOrList(json, tag, toAdd);
}

From source file:be.solidx.hot.data.mongo.DBObjectMapTransformer.java

License:Open Source License

public Map<String, Object> dbObjectTomap(DBObject dbObject) {
    if (dbObject == null)
        return null;

    Map<String, Object> map = new LinkedHashMap<String, Object>();
    for (String key : dbObject.keySet()) {
        if (dbObject.get(key) instanceof List<?>) {
            map.put(key.toString(), transformList((List<?>) dbObject.get(key)));
        } else if (dbObject.get(key) instanceof DBObject) {
            map.put(key.toString(), dbObjectTomap((DBObject) dbObject.get(key)));
        } else {/* w  w w.  ja  v  a  2s  .  c om*/
            map.put(key.toString(), key.equals("_id") ? dbObject.get(key).toString() : dbObject.get(key));
        }
    }
    return map;
}

From source file:be.solidx.hot.data.mongo.DBObjectNativeObjectTransformer.java

License:Open Source License

public static NativeObject dbObjectToNativeObject(DBObject dbObject) {
    NativeObject nativeObject = new NativeObject();
    if (dbObject == null) {
        return null;
    }/*w  w  w.  j  a  v a2 s .c om*/
    for (String key : dbObject.keySet()) {
        if (dbObject.get(key) instanceof List<?>) {
            nativeObject.put(key.toString(), nativeObject, listToNativeArray((List<?>) dbObject.get(key)));
        } else if (dbObject.get(key) instanceof DBObject) {
            nativeObject.put(key.toString(), nativeObject,
                    dbObjectToNativeObject((DBObject) dbObject.get(key)));
        } else {
            nativeObject.put(key.toString(), nativeObject,
                    key.equals("_id") ? dbObject.get(key).toString() : dbObject.get(key));
        }
    }
    return nativeObject;
}

From source file:be.solidx.hot.data.mongo.DBObjectPyDictionaryTransformer.java

License:Open Source License

public PyDictionary dbObjectToPyDictionary(DBObject dbObject) {
    if (dbObject == null) {
        return null;
    }//w  ww. jav  a2 s .  c  om
    PyDictionary pyDictionary = new PyDictionary();
    for (String key : dbObject.keySet()) {
        if (dbObject.get(key) instanceof List<?>) {
            pyDictionary.put(key.toString(), transformList((List<?>) dbObject.get(key)));
        } else if (dbObject.get(key) instanceof DBObject) {
            pyDictionary.put(key.toString(), dbObjectToPyDictionary((DBObject) dbObject.get(key)));
        } else {
            pyDictionary.put(key.toString(),
                    key.equals("_id") ? dbObject.get(key).toString() : dbObject.get(key));
        }
    }
    return pyDictionary;
}

From source file:ca.mariusbogoevici.springxd.mongo.source.TupleReadConverter.java

License:Apache License

@Override
public Tuple convert(DBObject source) {

    TupleBuilder tupleBuilder = new TupleBuilder();
    for (String key : source.keySet()) {
        if ("_id".equals(key)) {
            tupleBuilder.put("id", source.get(key));
        } else {//from   w w  w.  jav  a 2  s .c  om
            tupleBuilder.put(key, source.get(key));
        }
    }

    return tupleBuilder.build();
}

From source file:ch.bfh.uniboard.persistence.mongodb.PersistedPost.java

License:GNU General Public License

/**
 * Method allowing to retrieve a PersistedPost out of the DBObject returned by the database. If the passed DBObject
 * does not represent a PersistedPost, an empty PersistedPost is retuned
 *
 * @param doc the DBObject returned by the database
 * @return the corresponding persisted post
 *//*  w w  w  .ja v a  2s.  c o  m*/
public static PersistedPost fromDBObject(DBObject doc) {
    PersistedPost pp = new PersistedPost();

    //TODO remove try catch when DB will be cleaned
    //this is only needed since some messages in MongoDB are not byte array
    //but string (historical reasons
    try {
        pp.message = Base64.decode((String) doc.get("message"));
    } catch (ClassCastException e) {
        pp.message = JSON.serialize(doc.get("message")).getBytes();
    }

    //fill alpha attributes
    Attributes alpha = new Attributes();
    DBObject alphaList = (DBObject) doc.get("alpha");
    for (String key : alphaList.keySet()) {
        //String key = dbObj.keySet().iterator().next();
        alpha.add(key, inflateType(alphaList.get(key)));
    }
    pp.alpha = alpha;

    //fill beta attributes
    Attributes beta = new Attributes();
    DBObject betaList = (DBObject) doc.get("beta");
    for (String key : betaList.keySet()) {
        //String key = dbObj.keySet().iterator().next();
        beta.add(key, inflateType(betaList.get(key)));
    }
    pp.beta = beta;

    return pp;
}