for ( PropertyDescriptor propertyDesc : descriptor.getConstrainedProperties() ) { Property property = findPropertyByName( persistentClass, prefix + propertyDesc.getPropertyName() ); boolean hasNotNull;
private static void applyDDL(String prefix, PersistentClass persistentClass, Class<?> clazz, ValidatorFactory factory, Set<Class<?>> groups, boolean activateNotNull) { final BeanDescriptor descriptor = factory.getValidator().getConstraintsForClass( clazz ); //no bean level constraints can be applied, go to the properties for ( PropertyDescriptor propertyDesc : descriptor.getConstrainedProperties() ) { Property property = findPropertyByName( persistentClass, prefix + propertyDesc.getPropertyName() ); boolean hasNotNull = false; if ( property != null ) { hasNotNull = applyConstraints( propertyDesc.getConstraintDescriptors(), property, propertyDesc, groups, activateNotNull ); if ( property.isComposite() && propertyDesc.isCascaded() ) { Class<?> componentClass = ( ( Component ) property.getValue() ).getComponentClass(); /* * we can apply not null if the upper component let's us activate not null * and if the property is not null. * Otherwise, all sub columns should be left nullable */ final boolean canSetNotNullOnColumns = activateNotNull && hasNotNull; applyDDL( prefix + propertyDesc.getPropertyName() + ".", persistentClass, componentClass, factory, groups, canSetNotNullOnColumns ); } //FIXME add collection of components } } }
/** * Returns the class of a given property, but only if it is a constrained property of the * parent class. Otherwise, it returns null. */ final private static Class<?> getConstrainedPropertyClass(BeanDescriptor beanDescriptor, String propertyName) { Class<?> clasz = null; for (PropertyDescriptor descriptor : beanDescriptor.getConstrainedProperties()) { if (descriptor.getPropertyName().equals(propertyName)) { clasz = descriptor.getElementClass(); break; } } return clasz; }
/** * Returns the class of a given property, but only if it is a constrained property of the * parent class. Otherwise, it returns null. */ final private static Class<?> getConstrainedPropertyClass(BeanDescriptor beanDescriptor, String propertyName) { Class<?> clasz = null; for (PropertyDescriptor descriptor : beanDescriptor.getConstrainedProperties()) { if (descriptor.getPropertyName().equals(propertyName)) { clasz = descriptor.getElementClass(); break; } } return clasz; }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_VALIDATOR, id = "a") @SpecAssertion(section = Sections.CONSTRAINTMETADATA_BEANDESCRIPTOR, id = "d") public void testGetConstrainedPropertiesForUnconstrainedEntity() { BeanDescriptor beanDescriptor = getValidator().getConstraintsForClass( UnconstrainedEntity.class ); Set<PropertyDescriptor> constraintProperties = beanDescriptor.getConstrainedProperties(); assertEquals( constraintProperties.size(), 0, "We should get the empty set." ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_VALIDATOR, id = "a") @SpecAssertion(section = Sections.CONSTRAINTMETADATA_BEANDESCRIPTOR, id = "d") public void testGetConstrainedProperties() { BeanDescriptor beanDescriptor = getValidator().getConstraintsForClass( Order.class ); Set<PropertyDescriptor> constraintProperties = beanDescriptor.getConstrainedProperties(); assertEquals( constraintProperties.size(), 1, "There should be only one property" ); boolean hasOrderNumber = false; for ( PropertyDescriptor pd : constraintProperties ) { hasOrderNumber |= pd.getPropertyName().equals( "orderNumber" ); } assertTrue( hasOrderNumber, "Wrong property" ); }
@Test @SpecAssertions({ @SpecAssertion(section = "5.1", id = "b"), @SpecAssertion(section = "5.3", id = "b") }) public void testGetConstrainedPropertiesForUnconstrainedEntity() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( UnconstraintEntity.class ); Set<PropertyDescriptor> constraintProperties = beanDescriptor.getConstrainedProperties(); assertEquals( constraintProperties.size(), 0, "We should get the empty set." ); }
@Test @SpecAssertions({ @SpecAssertion(section = "5.1", id = "b"), @SpecAssertion(section = "5.3", id = "b") }) public void testGetConstrainedProperties() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Order.class ); Set<PropertyDescriptor> constraintProperties = beanDescriptor.getConstrainedProperties(); assertEquals( constraintProperties.size(), 1, "There should be only one property" ); boolean hasOrderNumber = false; for ( PropertyDescriptor pd : constraintProperties ) { hasOrderNumber |= pd.getPropertyName().equals( "orderNumber" ); } assertTrue( hasOrderNumber, "Wrong property" ); }
private boolean isPropertyConstrained(BeanHelper helper, PropertyDescriptor p) { Set<PropertyDescriptor> propertyDescriptors = helper.getBeanDescriptor().getConstrainedProperties(); Predicate<PropertyDescriptor> nameMatches = newPropertyNameMatches(p); return Iterables.any(propertyDescriptors, nameMatches); }
private boolean isPropertyConstrained(BeanHelper helper, PropertyDescriptor p) { Set<PropertyDescriptor> propertyDescriptors = helper.getBeanDescriptor().getConstrainedProperties(); Predicate<PropertyDescriptor> nameMatches = newPropertyNameMatches(p); return Iterables.any(propertyDescriptors, nameMatches); }
private boolean isPropertyConstrained(final BeanHelper helper, final PropertyDescriptor ppropertyDescription) { final Set<PropertyDescriptor> propertyDescriptors = helper.getBeanDescriptor().getConstrainedProperties(); final Predicate<PropertyDescriptor> nameMatches = newPropertyNameMatches(ppropertyDescription); return Iterables.any(propertyDescriptors, nameMatches); }
/** * Build a complete map of object property / list of {@link org.resthub.web.validation.ValidationConstraint} * from a given {@link javax.validation.metadata.BeanDescriptor} <tt>bd</tt> instance and a given {@link java.util.Locale} * <tt>locale</tt> */ private Map<String, List<ValidationConstraint>> getConstraints(BeanDescriptor bd, Locale locale) { Map<String, List<ValidationConstraint>> constraints = new HashMap<String, List<ValidationConstraint>>(); for (PropertyDescriptor pd : bd.getConstrainedProperties()) { // if property has defined constraints directly or delegates validation through cascading option if ((pd.getPropertyName() != null) && (pd.hasConstraints() || pd.isCascaded())) { constraints.put(pd.getPropertyName(), this.getValidationConstraints(pd, locale)); } } return constraints; }
@Override public <T> Set<ConstraintViolation<T>> validate(final T object, final Class<?>... groups) { final MinijaxConstraintValidatorContext<T> context = new MinijaxConstraintValidatorContext<>(object); final BeanDescriptor descriptor = getConstraintsForClass(object.getClass()); for (final PropertyDescriptor propertyDescriptor : descriptor.getConstrainedProperties()) { final Object value = ((MinijaxPropertyDescriptor) propertyDescriptor).getValue(object); validateProperty(context, propertyDescriptor, value); } return context.getResult(); }
@SuppressWarnings("unchecked") private <T> Set<ConstraintViolation<T>> validate(Map<Class<?>,Map<String,Map<Object,Set<ConstraintViolation<?>>>>> validationCache, T object, Class<?>... groups) { //get the bean descriptor BeanDescriptor bean = this.getConstraintsForClass(object.getClass()); //create empty constraint violation set Set<ConstraintViolation<T>> violations = new HashSet<ConstraintViolation<T>>(); //validate class level properties for(ConstraintDescriptor<?> descriptor : bean.findConstraints().declaredOn(ElementType.TYPE).unorderedAndMatchingGroups(groups).getConstraintDescriptors()) { violations.addAll(this.validateConstraint((Class<T>)object.getClass(), descriptor, object)); } //validate each constrained property individually for(PropertyDescriptor property : bean.getConstrainedProperties()) { violations.addAll(this.validateProperty(validationCache, object, property.getPropertyName(), groups)); } //update root objects for all violations for(ConstraintViolation<T> violation : violations) { if(violation instanceof ConstraintViolationImpl) { ConstraintViolationImpl<T> impl = (ConstraintViolationImpl<T>)violation; impl.setRootBean(object); impl.setRootBeanClass((Class<T>)object.getClass()); } } return violations; }
private void writePropertyValidators(final SourceWriter sw) throws UnableToCompleteException { for (final PropertyDescriptor p : beanHelper.getBeanDescriptor().getConstrainedProperties()) { if (beanHelper.hasField(p)) { writeValidatePropertyMethod(sw, p, true); sw.println(); } if (beanHelper.hasGetter(p)) { writeValidatePropertyMethod(sw, p, false); sw.println(); } } }
private void writePropertyValidators(SourceWriter sw) throws UnableToCompleteException { for (PropertyDescriptor p : beanHelper.getBeanDescriptor().getConstrainedProperties()) { if (beanHelper.hasField(p)) { writeValidatePropertyMethod(sw, p, true); sw.println(); } if (beanHelper.hasGetter(p)) { writeValidatePropertyMethod(sw, p, false); sw.println(); } } }
private void writePropertyValidators(SourceWriter sw) throws UnableToCompleteException { for (PropertyDescriptor p : beanHelper.getBeanDescriptor().getConstrainedProperties()) { if (beanHelper.hasField(p)) { writeValidatePropertyMethod(sw, p, true); sw.println(); } if (beanHelper.hasGetter(p)) { writeValidatePropertyMethod(sw, p, false); sw.println(); } } }
private void writeValidateAllNonInheritedProperties(final SourceWriter sw) { // private <T> void validateAllNonInheritedProperties( sw.println("private <T> void validateAllNonInheritedProperties("); sw.indent(); sw.indent(); // GwtValidationContext<T> context, BeanType object, // Set<ConstraintViolation<T>> violations, Class<?>... groups) { sw.println("GwtValidationContext<T> context,"); sw.println(beanHelper.getTypeCanonicalName() + " object,"); sw.println("Set<ConstraintViolation<T>> violations,"); sw.println("Class<?>... groups) {"); sw.outdent(); for (final PropertyDescriptor p : beanHelper.getBeanDescriptor().getConstrainedProperties()) { writeValidatePropertyCall(sw, p, false, true); } sw.outdent(); sw.println("}"); }
private void writeValidateAllNonInheritedProperties(SourceWriter sw) { // private <T> void validateAllNonInheritedProperties( sw.println("private <T> void validateAllNonInheritedProperties("); sw.indent(); sw.indent(); // GwtValidationContext<T> context, BeanType object, // Set<ConstraintViolation<T>> violations, Class<?>... groups) { sw.println("GwtValidationContext<T> context,"); sw.println(beanHelper.getTypeCanonicalName() + " object,"); sw.println("Set<ConstraintViolation<T>> violations,"); sw.println("Class<?>... groups) {"); sw.outdent(); for (PropertyDescriptor p : beanHelper.getBeanDescriptor().getConstrainedProperties()) { writeValidatePropertyCall(sw, p, false, true); } sw.outdent(); sw.println("}"); }
private void writeValidateAllNonInheritedProperties(SourceWriter sw) { // private <T> void validateAllNonInheritedProperties( sw.println("private <T> void validateAllNonInheritedProperties("); sw.indent(); sw.indent(); // GwtValidationContext<T> context, BeanType object, // Set<ConstraintViolation<T>> violations, Class<?>... groups) { sw.println("GwtValidationContext<T> context,"); sw.println(beanHelper.getTypeCanonicalName() + " object,"); sw.println("Set<ConstraintViolation<T>> violations,"); sw.println("Class<?>... groups) {"); sw.outdent(); for (PropertyDescriptor p : beanHelper.getBeanDescriptor().getConstrainedProperties()) { writeValidatePropertyCall(sw, p, false, true); } sw.outdent(); sw.println("}"); }