Search in sources :

Example 1 with PropertyModel

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"));
}
Also used : Mapper(dev.morphia.mapping.Mapper) State(dev.morphia.test.models.State) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) CityPopulation(dev.morphia.test.models.CityPopulation) Test(org.testng.annotations.Test)

Example 2 with PropertyModel

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;
}
Also used : PathTarget(dev.morphia.internal.PathTarget) PropertyHandler(dev.morphia.mapping.codec.pojo.PropertyHandler) OperationTarget(dev.morphia.query.OperationTarget) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Nullable(com.mongodb.lang.Nullable)

Example 3 with PropertyModel

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");
}
Also used : PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Test(org.testng.annotations.Test)

Example 4 with PropertyModel

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));
        }
    }
}
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 5 with PropertyModel

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

Aggregations

PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)22 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)10 Test (org.testng.annotations.Test)10 Document (org.bson.Document)4 Nullable (com.mongodb.lang.Nullable)3 Datastore (dev.morphia.Datastore)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 PathTarget (dev.morphia.internal.PathTarget)2 MappingException (dev.morphia.mapping.MappingException)2 PropertyHandler (dev.morphia.mapping.codec.pojo.PropertyHandler)2 DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)2 OperationTarget (dev.morphia.query.OperationTarget)2 MethodMappedSpecializedEntity (dev.morphia.test.models.methods.MethodMappedSpecializedEntity)2 Key (dev.morphia.Key)1 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)1 Mapper (dev.morphia.mapping.Mapper)1 NotMappableException (dev.morphia.mapping.NotMappableException)1 TestBase (dev.morphia.test.TestBase)1 CityPopulation (dev.morphia.test.models.CityPopulation)1