Example usage for com.mongodb DBObject get

List of usage examples for com.mongodb DBObject get

Introduction

In this page you can find the example usage for com.mongodb DBObject get.

Prototype

Object get(String key);

Source Link

Document

Gets a field from this object by a given name.

Usage

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

License:Apache License

private static boolean compareEntityId(String embedId, DBObject dbObject) {
    Object entityId = dbObject.get(InternalFieldEnum.ID.getDbName());
    if (embedId.equals(entityId)) {
        return true;
    }/*from  ww w.j  a  v a2s.  co  m*/
    return false;
}

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

License:Apache License

@Override
public void execute(PersistenceContext context) {
    MetaClass rootMetaClass = getRootEntityMetaClass(entityId, context);
    String parentId = helper.getParentId(entityId);
    BitSet parentBits = helper.checkArrayOnPath(entityId, rootMetaClass);

    //TODO : adjacent query/update in code level, should always hit PRIMARY
    // query root object with parent id
    DBObject rootQuery = buildGetQuery(parentBits, parentId, rootMetaClass);
    DBObject rootFields = buildGetFields(parentBits, parentId, false, rootMetaClass);
    DBObject rootObject = MongoExecutor.findOne(context, rootMetaClass, rootQuery, rootFields);
    if (rootObject == null) {
        throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND,
                "Embed Delete, parenet document doesn't exist!  " + parentId);
    }//from  w w  w .  ja  va  2s  .  c  om

    // add embed document into parent document
    DBObject createQuery = buildModifyQuery(rootQuery, entity);
    int newVersion = (Integer) rootObject.get(InternalFieldEnum.VERSION.getDbName()) + 1;
    String parentPath = getUpdatePath(parentBits, parentId, rootObject, rootMetaClass);
    MetaRelationship lastMetaField = helper.getLastMetaField(entityId, rootMetaClass);
    DBObject deleteBody = buildDeleteBody(lastMetaField, parentPath, entityId, newVersion, rootMetaClass);
    WriteResult result = MongoExecutor.update(context, rootMetaClass, createQuery, deleteBody);
    if (result.getN() == 0) {
        // something happens between get and replace
        throw new CmsDalException(DalErrCodeEnum.VERSION_CONFLICT, "Version check fails for " + entityId
                + " in class " + metaClass.getName() + "! entity is " + entity.toString());
    }
}

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

License:Apache License

@Override
public void execute(PersistenceContext context) {
    MetaClass rootMetaClass = getRootEntityMetaClass(entityId, context);
    BitSet arrayBits = helper.checkArrayOnPath(entityId, rootMetaClass);
    // find root object & embed objects                
    DBObject getQuery = buildGetQuery(arrayBits, entityId, rootMetaClass);
    DBObject getField = buildGetFields(arrayBits, entityId, true, rootMetaClass);

    DBObject rootObject = MongoExecutor.findOne(context, rootMetaClass, getQuery, getField);
    if (rootObject == null) {
        return;/*from   w  w w.  j a  va  2 s.  c  om*/
    }
    // extract embed object by id
    DBObject embedObject = EmbedDBObjectFilter.filter(entityId, rootObject, rootMetaClass,
            context.getQueryFields(), helper);
    if (embedObject != null) {
        resultEntity = new NewBsonEntity(entityType, embedObject);
        int rootVersion = (Integer) rootObject.get(InternalFieldEnum.VERSION.getDbName());
        resultEntity.setVersion(rootVersion);
    }
}

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

License:Apache License

protected DBObject buildModifyBody(BitSet arrayBits, DBObject rootObject, MetaClass rootMetaClass) {
    BasicDBObject embedObject = (BasicDBObject) EmbedDBObjectFilter.filter(entity.getId(), rootObject,
            rootMetaClass, null, helper);

    setEmbedObjectValue(embedObject);//from   w  ww.j  av a2 s. co  m

    BasicDBObject setModifyObject = new BasicDBObject();

    DBObject obj = (DBObject) rootObject.get(embedFieldName);
    if (obj == null) {
        throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND,
                "Modify, can not find embed field with Id: " + this.entity.getId());
    }
    // update root embed field's timestamp
    MetaField field = rootMetaClass.getFieldByFlattenValueDbName(embedFieldName);
    setModifyObject.put(field.getFlattenPropertyValueDbName(FieldProperty.TIMESTAMP), new Date());

    setModifyObject.put(embedFieldName, obj);
    BasicDBObject modifyBody = new BasicDBObject();
    modifyBody.put("$set", setModifyObject);

    // increase version on root document
    BasicDBObject versionObject = new BasicDBObject();
    versionObject.put(InternalFieldEnum.VERSION.getDbName(), 1);
    modifyBody.put("$inc", versionObject);

    buildRootUpdateObject(entity, null, modifyBody, rootMetaClass);

    return modifyBody;
}

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

License:Apache License

