Example usage for com.mongodb DBObject toMap

List of usage examples for com.mongodb DBObject toMap

Introduction

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

Prototype

Map toMap();

Source Link

Document

Returns a map representing this BSONObject.

Usage

From source file:org.mongoj.samples.service.persistence.UserPersistenceImpl.java

License:Open Source License

protected User getDocument(DBObject dbObject) throws SystemException {
    if (dbObject == null) {
        return null;
    }/*  w ww . java  2 s  .  c o  m*/

    User user = new UserImpl(dbObject.toMap());

    return user;
}

From source file:org.mule.module.mongo.MongoCloudConnector.java

License:Open Source License

/**
 * Convert a DBObject into Map./*w ww.j  ava 2s .  c  o  m*/
 * <p/>
 * {@sample.xml ../../../doc/mongo-connector.xml.sample mongo:dbObjectToMap}
 * 
 * @param input the input for this transformer
 * @return the converted Map representation
 */
@SuppressWarnings("rawtypes")
@Transformer(sourceTypes = { DBObject.class })
public static Map dbObjectToMap(final DBObject input) {
    return input.toMap();
}

From source file:org.netbeans.modules.mongodb.ui.windows.collectionview.treetable.DBObjectNode.java

License:Open Source License

public DBObjectNode(TreeTableNode parent, DBObject userObject) {
    super(parent, userObject, new ChildrenFactory<DBObject>() {

        @Override/*w w w.ja v  a 2s . com*/
        @SuppressWarnings("unchecked")
        public List<TreeTableNode> createChildren(TreeTableNode parent, DBObject userObject) {
            final Map<String, Object> map = userObject.toMap();
            final List<TreeTableNode> children = new ArrayList<>(map.size());
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                children.add(new JsonPropertyNode(parent, new JsonProperty(entry.getKey(), entry.getValue())));
            }
            return children;
        }
    });
}

From source file:org.opencb.opencga.storage.mongodb.variant.DBObjectToVariantAnnotationConverter.java

License:Apache License

