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.linuxbox.enkive.imap.mongo.MongoImapAccountCreator.java

License:Open Source License

@Override
public void addImapMessages(String username, Date fromDate, Date toDate) throws MessageSearchException {

    Calendar startTime = Calendar.getInstance();
    startTime.setTime(fromDate);/*  w  ww  .  j  a  v a 2s  .  c om*/
    Calendar endTime = Calendar.getInstance();
    endTime.setTime(toDate);

    while (startTime.before(endTime)) {
        Calendar endOfMonth = (Calendar) startTime.clone();
        endOfMonth.set(Calendar.DAY_OF_MONTH, endOfMonth.getActualMaximum(Calendar.DAY_OF_MONTH));

        if (endOfMonth.after(endTime))
            endOfMonth = (Calendar) endTime.clone();

        // Need to get all messages to add
        Set<String> messageIdsToAdd = getMailboxMessageIds(username, startTime.getTime(), endOfMonth.getTime());
        // Need to add messages
        // Get top UID, add from there
        if (!messageIdsToAdd.isEmpty()) {
            // Need to check if folder exists, if not create it and add to
            // user
            // mailbox list
            DBObject mailboxObject = getMessagesFolder(username, startTime.getTime());

            @SuppressWarnings("unchecked")
            HashMap<String, String> mailboxMsgIds = (HashMap<String, String>) mailboxObject
                    .get(MongoEnkiveImapConstants.MESSAGEIDS);

            TreeMap<String, String> sortedMsgIds = new TreeMap<String, String>();
            sortedMsgIds.putAll(mailboxMsgIds);
            long i = 0;
            if (!sortedMsgIds.isEmpty())
                i = Long.valueOf(sortedMsgIds.lastKey());

            for (String msgId : messageIdsToAdd) {
                i++;
                mailboxMsgIds.put(((Long.toString(i))), msgId);
            }
            mailboxObject.put(MongoEnkiveImapConstants.MESSAGEIDS, mailboxMsgIds);

            imapCollection.findAndModify(new BasicDBObject("_id", mailboxObject.get("_id")), mailboxObject);
        }
        startTime.set(Calendar.DAY_OF_MONTH, 1);
        startTime.add(Calendar.MONTH, 1);
    }
}

From source file:com.linuxbox.enkive.imap.mongo.MongoImapAccountCreator.java

License:Open Source License

private DBObject getMessagesFolder(String username, Date date) {
    Calendar mailboxTime = Calendar.getInstance();
    mailboxTime.setTime(date);/* w  ww.  ja  va  2s  .com*/
    DBObject mailboxObject = new BasicDBObject();
    String mailboxPath = MongoEnkiveImapConstants.ARCHIVEDMESSAGESFOLDERNAME + "/"
            + mailboxTime.get(Calendar.YEAR) + "/" + String.format("%02d", mailboxTime.get(Calendar.MONTH) + 1);

    // Get table of user mailboxes
    BasicDBObject userMailboxesSearchObject = new BasicDBObject();
    userMailboxesSearchObject.put(MongoEnkiveImapConstants.USER, username);
    DBObject userMailboxesObject = imapCollection.findOne(userMailboxesSearchObject);
    // Check for mailbox we're looking for
    @SuppressWarnings("unchecked")
    HashMap<String, String> mailboxTable = (HashMap<String, String>) userMailboxesObject
            .get(MongoEnkiveImapConstants.MAILBOXES);
    // If it exists, return the associated object
    // If it doesn't exist, create it, and any necessary upper level folders
    if (mailboxTable.containsKey(mailboxPath)) {
        DBObject mailboxSearchObject = new BasicDBObject();
        mailboxSearchObject.put("_id", ObjectId.massageToObjectId(mailboxTable.get(mailboxPath)));
        mailboxObject = imapCollection.findOne(mailboxSearchObject);
        return mailboxObject;
    } else {
        mailboxObject.put(MongoEnkiveImapConstants.MESSAGEIDS, new HashMap<String, String>());
        imapCollection.insert(mailboxObject);
        ObjectId id = (ObjectId) mailboxObject.get("_id");
        mailboxTable.put(mailboxPath, id.toString());
    }

    if (!mailboxTable.containsKey(
            MongoEnkiveImapConstants.ARCHIVEDMESSAGESFOLDERNAME + "/" + mailboxTime.get(Calendar.YEAR))) {
        BasicDBObject yearMailboxObject = new BasicDBObject();
        yearMailboxObject.put(MongoEnkiveImapConstants.MESSAGEIDS, new HashMap<String, String>());
        imapCollection.insert(yearMailboxObject);
        ObjectId id = (ObjectId) yearMailboxObject.get("_id");
        mailboxTable.put(
                MongoEnkiveImapConstants.ARCHIVEDMESSAGESFOLDERNAME + "/" + mailboxTime.get(Calendar.YEAR),
                id.toString());
    }
    userMailboxesObject.put(MongoEnkiveImapConstants.MAILBOXES, mailboxTable);
    imapCollection.findAndModify(userMailboxesSearchObject, userMailboxesObject);

    return mailboxObject;
}

