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:mongoDB.MongoDbClientAllImagesAsByteArrays.java

License:Open Source License

@Override
public int viewProfile(int requesterID, int profileOwnerID, HashMap<String, ByteIterator> result,
        boolean insertImage, boolean testMode) {

    int retVal = 0;
    if (requesterID < 0 || profileOwnerID < 0)
        return -1;

    com.mongodb.DB db = null;//from w  w  w  .  jav a2 s.  com
    try {
        db = mongo.getDB(database);
        db.requestStart();
        DBCollection collection = db.getCollection("users");
        DBObject q = new BasicDBObject().append("_id", profileOwnerID);
        q.removeField("tpic");
        BasicDBObject attrs = new BasicDBObject("tpic", 0);

        DBObject queryResult = null;
        queryResult = collection.findOne(q, attrs);

        if (queryResult.get("pic") != null) {
            byte[] myPic = (byte[]) queryResult.get("pic");
            result.put("pic", new ObjectByteIterator(myPic));
            if (testMode) {
                // Save loaded image from database into new image file
                FileOutputStream fos = new FileOutputStream(profileOwnerID + "--proimage.bmp");
                fos.write(myPic);
                fos.close();
            }
            queryResult.removeField("pic");
        }

        String x = queryResult.get("ConfFriends").toString();
        int frndCount = 0;
        if (x.equals("") || (!x.equals("") && (x.substring(2, x.length() - 1)).equals("")))
            frndCount = 0;
        else {
            x = x.substring(2, x.length() - 1);
            frndCount = x.split(",").length;
        }

        int pendCount = 0;
        if (requesterID == profileOwnerID) {
            x = queryResult.get("PendFriends").toString();
            if (x.equals("") || (!x.equals("") && (x.substring(2, x.length() - 1)).equals("")))
                pendCount = 0;
            else {
                x = x.substring(2, x.length() - 1);
                pendCount = x.split(",").length;
            }
        }

        // find number of resources posted for the user
        // queryResult = null;
        // queryResult = collection.findOne(q);
        x = queryResult.get("wallResources").toString();
        int resCount = 0;
        if (x.equals(""))
            resCount = 0;
        else {
            resCount = x.split(",").length;
        }

        if (queryResult != null) {
            // remove the ConfFriends and PendFriends and Resources
            // replace them with counts
            queryResult.removeField("ConfFriends");
            queryResult.removeField("PendFriends");
            queryResult.removeField("wallResources");
            result.putAll(queryResult.toMap());
            result.put("friendcount", new ObjectByteIterator(Integer.toString(frndCount).getBytes()));
            if (requesterID == profileOwnerID) {
                result.put("pendingcount", new ObjectByteIterator(Integer.toString(pendCount).getBytes()));
            }
            result.put("resourcecount", new ObjectByteIterator(Integer.toString(resCount).getBytes()));
        }

    } catch (Exception e) {
        System.out.println(e.toString());
        retVal = -1;
    } finally {
        if (db != null) {
            db.requestDone();
        }
    }
    return retVal;
}

From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java

License:Open Source License

