use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.
the class PathTarget method resolveField.
private MappedField resolveField(final String segment) {
MappedField mf = context.getMappedField(segment);
if (mf == null) {
mf = context.getMappedFieldByJavaField(segment);
}
if (mf == null) {
Iterator<MappedClass> subTypes = mapper.getSubTypes(context).iterator();
while (mf == null && subTypes.hasNext()) {
context = subTypes.next();
mf = resolveField(segment);
}
}
if (mf != null) {
context = mapper.getMappedClass(mf.getSubClass() != null ? mf.getSubClass() : mf.getConcreteType());
}
return mf;
}
use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.
the class AggregationPipelineImpl method toDBObject.
/**
* Converts a Projection to a DBObject for use by the Java driver.
*
* @param projection the project to apply
* @return the DBObject
*/
@SuppressWarnings("unchecked")
private DBObject toDBObject(final Projection projection) {
String target;
if (firstStage) {
MappedField field = mapper.getMappedClass(source).getMappedField(projection.getTarget());
target = field != null ? field.getNameToStore() : projection.getTarget();
} else {
target = projection.getTarget();
}
if (projection.getProjections() != null) {
List<Projection> list = projection.getProjections();
DBObject projections = new BasicDBObject();
for (Projection subProjection : list) {
projections.putAll(toDBObject(subProjection));
}
return new BasicDBObject(target, projections);
} else if (projection.getSource() != null) {
return new BasicDBObject(target, projection.getSource());
} else if (projection.getArguments() != null) {
if (target == null) {
return toExpressionArgs(projection.getArguments());
} else {
return new BasicDBObject(target, toExpressionArgs(projection.getArguments()));
}
} else {
return new BasicDBObject(target, projection.isSuppressed() ? 0 : 1);
}
}
use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.
the class IterableConverter method decode.
@Override
@SuppressWarnings("unchecked")
public Object decode(final Class targetClass, final Object fromDBObject, final MappedField mf) {
if (mf == null || fromDBObject == null) {
return fromDBObject;
}
final Class subtypeDest = mf.getSubClass();
final Collection values = createNewCollection(mf);
final Converters converters = getMapper().getConverters();
if (fromDBObject.getClass().isArray()) {
//This should never happen. The driver always returns list/arrays as a List
for (final Object o : (Object[]) fromDBObject) {
values.add(converters.decode((subtypeDest != null) ? subtypeDest : o.getClass(), o, mf));
}
} else if (fromDBObject instanceof Iterable) {
// (List/Set/Array[])
for (final Object o : (Iterable) fromDBObject) {
if (o instanceof DBObject) {
final List<MappedField> typeParameters = mf.getTypeParameters();
if (!typeParameters.isEmpty()) {
final MappedField mappedField = typeParameters.get(0);
if (mappedField instanceof EphemeralMappedField) {
values.add(converters.decode((subtypeDest != null) ? subtypeDest : o.getClass(), o, mappedField));
} else {
throw new UnsupportedOperationException("mappedField isn't an EphemeralMappedField");
}
} else {
values.add(converters.decode((subtypeDest != null) ? subtypeDest : o.getClass(), o, mf));
}
} else {
values.add(converters.decode((subtypeDest != null) ? subtypeDest : o.getClass(), o, mf));
}
}
} else {
//Single value case.
values.add(converters.decode((subtypeDest != null) ? subtypeDest : fromDBObject.getClass(), fromDBObject, mf));
}
//convert to and array if that is the destination type (not a list/set)
if (mf.getType().isArray()) {
return ReflectionUtils.convertToArray(subtypeDest, (List) values);
} else {
return values;
}
}
use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.
the class KeyConverter method decode.
@Override
public Object decode(final Class targetClass, final Object o, final MappedField optionalExtraInfo) {
if (o == null) {
return null;
}
if (!(o instanceof DBRef)) {
throw new ConverterException(String.format("cannot convert %s to Key because it isn't a DBRef", o.toString()));
}
DBRef ref = (DBRef) o;
MappedField actualType = getActualType(optionalExtraInfo);
final Class<?> keyType = actualType != null ? actualType.getConcreteType() : getMapper().getClassFromCollection(ref.getCollectionName());
final Key<?> key = new Key<Object>(keyType, ref.getCollectionName(), ref.getId());
return key;
}
use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.
the class QueryValidatorTest method shouldNotAllowOtherValuesForInOperator.
@Test
public void shouldNotAllowOtherValuesForInOperator() {
// expect
MappedClass mappedClass = new MappedClass(SimpleEntity.class, new Mapper());
MappedField mappedField = mappedClass.getMappedField("name");
assertThat(QueryValidator.isCompatibleForOperator(mappedClass, mappedField, String.class, IN, "value", new ArrayList<ValidationFailure>()), is(false));
}
Aggregations