@Override
public VariantAnnotation convertToDataModelType(DBObject object) {
    VariantAnnotation va = new VariantAnnotation();

    //ConsequenceType
    List<ConsequenceType> consequenceTypes = new LinkedList<>();
    Object cts = object.get(CONSEQUENCE_TYPE_FIELD);
    if (cts != null && cts instanceof BasicDBList) {
        for (Object o : ((BasicDBList) cts)) {
            if (o instanceof DBObject) {
                DBObject ct = (DBObject) o;

                //SO accession name
                List<String> soAccessionNames = new LinkedList<>();
                if (ct.containsField(SO_ACCESSION_FIELD)) {
                    if (ct.get(SO_ACCESSION_FIELD) instanceof List) {
                        List<Integer> list = (List) ct.get(SO_ACCESSION_FIELD);
                        for (Integer so : list) {
                            soAccessionNames.add(ConsequenceTypeMappings.accessionToTerm.get(so));
                        }/*from  ww w. j a  v a2  s. com*/
                    } else {
                        soAccessionNames
                                .add(ConsequenceTypeMappings.accessionToTerm.get(ct.get(SO_ACCESSION_FIELD)));
                    }
                }

                //ProteinSubstitutionScores
                List<Score> proteinSubstitutionScores = new LinkedList<>();
                if (ct.containsField(PROTEIN_SUBSTITUTION_SCORE_FIELD)) {
                    List<DBObject> list = (List) ct.get(PROTEIN_SUBSTITUTION_SCORE_FIELD);
                    for (DBObject dbObject : list) {
                        proteinSubstitutionScores.add(new Score(getDefault(dbObject, SCORE_SCORE_FIELD, 0.0),
                                getDefault(dbObject, SCORE_SOURCE_FIELD, ""),
                                getDefault(dbObject, SCORE_DESCRIPTION_FIELD, "")));
                    }
                }
                if (ct.containsField(POLYPHEN_FIELD)) {
                    DBObject dbObject = (DBObject) ct.get(POLYPHEN_FIELD);
                    proteinSubstitutionScores.add(new Score(getDefault(dbObject, SCORE_SCORE_FIELD, 0.0),
                            "polyphen", getDefault(dbObject, SCORE_DESCRIPTION_FIELD, "")));
                }
                if (ct.containsField(SIFT_FIELD)) {
                    DBObject dbObject = (DBObject) ct.get(SIFT_FIELD);
                    proteinSubstitutionScores.add(new Score(getDefault(dbObject, SCORE_SCORE_FIELD, 0.0),
                            "sift", getDefault(dbObject, SCORE_DESCRIPTION_FIELD, "")));
                }

                consequenceTypes.add(new ConsequenceType(getDefault(ct, GENE_NAME_FIELD, "") /*.toString()*/,
                        getDefault(ct, ENSEMBL_GENE_ID_FIELD, "") /*.toString()*/,
                        getDefault(ct, ENSEMBL_TRANSCRIPT_ID_FIELD, "") /*.toString()*/,
                        getDefault(ct, STRAND_FIELD, "") /*.toString()*/,
                        getDefault(ct, BIOTYPE_FIELD, "") /*.toString()*/,
                        getDefault(ct, C_DNA_POSITION_FIELD, 0), getDefault(ct, CDS_POSITION_FIELD, 0),
                        getDefault(ct, AA_POSITION_FIELD, 0),
                        getDefault(ct, AA_CHANGE_FIELD, "") /*.toString() */,
                        getDefault(ct, CODON_FIELD, "") /*.toString() */, proteinSubstitutionScores,
                        soAccessionNames, Collections.<ExpressionValue>emptyList()));
            }
        }

    }
    va.setConsequenceTypes(consequenceTypes);

    //Conserved Region Scores
    List<Score> conservedRegionScores = new LinkedList<>();
    if (object.containsField(CONSERVED_REGION_SCORE_FIELD)) {
        List<DBObject> list = (List) object.get(CONSERVED_REGION_SCORE_FIELD);
        for (DBObject dbObject : list) {
            conservedRegionScores.add(new Score(getDefault(dbObject, SCORE_SCORE_FIELD, 0.0),
                    getDefault(dbObject, SCORE_SOURCE_FIELD, ""),
                    getDefault(dbObject, SCORE_DESCRIPTION_FIELD, "")));
        }
    }
    va.setConservationScores(conservedRegionScores);

    //Population frequencies
    List<PopulationFrequency> populationFrequencies = new LinkedList<>();
    if (object.containsField(POPULATION_FREQUENCIES_FIELD)) {
        List<DBObject> list = (List) object.get(POPULATION_FREQUENCIES_FIELD);
        for (DBObject dbObject : list) {
            populationFrequencies.add(new PopulationFrequency(
                    getDefault(dbObject, POPULATION_FREQUENCY_STUDY_FIELD, ""),
                    getDefault(dbObject, POPULATION_FREQUENCY_POP_FIELD, ""),
                    getDefault(dbObject, POPULATION_FREQUENCY_SUPERPOP_FIELD, ""),
                    getDefault(dbObject, POPULATION_FREQUENCY_REFERENCE_ALLELE_FIELD, ""),
                    getDefault(dbObject, POPULATION_FREQUENCY_ALTERNATE_ALLELE_FIELD, ""),
                    (float) getDefault(dbObject, POPULATION_FREQUENCY_REFERENCE_FREQUENCY_FIELD, -1.0),
                    (float) getDefault(dbObject, POPULATION_FREQUENCY_ALTERNATE_FREQUENCY_FIELD, -1.0)));
        }
    }
    va.setPopulationFrequencies(populationFrequencies);

    // Drug-Gene Interactions
    Map<String, List<Object>> drugGeneInteractionMap = new HashMap<>();
    List<Object> drugs = new LinkedList<>();
    if (object.containsField(DRUG_FIELD)) {
        List<DBObject> list = (List) object.get(DRUG_FIELD);
        for (DBObject dbObject : list) {
            drugs.add(dbObject.toMap());
        }
        drugGeneInteractionMap.put("dgidb", drugs);
    }
    va.setGeneDrugInteraction(drugGeneInteractionMap);

    //XREfs
    List<Xref> xrefs = new LinkedList<>();
    Object xrs = object.get(XREFS_FIELD);
    if (xrs != null && xrs instanceof BasicDBList) {
        for (Object o : (BasicDBList) xrs) {
            if (o instanceof DBObject) {
                DBObject xref = (DBObject) o;

                xrefs.add(new Xref((String) xref.get(XREF_ID_FIELD), (String) xref.get(XREF_SOURCE_FIELD)));
            }
        }
    }
    va.setXrefs(xrefs);

    //Clinical Data
    if (object.containsField(CLINICAL_DATA_FIELD)) {
        DBObject clinicalData = ((DBObject) object.get(CLINICAL_DATA_FIELD));
        va.setClinical(clinicalData.toMap());
    }

    return va;
}

From source file:org.opencb.opencga.storage.mongodb.variant.VariantSourceMongoDBAdaptor.java

License:Apache License

/**
 * Populates the dictionary relating sources and samples.
 *
 * @return The QueryResult with information of how long the query took
 *///  w  ww  .  j av a 2s .c  o  m
