Example usage for com.mongodb DBObject put

List of usage examples for com.mongodb DBObject put

Introduction

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

Prototype

Object put(String key, Object v);

Source Link

Document

Sets a name/value pair in this object.

Usage

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

License:Apache License

private void deleteMetaAndContent(String storageKey, String tableName) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    DBObject query = new BasicDBObject();
    query.put(FIELD_STORAGE_KEY, new ObjectId(storageKey));
    GridFS gfs = new GridFS(db, tableName);
    gfs.remove(query);//from  ww  w.  ja  v  a  2 s  .co m
}

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

License:Apache License

private Set<Integer> queryChunkMap(String storageKey, String tableName) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    DBObject query = new BasicDBObject();
    query.put("storageKey", new ObjectId(storageKey));
    Set<Integer> result = new HashSet<Integer>();
    DBCollection chunkTable = db.getCollection(tableName + ".chunks");
    DBObject ref = new BasicDBObject();
    ref.put("files_id", new ObjectId(storageKey));
    DBObject keys = new BasicDBObject();
    keys.put("n", 1);
    DBCursor cursor = chunkTable.find(ref, keys);
    while (cursor.hasNext()) {
        DBObject o = cursor.next();//from ww  w . j  a  va2 s  . c  o  m
        result.add(Integer.parseInt(o.get("n").toString()));
    }
    return result;
}

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

License:Apache License

public String queryMd5(String storageKey, String tableName) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    DBObject query = new BasicDBObject();
    query.put("storageKey", new ObjectId(storageKey));
    DBCollection fileTable = db.getCollection(tableName + ".files");
    DBCursor cursor = fileTable.find(query);
    if (cursor.hasNext()) {
        DBObject o = cursor.next();//ww  w  . j a va2  s .  c  o  m
        return o.get("md5").toString();
    }
    return "";
}

From source file:co.edu.uniandes.csw.Arquidalgos.usuario.persistence._UsuarioPersistence.java

License:MIT License

public void updateUsuario(UsuarioDTO detail) {
    //      UsuarioEntity entity=entityManager.merge(UsuarioConverter.persistenceDTO2Entity(detail));
    //      UsuarioConverter.entity2PersistenceDTO(entity);

    DBCollection col = null;/*w ww  .  j  a va2 s .  c  o m*/
    DBObject query = null;
    BasicDBObject doc = null;
    DBObject update = new BasicDBObject();

    col = db.getCollection("UsuarioDTO");
    String id = ((UsuarioDTO) detail).getFacebookId();
    query = new BasicDBObject("id", id);
    doc = toDoc(detail);

    update.put("$set", doc);
    col.update(query, update);
}

From source file:com.andiandy.m101j.week3.hw2_3.BlogPostDAO.java

License:Apache License

public void addPostComment(final String name, final String email, final String body, final String permalink) {

    // XXX HW 3.3, Work Here
    // Hints://from   ww w  .  ja v  a 2 s .  com
    // - email is optional and may come in NULL. Check for that.
    // - best solution uses an update command to the database and a suitable
    //   operator to append the comment on to any existing list of comments
    DBObject query = QueryBuilder.start("permalink").is(permalink).get();
    DBObject post = postsCollection.find(query).next();
    BasicDBObject comment = new BasicDBObject();
    comment.append("author", name);
    if (email != null) {
        comment.append("email", email);
    }
    comment.append("body", body);
    post.put("comments", comment);
    postsCollection.update(query, new BasicDBObject("$push", new BasicDBObject("comments", comment)));

}

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);//w  w  w.  j  a  va2s.  co m
        root = newRoot;
    }

    Resource res = buildResource(root);

    return res;
}

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

License:Open Source License