From source file:com.linuxbox.enkive.message.retention.imap.mongodb.MongoDbImapMessageRetentionPolicyEnforcer.java

License:Open Source License

private void findAndRemoveMessagesFolder(String username, Date date) {
    Calendar mailboxTime = Calendar.getInstance();
    mailboxTime.setTime(date);// w  w w .  j  a  v  a2 s .co  m
    // Subtract a month, because we want to remove the previous month
    mailboxTime.add(Calendar.MONTH, -1);
    String mailboxPath = MongoEnkiveImapConstants.ARCHIVEDMESSAGESFOLDERNAME + "/"
            + mailboxTime.get(Calendar.YEAR) + "/" + String.format("%02d", mailboxTime.get(Calendar.MONTH) + 1);

    // Get table of user mailboxes
    BasicDBObject userMailboxesSearchObject = new BasicDBObject(MongoEnkiveImapConstants.USER, username);
    DBObject userMailboxesObject = imapColl.findOne(userMailboxesSearchObject);
    // Check for mailbox we're looking for
    @SuppressWarnings("unchecked")
    HashMap<String, String> mailboxTable = (HashMap<String, String>) userMailboxesObject
            .get(MongoEnkiveImapConstants.MAILBOXES);
    // If it exists, remove it
    if (mailboxTable.containsKey(mailboxPath)) {
        BasicDBObject mailboxSearchObject = new BasicDBObject("_id",
                ObjectId.massageToObjectId(mailboxTable.get(mailboxPath)));
        imapColl.remove(mailboxSearchObject);
        mailboxTable.remove(mailboxPath);
        userMailboxesObject.put(MongoEnkiveImapConstants.MAILBOXES, mailboxTable);
        imapColl.findAndModify(userMailboxesSearchObject, userMailboxesObject);
    }
}

From source file:com.linuxbox.enkive.message.search.mongodb.MongoMessageSearchService.java

License:Open Source License

protected DBObject buildQueryObject(Map<String, String> fields)
        throws MessageSearchException, EmptySearchResultsException {
    BasicDBList conjunctionList = new BasicDBList();

    for (MongoMessageQueryBuilder queryBuilder : queryBuilders) {
        DBObject query = queryBuilder.buildQueryPortion(fields);
        if (null != query) {
            conjunctionList.add(query);/* w w w.j av  a  2  s  . c  o  m*/
        }
    }

    DBObject result;

    if (conjunctionList.isEmpty()) {
        throw new EmptySearchResultsException("No understandable search terms specified.");
    } else if (conjunctionList.size() == 1) {
        result = (DBObject) conjunctionList.get(0);
    } else {
        result = new BasicDBObject();
        result.put("$and", conjunctionList);
    }

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("MongoDB message search query is: " + result);
    }

    return result;
}

From source file:com.linuxbox.enkive.statistics.services.retrieval.mongodb.MongoStatsDateQuery.java

License:Open Source License

public Map<String, Object> getQuery() {
    Map<String, Object> mongoQuery = new BasicDBObject();
    DBObject time = new BasicDBObject();
    String tsStartKey;/*from   www  . j ava 2  s  .  c om*/
    String tsEndKey;
    tsStartKey = STAT_TIMESTAMP + "." + CONSOLIDATION_MIN;
    tsEndKey = STAT_TIMESTAMP + "." + CONSOLIDATION_MAX;

    if (startTimestamp != null) {
        time = new BasicDBObject();
        time.put("$gte", startTimestamp);
        mongoQuery.put(tsStartKey, time);
    }
    if (endTimestamp != null) {
        time = new BasicDBObject();
        time.put("$lt", endTimestamp);
        mongoQuery.put(tsEndKey, time);
    }

    if (grainType != null) {
        if (grainType == 0) {
            mongoQuery.put(CONSOLIDATION_TYPE, null);
        } else {
            mongoQuery.put(CONSOLIDATION_TYPE, grainType);
        }
    }

    return mongoQuery;
}

From source file:com.linuxbox.enkive.statistics.services.retrieval.mongodb.MongoStatsQuery.java

License:Open Source License

