Search in sources :

Example 1 with DocumentReader

use of dev.morphia.mapping.codec.reader.DocumentReader in project morphia by mongodb.

the class MorphiaKeyCursor method fromDocument.

private <I> I fromDocument(Class<I> type, Document document) {
    Class<I> aClass = type;
    Mapper mapper = datastore.getMapper();
    if (document.containsKey(mapper.getOptions().getDiscriminatorKey())) {
        aClass = mapper.getClass(document);
    }
    DocumentReader reader = new DocumentReader(document);
    return datastore.getCodecRegistry().get(aClass).decode(reader, DecoderContext.builder().build());
}
Also used : Mapper(dev.morphia.mapping.Mapper) DocumentReader(dev.morphia.mapping.codec.reader.DocumentReader)

Example 2 with DocumentReader

use of dev.morphia.mapping.codec.reader.DocumentReader in project morphia by mongodb.

the class DocumentReaderTest method nestedDatabaseRead.

@Test
public void nestedDatabaseRead() {
    getDs().getMapper().map(Parent.class, Child.class);
    Parent parent = new Parent();
    parent.child = new Child();
    getDs().save(parent);
    MongoCollection<Document> collection = getDocumentCollection(Parent.class);
    Document first = collection.find().first();
    Parent decode = getDs().getCodecRegistry().get(Parent.class).decode(new DocumentReader(first), DecoderContext.builder().build());
    assertEquals(parent, decode);
}
Also used : DocumentReader(dev.morphia.mapping.codec.reader.DocumentReader) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 3 with DocumentReader

use of dev.morphia.mapping.codec.reader.DocumentReader in project morphia by mongodb.

the class ReferenceCodec method processId.

/**
 * Decodes an ID value
 *
 * @param datastore      the Datastore to use
 * @param decode         the value to decode
 * @param decoderContext the decoder context
 * @return the decoded value
 */
@NonNull
public static Object processId(Datastore datastore, Object decode, DecoderContext decoderContext) {
    Object id = decode;
    if (id instanceof Iterable) {
        Iterable<?> iterable = (Iterable<?>) id;
        List<Object> ids = new ArrayList<>();
        for (Object o : iterable) {
            ids.add(processId(datastore, o, decoderContext));
        }
        id = ids;
    } else if (id instanceof Document) {
        Document document = (Document) id;
        if (document.containsKey("$ref")) {
            id = processId(datastore, new DBRef(document.getString("$db"), document.getString("$ref"), document.get("$id")), decoderContext);
        } else if (document.containsKey(datastore.getMapper().getOptions().getDiscriminatorKey())) {
            try {
                id = datastore.getCodecRegistry().get(datastore.getMapper().getClass(document)).decode(new DocumentReader(document), decoderContext);
            } catch (CodecConfigurationException e) {
                throw new MappingException(Sofia.cannotFindTypeInDocument(), e);
            }
        }
    } else if (id instanceof DBRef) {
        DBRef ref = (DBRef) id;
        Object refId = ref.getId();
        if (refId instanceof Document) {
            refId = datastore.getCodecRegistry().get(Object.class).decode(new DocumentReader((Document) refId), decoderContext);
        }
        id = new DBRef(ref.getDatabaseName(), ref.getCollectionName(), refId);
    }
    return id;
}
Also used : CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) ArrayList(java.util.ArrayList) DBRef(com.mongodb.DBRef) DocumentReader(dev.morphia.mapping.codec.reader.DocumentReader) Document(org.bson.Document) MappingException(dev.morphia.mapping.MappingException) NonNull(com.mongodb.lang.NonNull)

Example 4 with DocumentReader

use of dev.morphia.mapping.codec.reader.DocumentReader in project morphia by mongodb.

the class LifecycleDecoder method decode.

@Override
@SuppressWarnings("unchecked")
public T decode(BsonReader reader, DecoderContext decoderContext) {
    Document document = getMorphiaCodec().getRegistry().get(Document.class).decode(reader, decoderContext);
    EntityModel model = getMorphiaCodec().getEntityModel();
    if (model.useDiscriminator()) {
        String discriminator = document.getString(model.getDiscriminatorKey());
        if (discriminator != null) {
            Class<?> discriminatorClass = getMorphiaCodec().getDiscriminatorLookup().lookup(discriminator);
            // need to load the codec to initialize cachedCodecs in field models
            Codec<?> codec = getMorphiaCodec().getRegistry().get(discriminatorClass);
            if (codec instanceof MorphiaCodec) {
                model = ((MorphiaCodec<?>) codec).getEntityModel();
            } else {
                throw new CodecConfigurationException(format("Non-entity class used as discriminator: '%s'.", discriminator));
            }
        }
    }
    final MorphiaInstanceCreator instanceCreator = model.getInstanceCreator();
    T entity = (T) instanceCreator.getInstance();
    model.callLifecycleMethods(PreLoad.class, entity, document, getMorphiaCodec().getDatastore());
    decodeProperties(new DocumentReader(document), decoderContext, instanceCreator, model);
    model.callLifecycleMethods(PostLoad.class, entity, document, getMorphiaCodec().getDatastore());
    return entity;
}
Also used : CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) MorphiaInstanceCreator(dev.morphia.mapping.codec.MorphiaInstanceCreator) DocumentReader(dev.morphia.mapping.codec.reader.DocumentReader) Document(org.bson.Document)

Example 5 with DocumentReader

use of dev.morphia.mapping.codec.reader.DocumentReader in project morphia by mongodb.

the class TestBase method fromDocument.

protected <T> T fromDocument(Class<T> type, Document document) {
    Class<T> aClass = type;
    Mapper mapper = getMapper();
    if (document.containsKey(mapper.getOptions().getDiscriminatorKey())) {
        aClass = mapper.getClass(document);
    }
    DocumentReader reader = new DocumentReader(document);
    return getDs().getCodecRegistry().get(aClass).decode(reader, DecoderContext.builder().build());
}
Also used : Mapper(dev.morphia.mapping.Mapper) DocumentReader(dev.morphia.mapping.codec.reader.DocumentReader)

Aggregations

DocumentReader (dev.morphia.mapping.codec.reader.DocumentReader)5 Document (org.bson.Document)3 Mapper (dev.morphia.mapping.Mapper)2 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)2 DBRef (com.mongodb.DBRef)1 NonNull (com.mongodb.lang.NonNull)1 MappingException (dev.morphia.mapping.MappingException)1 MorphiaInstanceCreator (dev.morphia.mapping.codec.MorphiaInstanceCreator)1 ArrayList (java.util.ArrayList)1 Test (org.testng.annotations.Test)1