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:com.edgytech.umongo.ServerPanel.java

License:Apache License

public void fsync(ButtonBase button) {
    DBObject cmd = new BasicDBObject("fsync", 1);
    if (false) {/*from   w w w .  ja  v a 2 s.co  m*/
        cmd.put("async", 1);
    }
    DB admin = getServerNode().getServerMongoClient().getDB("admin");
    new DbJobCmd(admin, cmd).addJob();
}

From source file:com.effektif.mongo.MongoWorkflowInstanceStore.java

License:Apache License

@Override
public WorkflowInstanceImpl getWorkflowInstanceImplById(WorkflowInstanceId workflowInstanceId) {
    if (workflowInstanceId == null) {
        return null;
    }//from  w w w  .j  a v  a  2s. c  o  m
    DBObject query = createLockQuery();
    query.put(_ID, new ObjectId(workflowInstanceId.getInternal()));

    BasicDBObject dbWorkflowInstance = workflowInstancesCollection.findOne("get-workflow-instance", query);
    if (dbWorkflowInstance == null) {
        return null;
    }

    return readWorkflowInstanceImpl(dbWorkflowInstance);
}

From source file:com.effektif.mongo.MongoWorkflowInstanceStore.java

License:Apache License

@Override
public WorkflowInstanceImpl lockWorkflowInstance(WorkflowInstanceId workflowInstanceId) {
    Exceptions.checkNotNullParameter(workflowInstanceId, "workflowInstanceId");

    DBObject query = createLockQuery();
    query.put(_ID, new ObjectId(workflowInstanceId.getInternal()));

    DBObject update = createLockUpdate();

    DBObject retrieveFields = new BasicDBObject().append(ARCHIVED_ACTIVITY_INSTANCES, false);

    BasicDBObject dbWorkflowInstance = workflowInstancesCollection.findAndModify("lock-workflow-instance",
            query, update, retrieveFields);
    if (dbWorkflowInstance == null) {
        return null;
    }/*from   w w  w .j a  va2 s. c  o m*/

    WorkflowInstanceImpl workflowInstance = readWorkflowInstanceImpl(dbWorkflowInstance);
    workflowInstance.trackUpdates(false);
    return workflowInstance;
}

From source file:com.effektif.mongo.MongoWorkflowInstanceStore.java

License:Apache License

@Override
public WorkflowInstanceImpl lockWorkflowInstanceWithJobsDue() {

    DBObject query = createLockQuery();
    query.put(JobFields.DONE, new BasicDBObject("$exists", false));
    query.put(JOBS + "." + JobFields.DUE_DATE, new BasicDBObject("$lte", Time.now().toDate()));

    DBObject update = createLockUpdate();

    DBObject retrieveFields = new BasicDBObject().append(ARCHIVED_ACTIVITY_INSTANCES, false);

    BasicDBObject dbWorkflowInstance = workflowInstancesCollection.findAndModify("lock-workflow-instance",
            query, update, retrieveFields, new BasicDBObject(START, 1), false, true, false);
    if (dbWorkflowInstance == null) {
        return null;
    }//from   w  ww  .  j ava  2 s  .c  o m

    WorkflowInstanceImpl workflowInstance = readWorkflowInstanceImpl(dbWorkflowInstance);
    workflowInstance.trackUpdates(false);
    return workflowInstance;
}

From source file:com.eharmony.matching.seeking.translator.mongodb.MongoQueryTranslator.java

License:Apache License

@SuppressWarnings("unchecked")
protected DBObject merge(DBObject[] queries) {
    if (queries.length == 1) {
        return queries[0];
    }//from ww w .  j  a  va 2  s  .c o  m
    DBObject merged = object();
    Set<DBObject> ands = new LinkedHashSet<DBObject>();
    String andSymbol = MongoOperator.AND.symbol();
    // multimap => map<key,Collection<values>> 
    Map<String, Collection<DBObject>> map = multimap(queries).asMap();
    for (Entry<String, Collection<DBObject>> entry : map.entrySet()) {
        String key = entry.getKey();
        // merge multiple ANDs
        if (andSymbol.equals(key)) {
            for (DBObject o : entry.getValue()) {
                ands.addAll((Set<DBObject>) o.get(andSymbol));
            }
        } else if (entry.getValue().size() == 1) {
            DBObject single = entry.getValue().iterator().next();
            merged.put(key, single.get(key));
        } else {
            for (DBObject o : entry.getValue()) {
                ands.add(o);
            }
        }
    }
    // add ANDs
    if (ands.size() > 0) {
        merged.put(andSymbol, ands);
    }
    return merged;
}

From source file:com.eharmony.matching.seeking.translator.mongodb.MongoQueryTranslator.java

License:Apache License

@Override
public DBObject between(String fieldName, Object from, Object to) {
    DBObject range = object();
    range.put(MongoOperator.GREATER_THAN_OR_EQUAL.symbol(), from);
    range.put(MongoOperator.LESS_THAN_OR_EQUAL.symbol(), to);
    return object(fieldName, range);
}

From source file:com.eharmony.matching.seeking.translator.mongodb.MongoQueryTranslator.java

License:Apache License

