Search in sources :

Example 1 with PathTarget

use of dev.morphia.internal.PathTarget 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 2 with PathTarget

use of dev.morphia.internal.PathTarget 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 3 with PathTarget

use of dev.morphia.internal.PathTarget 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 4 with PathTarget

use of dev.morphia.internal.PathTarget in project morphia by mongodb.

the class Projection method knownFields.

private Document knownFields(Mapper mapper, Class<?> clazz) {
    Document projection = new Document();
    mapper.getEntityModel(clazz).getProperties().stream().map(mf -> new PathTarget(mapper, mapper.getEntityModel(clazz), mf.getMappedName()).translatedPath()).forEach(name -> projection.put(name, 1));
    return projection;
}
Also used : Document(org.bson.Document) List(java.util.List) Sofia(dev.morphia.sofia.Sofia) PathTarget(dev.morphia.internal.PathTarget) Mapper(dev.morphia.mapping.Mapper) StringJoiner(java.util.StringJoiner) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Nullable(com.mongodb.lang.Nullable) Entity(dev.morphia.annotations.Entity) ArrayList(java.util.ArrayList) PathTarget(dev.morphia.internal.PathTarget) Document(org.bson.Document)

Example 5 with PathTarget

use of dev.morphia.internal.PathTarget in project morphia by mongodb.

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)

Aggregations

PathTarget (dev.morphia.internal.PathTarget)13 Mapper (dev.morphia.mapping.Mapper)8 Test (org.testng.annotations.Test)8 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)7 Document (org.bson.Document)3 Nullable (com.mongodb.lang.Nullable)2 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 StringJoiner (java.util.StringJoiner)2 DBObject (com.mongodb.DBObject)1 Datastore (dev.morphia.Datastore)1 Entity (dev.morphia.annotations.Entity)1 PropertyHandler (dev.morphia.mapping.codec.pojo.PropertyHandler)1 OperationTarget (dev.morphia.query.OperationTarget)1 UpdateOperator (dev.morphia.query.experimental.updates.UpdateOperator)1 Sofia (dev.morphia.sofia.Sofia)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1