Example usage for com.mongodb BasicDBObject toMap

List of usage examples for com.mongodb BasicDBObject toMap

Introduction

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

Prototype

public Map toMap() 

Source Link

Document

Converts a DBObject to a map.

Usage

From source file:bd.DArticulo.java

@Override
public ArrayList listar() {
    ArrayList datos = new ArrayList();
    CArticulo x = new CArticulo();
    conecion con = new conecion(table);
    DBCursor cursor = con.get_colletion().find();
    try {/*w w w  .j a v a 2s .com*/
        while (cursor.hasNext()) {
            x = new CArticulo();
            BasicDBObject agg = (BasicDBObject) cursor.next();
            x.set_datos((HashMap) agg.toMap());
            x.setId((String) agg.getString("_id"));
            datos.add(x);
        }
    } finally {
        cursor.close();
    }
    con.end();
    return datos;
}

From source file:bd.DCategoria.java

@Override
public ArrayList listar() {
    ArrayList datos = new ArrayList();
    CCategoria x = new CCategoria();
    conecion con = new conecion(table);
    DBCursor cursor = con.get_colletion().find();
    try {//from   w w  w .  j  a  v a  2  s  . co  m
        while (cursor.hasNext()) {
            x = new CCategoria();
            BasicDBObject agg = (BasicDBObject) cursor.next();
            x.set_datos((HashMap) agg.toMap());
            x.setId((String) agg.getString("_id"));
            datos.add(x);
        }
    } finally {
        cursor.close();
    }
    con.end();
    return datos;
}

From source file:bd.DPersona.java

@Override
public ArrayList listar() {
    ArrayList datos = new ArrayList();
    CPersona x = new CPersona();
    conecion con = new conecion(table);
    DBCursor cursor = con.get_colletion().find();
    try {/*from  w ww  .j  a v a2 s  . c  om*/
        while (cursor.hasNext()) {
            x = new CPersona();
            BasicDBObject agg = (BasicDBObject) cursor.next();
            x.set_datos((HashMap) agg.toMap());
            x.setId((String) agg.getString("_id"));
            datos.add(x);
        }
    } finally {
        cursor.close();
    }
    con.end();
    return datos;
}

From source file:bd.DSession.java

@Override
public ArrayList listar() {
    ArrayList datos = new ArrayList();
    CSession x = new CSession();
    conecion con = new conecion(table);
    DBCursor cursor = con.get_colletion().find();
    try {/*from ww  w.  j  a v  a  2  s  .  c o  m*/
        while (cursor.hasNext()) {
            x = new CSession();
            BasicDBObject agg = (BasicDBObject) cursor.next();
            x.set_datos((HashMap) agg.toMap());
            x.setId((String) agg.getString("_id"));
            datos.add(x);
        }
    } finally {
        cursor.close();
    }
    con.end();
    return datos;
}

From source file:bd.DVenta.java

@Override
public ArrayList listar() {
    ArrayList datos = new ArrayList();
    CVenta x = new CVenta();
    conecion con = new conecion(table);
    DBCursor cursor = con.get_colletion().find();
    try {/*from  ww  w . ja  va 2 s  .c  om*/
        while (cursor.hasNext()) {
            x = new CVenta();
            BasicDBObject agg = (BasicDBObject) cursor.next();
            x.set_datos((HashMap) agg.toMap());
            x.setId((String) agg.getString("_id"));
            datos.add(x);
        }
    } finally {
        cursor.close();
    }
    con.end();
    return datos;
}

From source file:com.ikanow.infinit.e.data_model.custom.InfiniteMongoSplitter.java

License:Apache License

private static BasicDBObject convertQuery(BasicDBObject originalQuery, Object sourceObj) {
    BasicDBObject modQuery = null;/*from   w  ww  .  j  a v a 2 s.  co m*/
    if (null != sourceObj) {
        if (sourceObj instanceof Collection) {
            modQuery = new BasicDBObject(originalQuery.toMap());
            @SuppressWarnings("rawtypes")
            Collection sources = (Collection) sourceObj;
            modQuery.put(DocumentPojo.sourceKey_, new BasicDBObject(DbManager.in_, sources));
        } //TESTED
        else if (sourceObj instanceof String) {
            modQuery = new BasicDBObject(originalQuery.toMap());
            String source = (String) sourceObj;
            modQuery.put(DocumentPojo.sourceKey_, source);
        } //TESTED
    }
    return modQuery;
}

From source file:com.linuxbox.enkive.workspace.searchResult.mongo.MongoSearchResultBuilder.java

License:Open Source License

