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.lowereast.guiceymongo.logging.MongoDbAppender.java

License:Open Source License

/**
 * Adds the ThrowableInformation object to an existing BSON object.
 * //ww w .ja v a  2  s .c  om
 * @param bson          The BSON object to add the throwable info to <i>(must not be null)</i>.
 * @param throwableInfo The ThrowableInformation object to add to the BSON object <i>(may be null)</i>.
 */
private void addThrowableInformation(DBObject bson, final ThrowableInformation throwableInfo) {
    if (throwableInfo != null) {
        Throwable currentThrowable = throwableInfo.getThrowable();
        List throwables = new BasicDBList();

        while (currentThrowable != null) {
            DBObject throwableBson = bsonifyThrowable(currentThrowable);

            if (throwableBson != null) {
                throwables.add(throwableBson);
            }

            currentThrowable = currentThrowable.getCause();
        }

        if (throwables.size() > 0) {
            bson.put("throwables", throwables);
        }
    }
}

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

License:Open Source License

/**
 * BSONifies the given class name./*  w w  w.  j  a  va2  s  . c om*/
 * 
 * @param className The class name to BSONify <i>(may be null)</i>.
 * @return The BSONified equivalent of the class name <i>(may be null)</i>.
 */
private DBObject bsonifyClassName(final String className) {
    DBObject result = null;

    if (className != null && className.trim().length() > 0) {
        result = new BasicDBObject();

        result.put("fullyQualifiedClassName", className);

        List packageComponents = new BasicDBList();
        String[] packageAndClassName = className.split("\\.");

        packageComponents
                .addAll(Arrays.asList(Arrays.copyOf(packageAndClassName, packageAndClassName.length - 1)));

        if (packageComponents.size() > 0) {
            result.put("package", packageComponents);
        }

        result.put("className", packageAndClassName[packageAndClassName.length - 1]);
    }

    return (result);
}

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

License:Open Source License

/**
 * Adds the given value to the given key, except if it's null (in which case this method does nothing).
 * //from  w ww .  j  a  va2  s  .c o  m
 * @param bson  The BSON object to add the key/value to <i>(must not be null)</i>.
 * @param key   The key of the object <i>(must not be null)</i>.
 * @param value The value of the object <i>(may be null)</i>.
 */
private void nullSafePut(DBObject bson, final String key, final Object value) {
    if (value != null) {
        if (value instanceof String) {
            String stringValue = (String) value;

            if (stringValue.trim().length() > 0) {
                bson.put(key, stringValue);
            }
        } else {
            bson.put(key, value);
        }
    }
}

From source file:com.lowereast.guiceymongo.util.DBObjectUtil.java

License:Apache License

private static Object navigateNext(DBObject current, String key, boolean create) {
    Object next = current.get(key);
    if (next == null && create) {
        next = new BasicDBObject();
        current.put(key, next);
    }//from   w  w  w .  j a va  2 s . c om
    return next;
}

From source file:com.machinelinking.storage.mongodb.MongoSelector.java

License:Apache License

public DBObject toDBObjectSelection() {
    final DBObject obj = new BasicDBObject();
    for (Criteria criteria : this.getCriterias()) {
        obj.put(criteria.field, toValue(criteria.operator, criteria.value));
    }/*from w  w w . j  ava  2 s.c  o m*/
    return obj;
}

From source file:com.machinelinking.storage.mongodb.MongoSelector.java

License:Apache License

public DBObject toDBObjectProjection() {
    final DBObject obj = new BasicDBObject();
    for (Criteria criteria : this.getCriterias()) {
        obj.put(criteria.field, 1);
    }/*from  ww w  . j  a  va  2s .c  o m*/
    for (String projection : this.getProjections()) {
        obj.put(projection, 1);
    }
    return obj;
}

From source file:com.mebigfatguy.mongobrowser.actions.EditKeyValueAction.java

License:Apache License

