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.ebay.cloud.cms.dal.persistence.flatten.impl.root.RootCountCommand.java

License:Apache License

@Override
public void execute(PersistenceContext context) {
    DBObject queryObject = new BasicDBObject();
    queryObject.put(InternalFieldEnum.ID.getDbName(), new BasicDBObject("$in", oids));
    queryObject.put(InternalFieldEnum.STATUS.getDbName(), StatusEnum.ACTIVE.toString());
    queryObject.put(InternalFieldEnum.BRANCH.getDbName(), branchId);
    resultCount = MongoExecutor.count(context, meta, queryObject);
}

From source file:com.ebay.cloud.cms.dal.persistence.flatten.impl.root.RootFieldModifyCommand.java

License:Apache License

private void buildArrayBody(DBObject modifyBody) {
    MetaField field = getField();// w  ww  .j ava 2 s.c o m
    BasicDBObject enityObject = (BasicDBObject) getEntity().getNode();
    Object givenValue = enityObject.get(field.getFlattenValueDbName());

    if (givenValue != null) {
        BasicDBObject eachDbo = new BasicDBObject();
        eachDbo.put("$each", givenValue);

        BasicDBObject addToSetDbo = new BasicDBObject();
        addToSetDbo.put(field.getFlattenValueDbName(), eachDbo);

        modifyBody.put("$addToSet", addToSetDbo);

        // field length, only update when we do have updates
        BasicDBObject inc = (BasicDBObject) modifyBody.get("$inc");
        inc.put(field.getFlattenPropertyValueDbName(FieldProperty.LENGTH),
                getEntity().getFieldLength(fieldName));
        // field time stamp
        BasicDBObject set = (BasicDBObject) modifyBody.get("$set");
        set.put(field.getFlattenPropertyValueDbName(FieldProperty.TIMESTAMP),
                getEntity().getFieldTimestamp(fieldName));
    }
}

From source file:com.ebay.cloud.cms.dal.persistence.flatten.impl.root.RootGetCommand.java

License:Apache License

private static DBObject buildGetRootQuery(String entityId, String branchId, MetaClass metadata,
        boolean needActive, PersistenceContext context) {
    DBObject rootQueryObject = new BasicDBObject();
    rootQueryObject.put(InternalFieldEnum.ID.getDbName(), entityId);
    if (needActive) {
        rootQueryObject.put(InternalFieldEnum.STATUS.getDbName(), StatusEnum.ACTIVE.toString());
    }// ww  w .j a  va2  s  .  c om
    rootQueryObject.put(InternalFieldEnum.BRANCH.getDbName(), branchId);
    // add read filter
    return addContextCriteria(metadata, context, rootQueryObject);
}

From source file:com.ebay.cloud.cms.dal.persistence.flatten.impl.root.RootGetCommand.java

License:Apache License

public static DBObject addContextCriteria(MetaClass metadata, PersistenceContext context,
        DBObject rootQueryObject) {/*from   ww  w  . j  a  va 2 s. c  o m*/
    DBObject compoundObject = rootQueryObject;
    List<SearchCriteria> criterias = context.getAdditionalCriteria().get(metadata.getName());
    if (criterias != null && !criterias.isEmpty()) {
        compoundObject = new BasicDBObject();
        List<DBObject> andObjects = new ArrayList<DBObject>();
        compoundObject.put("$and", andObjects);
        andObjects.add(rootQueryObject);
        for (SearchCriteria criteria : criterias) {
            DBObject dbo = SearchQuery.generateMongoQuery(criteria);
            andObjects.add(dbo);
        }
    }
    return compoundObject;
}

From source file:com.ebay.cloud.cms.dal.persistence.flatten.impl.root.RootReplaceCommand.java

License:Apache License

