Search in sources :

Example 6 with DocumentWriter

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);
}
Also used : Datastore(dev.morphia.Datastore) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Document(org.bson.Document)

Example 7 with DocumentWriter

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))));
}
Also used : DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 8 with DocumentWriter

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);
}
Also used : DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Test(org.testng.annotations.Test)

Example 9 with DocumentWriter

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());
    }
}
Also used : DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 10 with DocumentWriter

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);
}
Also used : Codec(org.bson.codecs.Codec) PropertyHandler(dev.morphia.mapping.codec.pojo.PropertyHandler) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Document(org.bson.Document)

Aggregations

DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)23 Document (org.bson.Document)18 Test (org.testng.annotations.Test)9 Codec (org.bson.codecs.Codec)5 Datastore (dev.morphia.Datastore)3 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)2 OperationTarget (dev.morphia.query.OperationTarget)2 Filter (dev.morphia.query.experimental.filters.Filter)2 Sample (dev.morphia.aggregation.experimental.stages.Sample)1 Skip (dev.morphia.aggregation.experimental.stages.Skip)1 Stage (dev.morphia.aggregation.experimental.stages.Stage)1 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)1 PropertyHandler (dev.morphia.mapping.codec.pojo.PropertyHandler)1 ReferenceException (dev.morphia.mapping.lazy.proxy.ReferenceException)1 NearFilter (dev.morphia.query.experimental.filters.NearFilter)1 User (dev.morphia.test.models.User)1 ArrayList (java.util.ArrayList)1 EncoderContext (org.bson.codecs.EncoderContext)1 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)1