Search in sources :

Example 11 with EntityModel

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

the class PathTargetTest method maps.

@Test
public void maps() {
    getMapper().map(Student.class);
    Mapper mapper = getMapper();
    EntityModel entityModel = mapper.getEntityModel(Student.class);
    PathTarget pathTarget = new PathTarget(mapper, entityModel, "grades.$.data.name");
    Assert.assertEquals(pathTarget.translatedPath(), "grades.$.d.name");
    Assert.assertEquals(mapper.getEntityModel(Grade.class).getProperty("data"), pathTarget.getTarget());
    pathTarget = new PathTarget(mapper, entityModel, "grades.$.d.name");
    Assert.assertEquals(pathTarget.translatedPath(), "grades.$.d.name");
    Assert.assertEquals(mapper.getEntityModel(Grade.class).getProperty("d"), pathTarget.getTarget());
}
Also used : Mapper(dev.morphia.mapping.Mapper) PathTarget(dev.morphia.internal.PathTarget) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Test(org.testng.annotations.Test)

Example 12 with EntityModel

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

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

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

the class Operations method versionUpdate.

protected void versionUpdate() {
    PropertyModel versionField = entityModel.getVersionProperty();
    if (versionField != null) {
        List<OperationTarget> operationTargets = ops.get("$inc");
        String version = versionField.getMappedName();
        boolean already = operationTargets != null && operationTargets.stream().anyMatch(tv -> {
            PathTarget target = tv.getTarget();
            return target != null && target.translatedPath().equals(version);
        });
        if (!already) {
            add("$inc", new OperationTarget(new PathTarget(datastore.getMapper(), entityModel, versionField.getName()), 1L));
        }
    }
}
Also used : Document(org.bson.Document) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) List(java.util.List) PathTarget(dev.morphia.internal.PathTarget) Map(java.util.Map) StringJoiner(java.util.StringJoiner) Entry(java.util.Map.Entry) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) HashMap(java.util.HashMap) Datastore(dev.morphia.Datastore) ArrayList(java.util.ArrayList) PathTarget(dev.morphia.internal.PathTarget) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel)

Example 14 with EntityModel

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

the class Projection method project.

private Document project(Mapper mapper, Class<?> clazz) {
    Document projection = new Document();
    iterate(mapper, projection, clazz, includes, 1);
    iterate(mapper, projection, clazz, excludes, 0);
    final EntityModel model = mapper.getEntityModel(clazz);
    Entity entityAnnotation = model.getEntityAnnotation();
    if (isIncluding() && entityAnnotation != null && entityAnnotation.useDiscriminator()) {
        projection.put(mapper.getOptions().getDiscriminatorKey(), 1);
    }
    return projection;
}
Also used : Entity(dev.morphia.annotations.Entity) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Document(org.bson.Document)

Example 15 with EntityModel

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

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)

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