private Set<String> getUpdateIndex(Map<String, DBObject> exsitingIndexMap, MetaClass meta) {
    Collection<IndexInfo> collection = meta.getIndexes();
    Set<String> updateIndexes = new HashSet<String>();
    for (IndexInfo info : collection) {
        if (exsitingIndexMap.containsKey(info.getIndexName())) {
            boolean diff = false;
            DBObject dbo = exsitingIndexMap.get(info.getIndexName());
            // key check : cast as BasicDBObject, so that we could go through the insert order
            // FIXME :: don't support compare key order. If we support key order specify, then we need to add this comparison 
            BasicDBObject keyObjs = (BasicDBObject) dbo.get("key");
            DBObject keyObject = buildIndexKeyObject(meta, info, onMainBranch);
            diff = !ListUtils.isEqualList(keyObjs.keySet(), keyObject.keySet());

            if (!diff) {
                // option check when keys check passed
                List<IndexOptionEnum> exsitingOption = new ArrayList<IndexInfo.IndexOptionEnum>();
                for (IndexOptionEnum ioe : IndexOptionEnum.values()) {
                    if (dbo.containsField(ioe.name().toLowerCase())
                            && Boolean.parseBoolean(dbo.get(ioe.name().toLowerCase()).toString())) {
                        exsitingOption.add(ioe);
                    }//from ww  w.  j a v  a 2 s .c o  m
                }
                if (!ListUtils.subtract(exsitingOption, info.getIndexOptions()).isEmpty()
                        || !ListUtils.subtract(info.getIndexOptions(), exsitingOption).isEmpty()) {
                    diff = true;
                }
            }
            if (diff) {
                updateIndexes.add(info.getIndexName());
            }
        }
    }
    return updateIndexes;
}

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

License:Apache License

private Map<String, DBObject> getCollectionIndexMap(PersistenceContext context, MetaClass metaClass) {
    DBCollection collection = context.getDBCollection(metaClass);
    List<DBObject> indexInfo = collection.getIndexInfo();

    Map<String, DBObject> indexMap = new HashMap<String, DBObject>();
    for (DBObject indexObject : indexInfo) {
        String name = (String) indexObject.get("name");
        indexMap.put(name, indexObject);
    }//from   w w w .  j  a va 2s .c o  m
    return indexMap;
}

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

License:Apache License

private void buildSetFieldBody(DBObject modifyBody) {
    MetaField field = getField();/*from   w w  w  .  j av  a 2s .  c  om*/
    BasicDBObject enityObject = (BasicDBObject) getEntity().getNode();
    Object fieldObject = enityObject.get(field.getFlattenValueDbName());

    if (fieldObject != null) {
        BasicDBObject set = (BasicDBObject) modifyBody.get("$set");
        set.put(field.getFlattenValueDbName(), fieldObject);
        // need update the field property
        for (FieldProperty fp : FieldProperty.values()) {
            String propertyValueDbName = field.getFlattenPropertyValueDbName(fp);
            if (enityObject.containsField(propertyValueDbName)) {
                set.put(propertyValueDbName, enityObject.get(propertyValueDbName));
            }
        }
    }
}

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();//from   w  w  w  .  ja  v a  2 s  . c om
    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.RootFieldModifyCommand.java

License:Apache License

private void buildJsonBody(DBObject modifyBody) {
    BasicDBObject set = (BasicDBObject) modifyBody.get("$set");

    MetaField field = getField();/*  ww w.j av  a 2s. c  om*/
    BasicDBObject enityObject = (BasicDBObject) getEntity().getNode();
    BasicDBObject fieldObject = (BasicDBObject) enityObject.get(field.getFlattenValueDbName());
    if (fieldObject != null) {
        BasicDBObject givenValue = fieldObject;
        if (givenValue != null) {
            for (String key : givenValue.keySet()) {
                set.put(field.getFlattenValueDbName() + DOT + key, givenValue.get(key));
            }
            // field properties
            // no length here
            set.put(field.getFlattenPropertyValueDbName(FieldProperty.TIMESTAMP),
                    getEntity().getFieldTimestamp(field.getName()));
        }
    }

}

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

License:Apache License

@Override
public void execute(PersistenceContext context) {
    DBObject queryObject = buildModifyQuery();
    DBObject updateObject = buildModifyBody();
    WriteResult result = null;/*from w  ww.j a v  a2s . c  o m*/
    result = MongoExecutor.update(context, entity.getMetaClass(), queryObject, updateObject);

    //throw exception if modify failed to match any entity
    if (result.getN() == 0) {
        DBObject getResult = RootGetCommand.findDBObject(entity.getId(), entity.getBranchId(), context,
                entity.getMetaClass(), ROOT_FIELDS, true);
        if (getResult == null) {
            throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND,
                    "Can't find entity: " + entity.toString());
        }
        Object currentVersion = getResult.get(InternalFieldEnum.VERSION.getDbName());
        throw new CmsDalException(DalErrCodeEnum.VERSION_CONFLICT, "current version is " + currentVersion
                + ", but version in repalce body is " + version + "! entity is " + entity.toString());
    }
}