Search in sources :

Example 6 with PropertyModel

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

the class Mapper method getId.

/**
 * Gets the ID value for an entity
 *
 * @param entity the entity to process
 * @return the ID value
 */
@Nullable
public Object getId(@Nullable Object entity) {
    if (entity == null) {
        return null;
    }
    try {
        final EntityModel model = getEntityModel(entity.getClass());
        final PropertyModel idField = model.getIdProperty();
        if (idField != null) {
            return idField.getValue(entity);
        }
    } catch (NotMappableException ignored) {
    }
    return null;
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Nullable(com.mongodb.lang.Nullable)

Example 7 with PropertyModel

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

the class Mapper method findIdProperty.

/**
 * @param type the class
 * @return the id property model
 * @morphia.internal
 * @since 2.2
 */
public PropertyModel findIdProperty(Class<?> type) {
    EntityModel entityModel = getEntityModel(type);
    PropertyModel idField = entityModel.getIdProperty();
    if (idField == null) {
        throw new MappingException(Sofia.idRequired(type.getName()));
    }
    return idField;
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel)

Example 8 with PropertyModel

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

Example 9 with PropertyModel

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

the class SetEntityOperator method toTarget.

@Override
public OperationTarget toTarget(PathTarget pathTarget) {
    return new OperationTarget(null, value()) {

        @Override
        @SuppressWarnings("unchecked")
        public Object encode(Datastore datastore) {
            Object value = value();
            EntityModel entityModel = datastore.getMapper().getEntityModel(value.getClass());
            PropertyModel versionProperty = entityModel.getVersionProperty();
            if (versionProperty == null) {
                return super.encode(datastore);
            }
            Codec<Object> codec = datastore.getCodecRegistry().get((Class<Object>) value.getClass());
            DocumentWriter writer = new DocumentWriter(datastore.getMapper());
            codec.encode(writer, value, EncoderContext.builder().build());
            Document document = writer.getDocument();
            document.remove(versionProperty.getMappedName());
            return document;
        }
    };
}
Also used : Datastore(dev.morphia.Datastore) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) OperationTarget(dev.morphia.query.OperationTarget) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Document(org.bson.Document)

Example 10 with PropertyModel

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

the class AggregationPipelineImpl method toDocument.

/**
 * Converts a Projection to a Document for use by the Java driver.
 *
 * @param projection the project to apply
 * @return the Document
 */
private Document toDocument(Projection projection) {
    String target = projection.getTarget();
    if (firstStage) {
        EntityModel entityModel = mapper.getEntityModel(source);
        PropertyModel property = entityModel != null && target != null ? entityModel.getProperty(target) : null;
        target = property != null ? property.getMappedName() : target;
    }
    List<Projection> list = projection.getProjections();
    if (list != null) {
        Document projections = new Document();
        for (Projection subProjection : list) {
            projections.putAll(toDocument(subProjection));
        }
        return new Document(target, projections);
    } else if (projection.getSource() != null) {
        return new Document(target, projection.getSource());
    } else if (projection.getArguments() != null) {
        List<Object> args = toExpressionArgs(projection.getArguments());
        if (target == null) {
            // Unwrap for single-argument expressions
            if (args.size() == 1) {
                Object firstArg = ((List<?>) args).get(0);
                if (firstArg instanceof Document) {
                    return (Document) firstArg;
                }
            }
            throw new UnsupportedOperationException("aggregation support pending");
        // return args;
        } else {
            // Unwrap for single-argument expressions
            if (args.size() == 1) {
                return new Document(target, ((List<?>) args).get(0));
            }
            return new Document(target, args);
        }
    } else {
        return new Document(target, projection.isSuppressed() ? 0 : 1);
    }
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.bson.Document)

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