Search in sources :

Example 16 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class EntityTypeAndIdValueValidator method apply.

/**
     * Checks the class of the {@code value} against the type of the ID for the {@code type}.  Always applies this validation, but there's
     * room to change this to not apply it if, for example, the type is not an entity.
     *
     * @param mappedClass        the MappedClass
     * @param mappedField        the MappedField
     * @param value              the value for the query
     * @param validationFailures the list to add any failures to. If validation passes or {@code appliesTo} returned false, this list will
     *                           not change.    @return true if the validation was applied.
     */
public boolean apply(final MappedClass mappedClass, final MappedField mappedField, final Object value, final List<ValidationFailure> validationFailures) {
    if (appliesTo(mappedClass, mappedField)) {
        Class classOfValue = value.getClass();
        Class classOfIdFieldForType = mappedClass.getMappedIdField().getConcreteType();
        if (!mappedField.getType().equals(classOfValue) && !classOfValue.equals(classOfIdFieldForType)) {
            validationFailures.add(new ValidationFailure(format("The value class needs to match the type of ID for the field. " + "Value was %s and was a %s and the ID of the type was %s", value, classOfValue, classOfIdFieldForType)));
        }
        return true;
    }
    return false;
}
Also used : MappedClass(org.mongodb.morphia.mapping.MappedClass)

Example 17 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class MappingValidator method validate.

/**
     * Validates a List of MappedClasses
     *
     * @param classes the MappedClasses to validate
     * @param mapper the Mapper to use for validation
     */
public void validate(final Mapper mapper, final List<MappedClass> classes) {
    final Set<ConstraintViolation> ve = new TreeSet<ConstraintViolation>(new Comparator<ConstraintViolation>() {

        @Override
        public int compare(final ConstraintViolation o1, final ConstraintViolation o2) {
            return o1.getLevel().ordinal() > o2.getLevel().ordinal() ? -1 : 1;
        }
    });
    final List<ClassConstraint> rules = getConstraints();
    for (final MappedClass c : classes) {
        for (final ClassConstraint v : rules) {
            v.check(mapper, c, ve);
        }
    }
    if (!ve.isEmpty()) {
        final ConstraintViolation worst = ve.iterator().next();
        final Level maxLevel = worst.getLevel();
        if (maxLevel.ordinal() >= Level.FATAL.ordinal()) {
            throw new ConstraintViolationException(ve);
        }
        // sort by class to make it more readable
        final List<LogLine> l = new ArrayList<LogLine>();
        for (final ConstraintViolation v : ve) {
            l.add(new LogLine(v));
        }
        sort(l);
        for (final LogLine line : l) {
            line.log(LOG);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) MappedClass(org.mongodb.morphia.mapping.MappedClass) TreeSet(java.util.TreeSet) Level(org.mongodb.morphia.mapping.validation.ConstraintViolation.Level)

Example 18 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class ContainsEmbeddedWithId method check.

@Override
public void check(final Mapper mapper, final MappedClass mc, final Set<ConstraintViolation> ve) {
    final Set<Class<?>> classesToInspect = new HashSet<Class<?>>();
    for (final Field field : ReflectionUtils.getDeclaredAndInheritedFields(mc.getClazz(), true)) {
        if (isFieldToInspect(field) && !field.isAnnotationPresent(Id.class)) {
            classesToInspect.add(field.getType());
        }
    }
    checkRecursivelyHasNoIdAnnotationPresent(classesToInspect, new HashSet<Class<?>>(), mc, ve);
}
Also used : Field(java.lang.reflect.Field) MappedClass(org.mongodb.morphia.mapping.MappedClass) HashSet(java.util.HashSet)

Example 19 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class QueryImpl method retrieveKnownFields.

@Override
public Query<T> retrieveKnownFields() {
    final MappedClass mc = ds.getMapper().getMappedClass(clazz);
    final List<String> fields = new ArrayList<String>(mc.getPersistenceFields().size() + 1);
    for (final MappedField mf : mc.getPersistenceFields()) {
        fields.add(mf.getNameToStore());
    }
    retrievedFields(true, fields.toArray(new String[fields.size()]));
    return this;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) ArrayList(java.util.ArrayList) MappedClass(org.mongodb.morphia.mapping.MappedClass)

Example 20 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class PathTargetTest method disableValidation.

@Test
public void disableValidation() {
    getMorphia().map(WithNested.class, Nested.class, NestedImpl.class, AnotherNested.class);
    Mapper mapper = getMorphia().getMapper();
    MappedClass mappedClass = mapper.getMappedClass(WithNested.class);
    final PathTarget pathTarget = new PathTarget(mapper, mappedClass, "nested.field.fail");
    pathTarget.disableValidation();
    Assert.assertEquals("nested.field.fail", pathTarget.translatedPath());
    Assert.assertNull(pathTarget.getTarget());
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Aggregations

MappedClass (org.mongodb.morphia.mapping.MappedClass)92 Test (org.junit.Test)73 MappedField (org.mongodb.morphia.mapping.MappedField)56 Mapper (org.mongodb.morphia.mapping.Mapper)53 ArrayList (java.util.ArrayList)48 BasicDBObject (com.mongodb.BasicDBObject)18 DBObject (com.mongodb.DBObject)12 BsonDocument (org.bson.BsonDocument)8 Document (org.bson.Document)8 MappingException (org.mongodb.morphia.mapping.MappingException)6 DBCollection (com.mongodb.DBCollection)5 List (java.util.List)5 SimpleEntity (org.mongodb.morphia.entities.SimpleEntity)5 ObjectId (org.bson.types.ObjectId)4 Key (org.mongodb.morphia.Key)4 Index (org.mongodb.morphia.annotations.Index)4 LinkedHashMap (java.util.LinkedHashMap)3 NotSaved (org.mongodb.morphia.annotations.NotSaved)3 UpdateResults (org.mongodb.morphia.query.UpdateResults)3 WriteResult (com.mongodb.WriteResult)2