Search in sources :

Example 1 with PropertyHandler

use of dev.morphia.mapping.codec.pojo.PropertyHandler 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 PropertyHandler

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

the class OperationTarget method encode.

/**
 * Encodes this target
 *
 * @param datastore the datastore
 * @return the encoded form
 * @morphia.internal
 */
public Object encode(Datastore datastore) {
    if (target == null) {
        if (value == null) {
            throw new NullPointerException();
        }
        return value;
    }
    PropertyModel mappedField = this.target.getTarget();
    Object mappedValue = value;
    PropertyModel model = mappedField != null ? mappedField.getEntityModel().getProperty(mappedField.getName()) : null;
    Codec cachedCodec = null;
    if (model != null && !(mappedValue instanceof LegacyQuery)) {
        cachedCodec = model.specializeCodec(datastore);
    }
    if (cachedCodec instanceof PropertyHandler) {
        mappedValue = ((PropertyHandler) cachedCodec).encode(mappedValue);
    } else {
        DocumentWriter writer = new DocumentWriter(datastore.getMapper());
        Object finalMappedValue = mappedValue;
        document(writer, () -> value(datastore, writer, "mapped", finalMappedValue, EncoderContext.builder().build()));
        mappedValue = writer.getDocument().get("mapped");
    }
    return new Document(target.translatedPath(), mappedValue);
}
Also used : Codec(org.bson.codecs.Codec) PropertyHandler(dev.morphia.mapping.codec.pojo.PropertyHandler) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Document(org.bson.Document)

Aggregations

PropertyHandler (dev.morphia.mapping.codec.pojo.PropertyHandler)2 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)2 Nullable (com.mongodb.lang.Nullable)1 PathTarget (dev.morphia.internal.PathTarget)1 DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)1 OperationTarget (dev.morphia.query.OperationTarget)1 Document (org.bson.Document)1 Codec (org.bson.codecs.Codec)1