Search in sources :

Example 31 with MappedField

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;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) MappedClass(org.mongodb.morphia.mapping.MappedClass)

Example 32 with MappedField

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);
    }
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 33 with MappedField

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;
    }
}
Also used : EphemeralMappedField(org.mongodb.morphia.mapping.EphemeralMappedField) MappedField(org.mongodb.morphia.mapping.MappedField) Collection(java.util.Collection) DBObject(com.mongodb.DBObject) List(java.util.List) ArrayList(java.util.ArrayList) EphemeralMappedField(org.mongodb.morphia.mapping.EphemeralMappedField) DBObject(com.mongodb.DBObject)

Example 34 with MappedField

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;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) DBRef(com.mongodb.DBRef) Key(org.mongodb.morphia.Key)

Example 35 with MappedField

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));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Mapper(org.mongodb.morphia.mapping.Mapper) ArrayList(java.util.ArrayList) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Aggregations

MappedField (org.mongodb.morphia.mapping.MappedField)68 MappedClass (org.mongodb.morphia.mapping.MappedClass)56 Test (org.junit.Test)50 ArrayList (java.util.ArrayList)47 Mapper (org.mongodb.morphia.mapping.Mapper)46 BasicDBObject (com.mongodb.BasicDBObject)13 DBObject (com.mongodb.DBObject)8 List (java.util.List)7 Key (org.mongodb.morphia.Key)6 SimpleEntity (org.mongodb.morphia.entities.SimpleEntity)5 ObjectId (org.bson.types.ObjectId)4 ValidationFailure (org.mongodb.morphia.query.validation.ValidationFailure)4 UpdateResults (org.mongodb.morphia.query.UpdateResults)3 DBCollection (com.mongodb.DBCollection)2 Index (org.mongodb.morphia.annotations.Index)2 NotSaved (org.mongodb.morphia.annotations.NotSaved)2 Version (org.mongodb.morphia.annotations.Version)2 MappingException (org.mongodb.morphia.mapping.MappingException)2 DBRef (com.mongodb.DBRef)1 WriteResult (com.mongodb.WriteResult)1