List of usage examples for com.mongodb.client.result UpdateResult wasAcknowledged
public abstract boolean wasAcknowledged();
From source file:com.yahoo.ycsb.db3.MongoDbClient.java
License:Open Source License
/** * Update a record in the database. Any field/value pairs in the specified * values HashMap will be written into the record with the specified record * key, overwriting any existing values with the same field name. * /*w w w .j a v a 2 s .co m*/ * @param table * The name of the table * @param key * The record key of the record to write. * @param values * A HashMap of field/value pairs to update in the record * @return Zero on success, a non-zero error code on error. See this class's * description for a discussion of error codes. */ @Override public Status update(String table, String key, HashMap<String, ByteIterator> values) { try { MongoCollection<Document> collection = database.getCollection(table); Document query = new Document("_id", key); Document fieldsToSet = new Document(); for (Map.Entry<String, ByteIterator> entry : values.entrySet()) { fieldsToSet.put(entry.getKey(), entry.getValue().toArray()); } Document update = new Document("$set", fieldsToSet); UpdateResult result = collection.updateOne(query, update); if (result.wasAcknowledged() && result.getMatchedCount() == 0) { System.err.println("Nothing updated for key " + key); return Status.NOT_FOUND; } return Status.OK; } catch (Exception e) { System.err.println(e.toString()); return Status.ERROR; } }
From source file:eu.operando.core.ose.mongo.RegulationsMongo.java
public boolean updateRegulation(String regId, PrivacyRegulationInput regulation) { boolean result = false; Bson filter = null;//from w w w. j a va 2 s. c o m try { filter = new Document("_id", new ObjectId(regId)); } catch (IllegalArgumentException ex) { ex.printStackTrace(); return result; } try { ObjectMapper mapper = new ObjectMapper(); String jsonInString = mapper.writeValueAsString(regulation); Document doc = Document.parse(jsonInString); try { UpdateResult ur = regulationsCollection.replaceOne(filter, doc); result = ur.wasAcknowledged(); } catch (MongoWriteException ex) { ex.printStackTrace(); } catch (MongoWriteConcernException ex) { ex.printStackTrace(); } catch (MongoException ex) { ex.printStackTrace(); } } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; }
From source file:eu.operando.core.pdb.mongo.OSPPrivacyPolicyMongo.java
/** * * @param ospId//w ww .jav a 2 s. c o m * @param ospPolicyInput * @return */ public boolean updateReasonPolicyOSP(String ospId, OSPReasonPolicyInput ospPolicyInput) { boolean result = false; Bson filter = null; try { filter = new Document("ospPolicyId", ospId); } catch (IllegalArgumentException ex) { ex.printStackTrace(); return result; } // create the actual osp reason policy object OSPReasonPolicy ospPolicy = new OSPReasonPolicy(); ospPolicy.setOspPolicyId(ospId); ospPolicy.setPolicies(ospPolicyInput.getPolicies()); System.out.println("update privacy policies " + ospPolicy.toString()); try { ObjectMapper mapper = new ObjectMapper(); String jsonInString = mapper.writeValueAsString(ospPolicy); Document doc = Document.parse(jsonInString); System.out.println("doc: " + doc.toString()); try { System.out.println("INSIDE"); UpdateOptions options = new UpdateOptions().upsert(true); UpdateResult ur = ospPPCollection.replaceOne(filter, doc, options); result = ur.wasAcknowledged(); System.out.println("RESULT: " + result + " -> " + ur.toString()); } catch (MongoWriteException ex) { ex.printStackTrace(); } catch (MongoWriteConcernException ex) { ex.printStackTrace(); } catch (MongoException ex) { ex.printStackTrace(); } } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; }
From source file:eu.operando.core.pdb.mongo.OSPPrivacyPolicyMongo.java
/** * * @param ospId/* www . j a v a 2 s . c o m*/ * @param ospPolicy * @return */ public boolean updateOSP(String ospId, OSPPrivacyPolicyInput ospPolicy) { boolean result = false; Bson filter = null; try { filter = new Document("_id", new ObjectId(ospId)); } catch (IllegalArgumentException ex) { ex.printStackTrace(); return result; } try { ObjectMapper mapper = new ObjectMapper(); String jsonInString = mapper.writeValueAsString(ospPolicy); Document doc = Document.parse(jsonInString); try { UpdateResult ur = ospCollection.replaceOne(filter, doc); result = ur.wasAcknowledged(); } catch (MongoWriteException ex) { ex.printStackTrace(); } catch (MongoWriteConcernException ex) { ex.printStackTrace(); } catch (MongoException ex) { ex.printStackTrace(); } } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; }
From source file:eu.operando.core.pdb.mongo.OSPPrivacyPolicyMongo.java
/** * * @param ospPolicyId//from ww w . j a va 2s .c o m * @param accessReason * @return */ public boolean privacyPolicyAccessReasonsPost(String ospPolicyId, AccessReason accessReason) { Boolean ret = false; System.out.println("accessReasonIdPost(" + ospPolicyId + ");"); // find Bson filter = null; try { filter = new Document("ospPolicyId", ospPolicyId); } catch (IllegalArgumentException ex) { ex.printStackTrace(); return ret; } List<Document> result = (List<Document>) ospPPCollection.find(filter).into(new ArrayList<Document>()); if (!result.isEmpty()) { Document doc = result.get(0); OSPReasonPolicy ospReasonObj = null; try { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); ospReasonObj = mapper.readValue(doc.toJson(), OSPReasonPolicy.class); System.out.println(ospReasonObj); //add policy id //ospReasonObj.setOspPolicyId(doc.get("_id").toString()); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //ospReasonObj.setOspPolicyId(ospPolicyId); ospReasonObj.getPolicies().add(accessReason); try { ObjectMapper mapper = new ObjectMapper(); String jsonInString = mapper.writeValueAsString(ospReasonObj); doc = Document.parse(jsonInString); try { UpdateResult ur = ospPPCollection.replaceOne(filter, doc); ret = ur.wasAcknowledged(); } catch (MongoWriteException ex) { ex.printStackTrace(); } catch (MongoWriteConcernException ex) { ex.printStackTrace(); } catch (MongoException ex) { ex.printStackTrace(); } } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return ret; }
From source file:eu.operando.core.pdb.mongo.OSPPrivacyPolicyMongo.java
/** * * @param ospPolicyId/*from w w w . ja va 2s . co m*/ * @param reasonId * @return */ public boolean accessReasonIdDelete(String ospPolicyId, String reasonId) { boolean ret = false; Bson filter = null; try { filter = new Document("ospPolicyId", ospPolicyId); } catch (IllegalArgumentException ex) { ex.printStackTrace(); return ret; } System.out.println("accessReasonIdDelete(" + ospPolicyId + ", " + reasonId + ");"); List<Document> result = (List<Document>) ospPPCollection.find(filter).into(new ArrayList<Document>()); if (!result.isEmpty()) { Document doc = result.get(0); OSPReasonPolicy ospReasonObj = null; try { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); ospReasonObj = mapper.readValue(doc.toJson(), OSPReasonPolicy.class); //add policy id //ospReasonObj.setOspPolicyId(doc.get("_id").toString()); List<AccessReason> arList = new ArrayList<AccessReason>(); for (AccessReason ar : ospReasonObj.getPolicies()) { if (!ar.getReasonid().endsWith(reasonId)) { arList.add(ar); } } ospReasonObj.setPolicies(arList); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { ObjectMapper mapper = new ObjectMapper(); String jsonInString = mapper.writeValueAsString(ospReasonObj); doc = Document.parse(jsonInString); try { UpdateResult ur = ospPPCollection.replaceOne(filter, doc); ret = ur.wasAcknowledged(); } catch (MongoWriteException ex) { ex.printStackTrace(); } catch (MongoWriteConcernException ex) { ex.printStackTrace(); } catch (MongoException ex) { ex.printStackTrace(); } } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return ret; }
From source file:eu.operando.core.pdb.mongo.OSPPrivacyPolicyMongo.java
/** * * @param ospPolicyId// w w w . j a v a 2 s .c o m * @param reasonId * @param accessReason * @return */ public boolean accessReasonIdUpdate(String ospPolicyId, String reasonId, AccessReason accessReason) { boolean ret = false; Bson filter = null; try { filter = new Document("ospPolicyId", ospPolicyId); } catch (IllegalArgumentException ex) { ex.printStackTrace(); return ret; } System.out.println("accessReasonIdUpdate(" + ospPolicyId + ", " + reasonId + ");"); List<Document> result = (List<Document>) ospPPCollection.find(filter).into(new ArrayList<Document>()); if (!result.isEmpty()) { Document doc = result.get(0); OSPReasonPolicy ospReasonObj = null; try { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); ospReasonObj = mapper.readValue(doc.toJson(), OSPReasonPolicy.class); //add policy id //ospReasonObj.setOspPolicyId(doc.get("_id").toString()); List<AccessReason> arList = new ArrayList<AccessReason>(); for (AccessReason ar : ospReasonObj.getPolicies()) { if (ar.getReasonid().endsWith(reasonId)) { arList.add(accessReason); } else { arList.add(ar); } } ospReasonObj.setPolicies(arList); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { ObjectMapper mapper = new ObjectMapper(); String jsonInString = mapper.writeValueAsString(ospReasonObj); doc = Document.parse(jsonInString); try { UpdateResult ur = ospPPCollection.replaceOne(filter, doc); ret = ur.wasAcknowledged(); } catch (MongoWriteException ex) { ex.printStackTrace(); } catch (MongoWriteConcernException ex) { ex.printStackTrace(); } catch (MongoException ex) { ex.printStackTrace(); } } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return ret; }
From source file:eu.operando.core.pdb.mongo.RegulationsMongo.java
public boolean updateRegulation(String regId, PrivacyRegulationInput regulation) { boolean result = false; Bson filter = null;/* www . j av a2s. com*/ try { filter = new Document("_id", new ObjectId(regId)); } catch (IllegalArgumentException ex) { ex.printStackTrace(); return result; } try { ObjectMapper mapper = new ObjectMapper(); String jsonInString = mapper.writeValueAsString(regulation); Document doc = Document.parse(jsonInString); try { UpdateResult ur = regCollection.replaceOne(filter, doc); result = ur.wasAcknowledged(); } catch (MongoWriteException ex) { ex.printStackTrace(); } catch (MongoWriteConcernException ex) { ex.printStackTrace(); } catch (MongoException ex) { ex.printStackTrace(); } } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; }
From source file:org.nuxeo.directory.mongodb.MongoDBSession.java
License:Apache License
@Override public void updateEntry(DocumentModel docModel) throws DirectoryException { checkPermission(SecurityConstants.WRITE); Map<String, Object> fieldMap = new HashMap<>(); List<String> referenceFieldList = new LinkedList<>(); for (String fieldName : schemaFieldMap.keySet()) { Property prop = docModel.getPropertyObject(schemaName, fieldName); if (fieldName.equals(getPasswordField()) && StringUtils.isEmpty((String) prop.getValue())) { continue; }/*from w w w . j a va2 s .c om*/ if (prop != null && prop.isDirty()) { Serializable value = prop.getValue(); if (fieldName.equals(getPasswordField())) { value = PasswordHelper.hashPassword((String) value, passwordHashAlgorithm); } fieldMap.put(prop.getName(), value); } if (getDirectory().isReference(fieldName)) { referenceFieldList.add(fieldName); } } String id = docModel.getId(); Document bson = MongoDBSerializationHelper.fieldMapToBson(getIdField(), id); List<Bson> updates = fieldMap.entrySet().stream().map(e -> Updates.set(e.getKey(), e.getValue())) .collect(Collectors.toList()); try { UpdateResult result = getCollection().updateOne(bson, Updates.combine(updates)); // Throw an error if no document matched the update if (!result.wasAcknowledged()) { throw new DirectoryException( "Error while updating the entry, the request has not been acknowledged by the server"); } if (result.getMatchedCount() == 0) { throw new DirectoryException( String.format("Error while updating the entry, no document was found with the id %s", id)); } } catch (MongoWriteException e) { throw new DirectoryException(e); } // update reference fields for (String referenceFieldName : referenceFieldList) { List<Reference> references = directory.getReferences(referenceFieldName); if (references.size() > 1) { // not supported log.warn("Directory " + getDirectory().getName() + " cannot update field " + referenceFieldName + " for entry " + docModel.getId() + ": this field is associated with more than one reference"); } else { Reference reference = references.get(0); @SuppressWarnings("unchecked") List<String> targetIds = (List<String>) docModel.getProperty(schemaName, referenceFieldName); if (reference instanceof MongoDBReference) { MongoDBReference mongoReference = (MongoDBReference) reference; mongoReference.setTargetIdsForSource(docModel.getId(), targetIds, this); } else { reference.setTargetIdsForSource(docModel.getId(), targetIds); } } } getDirectory().invalidateCaches(); }
From source file:org.seadpdt.impl.ROServicesImpl.java
License:Apache License
@POST @Path("/{id}/status") @Consumes(MediaType.APPLICATION_JSON)// w ww. j a v a2 s . c om public Response setROStatus(@PathParam("id") String id, String state) { try { Document statusUpdateDocument = Document.parse(state); statusUpdateDocument.append("date", DateFormat.getDateTimeInstance().format(new Date(System.currentTimeMillis()))); UpdateResult ur = publicationsCollection.updateOne(new Document("Aggregation.Identifier", id), new BasicDBObject("$push", new BasicDBObject("Status", statusUpdateDocument))); if (ur.wasAcknowledged()) { return Response.status(ClientResponse.Status.OK).build(); } else { return Response.status(ClientResponse.Status.NOT_FOUND) .entity(new JSONObject().put("Error", "Error while updating status")).build(); } } catch (org.bson.BsonInvalidOperationException e) { return Response.status(ClientResponse.Status.BAD_REQUEST) .entity(new JSONObject().put("Error", "Error while updating status")).build(); } }