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;
}
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);
}
}
}
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);
}
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;
}
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());
}
Aggregations