Search in sources :

Example 16 with EntityModel

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

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 17 with EntityModel

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

the class IndexHelper method collectTopLevelIndexes.

private List<Index> collectTopLevelIndexes(EntityModel entityModel) {
    List<Index> list = new ArrayList<>();
    final Indexes indexes = entityModel.getAnnotation(Indexes.class);
    if (indexes != null) {
        for (Index index : indexes.value()) {
            List<Field> fields = new ArrayList<>();
            for (Field field : index.fields()) {
                fields.add(fieldBuilder().value(findField(entityModel, index.options(), field.value())).type(field.type()).weight(field.weight()).build());
            }
            list.add(replaceFields(index, fields));
        }
    }
    EntityModel superClass = entityModel.getSuperClass();
    if (superClass != null) {
        list.addAll(collectTopLevelIndexes(superClass));
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel)

Example 18 with EntityModel

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

the class Mapper method register.

/**
 * @param entityModel the model to register
 * @return the model
 * @morphia.internal
 * @since 2.3
 */
public EntityModel register(EntityModel entityModel) {
    discriminatorLookup.addModel(entityModel);
    mappedEntities.put(entityModel.getType(), entityModel);
    if (entityModel.getCollectionName() != null) {
        mappedEntitiesByCollection.computeIfAbsent(entityModel.getCollectionName(), s -> new CopyOnWriteArraySet<>()).add(entityModel);
    }
    if (!entityModel.isInterface()) {
        new MappingValidator().validate(this, entityModel);
    }
    return entityModel;
}
Also used : Document(org.bson.Document) ClassGraph(io.github.classgraph.ClassGraph) MorphiaProxy(dev.morphia.mapping.codec.references.MorphiaProxy) Arrays(java.util.Arrays) MongoCollection(com.mongodb.client.MongoCollection) Key(dev.morphia.Key) ExternalEntity(dev.morphia.annotations.experimental.ExternalEntity) Embedded(dev.morphia.annotations.Embedded) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Sofia(dev.morphia.sofia.Sofia) Map(java.util.Map) Entity(dev.morphia.annotations.Entity) LinkedList(java.util.LinkedList) ScanResult(io.github.classgraph.ScanResult) EntityModelBuilder(dev.morphia.mapping.codec.pojo.EntityModelBuilder) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) MappingValidator(dev.morphia.mapping.validation.MappingValidator) ClassInfo(io.github.classgraph.ClassInfo) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) MorphiaInternal(dev.morphia.annotations.internal.MorphiaInternal) Annotation(java.lang.annotation.Annotation) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Nullable(com.mongodb.lang.Nullable) EntityInterceptor(dev.morphia.EntityInterceptor) WriteConcern(com.mongodb.WriteConcern) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) MappingValidator(dev.morphia.mapping.validation.MappingValidator)

Example 19 with EntityModel

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

the class Mapper method getEntityModel.

/**
 * Gets the {@link EntityModel} for the object (type). If it isn't mapped, create a new class and cache it (without validating).
 *
 * @param type the type to process
 * @return the EntityModel for the object given
 */
public EntityModel getEntityModel(Class type) {
    final Class actual = MorphiaProxy.class.isAssignableFrom(type) ? type.getSuperclass() : type;
    EntityModel model = mappedEntities.get(actual);
    if (model == null) {
        if (!isMappable(actual)) {
            throw new NotMappableException(type);
        }
        model = register(createEntityModel(type));
    }
    return model;
}
Also used : MorphiaProxy(dev.morphia.mapping.codec.references.MorphiaProxy) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel)

Example 20 with EntityModel

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

the class Mapper method getId.

/**
 * Gets the ID value for an entity
 *
 * @param entity the entity to process
 * @return the ID value
 */
@Nullable
public Object getId(@Nullable Object entity) {
    if (entity == null) {
        return null;
    }
    try {
        final EntityModel model = getEntityModel(entity.getClass());
        final PropertyModel idField = model.getIdProperty();
        if (idField != null) {
            return idField.getValue(entity);
        }
    } catch (NotMappableException ignored) {
    }
    return null;
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Nullable(com.mongodb.lang.Nullable)

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