List of usage examples for com.mongodb BasicDBObjectBuilder add
public BasicDBObjectBuilder add(final String key, final Object val)
From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java
License:Open Source License
private DBObject toDBObjectDocument(SpaceDocument document) { BasicDBObjectBuilder bson = BasicDBObjectBuilder.start(); Set<String> keys = document.getProperties().keySet(); bson.add(Constants.TYPE, document.getTypeName()); for (String property : keys) { Object value = document.getProperty(property); if (value == null) continue; if (spaceTypeDescriptor.getIdPropertyName().equals(property)) property = Constants.ID_PROPERTY; bson.add(property, toObject(value)); }//www .java 2s . co m return bson.get(); }
From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java
License:Open Source License
private DBObject toDBObjectPojo(Object pojo) { BasicDBObjectBuilder bson = BasicDBObjectBuilder.start(); Map<String, Method> getters = repository.getGetters(pojo.getClass()); Class<?> type = pojo.getClass(); bson.add(Constants.TYPE, type.getName()); for (String property : getters.keySet()) { Object value = null;/*from ww w . j a va2 s .c o m*/ try { value = repository.getGetter(type, property).get(pojo); if (value == null) continue; if (spaceTypeDescriptor.getIdPropertyName().equals(property)) property = Constants.ID_PROPERTY; bson.add(property, toObject(value)); } catch (IllegalArgumentException e) { throw new SpaceMongoException("Argument is: " + value, e); } catch (IllegalAccessException e) { throw new SpaceMongoException("Can not access method", e); } catch (InvocationTargetException e) { throw new SpaceMongoException("Can not invoke method", e); } } return bson.get(); }
From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java
License:Open Source License
public Object toObject(Object property) { if (property == null) return null; switch (type(property.getClass())) { case TYPE_CHAR: case TYPE_FLOAT: case TYPE_BYTE: case TYPE_BIGDECIMAL: case TYPE_BIGINT: return toSpecialType(property); case TYPE_OBJECT: if (property instanceof SpaceDocument) return toDBObject((SpaceDocument) property); else if (property instanceof Class) return toSpecialType(property); else if (property instanceof Locale) return toSpecialType(property); else if (property instanceof URI) return toSpecialType(property); else if (property instanceof Timestamp) return toSpecialType(property); if (!(property instanceof Serializable)) return toDBObject(property); byte[] result = serializeObject(property); BasicDBObjectBuilder blob = BasicDBObjectBuilder.start(); blob.add(Constants.TYPE, Constants.CUSTOM_BINARY); blob.add(Constants.VALUE, result); blob.add(Constants.HASH, Arrays.hashCode(result)); return blob.get(); case TYPE_ENUM: return toEnum(property); case TYPE_ARRAY: return toArray(property); case TYPE_COLLECTION: return toCollection(property); case TYPE_MAP: return toMap(property); default:/*from w w w . ja v a 2 s . com*/ return property; } }
From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java
License:Open Source License
private Object toEnum(Object property) { BasicDBObjectBuilder document = BasicDBObjectBuilder.start(); return document.add(Constants.TYPE, property.getClass().getName()).add(Constants.VALUE, property.toString()) .get();//from w w w.java 2 s . com }
From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java
License:Open Source License
private DBObject toSpecialType(Object property) { BasicDBObjectBuilder document = BasicDBObjectBuilder.start(); String toString = toString(property); return document.add(Constants.TYPE, property.getClass().getName()).add(Constants.VALUE, toString).get(); }
From source file:com.gigaspaces.persistency.MongoClientConnector.java
License:Open Source License
public void introduceType(SpaceTypeDescriptor typeDescriptor) { DBCollection m = getConnection().getCollection(METADATA_COLLECTION_NAME); BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().add(Constants.ID_PROPERTY, typeDescriptor.getTypeName()); try {//from ww w .java2 s . c om ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); IOUtils.writeObject(out, SpaceTypeDescriptorVersionedSerializationUtils.toSerializableForm(typeDescriptor)); builder.add(TYPE_DESCRIPTOR_FIELD_NAME, bos.toByteArray()); WriteResult wr = m.save(builder.get()); if (logger.isTraceEnabled()) logger.trace(wr); indexBuilder.ensureIndexes(typeDescriptor); } catch (IOException e) { logger.error(e); throw new SpaceMongoException( "error occurs while serialize and save type descriptor: " + typeDescriptor, e); } }
From source file:com.gigaspaces.persistency.MongoClientConnector.java
License:Open Source License
private static DBObject normalize(DBObject obj) { BasicDBObjectBuilder builder = BasicDBObjectBuilder.start(); Iterator<String> iterator = obj.keySet().iterator(); builder.push("$set"); while (iterator.hasNext()) { String key = iterator.next(); if (Constants.ID_PROPERTY.equals(key)) continue; Object value = obj.get(key); if (value == null) continue; builder.add(key, value); }/* w w w.j av a 2s . c om*/ return builder.get(); }
From source file:com.heisenberg.mongo.MongoWorkflowInstanceStore.java
License:Apache License
public WorkflowInstanceImpl lockWorkflowInstance(WorkflowInstanceQueryImpl processInstanceQuery) { BasicDBObjectBuilder builder = BasicDBObjectBuilder.start(); if (processInstanceQuery.workflowInstanceId != null) { builder.add(fields._id, new ObjectId(processInstanceQuery.workflowInstanceId)); }/*from www . java2s . co m*/ if (processInstanceQuery.activityInstanceId != null) { builder.add(fields.activityInstances + "." + fields._id, new ObjectId(processInstanceQuery.activityInstanceId)); } DBObject query = builder.push(fields.lock).add("$exists", false).pop().get(); DBObject update = BasicDBObjectBuilder.start().push("$set").push(fields.lock) .add(fields.time, Time.now().toDate()).add(fields.owner, processEngine.getId()).pop().pop().get(); DBObject retrieveFields = new BasicDBObject().append(fields.archivedActivityInstances, false); BasicDBObject dbProcessInstance = findAndModify(query, update, retrieveFields); if (dbProcessInstance == null) { return null; } WorkflowInstanceImpl workflowInstance = readProcessInstance(dbProcessInstance); workflowInstance.trackUpdates(false); return workflowInstance; }
From source file:com.ibm.ws.lars.rest.PersistenceBean.java
License:Apache License
/** {@inheritDoc} */ @Override//from w w w . ja v a 2s .c o m public void initialize() { // Make sure the fields we want to query are indexed DBCollection assets = db.getCollection(ASSETS_COLLECTION); DBCollection attachments = db.getCollection(ATTACHMENTS_COLLECTION); // Add text index BasicDBObjectBuilder textIndex = BasicDBObjectBuilder.start(); for (String indexField : searchIndexFields) { textIndex.add(indexField, "text"); } assets.ensureIndex(textIndex.get()); // Add Attachment(assetId) index attachments.ensureIndex(new BasicDBObject("assetId", 1)); }
From source file:com.impetus.client.mongodb.DocumentObjectMapper.java
License:Apache License
/** * Extract entity field.//www. j a va 2 s. c o m * * @param entity * the entity * @param dbObj * the db obj * @param column * the column * @throws PropertyAccessException * the property access exception */ static void extractFieldValue(Object entity, DBObject dbObj, Attribute column) throws PropertyAccessException { try { Object valueObject = PropertyAccessorHelper.getObject(entity, (Field) column.getJavaMember()); if (valueObject != null) { Class javaType = column.getJavaType(); switch (AttributeType.getType(javaType)) { case MAP: Map mapObj = (Map) valueObject; // BasicDBObjectBuilder builder = // BasicDBObjectBuilder.start(mapObj); BasicDBObjectBuilder b = new BasicDBObjectBuilder(); Iterator i = mapObj.entrySet().iterator(); while (i.hasNext()) { Map.Entry entry = (Map.Entry) i.next(); b.add(entry.getKey().toString(), MongoDBUtils.populateValue(entry.getValue(), entry.getValue().getClass())); } dbObj.put(((AbstractAttribute) column).getJPAColumnName(), b.get()); break; case SET: case LIST: Collection collection = (Collection) valueObject; BasicDBList basicDBList = new BasicDBList(); for (Object o : collection) { basicDBList.add(o); } dbObj.put(((AbstractAttribute) column).getJPAColumnName(), basicDBList); break; case POINT: Point p = (Point) valueObject; double[] coordinate = new double[] { p.getX(), p.getY() }; dbObj.put(((AbstractAttribute) column).getJPAColumnName(), coordinate); break; case ENUM: case PRIMITIVE: dbObj.put(((AbstractAttribute) column).getJPAColumnName(), MongoDBUtils.populateValue(valueObject, javaType)); break; } } } catch (PropertyAccessException paex) { log.error("Error while getting column {} value, caused by : .", ((AbstractAttribute) column).getJPAColumnName(), paex); throw new PersistenceException(paex); } }