/**
 * Helper method to get a search result from the DB and convert it into a
 * MongoSearchResult object.// www. ja  v a  2s . c  o m
 * @param searchResultObject   DB Object to extract from
 * @return new MongoSearchResult containing data from DB
 */
private MongoSearchResult extractResult(DBObject searchResultObject) {
    MongoSearchResult result = new MongoSearchResult(searchResultsColl);
    result.setId(((ObjectId) searchResultObject.get(UUID)).toString());

    BasicDBObject searchResults = (BasicDBObject) searchResultObject.get(SEARCHRESULTS);
    @SuppressWarnings("unchecked")
    // Map comes back as <String, String>, even though we put it in as <Long, String>
    Map<String, String> dbUUIDs = searchResults.toMap();
    HashMap<Long, String> searchResultUUIDs = new HashMap<Long, String>();
    Long maxUID = (long) 0;
    for (Map.Entry<String, String> entry : dbUUIDs.entrySet()) {
        Long UID = Long.parseLong(entry.getKey());
        if (UID > maxUID) {
            maxUID = UID;
        }
        searchResultUUIDs.put(UID, entry.getValue());
    }
    result.setMessageIds(searchResultUUIDs);
    result.setNextUID(maxUID + 1);

    result.setSearchQueryId((String) searchResultObject.get(SEARCHQUERYID));
    result.setSearchResultUtils(searchResultUtils);

    return result;

}

From source file:com.mobileman.kuravis.core.ws.user.UserController.java

License:Apache License

/**
 * UC1100 Privacy Settings - update Privacy Settings
 * // w w  w  .j  a  v a  2s . c om
 * @param body
 * @return result ok if password was successfuly recreated, error
 */
@RequestMapping(value = "/user/privacysettings", method = RequestMethod.POST, produces = {
        JsonUtil.MEDIA_TYPE_APPLICATION_JSON })
@ResponseBody
@RequiresAuthentication
@SuppressWarnings("unchecked")
public ResponseEntity<DBObject> updatePrivacySettings(@RequestBody BasicDBObject body) {
    log.info("updatePrivacySettings(" + body + ") - start");

    userService.updatePrivacySettings(body.toMap());

    ResponseEntity<DBObject> response = new ResponseEntity<DBObject>(ErrorUtils.success(), HttpStatus.OK);
    log.info("updatePrivacySettings(...) - end: " + response);
    return response;
}

From source file:com.petpet.c3po.dao.mongo.MongoPersistenceLayer.java

License:Apache License

