List of usage examples for com.mongodb BasicDBObjectBuilder start
@SuppressWarnings("unchecked") public static BasicDBObjectBuilder start(final Map documentAsMap)
From source file:com.gigaspaces.persistency.datasource.MongoQueryFactory.java
License:Open Source License
@SuppressWarnings("static-access") private static void replaceIdProperty(BasicDBObjectBuilder qResult, SpaceTypeDescriptor typeDescriptor) { DBObject q = qResult.get();//w w w. ja v a 2 s .co m if (q.containsField(typeDescriptor.getIdPropertyName())) { Object value = q.get(typeDescriptor.getIdPropertyName()); q.removeField(typeDescriptor.getIdPropertyName()); q.put(Constants.ID_PROPERTY, value); qResult.start(q.toMap()); } }
From source file:com.gigaspaces.persistency.datasource.MongoQueryFactory.java
License:Open Source License
public static BasicDBObjectBuilder bind(String parsedQuery, Object[] parameters, SpaceTypeDescriptor spaceTypeDescriptor) { SpaceDocumentMapper<DBObject> mapper = new DefaultSpaceDocumentMapper(spaceTypeDescriptor); DBObject obj = (DBObject) JSON.parse(parsedQuery); BasicDBObjectBuilder query = BasicDBObjectBuilder.start(obj.toMap()); if (parameters != null) { query = replaceParameters(parameters, mapper, query, 0); }//from ww w. j a v a2 s . c o m return query; }
From source file:com.linuxbox.enkive.workspace.searchResult.mongo.MongoSearchResult.java
License:Open Source License
@Override public void saveSearchResult() throws WorkspaceException { BasicDBObject searchResultObject = new BasicDBObject(); searchResultObject.put(SEARCHRESULTS, BasicDBObjectBuilder.start(getMessageIds()).get()); searchResultObject.put(SEARCHQUERYID, getSearchQueryId()); if (getId() != null && !getId().isEmpty()) { DBObject toUpdate = searchResultsColl.findOne(ObjectId.massageToObjectId(getId())); if (toUpdate != null) { searchResultsColl.update(toUpdate, searchResultObject); searchResultObject.put(UUID, toUpdate.get(UUID)); }/*ww w.j ava2 s .c om*/ } if (searchResultObject.getString(UUID) == null) { searchResultsColl.insert(searchResultObject); setId(searchResultObject.getString(UUID)); } if (LOGGER.isInfoEnabled()) LOGGER.info("Saved Search Results - " + getId()); }
From source file:com.streamreduce.core.service.SearchServiceImpl.java
License:Apache License
private SobaMessage createSobaMessageFromJson(JSONObject jsonObject) { DBObject dbObject = BasicDBObjectBuilder.start(jsonObject).get(); if (dbObject.containsField("details")) { JSONObject detailsAsJson = (JSONObject) dbObject.get("details"); DBObject details = BasicDBObjectBuilder.start(detailsAsJson).get(); dbObject.put("details", details); }// w w w . j a v a2 s. c o m return (SobaMessage) new Mapper().fromDBObject(SobaMessage.class, dbObject, new DefaultEntityCache()); }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
/** * Updates a object previously stored in a collection. * @param obj - value used to update the object * @param query - statement that is used to find the object in the collection * @param collection - collection where the object is searched *///from w w w.j av a 2s . co m public void update(final DBObject obj, final DBObject query, final String collection) { checkArgument(obj != null, "Uninitialized object"); checkArgument(isNotBlank(collection), "Uninitialized or invalid collection"); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final DBCollection dbcol = db.getCollection(collection); final BasicDBObject current = (BasicDBObject) dbcol.findOne(query); checkState(current != null, "Object not found"); dbcol.save(BasicDBObjectBuilder.start(obj.toMap()).append("_id", current.get("_id")).get()); }
From source file:org.apache.rya.indexing.geotemporal.mongo.MongoEventStorage.java
License:Apache License
@Override public Collection<Event> search(final Optional<RyaURI> subject, final Optional<Collection<IndexingExpr>> geoFilters, final Optional<Collection<IndexingExpr>> temporalFilters) throws EventStorageException { requireNonNull(subject);/* w w w . j av a 2 s.com*/ try { final Collection<IndexingExpr> geos = (geoFilters.isPresent() ? geoFilters.get() : new ArrayList<>()); final Collection<IndexingExpr> tempos = (temporalFilters.isPresent() ? temporalFilters.get() : new ArrayList<>()); final DBObject filterObj = queryAdapter.getFilterQuery(geos, tempos); final BasicDBObjectBuilder builder = BasicDBObjectBuilder.start(filterObj.toMap()); if (subject.isPresent()) { builder.append(EventDocumentConverter.SUBJECT, subject.get().getData()); } final MongoCursor<Document> results = mongo.getDatabase(ryaInstanceName).getCollection(COLLECTION_NAME) .find(BsonDocument.parse(builder.get().toString())).iterator(); final List<Event> events = new ArrayList<>(); while (results.hasNext()) { events.add(EVENT_CONVERTER.fromDocument(results.next())); } return events; } catch (final MongoException | DocumentConverterException | GeoTemporalIndexException e) { throw new EventStorageException("Could not get the Event.", e); } }
From source file:org.forgerock.openidm.repo.mongodb.impl.DBHelper.java
License:Open Source License
private static void populateDefaultUser(DBCollection collection, JsonValue completeConfig, String user, String pwd, String roles) { DBObject query = (DBObject) JSON.parse("{\"_openidm_id\": \"" + user + "\"}"); if (collection.count(query) > 0) { return;//from w w w .j av a 2 s .c o m } Map<String, Object> defAdmin = new LinkedHashMap<String, Object>(); defAdmin.put("_id", user); defAdmin.put("_openidm_id", user); defAdmin.put("userName", user); try { defAdmin.put("password", JSON.parse(pwd)); } catch (com.mongodb.util.JSONParseException e) { defAdmin.put("password", pwd); } String[] role = roles.split(","); defAdmin.put("roles", role); BasicDBObjectBuilder builder = BasicDBObjectBuilder.start(defAdmin); DBObject o = builder.get(); collection.insert(o); }
From source file:org.forgerock.openidm.repo.mongodb.impl.MongoDBRepoService.java
License:Open Source License
/** * Creates a new object in the object set. * <p>/*from ww w .jav a 2 s . com*/ * This method sets the {@code _id} property to the assigned identifier for the object, * and the {@code _rev} property to the revised object version (For optimistic concurrency) * * @param fullId the client-generated identifier to use, or {@code null} if server-generated identifier is requested. * @param obj the contents of the object to create in the object set. * @throws NotFoundException if the specified id could not be resolved. * @throws ForbiddenException if access to the object or object set is forbidden. * @throws PreconditionFailedException if an object with the same ID already exists. */ @Override public void create(String fullId, Map<String, Object> obj) throws ObjectSetException { String localId = getLocalId(fullId); String type = getObjectType(fullId, false); if (fullId == null || localId == null) { throw new NotFoundException( "The repository requires clients to supply an identifier for the object to create. Full identifier: " + fullId + " local identifier: " + localId); } else if (type == null) { throw new NotFoundException( "The object identifier did not include sufficient information to determine the object type: " + fullId); } obj.put(DocumentUtil.TAG_ID, localId); obj.put(DocumentUtil.MONGODB_PRIMARY_KEY, localId); obj.put(DocumentUtil.TAG_REV, "0"); BasicDBObjectBuilder builder = BasicDBObjectBuilder.start(obj); DBObject jo = builder.get(); jo = DocumentUtil.normalizeForWrite(jo); DBCollection collection = getCollection(type); collection.insert(jo); logger.debug("Completed create for id: {} revision: {}", fullId, jo.get(DocumentUtil.TAG_REV)); logger.trace("Create payload for id: {} doc: {}", fullId, jo); }
From source file:org.forgerock.openidm.repo.mongodb.impl.MongoDBRepoService.java
License:Open Source License
/** * Updates the specified object in the object set. * <p>/*w w w. j a v a2 s.c o m*/ * This implementation requires MVCC and hence enforces that clients state what revision they expect * to be updating * * If successful, this method updates metadata properties within the passed object, * including: a new {@code _rev} value for the revised object's version * * @param fullId the identifier of the object to be put, or {@code null} to request a generated identifier. * @param rev the version of the object to update; or {@code null} if not provided. * @param obj the contents of the object to put in the object set. * @throws ConflictException if version is required but is {@code null}. * @throws ForbiddenException if access to the object is forbidden. * @throws NotFoundException if the specified object could not be found. * @throws PreconditionFailedException if version did not match the existing object in the set. * @throws BadRequestException if the passed identifier is invalid */ @Override public void update(String fullId, String rev, Map<String, Object> obj) throws ObjectSetException { String localId = getLocalId(fullId); String type = getObjectType(fullId, false); if (rev == null) { throw new ConflictException("Object passed into update does not have revision it expects set."); } else { DocumentUtil.parseVersion(rev); obj.put(DocumentUtil.TAG_REV, rev); } DBCollection collection = getCollection(type); DBObject existingDoc = predefinedQueries.getByID(localId, collection); if (existingDoc == null) { throw new NotFoundException("Update on object " + fullId + " could not find existing object."); } obj.remove(DocumentUtil.TAG_ID); obj.put(DocumentUtil.MONGODB_PRIMARY_KEY, localId); BasicDBObjectBuilder builder = BasicDBObjectBuilder.start(obj); DBObject jo = builder.get(); jo = DocumentUtil.normalizeForWrite(jo); WriteResult res = collection.update(new BasicDBObject(DocumentUtil.TAG_ID, localId), jo); logger.trace("Updated doc for id {} to save {}", fullId, jo); }