@Override
public void execute(PersistenceContext context) {
    DBObject getResult = RootGetCommand.findDBObject(entity.getId(), entity.getBranchId(), context,
            entity.getMetaClass(), ROOT_FIELDS, false);
    if (getResult != null) {
        // check version
        int currentVersion = (Integer) getResult.get(InternalFieldEnum.VERSION.getDbName());
        int version = entity.getVersion();
        if ((version != IEntity.NO_VERSION) && (currentVersion != version)) {
            throw new CmsDalException(DalErrCodeEnum.VERSION_CONFLICT, "current version is " + currentVersion
                    + ", but version in repalce body is " + version + "! entity is " + entity.toString());
        }//from  w  w w.  j a  v a  2 s.c o  m

        Date createTime = (Date) getResult.get(InternalFieldEnum.CREATETIME.getDbName());

        DBObject queryObject = buildReplaceRootQuery(currentVersion);
        DBObject updateObject = buildReplaceRootUpdate(currentVersion, createTime);
        try {
            WriteResult result = MongoExecutor.update(context, entity.getMetaClass(), queryObject,
                    updateObject);
            if (result.getN() == 0) {
                throw new CmsDalException(DalErrCodeEnum.VERSION_CONFLICT,
                        "current version is not " + currentVersion + "! entity is " + entity.toString());
            }
            //set version for response
            entity.setVersion(currentVersion + 1);
        } catch (RuntimeException e) {
            entity.setVersion(currentVersion);
            throw e;
        } catch (Throwable t) {
            //if anything bad happens, need to set version back
            entity.setVersion(currentVersion);
        } finally {
            //in update scenario, have to set id value back into entity object because we
            //removed id field before
            entity.setId(entity.getId());
        }
    } else {
        // insert

        //2012/7/13 jianxu1 multi threading issue, if client A and client B send replace at same time
        //at T1, none of A and B find existing entity, so at T2, both A and B call insert, of we do not have 
        //unique index for combination of _branchId and _oid, we end up with duplicated entities.
        //FIX is: http://www.mongodb.org/display/DOCS/Indexes,  composite unique index
        //db.branches.ensureIndex({_branchId: 1, _oid: 1}, {unique: true});
        //TODO: change create repository to add composite unique index for branches, for main branch, we just need unique index on _oid

        DBObject insertObject = entity.getNode();
        insertObject.removeField("_id");
        insertObject.put(InternalFieldEnum.VERSION.getDbName(), IEntity.START_VERSION);
        MongoExecutor.insert(context, entity.getMetaClass(), insertObject);
    }
}

From source file:com.ebay.cloud.cms.dal.persistence.flatten.impl.root.RootReplaceCommand.java

License:Apache License

private DBObject buildReplaceRootQuery(int currentVersion) {
    // add query version in case somebody make changes between get and update      
    DBObject rootQueryObject = new BasicDBObject();
    rootQueryObject.put(InternalFieldEnum.ID.getDbName(), entity.getId());
    rootQueryObject.put(InternalFieldEnum.TYPE.getDbName(), entity.getType());
    //rootQueryObject.put(InternalFieldEnum.STATUS.getDbName(), StatusEnum.ACTIVE.toString());
    rootQueryObject.put(InternalFieldEnum.BRANCH.getDbName(), entity.getBranchId());
    rootQueryObject.put(InternalFieldEnum.VERSION.getDbName(), currentVersion);
    return rootQueryObject;
}

From source file:com.ebay.cloud.cms.dal.persistence.flatten.impl.root.RootReplaceCommand.java

License:Apache License

private DBObject buildReplaceRootUpdate(int currentVersion, Date createTime) {
    // add query version in case somebody make changes between get and update      
    DBObject rootUpdateObject = entity.getNode();
    rootUpdateObject.put(InternalFieldEnum.VERSION.getDbName(), currentVersion + 1);
    rootUpdateObject.put(InternalFieldEnum.CREATETIME.getDbName(), createTime);
    rootUpdateObject.put(InternalFieldEnum.STATUS.getDbName(), StatusEnum.ACTIVE.toString());
    // remove _id for sub branch
    rootUpdateObject.removeField("_id");
    return rootUpdateObject;
}

From source file:com.ebay.cloud.cms.dal.persistence.impl.embed.AbstractEmbedCommand.java

