Example usage for com.mongodb DBCollection findOne

List of usage examples for com.mongodb DBCollection findOne

Introduction

In this page you can find the example usage for com.mongodb DBCollection findOne.

Prototype

@Nullable
public DBObject findOne(final Object id) 

Source Link

Document

Get a single document from collection by '_id'.

Usage

From source file:ch.windmobile.server.social.mongodb.UserServiceImpl.java

License:Open Source License

@Override
public List<Favorite> removeFromFavorites(String email, List<Favorite> favoritesToRemove) throws UserNotFound {
    if (email == null) {
        throw new IllegalArgumentException("Email cannot be null");
    }/*  w  w w  . ja  va2 s  . c o m*/
    DBCollection col = db.getCollection(MongoDBConstants.COLLECTION_USERS);
    // Search user by email
    DBObject userDb = col.findOne(new BasicDBObject(MongoDBConstants.USER_PROP_EMAIL, email));
    if (userDb == null) {
        throw new UserNotFound("Unable to find user with email '" + email + "'");
    }

    DBObject favoritesDb = (DBObject) userDb.get(MongoDBConstants.USER_PROP_FAVORITES);
    if (favoritesDb == null) {
        favoritesDb = new BasicDBObject();
    }

    if (favoritesToRemove != null) {
        for (Favorite favoriteToRemove : favoritesToRemove) {
            DBObject favoriteItemsDb = (DBObject) favoritesDb.get(favoriteToRemove.getStationId());
            if (favoriteItemsDb != null) {
                favoritesDb.removeField(favoriteToRemove.getStationId());
            }
        }
    }
    userDb.put(MongoDBConstants.USER_PROP_FAVORITES, favoritesDb);
    col.save(userDb);

    List<Favorite> returnValue = new ArrayList<Favorite>(favoritesDb.keySet().size());
    for (String stationId : favoritesDb.keySet()) {
        Favorite favorite = new Favorite();
        favorite.setStationId(stationId);
        DBObject favoriteItemDb = (DBObject) favoritesDb.get(stationId);
        favorite.setLastMessageId((Long) favoriteItemDb.get(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID));
        returnValue.add(favorite);
    }
    return returnValue;
}

From source file:ch.windmobile.server.social.mongodb.UserServiceImpl.java

License:Open Source License

@Override
public void clearFavorites(String email) throws UserNotFound {
    if (email == null) {
        throw new IllegalArgumentException("Email cannot be null");
    }/*w  w w. j a  v  a 2  s  .  co m*/
    DBCollection col = db.getCollection(MongoDBConstants.COLLECTION_USERS);
    // Search user by email
    DBObject userDb = col.findOne(new BasicDBObject(MongoDBConstants.USER_PROP_EMAIL, email));
    if (userDb == null) {
        throw new UserNotFound("Unable to find user with email '" + email + "'");
    }

    userDb.removeField(MongoDBConstants.USER_PROP_FAVORITES);
    col.save(userDb);
}

From source file:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java

License:Apache License

private FileMeta findFileMeta(String storageKey, String tableName) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    DBObject query = new BasicDBObject();
    query.put(FIELD_STORAGE_KEY, new ObjectId(storageKey));
    DBCollection c = db.getCollection(tableName + ".files");
    DBObject r = c.findOne(query);
    if (r != null) {
        FileMeta fm = new FileMeta();
        fm.setId(r.get("_id").toString());
        fm.setAppid((int) r.get("appid"));
        fm.setContentType((String) r.get("contentType"));
        fm.setFilename((String) r.get("filename"));
        fm.setLength((long) r.get("length"));
        fm.setDocid((int) r.get("docid"));
        fm.setMd5((String) r.get("md5"));
        fm.setStorageKey(new ObjectId(r.get("storageKey").toString()));
        fm.setUploadDate((Date) r.get("uploadDate"));
        fm.setVid((String) r.get("vid"));
        return fm;
    }// w  ww .  java  2 s.c  o  m
    return null;
}

From source file:com.aperigeek.dropvault.web.dao.MongoFileService.java

License:Open Source License

