Package org.mongodb.morphia.mapping.validation

Examples of org.mongodb.morphia.mapping.validation.ConstraintViolation


  }

  @Override
  protected final void check(final MappedClass mc, final MappedField mf, final Set<ConstraintViolation> ve) {
    if (mf.hasAnnotation(a1) && mf.hasAnnotation(a2)) {
      ve.add(new ConstraintViolation(Level.FATAL, mc, mf, getClass(),
        "A field can be either annotated with @" + a1.getSimpleName() + " OR @" + a2.getSimpleName() + ", but not both."));
    }
  }
View Full Code Here


  protected void check(final MappedClass mc, final MappedField mf, final Set<ConstraintViolation> ve) {
    if (mf.isMap() && (!mf.hasAnnotation(Serialized.class))) {
      final Class<?> aClass = ReflectionUtils.getParameterizedClass(mf.getField(), 0);
      // WARN if not parameterized : null or Object...
      if (aClass == null || Object.class.equals(aClass)) {
        ve.add(new ConstraintViolation(Level.WARNING, mc, mf, getClass(),
          "Maps cannot be keyed by Object (Map<Object,?>); Use a parametrized type that is supported " + SUPPORTED));
      } else if (!aClass.equals(String.class) && !aClass.equals(ObjectId.class) && !ReflectionUtils.isPrimitiveLike(
        aClass)) {
        ve.add(new ConstraintViolation(Level.FATAL, mc, mf, getClass(),
          "Maps must be keyed by a simple type " + SUPPORTED + "; " + aClass + " is not supported as a map key type."));
      }
    }
  }
View Full Code Here

  protected void check(final MappedClass mc, final MappedField mf, final Set<ConstraintViolation> ve) {
    // a field can be a Value, Reference, or Embedded
    if (mf.hasAnnotation(Property.class)) {
      // make sure that the property type is supported
      if (mf.isSingleValue() && !mf.isTypeMongoCompatible() && !mc.getMapper().getConverters().hasSimpleValueConverter(mf)) {
        ve.add(new ConstraintViolation(Level.WARNING, mc, mf, getClass(),
          mf.getFullName() + " is annotated as @" + Property.class.getSimpleName() + " but is a type that cannot be mapped simply (type is "
            + mf.getType().getName() + ")."));
      }
    }
  }
View Full Code Here

        final Object testInstance = DefaultCreator.createInst(mc.getClazz());

        // check initial value
        if (Long.class.equals(type)) {
          if (mf.getFieldValue(testInstance) != null) {
            ve.add(new ConstraintViolation(Level.FATAL, mc, mf, getClass(),
              "When using @" + Version.class.getSimpleName() + " on a Long field, it must be initialized to null."));
          }
        } else if (long.class.equals(type)) {
          if ((Long) mf.getFieldValue(testInstance) != 0L) {
            ve.add(new ConstraintViolation(Level.FATAL, mc, mf, getClass(),
              "When using @" + Version.class.getSimpleName() + " on a long field, it must be initialized to 0."));
          }
        }
      } else {
        ve.add(new ConstraintViolation(Level.FATAL, mc, mf, getClass(),
          "@" + Version.class.getSimpleName() + " can only be used on a Long/long field."));
      }
    }
  }
View Full Code Here

  protected void check(final MappedClass mc, final MappedField mf, final Set<ConstraintViolation> ve) {
    final Reference ref = mf.getAnnotation(Reference.class);
    if (ref != null) {
      if (ref.lazy()) {
        if (!LazyFeatureDependencies.testDependencyFullFilled()) {
          ve.add(new ConstraintViolation(Level.SEVERE, mc, mf, getClass(),
            "Lazy references need CGLib and Proxytoys in the classpath."));
        }
      }
    }
  }
View Full Code Here

  protected void check(final MappedClass mc, final MappedField mf, final Set<ConstraintViolation> ve) {
    final Reference ref = mf.getAnnotation(Reference.class);
    if (ref != null && ref.lazy()) {
      final Class type = mf.getType();
      if (type.isArray()) {
        ve.add(new ConstraintViolation(Level.FATAL, mc, mf, getClass(),
          "The lazy attribute cannot be used for an Array. If you need a lazy array please use ArrayList instead."));
      }
    }
  }
View Full Code Here

      if (mf.hasAnnotation(Serialized.class)) {
        final Class<?> keyClass = ReflectionUtils.getParameterizedClass(mf.getField(), 0);
        final Class<?> valueClass = ReflectionUtils.getParameterizedClass(mf.getField(), 1);
        if (keyClass != null) {
          if (!Serializable.class.isAssignableFrom(keyClass)) {
            ve.add(new ConstraintViolation(Level.FATAL, mc, mf, getClass(),
              "Key class (" + keyClass.getName() + ") is not Serializable"));
          }
        }
        if (valueClass != null) {
          if (!Serializable.class.isAssignableFrom(keyClass)) {
            ve.add(new ConstraintViolation(Level.FATAL, mc, mf, getClass(),
              "Value class (" + valueClass.getName() + ") is not Serializable"));
          }
        }
      }
    }
View Full Code Here

  @Override
  protected void check(final MappedClass mc, final MappedField mf, final Set<ConstraintViolation> ve) {
    // an @Id field can not be a Value, Reference, or Embedded
    if (mf.hasAnnotation(Id.class)) {
      if (mf.hasAnnotation(Reference.class) || mf.hasAnnotation(Embedded.class) || mf.hasAnnotation(Property.class)) {
        ve.add(new ConstraintViolation(Level.FATAL, mc, mf, getClass(),
          mf.getFullName() + " is annotated as @" + Id.class.getSimpleName()
            + " and cannot be mixed with other annotations (like @Reference)"));
      }
    }
  }
View Full Code Here

      if (realType == null) {
        throw new MappingException("Type is null for this MappedField: " + mf);
      }

      if ((!realType.isInterface() && mc.getMapper().getMappedClass(realType).getIdField() == null)) {
        ve.add(new ConstraintViolation(Level.FATAL, mc, mf, getClass(),
          mf.getFullName() + " is annotated as a @" + Reference.class.getSimpleName() + " but the " + mf.getType().getName()
            + " class is missing the @" + Id.class.getSimpleName() + " annotation"));
      }
    }
  }
View Full Code Here

  public void check(final MappedClass mc, final Set<ConstraintViolation> ve) {

    final List<MappedField> idFields = mc.getFieldsAnnotatedWith(Id.class);

    if (idFields.size() > 1) {
      ve.add(new ConstraintViolation(Level.FATAL, mc, getClass(),
        "More than one @" + Id.class.getSimpleName() + " Field found (" + new FieldEnumString(idFields) + ")."));
    }
  }
View Full Code Here

TOP

Related Classes of org.mongodb.morphia.mapping.validation.ConstraintViolation

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.