List of usage examples for com.mongodb DBObject get
Object get(String key);
From source file:com.ebay.cloud.cms.dal.entity.flatten.impl.datahandler.ReferenceTypeHandler.java
License:Apache License
/** * @param metaField //w w w.j a v a 2 s . c o m * @param value */ @Override public Object read(IEntity curEntity, Object value, MetaField metaField) { CheckConditions.checkNotNull(metaField); if (value == null) { // return null, rather than throws exception. this null should be // ignored. Related: CMS-3121, CMS-3536 return null; } NewBsonEntity be = null; try { // if (value instanceof DBObject) { DBObject dbValue = (DBObject) value; MetaRelationship metaRelation = (MetaRelationship) metaField; MetaClass refMetaClass = metaRelation.getRefMetaClass(); String type = (String) dbValue.get(InternalFieldEnum.TYPE.getDbName()); if (!StringUtils.isNullOrEmpty(type) && !type.equals(refMetaClass.getName())) { refMetaClass = refMetaClass.getMetadataService().getMetaClass(type); if (refMetaClass == null) { refMetaClass = metaRelation.getRefMetaClass(); } } be = new NewBsonEntity(refMetaClass, dbValue); // } else if (value instanceof String || value == null) { // String id = (String) value; // MetaRelationship metaRelation = (MetaRelationship) metaField; // MetaClass refMetaClass = metaRelation.getRefMetaClass(); // be = new NewBsonEntity(refMetaClass); // be.setReference(true); // be.setId(id); // } } catch (IllegalArgumentException e) { String metaClassName = curEntity.getMetaClass().getName(); String repoName = curEntity.getRepositoryName(); String branchId = curEntity.getBranchId(); String fieldName = metaField.getName(); logger.error( "Fail to validate reference type field. Entity: {}, RepoName: {}, branchId: {}, fieldName: {}, errorMessage: {}", new Object[] { metaClassName, repoName, branchId, fieldName, e.getMessage() }); } return be; }
From source file:com.ebay.cloud.cms.dal.entity.impl.bson.datahandler.ReferenceTypeHandler.java
License:Apache License
/** * @param metaField /*from w w w. j ava 2 s . c o m*/ * @param value */ @Override public Object read(IEntity curEntity, Object value, MetaField metaField) { CheckConditions.checkNotNull(metaField); if (value == null) { // return null, rather than throws exception. this null should be // ignored. Related: CMS-3121, CMS-3536 return null; } BsonEntity be = null; try { DBObject dbValue = (DBObject) value; MetaRelationship metaRelation = (MetaRelationship) metaField; MetaClass refMetaClass = metaRelation.getRefMetaClass(); String type = (String) dbValue.get(InternalFieldEnum.TYPE.getDbName()); if (!StringUtils.isNullOrEmpty(type) && !type.equals(refMetaClass.getName())) { refMetaClass = refMetaClass.getMetadataService().getMetaClass(type); if (refMetaClass == null) { refMetaClass = metaRelation.getRefMetaClass(); } } be = new BsonEntity(refMetaClass, dbValue); } catch (IllegalArgumentException e) { String metaClassName = curEntity.getMetaClass().getName(); String repoName = curEntity.getRepositoryName(); String branchId = curEntity.getBranchId(); String fieldName = metaField.getName(); logger.error( "Fail to validate reference type field. Entity: {}, RepoName: {}, branchId: {}, fieldName: {}, errorMessage: {}", new Object[] { metaClassName, repoName, branchId, fieldName, e.getMessage() }); } return be; }
From source file:com.ebay.cloud.cms.dal.persistence.flatten.impl.embed.AbstractEmbedCommand.java
License:Apache License
protected void getArrayOffsetPath(BitSet arrayBits, String embedId, DBObject getObject, MetaClass rootMetaClass, List<String> updatePathList) { DBObject currentObject = getObject; List<String> idList = helper.generateAncestorIds(embedId); List<String> fieldDbValueNameList = helper.getEmbedPathSegs(embedId, rootMetaClass); for (int index = 0; index < fieldDbValueNameList.size(); index++) { String fieldDbValueName = fieldDbValueNameList.get(index); updatePathList.add(fieldDbValueName); // updatePathList.add(MetaField.VALUE_KEY); Object fieldValue = currentObject.get(fieldDbValueName); if (arrayBits.get(index)) { @SuppressWarnings("unchecked") List<DBObject> refValues = (List<DBObject>) fieldValue; //fieldObject.get(MetaField.VALUE_KEY); String idVal = idList.get(index); int offset = findOffsetById(refValues, idVal); currentObject = refValues.get(offset); updatePathList.add(String.valueOf(offset)); } else {/*from ww w . j av a2 s . c om*/ currentObject = (DBObject) fieldValue; } } }
From source file:com.ebay.cloud.cms.dal.persistence.flatten.impl.embed.AbstractEmbedCommand.java
License:Apache License
protected int findOffsetById(List<DBObject> refValues, String embedId) { DBObject matchObject = null;// w ww.j av a 2s. co m int elemIndex = 0; for (; elemIndex < refValues.size(); elemIndex++) { DBObject refObject = refValues.get(elemIndex); String objectId = (String) refObject.get(InternalFieldEnum.ID.getDbName()); if (objectId.equals(embedId)) { matchObject = refObject; break; } } if (matchObject == null) { throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, "Can not find embed document with id=" + embedId); } return elemIndex; }
From source file:com.ebay.cloud.cms.dal.persistence.flatten.impl.embed.AbstractEmbedCommand.java
License:Apache License
protected void buildRootUpdateObject(NewBsonEntity entity, Integer newVersion, DBObject updateBody, MetaClass rootMetaClass) {/*from w w w .j a va2 s.co m*/ 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); }
From source file:com.ebay.cloud.cms.dal.persistence.flatten.impl.embed.AbstractFieldEmbedCommand.java
License:Apache License
protected void buildEmbedModifyBody(String parentPath, DBObject modifyBody) { BasicDBObject setObject = (BasicDBObject) modifyBody.get("$set"); if (setObject == null) { setObject = new BasicDBObject(); }/*from ww w. j a v a 2 s . co m*/ StringBuilder modifier = new StringBuilder(); modifier.append(parentPath).append(DOT).append(InternalFieldEnum.MODIFIER.getDbName()); StringBuilder lastModified = new StringBuilder(); lastModified.append(parentPath).append(DOT).append(InternalFieldEnum.LASTMODIFIED.getDbName()); setObject.put(modifier.toString(), entity.getModifier()); setObject.put(lastModified.toString(), entity.getLastModified()); modifyBody.put("$set", setObject); }
From source file:com.ebay.cloud.cms.dal.persistence.flatten.impl.embed.AbstractFieldEmbedCommand.java
License:Apache License
protected DBObject buildSetBody(DBObject rootObject) { BasicDBObject setModifyObject = new BasicDBObject(); Object obj = (Object) rootObject.get(embedFieldName); if (obj == null) { throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, getOperation() + ", can not find embed field with Id: " + this.entity.getId()); }/*from w ww . j ava2 s.co m*/ // update root embed field's last modified time MetaField field = rootMetaClass.getFieldByFlattenValueDbName(embedFieldName); rootObject.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.embed.EmbedCreateCommand.java
License:Apache License
@Override public void execute(PersistenceContext context) { MetaClass rootMetaClass = getRootEntityMetaClass(entity.getId(), context); String parentId = helper.getParentId(entity.getId()); BitSet parentBits = helper.checkArrayOnPath(parentId, rootMetaClass); // query root object with parent id DBObject rootQuery = buildGetQuery(parentBits, parentId, rootMetaClass); String id = parentId;// w w w . j a v a 2 s. c o m if (parentBits.cardinality() == 0) { id = entity.getId(); } DBObject rootFields = buildGetRootFields(id, rootMetaClass); DBObject rootObject = MongoExecutor.findOne(context, rootMetaClass, rootQuery, rootFields); if (rootObject == null) { throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, "Create, parenet document doesn't exist! " + parentId); } // 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 lastField = helper.getLastMetaField(entity.getId(), rootMetaClass); DBObject createBody = buildCreateBody(lastField, parentPath, entity, newVersion, rootMetaClass, rootObject, parentId); WriteResult result = MongoExecutor.update(context, rootMetaClass, createQuery, createBody); if (result.getN() == 0) { // something happens between get and replace throw new CmsDalException(DalErrCodeEnum.VERSION_CONFLICT, "Version check fails" + "! entity is " + entity.toString()); } }
From source file:com.ebay.cloud.cms.dal.persistence.flatten.impl.embed.EmbedCreateCommand.java
License:Apache License
public DBObject buildCreateBody(MetaRelationship lastField, String parentPath, NewBsonEntity entity, int newVersion, MetaClass rootMetaClass, DBObject rootObject, String parentId) { BasicDBObject embedParentObject = (BasicDBObject) EmbedDBObjectFilter.filter(parentId, rootObject, rootMetaClass, null, helper); if (embedParentObject == null) { throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, "Create, can not find embed field with Id: " + parentId); }/*from w ww .j ava2 s . c o m*/ embedParentObject.remove("_id"); // for flatten storage, the length change might differ between the embed under root and multiple level embed List<String> embedSegList = helper.getEmbedPathSegs(entity.getId(), rootMetaClass); boolean justUnderRoot = embedSegList.size() == 1; BasicDBObject setModifyObject = new BasicDBObject(); if (lastField.getCardinality() == CardinalityEnum.Many) { BasicDBList valueList = (BasicDBList) embedParentObject.get(lastField.getFlattenValueDbName()); if (valueList == null) { // creation valueList = new BasicDBList(); valueList.add(entity.getNode()); // set the value embedParentObject.put(lastField.getFlattenValueDbName(), valueList); // set the field properties setFieldProperties(lastField, setModifyObject, embedParentObject, 1, new Date(), justUnderRoot); } else { // append int size = valueList.size(); valueList.add(entity.getNode()); setFieldProperties(lastField, setModifyObject, embedParentObject, size + 1, new Date(), justUnderRoot); } } else { embedParentObject.put(lastField.getFlattenValueDbName(), entity.getNode()); setFieldProperties(lastField, setModifyObject, embedParentObject, null, new Date(), justUnderRoot); } DBObject obj = (DBObject) rootObject.get(embedFieldName); if (obj == null) { throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, "Create, can not find embed field with Id: " + this.entity.getId()); } // update root field last modified MetaField rootEmbedfield = rootMetaClass.getFieldByFlattenValueDbName(embedFieldName); setModifyObject.put(rootEmbedfield.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.embed.EmbedDBObjectFilter.java
License:Apache License
@SuppressWarnings("unchecked") private static DBObject traverse(DBObject currObject, String embedId, List<String> pathList, int index, Collection<String> fields) { if (index == pathList.size()) { if (compareEntityId(embedId, currObject)) { return filterFields(currObject, fields); }/*from w w w . j a v a2 s .c om*/ return null; } String fieldDbValueName = pathList.get(index); Object fieldValue = currObject.get(fieldDbValueName); if (fieldValue == null) { return null; } if (fieldValue instanceof List) { List<DBObject> listValue = (List<DBObject>) fieldValue; for (DBObject childObject : listValue) { DBObject res = traverse(childObject, embedId, pathList, index + 1, fields); if (res != null) { return res; } } } else if (fieldValue instanceof DBObject) { DBObject childObject = (DBObject) fieldValue; return traverse(childObject, embedId, pathList, index + 1, fields); } return null; }