List of usage examples for org.springframework.data.mongodb.core.query Update set
public Update set(String key, Object value)
From source file:org.slc.sli.dal.repository.MongoEntityRepository.java
@Override public boolean patch(String type, String collectionName, String id, Map<String, Object> newValues) { boolean result = false; Entity entity = new MongoEntity(type, id, newValues, null); validator.validatePresent(entity);/*w ww. j a v a 2 s . co m*/ validator.validateNaturalKeys(entity, false); keyEncoder.encodeEntityKey(entity); if (subDocs.isSubDoc(collectionName)) { // prepare to find desired record to be patched Query query = new Query(); query.addCriteria(Criteria.where("_id").is(idConverter.toDatabaseId(id))); query.addCriteria(createTenantCriteria(collectionName)); // prepare update operation for record to be patched Update update = new Update(); for (Entry<String, Object> patch : newValues.entrySet()) { update.set("body." + patch.getKey(), patch.getValue()); } result = subDocs.subDoc(collectionName).doUpdate(query, update); } else if (containerDocumentAccessor.isContainerDocument(type)) { result = containerDocumentAccessor.update(type, id, newValues, collectionName); } else { result = super.patch(type, collectionName, id, newValues); if (result && EntityNames.EDUCATION_ORGANIZATION.equals(collectionName) && newValues.containsKey(ParameterConstants.PARENT_EDUCATION_AGENCY_REFERENCE)) { // TODO can be optimized to take an edOrg Id updateAllSchoolLineage(); } } if (result && denormalizer.isDenormalizedDoc(collectionName)) { Entity updateEntity; if (subDocs.isSubDoc(collectionName)) { updateEntity = subDocs.subDoc(collectionName).findById(id); } else { updateEntity = super.findById(collectionName, id); } Update update = new Update(); for (Map.Entry<String, Object> patch : newValues.entrySet()) { update.set(patch.getKey(), patch.getValue()); } denormalizer.denormalization(collectionName).doUpdate(updateEntity, update); } if (journal != null) { journal.journal(id, collectionName, false); } return result; }
From source file:org.slc.sli.dal.repository.MongoEntityRepository.java
@Override protected Update getUpdateCommand(Entity entity, boolean isSuperdoc) { // set up update query Update update = new Update(); // It is possible for the body and metaData keys to be absent in the case // of "orphaned" subDoc data when a document has been "hollowed out" Map<String, Object> entityBody = entity.getBody(); if (entityBody != null && entityBody.size() == 0) { update.unset("body"); } else {// ww w.j ava 2 s . c o m update.set("body", entityBody); } Map<String, Object> entityMetaData = entity.getMetaData(); if (entityMetaData != null && entityMetaData.size() == 0) { update.unset("metaData"); } else { update.set("metaData", entityMetaData); } // update should also set type in case of upsert String entityType = entity.getType(); if (entityType != null && !entityType.isEmpty()) { update.set("type", entityType); } // superdoc need to update subdoc fields outside body if (isSuperdoc && entity.getEmbeddedData() != null) { Set<String> subdocFields = FullSuperDoc.FULL_ENTITIES.get(entity.getType()); if (subdocFields != null) { for (String subdocField : subdocFields) { List<Entity> subdocEntities = entity.getEmbeddedData().get(subdocField); if (subdocEntities != null && subdocEntities.size() > 0) { List<Map<String, Object>> updateEntities = new ArrayList<Map<String, Object>>(); for (Entity subdocEntity : subdocEntities) { Map<String, Object> updateEntity = new HashMap<String, Object>(); updateEntity.put("_id", subdocEntity.getEntityId()); updateEntity.put("body", subdocEntity.getBody()); updateEntity.put("type", subdocEntity.getType()); updateEntity.put("metaData", subdocEntity.getMetaData()); updateEntities.add(updateEntity); } update.set(subdocField, updateEntities); } else { update.unset(subdocField); } } } } return update; }
From source file:org.slc.sli.dal.repository.MongoEntityRepository.java
/** * Updates the cached education organization hierarchy for the specified school * * @param schoolId/*from www .j av a 2 s . co m*/ * @return whether the update succeeded */ private boolean updateSchoolLineage(String schoolId) { try { NeutralQuery query = new NeutralQuery( new NeutralCriteria("_id", NeutralCriteria.OPERATOR_EQUAL, schoolId)); Update update = new Update(); update.set("metaData.edOrgs", new ArrayList<String>(fetchLineage(schoolId, new HashSet<String>()))); super.doUpdate(EntityNames.EDUCATION_ORGANIZATION, query, update); } catch (RuntimeException e) { LOG.error("Failed to update educational organization lineage for school with Id " + schoolId + ". " + e.getMessage() + ". Related data will not be visible. Re-ingestion required."); throw e; } return true; }
From source file:org.slc.sli.dal.repository.MongoRepository.java
@Override public boolean patch(String type, String collectionName, String id, Map<String, Object> newValues) { if (id.equals("")) { return false; }//from w w w. j a va2 s .c om // prepare to find desired record to be patched Query query = new Query(); query.addCriteria(Criteria.where("_id").is(idConverter.toDatabaseId(id))); query.addCriteria(createTenantCriteria(collectionName)); // prepare update operation for record to be patched Update update = new Update(); for (Entry<String, Object> patch : newValues.entrySet()) { update.set("body." + patch.getKey(), patch.getValue()); } WriteResult result = updateFirst(query, update, collectionName); return (result.getN() == 1); }
From source file:org.slc.sli.ingestion.processors.CommandProcessor.java
private void dumpMongoTrackingStats(String batchId) throws UnknownHostException { Map<String, ? extends Map<String, Pair<AtomicLong, AtomicLong>>> stats = Aspects .aspectOf(MongoTrackingAspect.class).getStats(); if (stats != null) { String hostName = InetAddress.getLocalHost().getHostName(); hostName = hostName.replaceAll("\\.", "#"); Update update = new Update(); update.set("executionStats." + hostName, stats); LOG.info("Dumping runtime stats to db for job {}", batchId); LOG.info(stats.toString());//from w w w . j a va2 s . c om // TODO: move to BatchJobDAO mongo.updateFirst(new Query(Criteria.where(BATCH_JOB_ID).is(batchId)), update, "newBatchJob"); Aspects.aspectOf(MongoTrackingAspect.class).reset(); } }
From source file:piecework.repository.concrete.ProcessInstanceRepositoryCustomImpl.java
@Override public ProcessInstance update(String id, Operation operation, String applicationStatus, String applicationStatusExplanation, String processStatus, Set<Task> tasks) { Query query = new Query(where("_id").is(id)); Update update = new Update(); if (applicationStatus != null) update.set("applicationStatus", applicationStatus); if (applicationStatusExplanation != null) update.set("applicationStatusExplanation", applicationStatusExplanation); if (processStatus != null) update.set("processStatus", processStatus); if (tasks != null) { for (Task task : tasks) { update.set("tasks." + task.getTaskInstanceId(), task); }/* w w w . j a v a2 s .co m*/ } update.push("operations", operation); update.set("lastModifiedTime", new Date()); FindAndModifyOptions options = new FindAndModifyOptions(); options.returnNew(true); return mongoOperations.findAndModify(query, update, options, ProcessInstance.class); }
From source file:piecework.repository.concrete.ProcessInstanceRepositoryCustomImpl.java
@Override public boolean update(String id, Task task) { Query query = new Query(); query.addCriteria(where("processInstanceId").is(id)); Update update = new Update(); update.set("tasks." + task.getTaskInstanceId(), task).set("lastModifiedTime", new Date()); FindAndModifyOptions options = new FindAndModifyOptions(); options.returnNew(true);// w w w . jav a 2s .c om ProcessInstance stored = mongoOperations.findAndModify(query, update, options, ProcessInstance.class); return true; }
From source file:piecework.repository.concrete.ProcessInstanceRepositoryCustomImpl.java
private ProcessInstance updateEfficiently(String id, String label, Map<String, List<Value>> data, Map<String, List<Message>> messages, List<Attachment> attachments, Submission submission, String applicationStatusExplanation) { Query query = new Query(where("_id").is(id)); Update update = new Update(); if (applicationStatusExplanation != null) update.set("applicationStatusExplanation", applicationStatusExplanation); include(update, attachments);// w ww .j av a 2 s .c o m include(update, data, id); include(update, label); include(update, submission); includeMessages(update, messages); update.set("lastModifiedTime", new Date()); return mongoOperations.findAndModify(query, update, OPTIONS, ProcessInstance.class); }
From source file:piecework.repository.concrete.ProcessInstanceRepositoryCustomImpl.java
private void include(Update update, Map<String, List<Value>> data, String id) { if (data != null && !data.isEmpty()) { MongoConverter converter = mongoOperations.getConverter(); MongoTypeMapper typeMapper = converter.getTypeMapper(); Set<String> keywords = new HashSet<String>(); for (Map.Entry<String, List<Value>> entry : data.entrySet()) { String key = "data." + entry.getKey(); List<Value> values = entry.getValue(); List<Object> dbObjects = new ArrayList<Object>(); for (Value value : values) { if (value != null) { Object dbObject = converter.convertToMongoType(value); Class<?> clz = null; if (value instanceof File) clz = File.class; else if (value instanceof User) clz = User.class; else if (value instanceof Secret) clz = Secret.class; else if (value instanceof DateValue) clz = DateValue.class; keywords.addAll(ProcessInstanceUtility.keywords(value)); if (clz != null) typeMapper.writeType(clz, DBObject.class.cast(dbObject)); dbObjects.add(dbObject); }//from w w w . j a v a 2s. co m } update.set(key, dbObjects); } if (!keywords.isEmpty()) { BasicDBList eachList = new BasicDBList(); for (String keyword : keywords) { eachList.add(keyword); } if (StringUtils.isNotEmpty(id)) eachList.add(id); update.addToSet("keywords", BasicDBObjectBuilder.start("$each", eachList).get()); } } }
From source file:piecework.repository.concrete.ProcessInstanceRepositoryCustomImpl.java
private void includeMessages(Update update, Map<String, List<Message>> messages) { if (messages != null) { if (messages.isEmpty()) { update.unset("messages"); } else {//from ww w. ja v a 2 s .co m update.set("messages", messages); // MongoConverter converter = mongoOperations.getConverter(); // for (Map.Entry<String, List<Message>> entry : messages.entrySet()) { // String key = "messages." + entry.getKey(); // List<Message> values = entry.getValue(); // List<Object> dbObjects = new ArrayList<Object>(); // // for (Message value : values) { // if (value != null) { // Object dbObject = converter.convertToMongoType(value); // dbObjects.add(dbObject); // } // } // // update.set(key, dbObjects); // } } } }