@Override public void save(Map obj) { obj = mongifyIdField(obj); if (obj.get("_id") == null){ //must create new id rawDataCollection.insert(new BasicDBObject(obj)); return; } DBObject idQuery = new BasicDBObject("_id", obj.get("_id")); obj.remove("_id"); Map<String,Object> setExpression = new HashMap<>(); setExpression.put("$set", obj); try { rawDataCollection.update(idQuery, new BasicDBObject(setExpression),true, false); //upsert true, multi false } catch (WriteConcernException ex) { log.error("Invalid JSON/failed to save object", obj.toString()); } }
@Test public void createValidation() { getMorphia().map(DocumentValidation.class); getDs().enableDocumentValidation(); assertEquals(Document.parse(DocumentValidation.class.getAnnotation(Validation.class).value()), getValidator()); try { getDs().save(new DocumentValidation("John", 1, new Date())); fail("Document should have failed validation"); } catch (WriteConcernException e) { assertTrue(e.getMessage().contains("Document failed validation")); } getDs().save(new DocumentValidation("Harold", 100, new Date())); }
@Test public void update() { getMorphia().map(DocumentValidation.class); getDs().enableDocumentValidation(); getDs().save(new DocumentValidation("Harold", 100, new Date())); Query<DocumentValidation> query = getDs().find(DocumentValidation.class); UpdateOperations<DocumentValidation> updates = getDs().createUpdateOperations(DocumentValidation.class) .set("number", 5); UpdateOptions options = new UpdateOptions() .bypassDocumentValidation(false); try { getDs().update(query, updates, options); fail("Document validation should have complained"); } catch (WriteConcernException e) { // expected } options.bypassDocumentValidation(true); getDs().update(query, updates, options); Assert.assertNotNull(query.field("number").equal(5).get()); }
@Test public void saveToNewCollection() { getMorphia().map(DocumentValidation.class); final Document validator = Document.parse("{ number : { $gt : 10 } }"); String collection = "newdocs"; addValidation(validator, collection); try { getAds().save(collection, new DocumentValidation("Harold", 8, new Date())); fail("Document validation should have complained"); } catch (WriteConcernException e) { // expected } getAds().save(collection, new DocumentValidation("Harold", 8, new Date()), new InsertOptions() .bypassDocumentValidation(true)); Query<DocumentValidation> query = getAds().createQuery(collection, DocumentValidation.class) .field("number").equal(8); Assert.assertNotNull(query.get()); }
public void process(Exchange exchange) throws Exception { Message in = exchange.getIn(); in.setHeader(Exchange.HTTP_RESPONSE_CODE, 500); Throwable caused = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class); if (caused.getCause() instanceof WriteConcernException) { in.setBody(JSON.serialize(((WriteConcernException) caused.getCause()).getCommandResult())); } else if (caused.getCause() instanceof CommandFailureException) { in.setBody(JSON.serialize(((CommandFailureException) caused .getCause()).getCommandResult())); } else if (caused.getCause() instanceof MongoException) { MongoException rootCause = (MongoException) caused.getCause(); in.setBody( "{\"code\" : \"" + rootCause.getCode() + "\" ,\"err\" : \"" + rootCause.getMessage().replace("\"", "\'") + "\" }"); } else if (caused.getCause() instanceof CamelMongoDbException) { in.setBody("{ \"err\" : \"" + caused.getCause().getMessage().replace("\"", "\'") + "\" }"); } else { String ret = "\"err\" : \"" + caused.getClass().getName() + " => " + caused.getMessage().replace("\"", "\'") + "\""; if (caused.getCause() != null) { ret = ret + ", \"cause\" : \"" + caused.getCause().getClass().getName() + " => " + caused.getCause().getMessage().replace("\"", "\'") + "\""; } in.setBody("{" + ret.replace("\n", "") + "}"); log.info("User error",caused); } }