Search in sources :

Example 26 with EntityModel

use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.

the class Mapper method getWriteConcern.

/**
 * Gets the write concern for entity or returns the default write concern for this datastore
 *
 * @param clazz the class to use when looking up the WriteConcern
 * @return the write concern for the type
 * @morphia.internal
 */
@Nullable
public WriteConcern getWriteConcern(Class clazz) {
    WriteConcern wc = null;
    EntityModel entityModel = getEntityModel(clazz);
    if (entityModel != null) {
        final Entity entityAnn = entityModel.getEntityAnnotation();
        if (entityAnn != null && !entityAnn.concern().isEmpty()) {
            wc = WriteConcern.valueOf(entityAnn.concern());
        }
    }
    return wc;
}
Also used : ExternalEntity(dev.morphia.annotations.experimental.ExternalEntity) Entity(dev.morphia.annotations.Entity) WriteConcern(com.mongodb.WriteConcern) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Nullable(com.mongodb.lang.Nullable)

Example 27 with EntityModel

use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.

the class MorphiaCodecProvider method get.

@Nullable
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> Codec<T> get(Class<T> type, CodecRegistry registry) {
    MorphiaCodec<T> codec = (MorphiaCodec<T>) codecs.get(type);
    if (codec == null && (mapper.isMapped(type) || mapper.isMappable(type))) {
        EntityModel model = mapper.getEntityModel(type);
        codec = new MorphiaCodec<>(datastore, model, propertyCodecProviders, mapper.getDiscriminatorLookup(), registry);
        if (model.hasLifecycle(PostPersist.class) || model.hasLifecycle(PrePersist.class) || mapper.hasInterceptors()) {
            codec.setEncoder(new LifecycleEncoder(codec));
        }
        if (model.hasLifecycle(PreLoad.class) || model.hasLifecycle(PostLoad.class) || mapper.hasInterceptors()) {
            codec.setDecoder(new LifecycleDecoder(codec));
        }
        codecs.put(type, codec);
    }
    return codec;
}
Also used : LifecycleDecoder(dev.morphia.mapping.codec.pojo.LifecycleDecoder) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) MorphiaCodec(dev.morphia.mapping.codec.pojo.MorphiaCodec) LifecycleEncoder(dev.morphia.mapping.codec.pojo.LifecycleEncoder) Nullable(com.mongodb.lang.Nullable)

Example 28 with EntityModel

use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.

the class AggregationImpl method execute.

@Override
public <R> MorphiaCursor<R> execute(Class<R> resultType) {
    MongoCursor<R> cursor;
    if (datastore.getMapper().isMappable(resultType) && !resultType.equals(this.collection.getDocumentClass())) {
        MongoCollection<Document> collection = this.collection.withDocumentClass(Document.class);
        MongoCursor<Document> results = collection.aggregate(getDocuments()).iterator();
        EntityModel entityModel = datastore.getMapper().getEntityModel(this.collection.getDocumentClass());
        cursor = new MappingCursor<>(results, datastore.getCodecRegistry().get(resultType), entityModel.getDiscriminatorKey());
    } else {
        cursor = collection.aggregate(getDocuments(), resultType).iterator();
    }
    return new MorphiaCursor<>(cursor);
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Document(org.bson.Document) MorphiaCursor(dev.morphia.query.internal.MorphiaCursor)

Example 29 with EntityModel

use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.

the class PathTarget method resolveProperty.

@Nullable
private PropertyModel resolveProperty(String segment) {
    if (context != null) {
        PropertyModel model = context.getProperty(segment);
        if (model == null) {
            Iterator<EntityModel> subTypes = context.getSubtypes().iterator();
            while (model == null && subTypes.hasNext()) {
                context = subTypes.next();
                model = resolveProperty(segment);
            }
        }
        if (model != null) {
            try {
                context = mapper.getEntityModel(model.getNormalizedType());
            } catch (NotMappableException ignored) {
                context = null;
            }
        }
        return model;
    } else {
        return null;
    }
}
Also used : NotMappableException(dev.morphia.mapping.NotMappableException) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Nullable(com.mongodb.lang.Nullable)

Example 30 with EntityModel

use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.

the class AggregationPipelineImpl method toDocument.

/**
 * Converts a Projection to a Document for use by the Java driver.
 *
 * @param projection the project to apply
 * @return the Document
 */
private Document toDocument(Projection projection) {
    String target = projection.getTarget();
    if (firstStage) {
        EntityModel entityModel = mapper.getEntityModel(source);
        PropertyModel property = entityModel != null && target != null ? entityModel.getProperty(target) : null;
        target = property != null ? property.getMappedName() : target;
    }
    List<Projection> list = projection.getProjections();
    if (list != null) {
        Document projections = new Document();
        for (Projection subProjection : list) {
            projections.putAll(toDocument(subProjection));
        }
        return new Document(target, projections);
    } else if (projection.getSource() != null) {
        return new Document(target, projection.getSource());
    } else if (projection.getArguments() != null) {
        List<Object> args = toExpressionArgs(projection.getArguments());
        if (target == null) {
            // Unwrap for single-argument expressions
            if (args.size() == 1) {
                Object firstArg = ((List<?>) args).get(0);
                if (firstArg instanceof Document) {
                    return (Document) firstArg;
                }
            }
            throw new UnsupportedOperationException("aggregation support pending");
        // return args;
        } else {
            // Unwrap for single-argument expressions
            if (args.size() == 1) {
                return new Document(target, ((List<?>) args).get(0));
            }
            return new Document(target, args);
        }
    } else {
        return new Document(target, projection.isSuppressed() ? 0 : 1);
    }
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.bson.Document)

Aggregations

EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)94 Test (org.testng.annotations.Test)56 Document (org.bson.Document)36 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)22 ArrayList (java.util.ArrayList)16 Datastore (dev.morphia.Datastore)14 Entity (dev.morphia.annotations.Entity)14 PathTarget (dev.morphia.internal.PathTarget)12 Mapper (dev.morphia.mapping.Mapper)12 Nullable (com.mongodb.lang.Nullable)10 Index (dev.morphia.annotations.Index)10 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)8 VersionedChildEntity (dev.morphia.test.models.versioned.VersionedChildEntity)8 List (java.util.List)8 ExternalEntity (dev.morphia.annotations.experimental.ExternalEntity)6 WriteConcern (com.mongodb.WriteConcern)4 Key (dev.morphia.Key)4 IndexOptions (dev.morphia.annotations.IndexOptions)4 MappingException (dev.morphia.mapping.MappingException)4 EntityModelBuilder (dev.morphia.mapping.codec.pojo.EntityModelBuilder)4