@Override
public void actionPerformed(ActionEvent e) {
    JTree tree = context.getTree();
    MongoTreeNode[] selectedNodes = TreeUtils.getSelectedNodes(tree);

    if (selectedNodes.length == 1) {

        MongoTreeNode.KV keyValue = ((MongoTreeNode.KV) selectedNodes[0].getUserObject());

        String key = keyValue.getKey();
        Object value = keyValue.getValue();

        KeyValueDialog dialog = new KeyValueDialog(key, value);
        dialog.setLocationRelativeTo(tree);
        dialog.setModal(true);/*  w  ww  .j a  va2  s.c om*/
        dialog.setVisible(true);

        if (dialog.isOK()) {
            DBObject dbObject = (DBObject) ((MongoTreeNode) selectedNodes[0].getParent()).getUserObject();
            key = dialog.getKey();
            value = dialog.getValue();

            dbObject.put(key, value);
            keyValue.setValue(value);

            MongoTreeNode collectionNode = TreeUtils.findCollectionNode(selectedNodes[0]);
            DBCollection collection = (DBCollection) collectionNode.getUserObject();
            collection.save(dbObject);
        }
    }
}

From source file:com.mebigfatguy.mongobrowser.actions.NewKeyValueAction.java

License:Apache License

@Override
public void actionPerformed(ActionEvent e) {
    JTree tree = context.getTree();
    KeyValueDialog dialog = new KeyValueDialog();
    dialog.setLocationRelativeTo(tree);/*  w w w  .  j a  va2  s.co  m*/
    dialog.setModal(true);
    dialog.setVisible(true);
    if (dialog.isOK()) {
        String key = dialog.getKey();
        Object value = dialog.getValue();
        TreePath path = tree.getSelectionPath();
        MongoTreeNode selectedNode = (MongoTreeNode) path.getLastPathComponent();
        DBObject object;

        if (selectedNode.getType() == MongoTreeNode.Type.KeyValue) {
            object = (DBObject) ((MongoTreeNode.KV) selectedNode.getUserObject()).getValue();
        } else {
            object = (DBObject) selectedNode.getUserObject();
        }

        object.put(key, value);
        MongoTreeNode kv = new MongoTreeNode(new MongoTreeNode.KV(key, object.get(key)), false);
        selectedNode.add(kv);
        if (value instanceof DBObject) {
            MongoTreeNode slug = new MongoTreeNode();
            kv.add(slug);
        }
        MongoTreeNode collectionNode = TreeUtils.findCollectionNode(selectedNode);
        DBCollection collection = (DBCollection) collectionNode.getUserObject();
        collection.save(object);
        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
        model.nodeStructureChanged((MongoTreeNode) model.getRoot());
        TreePath selection = new TreePath(kv.getPath());
        tree.scrollPathToVisible(selection);
        tree.setSelectionPath(selection);
    }
}

From source file:com.mobileman.kuravis.core.Data.java

License:Apache License

/**
 * @param template//www.  j  ava  2  s .  c  o  m
 * @param userService 
 */