License:Apache License

protected DBObject buildGetQuery(BitSet arrayBits, String embedId, MetaClass rootMetaClass,
        PersistenceContext context) {//from  w  w  w.  j a  v a 2  s. c  o  m
    DBObject queryObject = new BasicDBObject();
    queryObject.put(InternalFieldEnum.BRANCH.getDbName(), branchId);
    queryObject.put(InternalFieldEnum.STATUS.getDbName(), StatusEnum.ACTIVE.toString());

    String embedPath = helper.getEmbedPath(embedId, rootMetaClass);
    if (StringUtils.isNullOrEmpty(embedPath)) {
        // generate field for root
        queryObject.put(InternalFieldEnum.ID.getDbName(), embedId);
        queryObject.put(InternalFieldEnum.STATUS.getDbName(), StatusEnum.ACTIVE.toString());
    } else if (arrayBits.isEmpty()) {
        // generate dot notation field for single 
        String idFieldName = embedPath + EntityIDHelper.DOT + InternalFieldEnum.ID.getDbName();
        String statusFieldName = embedPath + EntityIDHelper.DOT + InternalFieldEnum.STATUS.getDbName();
        queryObject.put(idFieldName, embedId);
        queryObject.put(statusFieldName, StatusEnum.ACTIVE.toString());
    } else {
        // generate $elemMatch for array 
        DBObject idQueryObject = new BasicDBObject();
        idQueryObject.put(InternalFieldEnum.ID.getDbName(), embedId);
        idQueryObject.put(InternalFieldEnum.STATUS.getDbName(), StatusEnum.ACTIVE.toString());
        BasicDBObject elemMatchObject = new BasicDBObject();
        elemMatchObject.put("$elemMatch", idQueryObject);
        queryObject.put(embedPath, elemMatchObject);
    }

    return RootGetCommand.addContextCriteria(rootMetaClass, context, queryObject);
}

From source file:com.ebay.cloud.cms.dal.persistence.impl.embed.AbstractEmbedCommand.java

License:Apache License

protected DBObject buildModifyQuery(DBObject rootQuery, BsonEntity entity) {
    int currentVersion = entity.getVersion();
    if (currentVersion != IEntity.NO_VERSION) {
        rootQuery.put(InternalFieldEnum.VERSION.getDbName(), currentVersion);
    }/*ww w  .ja  v  a2  s  .co m*/
    if (helper.isEmbedEntity(entity.getId())) {
        entity.removeField(InternalFieldEnum.VERSION.getName());
    }
    return rootQuery;
}

From source file:com.ebay.cloud.cms.dal.persistence.impl.embed.AbstractEmbedCommand.java

License:Apache License

protected void buildRootUpdateObject(BsonEntity entity, Integer newVersion, DBObject updateBody,
        MetaClass rootMetaClass) {//from  www .j av  a2s.  com
    BasicDBObject setObject = (BasicDBObject) updateBody.get("$set");
    if (setObject == null) {
        setObject = new BasicDBObject();
    }

    // populate the data to the root entity for the internal fields
    if (newVersion != null) {
        setObject.put(InternalFieldEnum.VERSION.getDbName(), newVersion);
    }
    setObject.put(InternalFieldEnum.MODIFIER.getDbName(), entity.getModifier());
    setObject.put(InternalFieldEnum.LASTMODIFIED.getDbName(), new Date());
    setObject.put(InternalFieldEnum.STATUS.getDbName(), StatusEnum.ACTIVE.toString());
    setObject.put(InternalFieldEnum.BRANCH.getDbName(), entity.getBranchId());
    setObject.put(InternalFieldEnum.TYPE.getDbName(), rootMetaClass.getName());
    String rootId = helper.getRootId(entity.getId());
    setObject.put(InternalFieldEnum.ID.getDbName(), rootId);

    setInternalField(entity, setObject, InternalFieldEnum.COMMENT);
    setInternalField(entity, setObject, InternalFieldEnum.USER);

    updateBody.put("$set", setObject);
}