use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.
the class Mapper method getWriteConcern.
/**
* Gets the write concern for entity or returns the default write concern for this datastore
*
* @param clazz the class to use when looking up the WriteConcern
* @return the write concern for the type
* @morphia.internal
*/
@Nullable
public WriteConcern getWriteConcern(Class clazz) {
WriteConcern wc = null;
EntityModel entityModel = getEntityModel(clazz);
if (entityModel != null) {
final Entity entityAnn = entityModel.getEntityAnnotation();
if (entityAnn != null && !entityAnn.concern().isEmpty()) {
wc = WriteConcern.valueOf(entityAnn.concern());
}
}
return wc;
}
use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.
the class MorphiaCodecProvider method get.
@Nullable
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> Codec<T> get(Class<T> type, CodecRegistry registry) {
MorphiaCodec<T> codec = (MorphiaCodec<T>) codecs.get(type);
if (codec == null && (mapper.isMapped(type) || mapper.isMappable(type))) {
EntityModel model = mapper.getEntityModel(type);
codec = new MorphiaCodec<>(datastore, model, propertyCodecProviders, mapper.getDiscriminatorLookup(), registry);
if (model.hasLifecycle(PostPersist.class) || model.hasLifecycle(PrePersist.class) || mapper.hasInterceptors()) {
codec.setEncoder(new LifecycleEncoder(codec));
}
if (model.hasLifecycle(PreLoad.class) || model.hasLifecycle(PostLoad.class) || mapper.hasInterceptors()) {
codec.setDecoder(new LifecycleDecoder(codec));
}
codecs.put(type, codec);
}
return codec;
}
use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.
the class AggregationImpl method execute.
@Override
public <R> MorphiaCursor<R> execute(Class<R> resultType) {
MongoCursor<R> cursor;
if (datastore.getMapper().isMappable(resultType) && !resultType.equals(this.collection.getDocumentClass())) {
MongoCollection<Document> collection = this.collection.withDocumentClass(Document.class);
MongoCursor<Document> results = collection.aggregate(getDocuments()).iterator();
EntityModel entityModel = datastore.getMapper().getEntityModel(this.collection.getDocumentClass());
cursor = new MappingCursor<>(results, datastore.getCodecRegistry().get(resultType), entityModel.getDiscriminatorKey());
} else {
cursor = collection.aggregate(getDocuments(), resultType).iterator();
}
return new MorphiaCursor<>(cursor);
}
use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.
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;
}
}
use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.
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