public static void setUp(MongoTemplate template, UserService userService) {
    if (initialized) {
        return;
    }

    initialized = true;

    for (String collection : EntityUtils.getAllEntityNames()) {
        template.getDb().getCollection(collection).remove(new BasicDBObject(), WriteConcern.SAFE);
    }

    List<DBObject> users = UserServiceTest.getUsers();
    for (DBObject user : users) {
        if ("peter.novak@test.com".equals(user.get("email"))) {
            continue;
        }

        userService.signup(user);
        DBObject account = template.getDb().getCollection(EntityUtils.USERACCOUNT)
                .findOne(new BasicDBObject("email", user.get("email")));
        account.put("roles", Arrays.asList(String.class.cast(user.get("roles")).toLowerCase()));
        template.getDb().getCollection(EntityUtils.USERACCOUNT)
                .update(new BasicDBObject("email", user.get("email")), account);

        DBObject savedUser = template.getDb().getCollection(EntityUtils.USER)
                .findOne(new BasicDBObject("email", user.get("email")));
        savedUser.put("state", Arrays.asList(user.get("state")));
        template.getDb().getCollection(EntityUtils.USER).update(new BasicDBObject("email", user.get("email")),
                savedUser);
    }

    List<DBObject> diseases = DiseaseServiceTest.getDiseases();
    for (DBObject dbObject : diseases) {
        template.getDb().getCollection(Disease.ENTITY_NAME).save(dbObject);
    }

    List<Unit> units = UnitServiceTest.getUnits();
    for (Unit dbObject : units) {
        template.save(dbObject);
    }

    List<TreatmentType> treatmentTypes = TreatmentTypeServiceTest.getTreatmentTypes();
    for (TreatmentType dbObject : treatmentTypes) {
        template.save(dbObject);
    }

    List<Psychotherapy> psychotherapies = PsychotherapyTest.getPsychotherapies();
    for (Psychotherapy object : psychotherapies) {
        template.save(object);
    }

    List<Physiotherapie> physiotherapies = PhysiotherapieTest.getPsychotherapies();
    for (Physiotherapie object : physiotherapies) {
        template.save(object);
    }

    users = UserServiceTest.getUsers();
    int idx = 0;
    for (DBObject user : users) {
        DBObject disease = diseases.get(idx++ % 3);
        DBObject diseaseCopy = new BasicDBObject(EntityUtils.ID, disease.get(EntityUtils.ID))
                .append(Disease.NAME, disease.get(Disease.NAME));
        DiseaseState state = idx % 5 == 0 ? User.DiseaseState.CURED : User.DiseaseState.IN_THERAPY;
        DBObject userDisease = new BasicDBObject("createdOn", new Date()).append("state", state.getValue())
                .append("disease", diseaseCopy);
        user.put("diseases", Arrays.asList(userDisease));
        template.getDb().getCollection(EntityUtils.USER).save(user);
    }

    List<DBObject> accounts = UserServiceTest.getAccounts();
    for (DBObject dbObject : accounts) {
        template.getDb().getCollection(EntityUtils.USERACCOUNT).save(dbObject);
    }

    List<DBObject> treatments = TreatmentServiceTest.getTreatments();
    for (DBObject dbObject : treatments) {
        template.getDb().getCollection(EntityUtils.TREATMENT).save(dbObject);
    }

    List<DBObject> treatmentreviews = TreatmentReviewServiceTest.createTreatmentReviews(diseases.get(3),
            treatments.get(4));
    for (DBObject dbObject : treatmentreviews) {
        template.getDb().getCollection(TreatmentReview.ENTITY_NAME).save(dbObject);
    }

    for (DBObject dbObject : TreatmentReviewServiceTest.getTreatmentReviewsSummaries()) {
        template.getDb().getCollection(TreatmentReviewSummary.ENTITY_NAME).save(dbObject);
    }

    for (DBObject dbObject : TreatmentReviewServiceTest.getTreatmentReviewSummaryAgeStatistics()) {
        template.getDb().getCollection(EntityUtils.TREATMENT_SUMMARY_AGE_STATISTICS).save(dbObject);
    }

    initialized = true;
}

From source file:com.mobileman.kuravis.core.domain.util.EntityUtils.java

License:Apache License

/**
 * @param treatmentReview//  w w  w .ja  v a 2  s. com
 * @param entityId 
 * @param type
 * @param user 
 * @return event object
 */
public static DBObject createTreatmentReviewEvent(DBObject treatmentReview, String entityId,
        TreatmentReviewEventType type, DBObject user) {
    DBObject event = entityId == null ? newDBObjectId() : new BasicDBObject(ID, entityId);
    EntityUtils.setBasePropertiesOnCreate(event);

    DBObject baseUser = EntityUtils.createBaseUser(user);
    baseUser.put(User.ATTR_GENDER, user.get(User.ATTR_GENDER));
    baseUser.put(User.ATTR_YEAR_OF_BIRTH, user.get(User.ATTR_YEAR_OF_BIRTH));
    baseUser.put("settings",
            new BasicDBObject("profile", DBObject.class.cast(user.get("settings")).get("profile")));

    event.put("user", baseUser);
    event.put("treatmentReviewId", getEntityId(treatmentReview));
    event.put("treatmentReviewAuthorId", getEntityId(treatmentReview.get(TreatmentReview.AUTHOR)));
    event.put("type", type.getValue().toLowerCase());

    return event;
}