@Override
public int listFriends(int requesterID, int profileOwnerID, Set<String> fields,
        Vector<HashMap<String, ByteIterator>> result, boolean insertImage, boolean testMode) {
    int retVal = 0;
    if (requesterID < 0 || profileOwnerID < 0)
        return -1;

    // first get all confirmed friendids for profileOwnerID
    com.mongodb.DB db = null;/*from www .j  a v a  2 s.  co m*/
    try {
        db = mongo.getDB(database);
        db.requestStart();
        DBCollection collection = db.getCollection("users");
        DBObject q = new BasicDBObject().append("_id", profileOwnerID);
        q.removeField("pic");
        BasicDBObject attrs = new BasicDBObject("pic", 0);

        DBObject queryResult = null;
        queryResult = collection.findOne(q, attrs);

        String x = queryResult.get("ConfFriends").toString();

        if (!x.equals("")) {
            x = x.substring(2, x.length() - 1);
            if (!x.equals("")) {
                String friendIds[] = x.split(",");
                BasicDBObject query = new BasicDBObject();
                if (!friendListReq) {
                    List<Integer> list = new ArrayList<Integer>();
                    for (int i = 0; i < friendIds.length; i++) {
                        // add to list
                        list.add(Integer.parseInt(friendIds[i].trim()));
                        int cnt = 0;
                        if (i == friendIds.length - 1 || ((i + 1) % 10) == 0) {
                            // query
                            query.put("_id", new BasicDBObject("$in", list));
                            query.removeField("pic");
                            // DBCursor cursor = collection.find(query,
                            // fieldsObj);
                            DBCursor cursor = collection.find(query, attrs);
                            while (cursor.hasNext()) {
                                cnt++;
                                // System.out.println(cursor.next());
                                HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
                                DBObject curs = cursor.next();
                                if (curs.get("tpic") != null) {
                                    byte[] myPic = (byte[]) curs.get("tpic");
                                    vals.put("tpic", new ObjectByteIterator(myPic));
                                    if (testMode) {
                                        // Save loaded image from database
                                        // into new image file
                                        FileOutputStream fos = new FileOutputStream(
                                                profileOwnerID + "-" + cnt + "-mthumbimage.bmp");
                                        fos.write(myPic);
                                        fos.close();
                                    }
                                    curs.removeField("tpic");
                                }
                                vals.putAll(curs.toMap());
                                vals.remove("ConfFriends");
                                vals.remove("PendFriends");
                                vals.remove("wallResources");
                                // needed to do this so the coreworkload
                                // will not need to know the datastore typr
                                if (vals.get("_id") != null) {
                                    String tmp = vals.get("_id") + "";
                                    vals.remove("_id");
                                    vals.put("userid", new ObjectByteIterator(tmp.getBytes()));
                                }
                                result.add(vals);
                            }
                            cursor.close();
                            // empty list
                            list.clear();
                        }
                    }
                } else if (friendListReq) {// retrive one list
                    List<Integer> list = new ArrayList<Integer>();
                    for (int i = 0; i < friendIds.length; i++) {
                        // put all in one list and retrieve instead of
                        // retrieving one by one
                        list.add(Integer.parseInt(friendIds[i].trim()));
                    }
                    query.put("_id", new BasicDBObject("$in", list));
                    query.removeField("pic");
                    // DBCursor cursor = collection.find(query, fieldsObj);
                    DBCursor cursor = collection.find(query, attrs);

                    int cnt = 0;
                    while (cursor.hasNext()) {
                        cnt++;
                        HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
                        DBObject curs = cursor.next();
                        if (curs.get("tpic") != null) {
                            byte[] myPic = (byte[]) curs.get("tpic");
                            vals.put("tpic", new ObjectByteIterator(myPic));
                            if (testMode) {
                                // Save loaded image from database into new
                                // image file
                                FileOutputStream fos = new FileOutputStream(
                                        profileOwnerID + "-" + cnt + "-mthumbimage.bmp");
                                fos.write(myPic);
                                fos.close();
                            }
                            curs.removeField("tpic");
                        }

                        vals.putAll(curs.toMap());
                        vals.remove("ConfFriends");
                        vals.remove("PendFriends");
                        vals.remove("wallResurces");
                        // needed to do this so the coreworkload will not
                        // need to know the datastore typr
                        if (vals.get("_id") != null) {
                            String tmp = vals.get("_id") + "";
                            vals.remove("_id");
                            vals.put("userid", new ObjectByteIterator(tmp.getBytes()));
                        }
                        result.add(vals);
                    }
                    cursor.close();
                }
            }
        }

    } catch (Exception e) {
        System.out.println(e.toString());
        retVal = -1;
    } finally {
        if (db != null) {
            db.requestDone();
        }
    }
    return retVal;

}

From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java

License:Open Source License

