use of dev.morphia.mapping.codec.pojo.EntityModel 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.EntityModel in project morphia by MorphiaOrg.
the class MorphiaReferenceCodec method decode.
@Override
public MorphiaReference decode(BsonReader reader, DecoderContext decoderContext) {
Object value = getDatastore().getCodecRegistry().get(bsonTypeClassMap.get(reader.getCurrentBsonType())).decode(reader, decoderContext);
value = processId(getDatastore(), value, decoderContext);
TypeData typeData = getTypeData().getTypeParameters().get(0);
EntityModel fieldEntityModel = getEntityModelForField();
if (Set.class.isAssignableFrom(typeData.getType())) {
return new SetReference<>(getDatastore(), mapper, fieldEntityModel, (List) value);
} else if (Collection.class.isAssignableFrom(typeData.getType())) {
return new ListReference<>(getDatastore(), mapper, fieldEntityModel, (List) value);
} else if (Map.class.isAssignableFrom(typeData.getType())) {
return new MapReference<>(getDatastore(), mapper, (Map) value, fieldEntityModel);
} else {
return new SingleReference<>(getDatastore(), mapper, fieldEntityModel, value);
}
}
use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.
the class LegacyQuery method toDocument.
/**
* Converts the query to a Document and updates for any discriminator values as my be necessary
*
* @return the query
* @morphia.internal
*/
@Override
public Document toDocument() {
final Document query = getQueryDocument();
EntityModel model = datastore.getMapper().getEntityModel(getEntityClass());
Entity entityAnnotation = model.getEntityAnnotation();
if (entityAnnotation != null && entityAnnotation.useDiscriminator() && !query.containsKey("_id") && !query.containsKey(model.getDiscriminatorKey())) {
List<EntityModel> subtypes = datastore.getMapper().getEntityModel(getEntityClass()).getSubtypes();
List<String> values = new ArrayList<>();
values.add(model.getDiscriminator());
for (EntityModel subtype : subtypes) {
values.add(subtype.getDiscriminator());
}
query.put(model.getDiscriminatorKey(), new Document("$in", values));
}
return query;
}
use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.
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;
}
use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.
the class Mapper method register.
/**
* @param entityModel the model to register
* @return the model
* @morphia.internal
* @since 2.3
*/
public EntityModel register(EntityModel entityModel) {
discriminatorLookup.addModel(entityModel);
mappedEntities.put(entityModel.getType(), entityModel);
if (entityModel.getCollectionName() != null) {
mappedEntitiesByCollection.computeIfAbsent(entityModel.getCollectionName(), s -> new CopyOnWriteArraySet<>()).add(entityModel);
}
if (!entityModel.isInterface()) {
new MappingValidator().validate(this, entityModel);
}
return entityModel;
}
Aggregations