Search in sources :

Example 11 with DocumentWriter

use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.

the class MorphiaQuery method getQueryDocument.

Document getQueryDocument() {
    DocumentWriter writer = new DocumentWriter(mapper, seedQuery);
    document(writer, () -> {
        EncoderContext context = EncoderContext.builder().build();
        for (Filter filter : filters) {
            filter.encode(datastore, writer, context);
        }
    });
    Document query = writer.getDocument();
    if (mapper.isMappable(getEntityClass())) {
        mapper.updateQueryWithDiscriminators(mapper.getEntityModel(getEntityClass()), query);
    }
    return query;
}
Also used : EncoderContext(org.bson.codecs.EncoderContext) Filter(dev.morphia.query.experimental.filters.Filter) NearFilter(dev.morphia.query.experimental.filters.NearFilter) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Document(org.bson.Document)

Example 12 with DocumentWriter

use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.

the class SetEntityOperator method toTarget.

@Override
public OperationTarget toTarget(PathTarget pathTarget) {
    return new OperationTarget(null, value()) {

        @Override
        @SuppressWarnings("unchecked")
        public Object encode(Datastore datastore) {
            Object value = value();
            EntityModel entityModel = datastore.getMapper().getEntityModel(value.getClass());
            PropertyModel versionProperty = entityModel.getVersionProperty();
            if (versionProperty == null) {
                return super.encode(datastore);
            }
            Codec<Object> codec = datastore.getCodecRegistry().get((Class<Object>) value.getClass());
            DocumentWriter writer = new DocumentWriter(datastore.getMapper());
            codec.encode(writer, value, EncoderContext.builder().build());
            Document document = writer.getDocument();
            document.remove(versionProperty.getMappedName());
            return document;
        }
    };
}
Also used : Datastore(dev.morphia.Datastore) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) OperationTarget(dev.morphia.query.OperationTarget) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Document(org.bson.Document)

Example 13 with DocumentWriter

use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.

the class ExpressionHelper method document.

public static Document document(Mapper mapper, Document seed, Consumer<BsonWriter> body) {
    DocumentWriter writer = new DocumentWriter(mapper, seed);
    writer.writeStartDocument();
    body.accept(writer);
    writer.writeEndDocument();
    return writer.getDocument();
}
Also used : DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter)

Example 14 with DocumentWriter

use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.

the class TestDocumentWriter method arrays.

@Test
public void arrays() {
    DocumentWriter writer = new DocumentWriter(getMapper());
    document(writer, () -> {
        array(writer, "stuff", () -> {
            writer.writeString("hello");
            writer.writeInt32(42);
        });
        writer.writeName("next");
        writer.writeString("something simple");
    });
    Assert.assertEquals(writer.getDocument(), new Document("stuff", asList("hello", 42)).append("next", "something simple"));
}
Also used : DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 15 with DocumentWriter

use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.

the class TestDocumentWriter method nestedArrays.

@Test
public void nestedArrays() {
    DocumentWriter writer = new DocumentWriter(getMapper());
    document(writer, () -> {
        array(writer, "top", () -> {
            array(writer, () -> {
                writer.writeInt32(1);
                writer.writeInt32(2);
                writer.writeInt32(3);
                document(writer, () -> {
                    writer.writeString("nested", "string");
                });
            });
        });
    });
    Document top = new Document("top", of(of(1, 2, 3, new Document("nested", "string"))));
    Assert.assertEquals(top, writer.getDocument());
}
Also used : DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Document(org.bson.Document) Test(org.testng.annotations.Test)

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