Search in sources :

Example 21 with EntityModel

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

the class Mapper method findIdProperty.

/**
 * @param type the class
 * @return the id property model
 * @morphia.internal
 * @since 2.2
 */
public PropertyModel findIdProperty(Class<?> type) {
    EntityModel entityModel = getEntityModel(type);
    PropertyModel idField = entityModel.getIdProperty();
    if (idField == null) {
        throw new MappingException(Sofia.idRequired(type.getName()));
    }
    return idField;
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel)

Example 22 with EntityModel

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

the class MorphiaReferenceCodec method decode.

@Override
public MorphiaReference decode(BsonReader reader, DecoderContext decoderContext) {
    Object value = getDatastore().getCodecRegistry().get(bsonTypeClassMap.get(reader.getCurrentBsonType())).decode(reader, decoderContext);
    value = processId(getDatastore(), value, decoderContext);
    TypeData typeData = getTypeData().getTypeParameters().get(0);
    EntityModel fieldEntityModel = getEntityModelForField();
    if (Set.class.isAssignableFrom(typeData.getType())) {
        return new SetReference<>(getDatastore(), mapper, fieldEntityModel, (List) value);
    } else if (Collection.class.isAssignableFrom(typeData.getType())) {
        return new ListReference<>(getDatastore(), mapper, fieldEntityModel, (List) value);
    } else if (Map.class.isAssignableFrom(typeData.getType())) {
        return new MapReference<>(getDatastore(), mapper, (Map) value, fieldEntityModel);
    } else {
        return new SingleReference<>(getDatastore(), mapper, fieldEntityModel, value);
    }
}
Also used : TypeData(dev.morphia.mapping.codec.pojo.TypeData) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Collection(java.util.Collection) List(java.util.List)

Example 23 with EntityModel

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

the class LegacyQuery method toDocument.

/**
 * Converts the query to a Document and updates for any discriminator values as my be necessary
 *
 * @return the query
 * @morphia.internal
 */
@Override
public Document toDocument() {
    final Document query = getQueryDocument();
    EntityModel model = datastore.getMapper().getEntityModel(getEntityClass());
    Entity entityAnnotation = model.getEntityAnnotation();
    if (entityAnnotation != null && entityAnnotation.useDiscriminator() && !query.containsKey("_id") && !query.containsKey(model.getDiscriminatorKey())) {
        List<EntityModel> subtypes = datastore.getMapper().getEntityModel(getEntityClass()).getSubtypes();
        List<String> values = new ArrayList<>();
        values.add(model.getDiscriminator());
        for (EntityModel subtype : subtypes) {
            values.add(subtype.getDiscriminator());
        }
        query.put(model.getDiscriminatorKey(), new Document("$in", values));
    }
    return query;
}
Also used : Entity(dev.morphia.annotations.Entity) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) ArrayList(java.util.ArrayList) Document(org.bson.Document)

Example 24 with EntityModel

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

the class FindOptions method apply.

/**
 * @param iterable the iterable to use
 * @param mapper   the mapper to use
 * @param type     the result type
 * @param <T>      the result type
 * @return the iterable instance for the query results
 * @morphia.internal
 */
public <T> FindIterable<T> apply(FindIterable<T> iterable, Mapper mapper, Class<?> type) {
    if (isLogQuery()) {
        // reset to a new ID
        logQuery();
    }
    if (projection != null) {
        iterable.projection(projection.map(mapper, type));
    }
    tryInvoke(v4_1_0, () -> {
        return iterable.allowDiskUse(allowDiskUse);
    });
    iterable.batchSize(batchSize);
    iterable.collation(collation);
    iterable.comment(comment);
    if (cursorType != null) {
        iterable.cursorType(cursorType);
    }
    iterable.hint(hint);
    iterable.hintString(hintString);
    iterable.limit(limit);
    iterable.max(max);
    iterable.maxAwaitTime(maxAwaitTimeMS, TimeUnit.MILLISECONDS);
    iterable.maxTime(maxTimeMS, TimeUnit.MILLISECONDS);
    iterable.min(min);
    iterable.noCursorTimeout(noCursorTimeout);
    iterable.oplogReplay(oplogReplay);
    iterable.partial(partial);
    iterable.returnKey(returnKey);
    iterable.showRecordId(showRecordId);
    iterable.skip(skip);
    if (sort != null) {
        Document mapped = new Document();
        EntityModel model = mapper.getEntityModel(type);
        for (Entry<String, Object> entry : sort.entrySet()) {
            Object value = entry.getValue();
            boolean metaScore = value instanceof Document && ((Document) value).get("$meta") != null;
            mapped.put(new PathTarget(mapper, model, entry.getKey(), !metaScore).translatedPath(), value);
        }
        iterable.sort(mapped);
    }
    return iterable;
}
Also used : PathTarget(dev.morphia.internal.PathTarget) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) DBObject(com.mongodb.DBObject) Document(org.bson.Document)

Example 25 with EntityModel

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

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)

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