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