@Override
public int viewFriendReq(int profileOwnerID, Vector<HashMap<String, ByteIterator>> values, boolean insertImage,
        boolean testMode) {
    int retVal = 0;
    if (profileOwnerID < 0)
        return -1;

    // first get all pending friendids for profileOwnerID
    com.mongodb.DB db = null;//w ww  .  j  a  v  a  2s. c  o m

    try {
        db = mongo.getDB(database);
        db.requestStart();
        DBCollection collection = db.getCollection("users");
        DBObject q = new BasicDBObject().append("_id", profileOwnerID);
        q.removeField("pic");
        BasicDBObject fields = new BasicDBObject("pic", 0);

        DBObject queryResult = null;
        queryResult = collection.findOne(q, fields);

        String x = queryResult.get("PendFriends").toString();
        if (!x.equals("")) {
            x = x.substring(2, x.length() - 1);
            if (!x.equals("")) {
                String friendIds[] = x.split(",");
                BasicDBObject query = new BasicDBObject();

                List<Integer> list = new ArrayList<Integer>();
                if (!friendListReq) {
                    int cnt = 0;
                    for (int i = 0; i < friendIds.length; i++) {
                        cnt++;
                        HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
                        DBObject frnd = new BasicDBObject().append("_id",
                                Integer.parseInt(friendIds[i].trim()));
                        frnd.removeField("pic");
                        DBObject frndQueryResult = null;
                        frndQueryResult = collection.findOne(frnd, fields);
                        if (frndQueryResult.get("tpic") != null) {
                            byte[] myPic = (byte[]) frndQueryResult.get("tpic");
                            vals.put("tpic", new ObjectByteIterator(myPic));
                            if (testMode) {
                                // Save loaded image from database into new
                                // image file
                                FileOutputStream fos = new FileOutputStream(
                                        profileOwnerID + "-" + i + "-mthumbimage.bmp");
                                fos.write(myPic);
                                fos.close();
                            }
                            frndQueryResult.removeField("tpic");
                        }
                        if (frndQueryResult != null) {
                            frndQueryResult.removeField("ConfFriends");
                            frndQueryResult.removeField("PendFriends");
                            frndQueryResult.removeField("wallResources");
                            vals.putAll(frndQueryResult.toMap());
                        }
                        if (vals.get("_id") != null) {
                            String tmp = vals.get("_id") + "";
                            vals.remove("_id");
                            vals.put("userid", new ObjectByteIterator(tmp.getBytes()));
                        }
                        values.add(vals);
                    }
                } else if (friendListReq) {// retrive one list
                    for (int i = 0; i < friendIds.length; i++) {
                        // put all in one list and retrieve instead of
                        // retrieving one by one
                        list.add(Integer.parseInt(friendIds[i].trim()));
                    }
                    query.put("_id", new BasicDBObject("$in", list));
                    query.removeField("pic");
                    DBCursor cursor = collection.find(query, fields);
                    int cnt = 0;
                    while (cursor.hasNext()) {
                        cnt++;
                        // System.out.println(cursor.next());
                        HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
                        DBObject curs = cursor.next();
                        if (curs.get("tpic") != null) {
                            byte[] myPic = (byte[]) curs.get("tpic");
                            vals.put("tpic", new ObjectByteIterator(myPic));
                            if (testMode) {
                                // Save loaded image from database into new
                                // image file
                                FileOutputStream fos = new FileOutputStream(
                                        profileOwnerID + "-" + cnt + "-mthumbimage.bmp");
                                fos.write(myPic);
                                fos.close();
                            }
                            curs.removeField("tpic");
                        }
                        vals.putAll(curs.toMap());
                        vals.remove("PendFriends");
                        vals.remove("ConfFriends");
                        vals.remove("wallResources");
                        // needed to do this so the coreworkload will not
                        // need to know the datastore typr
                        if (vals.get("_id") != null) {
                            String tmp = vals.get("_id") + "";
                            vals.remove("_id");
                            vals.put("userid", new ObjectByteIterator(tmp.getBytes()));
                        }
                        values.add(vals);
                    }
                    cursor.close();
                }

            }

        }

    } catch (Exception e) {
        System.out.println(e.toString());
        retVal = -1;
    } finally {
        if (db != null) {
            db.requestDone();
        }
    }
    return retVal;

}

From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java

License:Open Source License