private QueryResult populateSamplesInSources() {
    MongoDBCollection coll = db.getCollection(collectionName);
    DBObject projection = new BasicDBObject(DBObjectToVariantSourceConverter.FILEID_FIELD, true)
            .append(DBObjectToVariantSourceConverter.SAMPLES_FIELD, true);
    QueryResult queryResult = coll.find((DBObject) null, projection, null);

    List<DBObject> result = queryResult.getResult();
    for (DBObject dbo : result) {
        if (!dbo.containsField(DBObjectToVariantSourceConverter.FILEID_FIELD)) {
            continue;
        }
        String key = dbo.get(DBObjectToVariantSourceConverter.FILEID_FIELD).toString();
        DBObject value = (DBObject) dbo.get(DBObjectToVariantSourceConverter.SAMPLES_FIELD);
        samplesInSources.put(key, new ArrayList(value.toMap().keySet()));
    }

    return queryResult;
}

From source file:org.restlet.example.ext.oauth.mongo.MongoClient.java

License:Open Source License

@SuppressWarnings("unchecked")
public Map<String, Object> getProperties() {
    DBObject properties = (DBObject) client.get(PROPERTIES);
    return properties.toMap();
}

From source file:org.restlet.example.ext.oauth.mongo.MongoTokenManager.java

License:Open Source License

@SuppressWarnings("unchecked")
public AuthSession restoreSession(String code) throws OAuthException {
    DBObject sessionObj = sessions.findOne(new BasicDBObject("_id", code));

    if (sessionObj == null) {
        throw new OAuthException(OAuthError.invalid_grant, "Invalid code.", null);
    }//from w w  w. ja  v a 2s  . c o m

    return AuthSession.toAuthSession((Map<String, Object>) sessionObj.toMap());
}

From source file:org.s1.mongodb.MongoDBFormat.java

License:Apache License

/**
 *
 * @param obj/*from   ww  w.  ja va  2  s  . c o  m*/
 * @return
 */
public static Map<String, Object> toMap(DBObject obj) {
    if (obj == null)
        return null;
    Map<String, Object> m = obj.toMap();
    /*if(m.get("_id")!=null) {
    m.put("_id", m.get("_id").toString());
    }*/
    m.remove("_id");

    m = Objects.iterate(m, new Closure<ObjectIterator.IterateBean, Object>() {
        @Override
        public Object call(ObjectIterator.IterateBean input) {
            Object o = input.getValue();
            if (o instanceof Map) {
                Map m = (Map) o;
                if (m.containsKey("$date") && m.size() == 1) {
                    o = m.get("$date");
                }
                if (m.containsKey("_serializable") && m.size() == 1) {
                    try {
                        byte[] b = (byte[]) m.get("_serializable");
                        ByteArrayInputStream bin = new ByteArrayInputStream(b);
                        ObjectInputStream ois = new ObjectInputStream(bin);
                        o = ois.readObject();
                    } catch (Exception e) {
                        throw S1SystemError.wrap(e);
                    }
                }
            }
            return o;
        }
    });
    return m;
}

From source file:org.shenjitang.mongodbutils.MongoDbOperater.java

public <T> T findOneObj(String dbName, String collName, Map queryMap, Class<T> clazz)
        throws InstantiationException, IllegalAccessException, InvocationTargetException {
    DB db = mongoClient.getDB(dbName);/*from   w  ww  .j a v  a 2  s  .  c  om*/
    DBCollection coll = db.getCollection(collName);
    BasicDBObject query = new BasicDBObject(queryMap);
    DBObject map = coll.findOne(query);
    if (map == null) {
        return null;
    }
    T obj = clazz.newInstance();
    ConvertUtils.register(new DateConverter(null), Date.class);
    ConvertUtils.register(new IntegerConverter(null), Integer.class);
    BeanUtils.populate(obj, map.toMap());
    return obj;
}

From source file:org.slc.sli.ingestion.util.IndexObject.java

License:Apache License

@SuppressWarnings("boxing")
public final void setKeys(DBObject obj) {
    //This DBObject.toMap returns a LinkedHashMap, so order
    // is preserved
    @SuppressWarnings("unchecked")
    Map<String, Object> objMap = obj.toMap();
    for (Map.Entry<String, Object> entry : objMap.entrySet()) {
        Object value = entry.getValue();
        if (value instanceof Double) {
            keys.put(entry.getKey(), ((Double) value).intValue());
        } else {/*from w ww.j  a  va2  s  . c  om*/
            keys.put(entry.getKey(), (Integer) value);
        }
    }
}