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());
}
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);
}
}
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));
}
}
}
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;
}
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;
}
Aggregations