@Override
public int viewTopKResources(int requesterID, int profileOwnerID, int k,
        Vector<HashMap<String, ByteIterator>> result) {
    int retVal = 0;
    if (profileOwnerID < 0 || requesterID < 0 || k < 0)
        return -1;

    com.mongodb.DB db = null;//from ww  w  . j  av  a 2 s.co m

    try {
        db = mongo.getDB(database);
        db.requestStart();
        if (scanResources) {
            DBCollection collection = db.getCollection("resources");
            // find all resources that belong to profileOwnerID
            // sort them by _id desc coz we want latest ones and get the top
            // k
            DBObject q = new BasicDBObject().append("walluserid", Integer.toString(profileOwnerID));
            DBCursor queryResult = null;
            queryResult = collection.find(q);
            // DBObject s = new BasicDBObject().append("_id", -1); //desc
            DBObject s = new BasicDBObject(); // desc
            s.put("_id", -1);
            queryResult = queryResult.sort(s);
            queryResult = queryResult.limit(k);
            Iterator it = queryResult.iterator();
            while (it.hasNext()) {
                HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
                DBObject oneRes = new BasicDBObject();
                oneRes.putAll((DBObject) it.next());
                vals.putAll(oneRes.toMap());
                // needed to do this so the coreworkload will not need to
                // know the datastore type
                if (vals.get("_id") != null) {
                    String tmp = vals.get("_id") + "";
                    vals.remove("_id");
                    vals.put("rid", new ObjectByteIterator(tmp.getBytes()));
                }
                if (vals.get("walluserid") != null) {
                    String tmp = vals.get("walluserid") + "";
                    vals.remove("walluserid");
                    vals.put("walluserid", new ObjectByteIterator(tmp.getBytes()));
                }
                if (vals.get("creatorid") != null) {
                    String tmp = vals.get("creatorid") + "";
                    vals.remove("creatorid");
                    vals.put("creatorid", new ObjectByteIterator(tmp.getBytes()));
                }
                result.add(vals);
            }
            queryResult.close();

        } else {
            DBCollection collection = db.getCollection("users");
            DBObject q = new BasicDBObject().append("_id", profileOwnerID);
            DBObject queryResult = null;
            queryResult = collection.findOne(q);
            String x = queryResult.get("wallResources").toString();
            if (!x.equals("[ ]")) {
                x = x.substring(1, x.length() - 1);
                String resourceIds[] = x.split(",");
                BasicDBObject query = new BasicDBObject();
                List<Integer> list = new ArrayList<Integer>();
                for (int i = resourceIds.length - 1; i >= resourceIds.length
                        - Math.min(k, resourceIds.length); i--) { // to
                    // limit
                    // it
                    list.add(Integer.parseInt(resourceIds[i].trim()));
                }
                collection = db.getCollection("resources");
                query.put("_id", new BasicDBObject("$in", list));
                DBCursor cursor = collection.find(query);
                while (cursor.hasNext()) {
                    HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
                    vals.putAll(cursor.next().toMap());
                    if (vals.get("_id") != null) {
                        String tmp = vals.get("_id") + "";
                        vals.remove("_id");
                        vals.put("rid", new ObjectByteIterator(tmp.getBytes()));
                    }
                    if (vals.get("walluserid") != null) {
                        String tmp = vals.get("walluserid") + "";
                        vals.remove("walluserid");
                        vals.put("walluserid", new ObjectByteIterator(tmp.getBytes()));
                    }
                    if (vals.get("creatorid") != null) {
                        String tmp = vals.get("creatorid") + "";
                        vals.remove("creatorid");
                        vals.put("creatorid", new ObjectByteIterator(tmp.getBytes()));
                    }
                    result.add(vals);
                }
                cursor.close();
            }
        }
    } catch (Exception e) {
        System.out.println(e.toString());
        retVal = -1;
    } finally {
        if (db != null) {
            db.requestDone();
        }
    }
    return retVal;

}

From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java

License:Open Source License

public int getCreatedResources(int creatorID, Vector<HashMap<String, ByteIterator>> result) {
    int retVal = 0;
    if (creatorID < 0)
        return -1;

    com.mongodb.DB db = null;/*from  w w w.  ja  va  2s  . c om*/

    try {
        db = mongo.getDB(database);
        db.requestStart();
        DBCollection collection = db.getCollection("resources");
        // find all resources that belong to profileOwnerID
        // sort them by _id desc coz we want latest ones and get the top k
        DBObject q = new BasicDBObject().append("creatorid", Integer.toString(creatorID));
        DBCursor queryResult = null;
        queryResult = collection.find(q);
        // DBObject s = new BasicDBObject().append("_id", -1); //desc
        DBObject s = new BasicDBObject(); // desc
        s.put("_id", -1);
        queryResult = queryResult.sort(s);
        Iterator it = queryResult.iterator();
        while (it.hasNext()) {
            HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
            DBObject oneRes = new BasicDBObject();
            oneRes.putAll((DBObject) it.next());
            vals.putAll(oneRes.toMap());
            // needed to do this so the coreworkload will not need to know
            // the datastore typr
            if (vals.get("_id") != null) {
                String tmp = vals.get("_id") + "";
                vals.remove("_id");
                vals.put("rid", new ObjectByteIterator(tmp.getBytes()));
            }
            if (vals.get("creatorid") != null) {
                String tmp = vals.get("creatorid") + "";
                vals.remove("creatorid");
                vals.put("creatorid", new ObjectByteIterator(tmp.getBytes()));
            }
            result.add(vals);
        }
        queryResult.close();

    } catch (Exception e) {
        System.out.println(e.toString());
        retVal = -1;
    } finally {
        if (db != null) {
            db.requestDone();
        }
    }
    return retVal;

}

From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java

License:Open Source License