public List<Resource> getChildren(Resource resource) {
    DBCollection col = mongo.getDataBase().getCollection("files");

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

    List<Resource> children = new ArrayList<Resource>();

    DBCursor cursor = col.find(filter);//from w ww  .  jav a 2 s. com
    while (cursor.hasNext()) {
        children.add(buildResource(cursor.next()));
    }

    return children;
}

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;
    }/* ww  w.ja  va  2  s  .com*/

    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 mkcol(String username, String resource)
        throws ResourceAlreadyExistsException, ResourceNotFoundException {
    String[] path = resource.split("/");
    Resource parent = getResourceAt(getRootFolder(username), Arrays.copyOfRange(path, 0, path.length - 2));

    if (getChild(parent, path[path.length - 1]) != null) {
        throw new ResourceAlreadyExistsException();
    }/*from   w  w  w  .  ja  v  a  2 s .c o  m*/

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

    DBObject obj = new BasicDBObject();
    obj.put("type", Resource.ResourceType.FOLDER.toString());
    obj.put("user", username);
    obj.put("name", path[path.length - 1]);
    obj.put("creationDate", new Date());
    obj.put("modificationDate", new Date());
    obj.put("parent", parent.getId());

    col.insert(obj);

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

    return buildResource(obj);
}

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

License:Open Source License

public void put(final String username, String resource, InputStream data, long length, String contentType,
        final char[] password) throws ResourceNotFoundException, IOException {

    final String[] path = resource.split("/");
    Resource parent = getResourceAt(getRootFolder(username), Arrays.copyOfRange(path, 0, path.length - 2));

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

    ContentDetector contentDetector = null;
    if (contentType == null) {
        PipedInputStream pipeIn = new PipedInputStream();
        PipedOutputStream pipeOut = new PipedOutputStream(pipeIn);
        TeeInputStream tee = new TeeInputStream(data, pipeOut, true);
        contentDetector = new ContentDetector(path[path.length - 1], pipeIn);
        contentDetector.start();/*from   w  w  w  .  j  a v  a  2  s  .  co m*/
        data = tee;
    }

    final File dataFile = createDataFile(data, username, password);

    if (contentDetector != null) {
        try {
            contentDetector.join();
            contentType = contentDetector.getContentType();
        } catch (InterruptedException ex) {
            Logger.getLogger(MongoFileService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    Resource child = getChild(parent, path[path.length - 1]);
    if (child != null) {
        DBObject filter = new BasicDBObject();
        filter.put("_id", child.getId());
        DBObject update = new BasicDBObject("modificationDate", new Date());
        update.put("contentLength", length);
        update.put("contentType", contentType);
        files.update(filter, new BasicDBObject("$set", update));

        contents.update(new BasicDBObject("resource", child.getId()),
                new BasicDBObject("$set", new BasicDBObject("file", dataFile.getAbsolutePath())));
    } else {
        DBObject childObj = new BasicDBObject();
        ObjectId objId = new ObjectId();
        childObj.put("_id", objId);
        childObj.put("user", username);
        childObj.put("name", path[path.length - 1]);
        childObj.put("parent", parent.getId());
        childObj.put("type", Resource.ResourceType.FILE.toString());
        childObj.put("creationDate", new Date());
        childObj.put("modificationDate", new Date());
        childObj.put("contentType", contentType);
        childObj.put("contentLength", length);

        files.insert(childObj);

        DBObject content = new BasicDBObject();
        content.put("resource", objId);
        content.put("file", dataFile.getAbsolutePath());

        contents.insert(content);

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

        child = buildResource(childObj);
    }

    final String fContentType = contentType;
    final Resource fChild = child;
    new Thread() {
        public void run() {
            try {
                Map<String, String> metadata = extractionService.extractContent(path[path.length - 1],
                        readFile(dataFile, username, password), fContentType);

                metadata.put("name", path[path.length - 1]);

                indexService.remove(username, new String(password), fChild.getId().toString());
                indexService.index(username, new String(password), fChild.getId().toString(), metadata);
            } catch (Exception ex) {
                Logger.getLogger(MongoFileService.class.getName()).log(Level.SEVERE,
                        "Index failed for " + path[path.length - 1], ex);
            }
        }
    }.start();
}