public DBObject mapReduce(int key, String property, Filter filter, List<Integer> bins) {
    LOG.debug("Starting mapReduce for the following property: {}", property);
    long start = System.currentTimeMillis();
    Property prop = getCache().getProperty(property);
    String propType = prop.getType();
    String map = "";
    String map2 = "";
    String reduce = "";
    if (propType.equals(PropertyType.STRING.toString()) || propType.equals(PropertyType.BOOL.toString())) {
        map = "function() {\n" + "    property = '" + property + "';\n" + "    for (mr in this.metadata){\n"
                + "        metadataRecord=this.metadata[mr];\n"
                + "        if(metadataRecord.property == property)\n" + "        {\n"
                + "            if (metadataRecord.status == 'CONFLICT'){\n" + "                emit({\n"
                + "                    property: property,\n" + "                    value: 'CONFLICT'\n"
                + "                }, 1);\n" + "            } else {\n" + "                emit({\n"
                + "                    property: property,\n"
                + "                    value: metadataRecord.sourcedValues[0].value\n"
                + "                }, 1);\n" + "\n" + "            }\n" + "            return;\n"
                + "        }\n" + "    }\n" + "    emit({\n" + "        property: property,\n"
                + "        value: 'Unknown'\n" + "        }, 1);\n" + "}";

        reduce = "function reduce(key, values) {\n" + "    var res = 0;\n"
                + "    values.forEach(function(v) {\n" + "        res += v;\n" + "    });\n"
                + "    return res;\n" + "}";

    } else if (propType.equals(PropertyType.INTEGER.toString())
            || propType.equals(PropertyType.FLOAT.toString())) {
        map = "function() {\n" + "    property = '" + property + "';\n" + "    thresholds = "
                + getBinThresholds(bins) + ";\n" + "    for (mr in this.metadata){\n"
                + "        metadataRecord=this.metadata[mr];\n"
                + "        if(metadataRecord.property == property){\n"
                + "            if (metadataRecord.status == 'CONFLICT'){\n" + "                emit({\n"
                + "                    property: property,\n" + "                    value: 'CONFLICT'\n"
                + "                }, 1);\n" + "            } else {\n"
                + "                var val=metadataRecord.sourcedValues[0].value;\n"
                + "                var skipped=false;\n" + "                if (thresholds.length > 0)\n"
                + "                    for (t in thresholds){\n"
                + "                        threshold = thresholds[t];  \n"
                + "                        if (val>=threshold[0] && val<=threshold[1]){\n"
                + "                             emit({\n"
                + "                                property: property,\n"
                + "                                value: threshold[0]+'-'+threshold[1]\n"
                + "                            }, 1);\n" + "                             skipped=true;\n"
                + "                             break;\n" + "                         }\n"
                + "                    }\n" + "            }\n" + "            return;\n" + "        }\n"
                + "    }\n" + "    emit({\n" + "        property: property,\n" + "        value: 'Unknown'\n"
                + "        }, 1);\n" + "}";
        reduce = "function reduce(key, values) {\n" + "    var res = 0;\n"
                + "    values.forEach(function(v) {\n" + "        res += v;\n" + "    });\n"
                + "    return res;\n" + "}";

    } else if (propType.equals(PropertyType.DATE.toString())) {
        map = "function() {\n" + "    property = '" + property + "';\n" + "    for (mr in this.metadata){\n"
                + "        metadataRecord=this.metadata[mr];\n"
                + "        if(metadataRecord.property == property){\n"
                + "            if (metadataRecord.status == 'CONFLICT'){\n" + "                emit({\n"
                + "                    property: property,\n" + "                    value: 'CONFLICT'\n"
                + "                }, 1);\n" + "            } else {\n"
                + "                var date = new Date(metadataRecord.sourcedValues[0].value);\n"
                + "                var val=date.getFullYear();\n" + "                emit({\n"
                + "                    property: property,\n" + "                    value: val\n"
                + "                }, 1);\n" + "            }\n" + "            return;\n" + "        }\n"
                + "    }\n" + "    emit({\n" + "        property: property,\n" + "        value: 'Unknown'\n"
                + "        }, 1);\n" + "}";

        reduce = "function reduce(key, values) {\n" + "    var res = 0;\n"
                + "    values.forEach(function(v) {\n" + "        res += v;\n" + "    });\n"
                + "    return res;\n" + "}";

    }// w  w  w.j av a 2s  .c om
    DBObject query = this.getCachedFilter(filter);
    LOG.debug("Filter query is:\n{}", query);
    String queryString = query.toString();
    DBCollection elmnts = getCollection(Element.class);
    MapReduceCommand cmd = new MapReduceCommand(elmnts, map, reduce, null, INLINE, query);
    MapReduceOutput output = elmnts.mapReduce(cmd);
    // List<BasicDBObject> results = (List<BasicDBObject>) output.getCommandResult().get( "results" );
    Iterator<DBObject> iterator = output.results().iterator();
    List<BasicDBObject> results = new ArrayList<BasicDBObject>();
    while (iterator.hasNext()) {
        results.add((BasicDBObject) iterator.next());

    }

    LOG.debug("MapReduce produced {} results", results.size());
    DBCollection histCollection = this.db.getCollection(TBL_HISTOGRAMS);
    BasicDBObject old = new BasicDBObject("_id", key);
    BasicDBObject res = new BasicDBObject(old.toMap());
    res.put("results", results);
    histCollection.update(old, res, true, false);

    DBCursor cursor = histCollection.find(new BasicDBObject("_id", key));

    if (cursor.count() == 0) {
        return null;
    }
    long end = System.currentTimeMillis();
    LOG.debug("MapReduce took {} seconds", (end - start) / 1000);
    return (DBObject) cursor.next().get("results");
}

From source file:com.petpet.c3po.dao.mongo.MongoPersistenceLayer.java

License:Apache License

