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:ch.windmobile.server.social.mongodb.UserServiceImpl.java

License:Open Source License

@Override
public List<Favorite> addToFavorites(String email, List<Favorite> localFavorites) throws UserNotFound {
    if (email == null) {
        throw new IllegalArgumentException("Email cannot be null");
    }//from  ww  w. ja  v a 2 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 (localFavorites != null) {
        for (Favorite localFavorite : localFavorites) {
            DBObject favoriteItemsDb = (DBObject) favoritesDb.get(localFavorite.getStationId());
            long lastMessageIdDb = -1;
            if (favoriteItemsDb == null) {
                favoriteItemsDb = new BasicDBObject();
            } else {
                if (favoriteItemsDb.containsField(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID)) {
                    lastMessageIdDb = (Long) favoriteItemsDb
                            .get(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID);
                }
            }

            favoriteItemsDb.put(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID,
                    Math.max(lastMessageIdDb, localFavorite.getLastMessageId()));
            favoritesDb.put(localFavorite.getStationId(), favoriteItemsDb);
        }
    }
    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 List<Favorite> removeFromFavorites(String email, List<Favorite> favoritesToRemove) throws UserNotFound {
    if (email == null) {
        throw new IllegalArgumentException("Email cannot be null");
    }//w w  w  .  j av  a  2s. 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:cn.cnic.bigdatalab.flume.sink.mongodb.EventParser.java

License:Apache License

public DBObject parse(Event event) {

    DBObject dbObject = new BasicDBObject();
    if (definition.getBodyType() != MongoDataType.NULL) {
        Object obj = null;//from www.  j av  a2 s .co m
        if (definition.getBodyType() == MongoDataType.BINARY && definition.getBodyEncoding().equals("raw")) {
            obj = event.getBody();
        } else if (definition.getBodyType() == MongoDataType.STRING) {
            Charset charset = Charset.forName(definition.getBodyEncoding());
            obj = new String(event.getBody(), charset);
        } else {
            SimpleFieldDefinition fd = new SimpleFieldDefinition();
            fd.setType(definition.getBodyType());
            fd.setEncoding(definition.getBodyEncoding());
            obj = parseValue(fd, new String(event.getBody(), Charsets.UTF_8));
        }

        if (!"".equals(definition.getBodyField())) {
            dbObject.put(definition.getBodyField(), obj);
        } else if (obj instanceof DBObject) {
            dbObject = (DBObject) obj;
        } else {
            log.warn("Could not map body to JSON document: {}", obj);
        }
    }

    final Map<String, String> eventHeaders = event.getHeaders();
    if (definition.allowsAdditionalProperties()) {
        for (final Map.Entry<String, String> headerEntry : eventHeaders.entrySet()) {
            final String fieldName = headerEntry.getKey();
            final String fieldValue = headerEntry.getValue();
            FieldDefinition def = definition.getFieldDefinitionByName(fieldName);
            if (def == null) {
                dbObject.put(fieldName, parseValue(null, fieldValue));
            } else {
                final String mappedName = (def.getMappedName() == null) ? def.getFieldName()
                        : def.getMappedName();
                if (eventHeaders.containsKey(fieldName)) {
                    dbObject.put(mappedName, parseValue(def, fieldValue));
                }
            }
        }
    } else {
        for (FieldDefinition def : definition.getFields()) {
            final String fieldName = def.getFieldName();
            final String mappedName = (def.getMappedName() == null) ? def.getFieldName() : def.getMappedName();
            if (containsKey(eventHeaders, fieldName)) {
                dbObject.put(mappedName, parseValue(def, getFieldName(eventHeaders, fieldName)));
            }
        }
    }

    return dbObject;
}

From source file:cn.cnic.bigdatalab.flume.sink.mongodb.EventParser.java

License:Apache License

private DBObject populateDocument(DocumentFieldDefinition fd, String document) {
    DBObject dbObject = null;
    final String delimiter = fd.getDelimiter();
    if (!StringUtils.isEmpty(delimiter)) {
        String[] documentAsArrray = document.split(Pattern.quote(delimiter));
        dbObject = new BasicDBObject();
        Map<String, FieldDefinition> documentMapping = new LinkedHashMap<String, FieldDefinition>(
                fd.getDocumentMapping());
        int i = 0;
        for (Map.Entry<String, FieldDefinition> documentField : documentMapping.entrySet()) {
            if (DOCUMENT_TYPE.equalsIgnoreCase(documentField.getValue().getType().name())) {
                dbObject.put(documentField.getKey(), parseValue(documentField.getValue(), StringUtils.join(
                        Arrays.copyOfRange(documentAsArrray, i, documentAsArrray.length), fd.getDelimiter())));
                i += ((DocumentFieldDefinition) documentField.getValue()).getDocumentMapping().size();
            } else {
                dbObject.put(documentField.getKey(),
                        parseValue(documentField.getValue(), documentAsArrray[i++]));
            }/*from  w ww .j ava  2  s.  c o  m*/
        }
    } else {
        throw new MongoSinkException("Delimiter char must be set");
    }

    return dbObject;
}

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

License:Apache License

public void validate() {
    if (_fs == null)
        throw new MongoException("no _fs");
    if (_md5 == null)
        throw new MongoException("no _md5 stored");

    DBObject cmd = new BasicDBObject("filemd5", _id);
    cmd.put("root", _fs._bucketName);
    DBObject res = _fs._db.command(cmd);
    if (res != null && res.containsField("md5")) {
        String m = res.get("md5").toString();
        if (m.equals(_md5))
            return;
        throw new MongoException("md5 differ.  mine [" + _md5 + "] theirs [" + m + "]");
    }/*from   ww  w . j  a  va 2s.c  o m*/

    // no md5 from the server
    throw new MongoException("no md5 returned from server: " + res);

}

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);/*from  w  w  w  . j a  va2  s. c  o  m*/
    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;
    }
    return null;
}

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

License:Apache License

protected GridFSDBFile readFile(String storageKey, String tableName) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    DBObject query = new BasicDBObject();
    query.put(FIELD_STORAGE_KEY, new ObjectId(storageKey));
    GridFSDBFile gfsFile = new GridFS(db, tableName).findOne(query);
    return gfsFile;
}

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

License:Apache License

protected GridFSDBFile readFile(int appid, int docid, String vid, String tableName) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    db.requestStart();//www.j a  v a 2 s  .c o m
    DBObject query = new BasicDBObject();
    query.put(FIELD_DOC_ID, docid);
    query.put(FILED_VERSION_ID, vid);
    query.put(FIELD_APP_ID, appid);
    GridFSDBFile gfsFile = new GridFS(db, tableName).findOne(query);
    db.requestEnsureConnection();
    db.requestDone();
    return gfsFile;
}

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

License:Apache License

protected GridFSDBFile readTrivialFile(String spaceName, String fileName, String tableName) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    DBObject query = new BasicDBObject();
    query.put(FIELD_SPACE_NAME, new ObjectId(spaceName));
    query.put(FIELD_FILE_NAME, fileName);
    GridFSDBFile gfsFile = new GridFS(db, tableName).findOne(query);
    return gfsFile;
}

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

License:Apache License

@Override
public void removeTrivial(String spaceName) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    DBObject query = new BasicDBObject();
    query.put(FIELD_SPACE_NAME, new ObjectId(spaceName));
    GridFS gfs = new GridFS(db, TABLE_TRIVIAL);
    gfs.remove(query);//from  w w w . ja v a  2 s .  com
}