use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class LifecycleEncoder method encode.
@Override
public void encode(BsonWriter writer, T value, EncoderContext encoderContext) {
EntityModel model = getMorphiaCodec().getEntityModel();
Datastore datastore = getMorphiaCodec().getDatastore();
Document document = new Document();
model.callLifecycleMethods(PrePersist.class, value, document, datastore);
final DocumentWriter documentWriter = new DocumentWriter(datastore.getMapper(), document);
super.encode(documentWriter, value, encoderContext);
document = documentWriter.getDocument();
model.callLifecycleMethods(PostPersist.class, value, document, datastore);
getMorphiaCodec().getRegistry().get(Document.class).encode(writer, document, encoderContext);
}
use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class TestDocumentWriter method arraysWithDocs.
@Test
public void arraysWithDocs() {
DocumentWriter writer = new DocumentWriter(getMapper());
document(writer, () -> {
array(writer, "stuff", () -> {
document(writer, () -> {
writer.writeInt32("doc", 42);
});
});
});
Assert.assertEquals(writer.getDocument(), new Document("stuff", of(new Document("doc", 42))));
}
use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class TestDocumentWriter method nesting.
@Test
public void nesting() throws JSONException {
String expected = "{$group : {_id : {$dateToString: {format: \"%Y-%m-%d\", date: \"$date\"}}, totalSaleAmount: {$sum: " + "{$multiply: [ \"$price\", \"$quantity\" ]}}, averageQuantity: {$avg: \"$quantity\"},count: {$sum: 1}}}";
DocumentWriter writer = new DocumentWriter(getMapper());
document(writer, () -> {
document(writer, "$group", () -> {
document(writer, "_id", () -> {
document(writer, "$dateToString", () -> {
writer.writeString("format", "%Y-%m-%d");
writer.writeString("date", "$date");
});
});
document(writer, "totalSaleAmount", () -> {
document(writer, "$sum", () -> {
array(writer, "$multiply", () -> {
writer.writeString("$price");
writer.writeString("$quantity");
});
});
});
document(writer, "averageQuantity", () -> writer.writeString("$avg", "$quantity"));
document(writer, "count", () -> {
writer.writeInt32("$sum", 1);
});
});
});
String s = writer.getDocument().toJson();
JSONAssert.assertEquals(expected, s, false);
}
use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class TestDocumentWriter method basic.
@Test
public void basic() {
for (int i = 0; i < 3; i++) {
DocumentWriter writer = new DocumentWriter(getMapper());
Document expected = new Document();
int finalI = i;
document(writer, () -> {
for (int j = 0; j < finalI; j++) {
writer.writeInt32("entry " + j, j);
expected.put("entry " + j, j);
}
});
Assert.assertEquals(expected, writer.getDocument());
}
}
use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class OperationTarget method encode.
/**
* Encodes this target
*
* @param datastore the datastore
* @return the encoded form
* @morphia.internal
*/
public Object encode(Datastore datastore) {
if (target == null) {
if (value == null) {
throw new NullPointerException();
}
return value;
}
PropertyModel mappedField = this.target.getTarget();
Object mappedValue = value;
PropertyModel model = mappedField != null ? mappedField.getEntityModel().getProperty(mappedField.getName()) : null;
Codec cachedCodec = null;
if (model != null && !(mappedValue instanceof LegacyQuery)) {
cachedCodec = model.specializeCodec(datastore);
}
if (cachedCodec instanceof PropertyHandler) {
mappedValue = ((PropertyHandler) cachedCodec).encode(mappedValue);
} else {
DocumentWriter writer = new DocumentWriter(datastore.getMapper());
Object finalMappedValue = mappedValue;
document(writer, () -> value(datastore, writer, "mapped", finalMappedValue, EncoderContext.builder().build()));
mappedValue = writer.getDocument().get("mapped");
}
return new Document(target.translatedPath(), mappedValue);
}
Aggregations