public Resource getRootFolder(String username) {
    DBCollection files = mongo.getDataBase().getCollection("files");

    DBObject filter = new BasicDBObject();
    filter.put("user", username);
    filter.put("root", true);

    DBObject root = files.findOne(filter);

    if (root == null) {
        BasicDBObject newRoot = new BasicDBObject();
        newRoot.append("type", Resource.ResourceType.FOLDER.toString());
        newRoot.append("name", username);
        newRoot.append("root", true);
        newRoot.append("user", username);
        newRoot.append("creationDate", new Date());
        newRoot.append("modificationDate", new Date());
        files.insert(newRoot);//from  ww w  . j av  a2  s. c  om
        root = newRoot;
    }

    Resource res = buildResource(root);

    return res;
}

From source file:com.aperigeek.dropvault.web.dao.MongoFileService.java

License:Open Source License

public Resource getChild(Resource resource, String name) {
    if ("".equals(name) || ".".equals(name)) {
        return resource;
    }/*from  ww  w  .  jav a 2  s  .c o  m*/

    DBCollection col = mongo.getDataBase().getCollection("files");

    DBObject filter = new BasicDBObject();
    filter.put("name", name);
    filter.put("parent", resource.getId());

    DBObject child = col.findOne(filter);

    Resource childRes = buildResource(child);

    return childRes;
}

From source file:com.aperigeek.dropvault.web.dao.MongoFileService.java

License:Open Source License

public Resource getResourceAt(Resource parent, String... path) throws ResourceNotFoundException {
    DBCollection col = mongo.getDataBase().getCollection("files");

    DBObject obj = null;//  w w w. ja v a2  s  .c o m
    ObjectId id = parent.getId();

    for (String name : path) {
        if ("".equals(name) || ".".equals(name)) {
            continue;
        }

        DBObject filter = new BasicDBObjectBuilder().add("name", name).add("parent", id).get();
        obj = col.findOne(filter);

        if (obj == null) {
            throw new ResourceNotFoundException();
        }

        id = (ObjectId) obj.get("_id");
    }

    return buildResource(obj);
}

From source file:com.aperigeek.dropvault.web.dao.MongoFileService.java

License:Open Source License

public Resource getResource(String id) {
    ObjectId oid = new ObjectId(id);
    DBObject query = new BasicDBObject("_id", oid);
    DBCollection files = mongo.getDataBase().getCollection("files");
    DBObject result = files.findOne(query);
    return buildResource(result);
}

From source file:com.aperigeek.dropvault.web.dao.MongoFileService.java

License:Open Source License

public Resource getParent(Resource res) {
    DBCollection files = mongo.getDataBase().getCollection("files");
    DBObject resQuery = new BasicDBObject("_id", res.getId());
    DBObject dbRes = files.findOne(resQuery);
    DBObject dbParent = files.findOne(new BasicDBObject("_id", dbRes.get("parent")));
    return buildResource(dbParent);
}

From source file:com.aperigeek.dropvault.web.dao.MongoFileService.java

License:Open Source License

public void move(String username, Resource source, String dest) throws ResourceNotFoundException {
    String[] path = dest.split("/");
    Resource parent = getRootFolder(username);
    for (int i = 0; i < path.length - 1; i++) {
        parent = getChild(parent, path[i]);
        if (parent == null) {
            throw new ResourceNotFoundException();
        }//  w w w.j  a v a  2  s  .com
    }

    DBCollection files = mongo.getDataBase().getCollection("files");

    DBObject update = new BasicDBObject("$set", new BasicDBObjectBuilder().append("parent", parent.getId())
            .append("name", path[path.length - 1]).get());

    DBObject filter = new BasicDBObject("_id", source.getId());

    DBObject current = files.findOne(filter);
    files.update(new BasicDBObject("_id", (ObjectId) current.get("parent")),
            new BasicDBObject("$set", new BasicDBObject("modificationDate", new Date())));

    files.update(filter, update);

    files.update(new BasicDBObject("_id", parent.getId()),
            new BasicDBObject("$set", new BasicDBObject("modificationDate", new Date())));
}

From source file:com.aperigeek.dropvault.web.dao.MongoFileService.java

License:Open Source License

public InputStream get(String username, Resource resource, char[] password) throws IOException {
    DBCollection col = mongo.getDataBase().getCollection("contents");

    DBObject filter = new BasicDBObject();
    filter.put("resource", resource.getId());

    DBObject result = col.findOne(filter);
    if (result.containsField("file")) {
        String fileName = (String) result.get("file");
        File dataFile = new File(fileName);
        return readFile(dataFile, username, password);
    } else {//from   w w w . j  a v a 2 s. c  o m
        byte[] binary = (byte[]) result.get("binary");
        return new ByteArrayInputStream(binary);
    }
}