public static Set<ConstraintDescriptor<?>> extractAllConstraintDescriptors(FacesContext context, PrimeApplicationContext applicationContext, ValueExpression ve) { PropertyDescriptor propertyDescriptor = extractPropertyDescriptor(context, applicationContext, ve); if (propertyDescriptor != null) { return propertyDescriptor.getConstraintDescriptors(); } return null; }
Property property = findPropertyByName( persistentClass, prefix + propertyDesc.getPropertyName() ); boolean hasNotNull; if ( property != null ) { hasNotNull = applyConstraints( propertyDesc.getConstraintDescriptors(), property, propertyDesc, groups, activateNotNull, dialect ); if ( property.isComposite() && propertyDesc.isCascaded() ) { Class<?> componentClass = ( (Component) property.getValue() ).getComponentClass(); prefix + propertyDesc.getPropertyName() + ".", persistentClass, componentClass, factory, groups, canSetNotNullOnColumns,
private static void applyLength(Property property, ConstraintDescriptor<?> descriptor, PropertyDescriptor propertyDescriptor) { if ( "org.hibernate.validator.constraints.Length".equals( descriptor.getAnnotation().annotationType().getName() ) && String.class.equals( propertyDescriptor.getElementClass() ) ) { @SuppressWarnings("unchecked") int max = (Integer) descriptor.getAttributes().get( "max" ); @SuppressWarnings("unchecked") final Iterator<Selectable> itor = property.getColumnIterator(); if ( itor.hasNext() ) { final Selectable selectable = itor.next(); if ( Column.class.isInstance( selectable ) ) { Column col = (Column) selectable; if ( max < Integer.MAX_VALUE ) { col.setLength( max ); } } } } }
private String validateMethodFieldName(PropertyDescriptor p) { return "validateProperty_" + p.getPropertyName(); }
sw.print(PropertyDescriptorImpl.class.getCanonicalName()); sw.print(" "); sw.print(ppropertyDescription.getPropertyName()); sw.println("_pd ="); sw.indent(); sw.println("\"" + ppropertyDescription.getPropertyName() + "\","); sw.println(ppropertyDescription.getElementClass().getCanonicalName() + ".class,"); sw.print(Boolean.toString(ppropertyDescription.isCascaded()) + ","); .getConstraintDescriptors()) { if (areConstraintDescriptorGroupsValid(constraint)) { sw.println(","); // Print the , for the previous line sw.print(constraintDescriptorVar(ppropertyDescription.getPropertyName(), count)); count++;
/** * 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; }
/** * Build a list of {@link org.resthub.web.validation.ValidationConstraint} associated to a given * {@link javax.validation.metadata.PropertyDescriptor} <tt>pd</tt> instance and a given {@link java.util.Locale} * <tt>locale</tt> */ private List<ValidationConstraint> getValidationConstraints(PropertyDescriptor pd, Locale locale) { List<ValidationConstraint> validationConstraints = new ArrayList<ValidationConstraint>(); // copy any directly defined constraint into wrapper for (ConstraintDescriptor cd : pd.getConstraintDescriptors()) { ValidationConstraint validationConstraint = new ValidationConstraint(); validationConstraint.setType(this.getType(cd)); validationConstraint.setMessage(this.getMessage(cd, locale)); validationConstraint.setAttributes(this.getAttributes(cd)); validationConstraints.add(validationConstraint); } // manage cascading option by adding a custom "Valid" type and referencing underling model class name if (pd.isCascaded()) { ValidationConstraint validationConstraint = new ValidationConstraint(); validationConstraint.setType("Valid"); validationConstraint.addAttribute("model", pd.getElementClass().getCanonicalName()); validationConstraints.add(validationConstraint); } return validationConstraints; }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_BEANDESCRIPTOR, id = "b") @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CASCADABLEDESCRIPTOR, id = "a") public void testGetConstraintsForUnConstrainedProperty() { BeanDescriptor beanDescriptor = getValidator().getConstraintsForClass( Customer.class ); PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty( "orderList" ); assertEquals( propertyDescriptor.getConstraintDescriptors().size(), 0, "There should be no constraint descriptors" ); assertTrue( propertyDescriptor.isCascaded(), "The property should be cascaded" ); }
public static Set<ConstraintDescriptor<?>> extractConstraintDescriptors(FacesContext context, PrimeApplicationContext applicationContext, ValueExpression ve, Class... groups) { PropertyDescriptor propertyDescriptor = extractPropertyDescriptor(context, applicationContext, ve); if (propertyDescriptor != null) { return propertyDescriptor.findConstraints().unorderedAndMatchingGroups(groups).getConstraintDescriptors(); } return null; }
/** * 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; }
@Test @SpecAssertion(section = "5.4", id = "a") public void testIsCascaded() { PropertyDescriptor descriptor = getPropertyDescriptor( Customer.class, "orderList" ); assertTrue( descriptor.isCascaded(), "Should be cascaded" ); }
@Override public PropertyDescriptor getConstraintsForProperty(String name) { if(name == null) { throw new IllegalArgumentException("Property name cannot be null."); } if(!this.backingReflector.getPropertyNames().contains(name)) { return null; } PropertyDescriptor descriptor = DescriptorFactory.INSTANCE.getPropertyDescriptor(this.backingReflector, name); if(!descriptor.hasConstraints() && !descriptor.isCascaded()) { return null; } return descriptor; }
private String validateMethodGetterName(PropertyDescriptor p) { return "validateProperty_get" + p.getPropertyName(); }
/** * 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 @SpecAssertions({ @SpecAssertion(section = "5.3", id = "b"), @SpecAssertion(section = "5.3", id = "b"), @SpecAssertion(section = "5.4", id = "a") }) public void testGetConstraintForUnConstrainedProperty() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Customer.class ); PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty( "orderList" ); assertEquals( propertyDescriptor.getConstraintDescriptors().size(), 0, "There should be no constraint descriptors" ); assertTrue( propertyDescriptor.isCascaded(), "The property should be cascaded" ); }
private boolean isGetterConstrained(BeanDescriptor beanDescriptor, Method method, String property) { PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty( property ); return propertyDescriptor != null && propertyDescriptor.findConstraints() .declaredOn( ElementType.METHOD ) .hasConstraints(); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CASCADABLEDESCRIPTOR, id = "a") public void testIsNotCascaded() { PropertyDescriptor descriptor = getPropertyDescriptor( Order.class, "orderNumber" ); assertFalse( descriptor.isCascaded(), "Should not be cascaded" ); }
@Override public Set<PropertyDescriptor> getConstrainedProperties() { Set<PropertyDescriptor> propertyDescriptors = new HashSet<PropertyDescriptor>(); //this was added as part of the work-around for the problems found in issue #32. this just gets us passed //one set of exceptions and not passed the entire problem. there was a null pointer exception being thrown //by one of the generated classes at this point because it's backing reflector hadn't been generated. if(this.backingReflector == null || this.backingReflector.getPropertyNames() == null) { return propertyDescriptors; } for(String propertyName : this.backingReflector.getPropertyNames()) { PropertyDescriptor descriptor = DescriptorFactory.INSTANCE.getPropertyDescriptor(this.backingReflector, propertyName); if(descriptor.hasConstraints() || descriptor.isCascaded()) { propertyDescriptors.add(descriptor); } } return propertyDescriptors; }
@Override public List<Constraint> resolveForProperty(String property, Class<?> clazz) { List<Constraint> constraints = new ArrayList<>(); BeanDescriptor beanDescriptor = this.validator.getConstraintsForClass(clazz); PropertyDescriptor propertyDescriptor = beanDescriptor .getConstraintsForProperty(property); if (propertyDescriptor != null) { for (ConstraintDescriptor<?> constraintDescriptor : propertyDescriptor .getConstraintDescriptors()) { constraints.add(new Constraint( constraintDescriptor.getAnnotation().annotationType().getName(), constraintDescriptor.getAttributes())); } } return constraints; }
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 } } }