@Override
public int viewCommentOnResource(int requesterID, int profileOwnerID, int resourceID,
        Vector<HashMap<String, ByteIterator>> result) {
    int retVal = 0;
    if (profileOwnerID < 0 || requesterID < 0 || resourceID < 0)
        return -1;

    com.mongodb.DB db = null;/*ww  w.  ja  v a2 s. com*/

    try {
        db = mongo.getDB(database);
        db.requestStart();
        if (!manipulationArray) {
            DBCollection collection = db.getCollection("manipulation");
            // find all resources that belong to profileOwnerID
            // sort them by _id desc coz we want latest ones and get the top
            // k
            DBObject q = new BasicDBObject().append("rid", Integer.toString(resourceID));
            DBCursor queryResult = null;
            queryResult = collection.find(q);
            Iterator<DBObject> it = queryResult.iterator();
            while (it.hasNext()) {
                HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
                DBObject oneRes = new BasicDBObject();
                oneRes.putAll((DBObject) it.next());
                vals.putAll(oneRes.toMap());
                result.add(vals);
            }
            queryResult.close();
        } else {
            DBCollection collection = db.getCollection("resources");
            DBObject q = new BasicDBObject().append("_id", resourceID);
            DBObject queryResult = null;
            queryResult = collection.findOne(q);
            if (queryResult.get("Manipulations") != "" && queryResult.get("Manipulations") != null) {
                ArrayList<DBObject> mans = (ArrayList<DBObject>) queryResult.get("Manipulations");
                for (int i = 0; i < mans.size(); i++) {
                    result.add((HashMap<String, ByteIterator>) mans.get(i).toMap());
                }
            }
        }

    } catch (Exception e) {
        System.out.println(e.toString());
        retVal = -1;
    } finally {
        if (db != null) {
            db.requestDone();
        }
    }
    return retVal;
}

From source file:mvm.rya.mongodb.dao.SimpleMongoDBNamespaceManager.java

License:Apache License

@Override
public String getNamespace(String prefix) throws RyaDAOException {
    DBObject query = new BasicDBObject().append(PREFIX, prefix);
    DBCursor cursor = nsColl.find(query);
    String nameSpace = prefix;/*  w  w  w.j  a v a  2 s  .  co  m*/
    while (cursor.hasNext()) {
        DBObject obj = cursor.next();
        nameSpace = (String) obj.toMap().get(NAMESPACE);
    }
    return nameSpace;
}

From source file:mvm.rya.mongodb.dao.SimpleMongoDBStorageStrategy.java

License:Apache License

@Override
public RyaStatement deserializeDBObject(DBObject queryResult) {
    Map result = queryResult.toMap();
    String subject = (String) result.get(SUBJECT);
    String object = (String) result.get(OBJECT);
    String objectType = (String) result.get(OBJECT_TYPE);
    String predicate = (String) result.get(PREDICATE);
    String context = (String) result.get(CONTEXT);
    RyaType objectRya = null;/*from   ww  w . j a va 2 s. co m*/
    if (objectType.equalsIgnoreCase("http://www.w3.org/2001/XMLSchema#anyURI")) {
        objectRya = new RyaURI(object);
    } else {
        objectRya = new RyaType(factory.createURI(objectType), object);
    }

    if (!context.isEmpty()) {
        return new RyaStatement(new RyaURI(subject), new RyaURI(predicate), objectRya, new RyaURI(context));
    }
    return new RyaStatement(new RyaURI(subject), new RyaURI(predicate), objectRya);
}

From source file:nl.kpmg.lcm.server.data.dao.mongo.MongoStorageReadConverter.java

License:Apache License

@Override
public Storage convert(DBObject source) {
    Storage storage = new Storage();
    storage.setId(source.get("_id").toString());
    storage.setName((String) source.get("name"));
    storage.setStatus((String) source.get("status"));
    storage.setType((String) source.get("type"));
    Map sourceMap = source.toMap();
    Map enrichmentPropertiesMap = (Map) sourceMap.get("enrichment-properties");
    if (enrichmentPropertiesMap != null) {
        storage.setEnrichmentProperties(enrichmentPropertiesMap);
    }//from   w  ww.  ja  v  a2s  . c  o m
    Map optionsMap = (Map) sourceMap.get("options");
    if (optionsMap != null) {
        storage.setOptions(optionsMap);
    }

    Map credentialsMap = (Map) sourceMap.get("credentials");
    if (credentialsMap != null) {
        storage = mapStorage(storage, credentialsMap);
    }

    return storage;
}

From source file:org.apache.metamodel.mongodb.common.MongoDBUtils.java

License:Apache License

/**
 * Converts a MongoDB data object {@link DBObject} into MetaModel
 * {@link Row}.//  w w w .j  a  va  2  s.c  om
 * 
 * @param dbObject
 *            a MongoDB object storing data.
 * @param header
 *            a header describing the columns of the data stored.
 * @return the MetaModel {@link Row} result object.
 */
public static Row toRow(DBObject dbObject, DataSetHeader header) {
    if (dbObject == null) {
        return null;
    }

    final Map<?, ?> map = dbObject.toMap();
    return toRow(map, header);
}