public DBObject mapReduceAllValues(int key, String property, Filter filter, List<Integer> bins) {
    LOG.debug("Starting mapReduce for the following property: {}", property);
    long start = System.currentTimeMillis();
    Property prop = getCache().getProperty(property);
    String propType = prop.getType();
    String map = "";
    String map2 = "";
    String reduce = "";
    if (propType.equals(PropertyType.STRING.toString()) || propType.equals(PropertyType.BOOL.toString())) {
        map = "function() {\n" + "    property = '" + property + "';\n" + "    for (mr in this.metadata){\n"
                + "        metadataRecord=this.metadata[mr];\n"
                + "        if(metadataRecord.property == property)\n" + "        {\n"
                + "            for (i in metadataRecord.sourcedValues)\n" + "            {\n"
                + "                sv=metadataRecord.sourcedValues[i];\n" + "                emit({\n"
                + "                    property: property,\n" + "                    value: sv.value\n"
                + "                }, 1);\n" + "\n" + "            }\n" + "            return;\n"
                + "        }\n" + "    }\n" + "    emit({\n" + "        property: property,\n"
                + "        value: 'Unknown'\n" + "        }, 1);\n" + "}";

        reduce = "function reduce(key, values) {\n" + "    var res = 0;\n"
                + "    values.forEach(function(v) {\n" + "        res += v;\n" + "    });\n"
                + "    return res;\n" + "}";

    } else if (propType.equals(PropertyType.INTEGER.toString())
            || propType.equals(PropertyType.FLOAT.toString())) {
        map = "function() {\n" + "    property = '" + property + "';\n" + "    thresholds = "
                + getBinThresholds(bins) + ";\n" + "    for (mr in this.metadata)" + "    {\n"
                + "        metadataRecord=this.metadata[mr];\n"
                + "        if(metadataRecord.property == property)" + "        {\n"
                + "           for (i in metadataRecord.sourcedValues)" + "           {\n"
                + "                sv=metadataRecord.sourcedValues[i];\n"
                + "                var val=sv.value;\n" + "                if (thresholds.length > 0)\n"
                + "                    for (t in thresholds){\n"
                + "                        threshold = thresholds[t];  \n"
                + "                        if (val>=threshold[0] && val<=threshold[1]){\n"
                + "                             emit({\n"
                + "                                property: property,\n"
                + "                                value: threshold[0]+'-'+threshold[1]\n"
                + "                            }, 1);\n" + "                         }\n"
                + "                    }\n" + "            }\n" + "            return;\n" + "         }\n"
                + "    }\n" + "    emit({\n" + "        property: property,\n" + "        value: 'Unknown'\n"
                + "        }, 1);\n" + "}";
        reduce = "function reduce(key, values) {\n" + "    var res = 0;\n"
                + "    values.forEach(function(v) {\n" + "        res += v;\n" + "    });\n"
                + "    return res;\n" + "}";

    } else if (propType.equals(PropertyType.DATE.toString())) {
        map = "function() {\n" + "    property = '" + property + "';\n" + "    for (mr in this.metadata){\n"
                + "        metadataRecord=this.metadata[mr];\n"
                + "        if(metadataRecord.property == property){\n"
                + "           for (i in metadataRecord.sourcedValues){\n"
                + "               sv=metadataRecord.sourcedValues[i];\n"
                + "               var date = new Date(sv.value);\n"
                + "               var val=date.getFullYear();\n" + "               emit({\n"
                + "                    property: property,\n" + "                    value: val\n"
                + "               }, 1);\n" + "            }\n" + "            return;\n" + "        }\n"
                + "    }\n" + "    emit({\n" + "        property: property,\n" + "        value: 'Unknown'\n"
                + "        }, 1);\n" + "}";

        reduce = "function reduce(key, values) {\n" + "    var res = 0;\n"
                + "    values.forEach(function(v) {\n" + "        res += v;\n" + "    });\n"
                + "    return res;\n" + "}";

    }/*from ww  w .j a  v  a 2  s. c  o  m*/
    DBObject query = this.getCachedFilter(filter);
    LOG.debug("Filter query is:\n{}", query);
    String queryString = query.toString();
    DBCollection elmnts = getCollection(Element.class);
    MapReduceCommand cmd = new MapReduceCommand(elmnts, map, reduce, null, INLINE, query);
    MapReduceOutput output = elmnts.mapReduce(cmd);
    // List<BasicDBObject> results = (List<BasicDBObject>) output.getCommandResult().get( "results" );
    Iterator<DBObject> iterator = output.results().iterator();
    List<BasicDBObject> results = new ArrayList<BasicDBObject>();
    while (iterator.hasNext()) {
        results.add((BasicDBObject) iterator.next());

    }

    LOG.debug("MapReduce produced {} results", results.size());
    DBCollection histCollection = this.db.getCollection(TBL_HISTOGRAMS);
    BasicDBObject old = new BasicDBObject("_id", key);
    BasicDBObject res = new BasicDBObject(old.toMap());
    res.put("results", results);
    histCollection.update(old, res, true, false);

    DBCursor cursor = histCollection.find(new BasicDBObject("_id", key));

    if (cursor.count() == 0) {
        return null;
    }
    long end = System.currentTimeMillis();
    LOG.debug("MapReduce took {} seconds", (end - start) / 1000);
    return (DBObject) cursor.next().get("results");
}