Example usage for com.mongodb DBObject get

List of usage examples for com.mongodb DBObject get

Introduction

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

Prototype

Object get(String key);

Source Link

Document

Gets a field from this object by a given name.

Usage

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  2s.  co  m
        byte[] binary = (byte[]) result.get("binary");
        return new ByteArrayInputStream(binary);
    }
}

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

License:Open Source License

public void delete(String username, String password, Resource resource) {
    DBCollection files = mongo.getDataBase().getCollection("files");
    DBCollection contents = mongo.getDataBase().getCollection("contents");

    for (Resource child : getChildren(resource)) {
        delete(username, password, child);
    }/*from w  ww  .  ja  v  a 2 s .  c o  m*/

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

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

    files.remove(filter);
    contents.remove(new BasicDBObject("resource", resource.getId()));

    try {
        indexService.remove(username, password, resource.getId().toString());
    } catch (IndexException ex) {
        Logger.getLogger(MongoFileService.class.getName()).log(Level.SEVERE, null, ex);
    }
}

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

License:Open Source License

protected Resource buildResource(DBObject obj) {
    if (obj == null) {
        return null;
    }/*  ww w  . j  a  v  a2s.  c o  m*/

    Resource childRes = new Resource((ObjectId) obj.get("_id"), (String) obj.get("name"),
            (Date) obj.get("creationDate"), (Date) obj.get("modificationDate"));

    if ("FILE".equals(obj.get("type"))) {
        childRes.setType(Resource.ResourceType.FILE);
        childRes.setContentLength(((Number) obj.get("contentLength")).intValue()); // TODO: move to longValue
        childRes.setContentType((String) obj.get("contentType"));
    }

    return childRes;
}

From source file:com.app.mongoDao.MongoBookDao.java

@Override
public List<Book> listBook() {
    ArrayList<Book> booklist = new ArrayList<>();
    try {/*from w ww.  ja  v a2 s. c  o m*/
        DBCollection data = DatabaseConfig.configure();
        BasicDBObject orderBy = new BasicDBObject("id", 1);
        DBCursor docs = data.find().sort(orderBy);

        while (docs.hasNext()) {
            DBObject doc = docs.next();
            Book librarybook = new Book(doc.get("bookname").toString(), doc.get("author").toString());
            booklist.add(librarybook);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return booklist;
}

From source file:com.appdynamics.monitors.mongo.MongoDBMonitor.java

License:Apache License

private void printReplicaStats(DBObject replicaStats) {
    if (replicaStats != null) {
        String replicaStatsPath = getReplicaStatsMetricPrefix();
        BasicDBList members = (BasicDBList) replicaStats.get("members");
        for (int i = 0; i < members.size(); i++) {
            DBObject member = (DBObject) members.get(i);
            printMetric(replicaStatsPath + member.get("name") + METRIC_SEPARATOR + "Health",
                    (Number) member.get("health"));
            printMetric(replicaStatsPath + member.get("name") + METRIC_SEPARATOR + "State",
                    (Number) member.get("state"));
            printMetric(replicaStatsPath + member.get("name") + METRIC_SEPARATOR + "Uptime",
                    (Number) member.get("uptime"));
        }//from   www .  j a  v  a  2  s . c o m
    }
}

From source file:com.appdynamics.monitors.mongo.MongoDBMonitor.java

License:Apache License

private void printDBStats(DBObject dbStats) {
    if (dbStats != null) {
        String dbStatsPath = getDBStatsMetricPrefix(dbStats.get("db").toString());
        printNumericMetricsFromMap(dbStats.toMap(), dbStatsPath);
    }/*from www  .  ja v a 2 s .  c o m*/
}

From source file:com.arhs.spring.cache.mongo.MongoCache.java

License:Open Source License

private void updateExpireIndex(Index newExpireIndex) {
    final IndexOperations indexOperations = mongoTemplate.indexOps(collectionName);
    final DBCollection collection = mongoTemplate.getCollection(collectionName);
    final List<DBObject> indexes = collection.getIndexInfo();

    final Optional<DBObject> expireOptional = indexes.stream()
            .filter(index -> INDEX_NAME.equals(index.get("name"))).findFirst();

    if (expireOptional.isPresent()) {
        final DBObject expire = expireOptional.get();
        final long ttl = (long) expire.get("expireAfterSeconds");

        if (ttl != this.ttl) {
            indexOperations.dropIndex(INDEX_NAME);
        }/*from   ww  w  .  j  av a2  s  .  co  m*/
    }

    indexOperations.ensureIndex(newExpireIndex);
}

From source file:com.arquivolivre.mongocom.management.CollectionManager.java

License:Apache License

private <A extends Object> void loadObject(A object, DBObject document)
        throws IllegalAccessException, IllegalArgumentException, SecurityException, InstantiationException {
    Field[] fields = object.getClass().getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);/* w  w  w .  j a v  a  2s  .  c o m*/
        String fieldName = field.getName();
        Object fieldContent = document.get(fieldName);
        if (fieldContent instanceof BasicBSONList) {
            Class<?> fieldArgClass = null;
            ParameterizedType genericFieldType = (ParameterizedType) field.getGenericType();
            Type[] fieldArgTypes = genericFieldType.getActualTypeArguments();
            for (Type fieldArgType : fieldArgTypes) {
                fieldArgClass = (Class<?>) fieldArgType;
            }
            List<Object> list = new ArrayList<>();
            boolean isInternal = field.isAnnotationPresent(Internal.class);
            for (Object item : (BasicBSONList) fieldContent) {
                if (isInternal) {
                    Object o = fieldArgClass.newInstance();
                    loadObject(o, (DBObject) item);
                    list.add(o);
                } else {
                    list.add(item);
                }
            }
            field.set(object, list);
        } else if ((fieldContent != null) && field.getType().isEnum()) {
            field.set(object, Enum.valueOf((Class) field.getType(), (String) fieldContent));
        } else if ((fieldContent != null) && field.isAnnotationPresent(Reference.class)) {
            field.set(object, findById(field.getType(), ((org.bson.types.ObjectId) fieldContent).toString()));
        } else if (field.isAnnotationPresent(ObjectId.class)) {
            field.set(object, ((org.bson.types.ObjectId) document.get("_id")).toString());
        } else if (field.getType().isPrimitive() && (fieldContent == null)) {
        } else if (fieldContent != null) {
            field.set(object, fieldContent);
        }
    }
}

From source file:com.arquivolivre.mongocom.utils.IntegerGenerator.java

License:Apache License

@Override
public Integer generateValue(Class parent, DB db) {
    DBCollection collection = db.getCollection("values_" + parent.getSimpleName());
    DBObject o = collection.findOne();
    int value = 0;
    if (o != null) {
        value = (int) o.get("generatedValue");
    } else {//from   w w w .  j  a va2  s  . c o m
        o = new BasicDBObject("generatedValue", value);
    }
    o.put("generatedValue", ++value);
    collection.save(o);
    return value;
}

From source file:com.avanza.ymer.FakeDocumentCollection.java

License:Apache License

@Override
public void update(DBObject newVersion) {
    Iterator<DBObject> it = collection.iterator();
    while (it.hasNext()) {
        DBObject dbObject = it.next();
        if (dbObject.get("_id").equals(newVersion.get("_id"))) {
            it.remove();// w  w  w.ja v  a 2  s .c  o m
            collection.add(newVersion);
            return;
        }
    }
    // No object found, do insert
    insert(newVersion);
}