@Override
public <T, R> DBObject translateProjection(Query<T, R> query) {
    if (query.getReturnFields().size() > 0) {
        DBObject fields = object();
        for (String field : query.getReturnFields()) {
            fields.put(getPropertyResolver().resolve(field, query.getEntityClass()), 1);
        }/*from   w  w  w  .j  a  va  2s . c  o  m*/
        // if _id is not present then exclude it
        if (!fields.containsField(Mapper.ID_KEY)) {
            fields.put(Mapper.ID_KEY, 0);
        }
        return fields;
    } else {
        return null;
    }
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

License:Apache License

/**
 * Deletes an item from the db//from   w ww.jav a 2s. co  m
 *
 * @param entity
 *            The item to delete
 * @throws IOException
 */
public void delete(UXBEntity entity) throws IOException {
    if (entity == null) {
        logger.warn("no UXBEntity given, no action");
        return;
    }

    // delete item
    DBObject item = getNewsById(Long.parseLong(entity.getUuid()), entity.getLanguage());
    if (item != null) {
        // delete file from filesystem
        URL url = new URL((String) item.get("url"));
        File file = new File(webpath + url.getPath());
        if (file.exists()) {
            // Try acquiring the lock without blocking. This method returns
            // null or throws an exception if the file is already locked.
            try {
                FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
                // Try to lock the file
                FileLock lock = channel.tryLock();
                // Delete the file
                boolean deleteSuccess = file.delete();
                if (!deleteSuccess) {
                    logger.debug("Couldn't delete file, file not found. Maybe someone else has deleted it.");
                }
                // Release the lock
                lock.release();
                lock.channel().close();
            } catch (OverlappingFileLockException e) {
                logger.info("File is already locked in this thread or virtual machine");
            }
        }
        // delete the article
        DBObject query = new BasicDBObject();
        query.put("_id", item.get("_id"));
        query.put("fs_id", item.get("fs_id"));
        query.put("language", item.get("language"));
        dbNews.remove(query);
    }

}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

License:Apache License

/**
 * Deletes every item older than the creationTime of the UXBEntity.
 *
 * @param entity//from  www .  j a  v  a 2  s  .  co  m
 *            Entity containing the expireDate (= createTime of the entity)
 */
public void cleanup(UXBEntity entity) {

    if (entity == null) {
        logger.warn("no UXBEntity given, no action");
        return;
    }

    // delete newsdrilldown
    DBCursor items = getNewsByLastmodified(entity.getCreateTime());
    for (DBObject item : items) {
        DBObject query = new BasicDBObject();
        query.put("_id", item.get("_id"));
        query.put("fs_id", item.get("fs_id"));
        query.put("language", item.get("language"));
        dbNews.remove(query);
    }

    // delete categories
    items = getCategoriesByLastmodified(entity.getCreateTime());
    for (DBObject item : items) {
        DBObject query = new BasicDBObject();
        query.put("_id", item.get("_id"));
        query.put("fs_id", item.get("fs_id"));
        query.put("language", item.get("language"));
        dbCategories.remove(query);
    }

    // delete metaCategories
    items = getMetaCategoriesByLastmodified(entity.getCreateTime());
    for (DBObject item : items) {
        DBObject query = new BasicDBObject();
        query.put("_id", item.get("_id"));
        query.put("fs_id", item.get("fs_id"));
        query.put("language", item.get("language"));
        dbMetaCategories.remove(query);
    }

}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

License:Apache License

/**
 * handle the update and save process for the news.
 *
 * @param news/*from   www  .j  a v  a2 s. co  m*/
 *            the News object
 * @throws Exception
 *             the exception
 */
private void handleNews(News news) throws Exception {

    // 1. save the product
    DBObject item = getNewsById(news.getFs_id(), news.getLanguage());
    if (item != null) {
        item.put("fs_id", news.getFs_id());
        item.put("language", news.getLanguage());
        item.put("headline", news.getHeadline());
        item.put("subHeadline", news.getSubheadline());
        item.put("headline", news.getHeadline());
        item.put("teaser", news.getTeaser());
        item.put("content", news.getContent());
        item.put("date", news.getDate());
        item.put("url", news.getUrl());
        item.put("version", news.getVersion());
        item.put("lastmodified", news.getLastmodified());

        BasicDBObject query = new BasicDBObject();
        news.setId((Long) item.get("_id"));
        query.put("_id", news.getId());

        dbNews.update(query, item);
    } else {
        item = new BasicDBObject();

        item.put("fs_id", news.getFs_id());
        item.put("language", news.getLanguage());
        item.put("headline", news.getHeadline());
        item.put("subHeadline", news.getSubheadline());
        item.put("headline", news.getHeadline());
        item.put("teaser", news.getTeaser());
        item.put("content", news.getContent());
        item.put("date", news.getDate());
        item.put("url", news.getUrl());
        item.put("version", news.getVersion());
        item.put("lastmodified", news.getLastmodified());

        news.setId(generateIdentifier(NEWS_COLLECTION_NAME, mdb));
        item.put("_id", news.getId());

        dbNews.insert(item);
    }

    // 2. save categories
    // add categories to news
    BasicDBList catList = new BasicDBList();
    if (news.getCategories() != null) {
        for (NewsCategory cat : news.getCategories()) {
            handleCategory(cat, news);

            DBObject tmpCat = getCategoryById(cat.getFs_id(), cat.getLanguage());
            //catList.add(new DBRef(this.mdb, this.CATEGORY_COLLECTION_NAME, tmpCat.get("_id")));
            catList.add(tmpCat.get("_id"));
        }
    }

    // add list of categories to the news
    //      item.put("categories", catList);
    item.put("categories_$$manyToManyIds", catList);

    BasicDBObject query = new BasicDBObject();
    query.put("_id", news.getId());

    dbNews.update(query, item);
}