use of dev.morphia.mapping.codec.pojo.PropertyModel in project morphia by mongodb.
the class TestMapping method testBasicMapping.
@Test
public void testBasicMapping() {
Mapper mapper = getDs().getMapper();
mapper.map(List.of(State.class, CityPopulation.class));
final State state = new State();
state.state = "NY";
state.biggest = new CityPopulation("NYC", 8336817L);
state.smallest = new CityPopulation("Red House", 38L);
getDs().save(state);
Query<State> query = getDs().find(State.class).filter(eq("_id", state.id));
State loaded = query.first();
assertEquals(loaded, state);
assertEquals(mapper.getEntityModel(State.class).getProperties().stream().map(PropertyModel::getMappedName).collect(toList()), List.of("_id", "state", "biggestCity", "smallestCity"));
}
use of dev.morphia.mapping.codec.pojo.PropertyModel in project morphia by mongodb.
the class Filter method getValue.
@Nullable
protected Object getValue(Datastore datastore) {
if (!mapped) {
PathTarget target = pathTarget(datastore.getMapper());
OperationTarget operationTarget = new OperationTarget(pathTarget, value);
this.value = operationTarget.getValue();
PropertyModel property = target.getTarget();
if (property != null && property.specializeCodec(datastore) instanceof PropertyHandler) {
this.value = ((Document) operationTarget.encode(datastore)).get(field);
}
mapped = true;
}
return value;
}
use of dev.morphia.mapping.codec.pojo.PropertyModel in project morphia by mongodb.
the class TestPropertyModel method arrayFieldMapping.
@Test
public void arrayFieldMapping() {
final PropertyModel property = getMappedField("arrayOfInt");
Assert.assertFalse(property.isScalarValue());
Assert.assertTrue(property.isMultipleValues());
Assert.assertTrue(property.isArray());
Assert.assertTrue(property.getType().isArray());
Assert.assertEquals(property.getName(), "arrayOfInt");
Assert.assertEquals(property.getMappedName(), "arrayOfInt");
}
use of dev.morphia.mapping.codec.pojo.PropertyModel 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.PropertyModel 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;
}
}
Aggregations