public Map<String, Object> getQuery() {
    Map<String, Object> mongoQuery = new BasicDBObject();

    if (gathererName != null) {
        mongoQuery.put(STAT_GATHERER_NAME, gathererName);
    }/*  w  ww  .j  a v a 2s. c  o  m*/

    if (grainType != null) {
        if (grainType == 0) {
            mongoQuery.put(CONSOLIDATION_TYPE, null);
        } else {
            mongoQuery.put(CONSOLIDATION_TYPE, grainType);
        }
    }

    DBObject time = new BasicDBObject();
    String tsKey;
    if (isPointQuery) {
        tsKey = STAT_TIMESTAMP + "." + STAT_TS_POINT;
    } else {
        tsKey = STAT_TIMESTAMP + "." + CONSOLIDATION_MIN;
    }

    if (startTimestamp != null) {
        time.put("$gte", startTimestamp);
    }
    if (endTimestamp != null) {
        time.put("$lt", endTimestamp);
    }

    if (!time.toMap().isEmpty()) {
        mongoQuery.put(tsKey, time);
    }

    return mongoQuery;
}

From source file:com.linuxbox.enkive.workspace.mongo.MongoWorkspaceService.java

License:Open Source License

@Override
public Workspace getActiveWorkspace(String userId) throws WorkspaceException {
    DBObject workspaceList = userWorkspacesColl.findOne(userId);
    String workspaceUUID;//from w w w .j a v a 2 s .  c om
    if (workspaceList == null) {
        workspaceList = new BasicDBObject();
        workspaceList.put(UUID, userId);

        Workspace workspace = workspaceBuilder.getWorkspace();
        workspace.setCreator(userId);
        workspace.setWorkspaceName("Default Workspace");
        workspace.saveWorkspace();

        workspaceUUID = workspace.getWorkspaceUUID();
        workspaceList.put(ACTIVEWORKSPACE, workspaceUUID);
        Collection<String> workspaces = new HashSet<String>();
        workspaces.add(workspaceUUID);
        workspaceList.put(MongoWorkspaceConstants.WORKSPACELIST, workspaces);
        userWorkspacesColl.save(workspaceList);
    }
    workspaceUUID = (String) workspaceList.get(ACTIVEWORKSPACE);
    return getWorkspace(workspaceUUID);
}

From source file:com.lowereast.guiceymongo.GuiceyCollection.java

License:Apache License

public Item findAndModify(DBObject query, DBObject update, DBObject sort, boolean returnNewValue,
        FieldSet fields, boolean upsert) {
    DBObject command = new BasicDBObject("findandmodify", _collection.getName()).append("update", update);
    if (query != null && !EMPTY.equals(query))
        command.put("query", query);
    if (sort != null && !EMPTY.equals(sort))
        command.put("sort", sort);
    if (returnNewValue)
        command.put("new", true);
    if (fields != null)
        command.put("fields", fields.build());
    if (upsert)//from   w  w w  . j a va2  s. c o m
        command.put("upsert", upsert);

    DBObject result = _collection.getDB().command(command);
    if (result.containsField("ok"))
        return _wrapper.wrap((DBObject) result.get("value"));
    throw new MongoException((Integer) result.get("code"), (String) result.get("errmsg"));
}

From source file:com.lowereast.guiceymongo.GuiceyCollection.java

License:Apache License

public Item findAndRemove(DBObject query, DBObject sort) {
    DBObject command = new BasicDBObject("findandmodify", _collection.getName()).append("remove", true);
    if (query != null && !EMPTY.equals(query))
        command.put("query", query);
    if (sort != null && !EMPTY.equals(sort))
        command.put("sort", sort);

    DBObject result = _collection.getDB().command(command);
    if (1 == (Integer) result.get("ok"))
        return _wrapper.wrap((DBObject) result.get("value"));
    throw new MongoException((Integer) result.get("code"), (String) result.get("errmsg"));
}

From source file:com.lowereast.guiceymongo.logging.MongoDbAppender.java

License:Open Source License

/**
 * BSONifies a single Log4J LoggingEvent object.
 * //from   w  w  w .  j a  va2s .  co m
 * @param loggingEvent The LoggingEvent object to BSONify <i>(may be null)</i>.
 * @return The BSONified equivalent of the LoggingEvent object <i>(may be null)</i>.
 */
private DBObject bsonifyLoggingEvent(final LoggingEvent loggingEvent) {
    DBObject result = null;

    if (loggingEvent != null) {
        result = new BasicDBObject(this.extraInformation);

        result.put("timestamp", new Date(loggingEvent.getTimeStamp()));
        nullSafePut(result, "level", loggingEvent.getLevel().toString());
        nullSafePut(result, "thread", loggingEvent.getThreadName());
        nullSafePut(result, "message", loggingEvent.getMessage());

        addLocationInformation(result, loggingEvent.getLocationInformation());
        addThrowableInformation(result, loggingEvent.getThrowableInformation());
    }

    return (result);
}