Package grails.validation

Examples of grails.validation.Constrained


    /* (non-Javadoc)
     * @see org.codehaus.groovy.grails.domain.GrailsDomainClassProperty#isRequired()
     */
    public boolean isOptional() {
        Constrained constrained = (Constrained) domainClass.getConstrainedProperties().get(name);
        return (constrained != null) && constrained.isNullable();
    }
View Full Code Here


        }

        GrailsDomainClassProperty prop1 = (GrailsDomainClassProperty)o1;
        GrailsDomainClassProperty prop2 = (GrailsDomainClassProperty)o2;

        Constrained cp1 = (Constrained)constrainedProperties.get(prop1.getName());
        Constrained cp2 = (Constrained)constrainedProperties.get(prop2.getName());

        if (cp1 == null & cp2 == null) {
            return prop1.getName().compareTo(prop2.getName());
        }

        if (cp1 == null) {
            return 1;
        }

        if (cp2 == null) {
            return -1;
        }

        if (cp1.getOrder() > cp2.getOrder()) {
            return 1;
        }

        if (cp1.getOrder() < cp2.getOrder()) {
            return -1;
        }

        return 0;
    }
View Full Code Here

        GroovyClassLoader gcl = new GroovyClassLoader();

        DefaultGrailsDomainClass bookClass = new DefaultGrailsDomainClass(gcl.parseClass(bookClassSource, "Book"));

        Map constraints = bookClass.getConstrainedProperties();
        Constrained p = (Constrained)constraints.get("title");
        assertFalse("Title property should be required", p.isNullable());
        p = (Constrained)constraints.get("description");
        assertTrue("Description property should be optional", p.isNullable());
        p = (Constrained)constraints.get("author");
        assertFalse("Author property should be required", p.isNullable());
        p = (Constrained)constraints.get("assistent");
        assertTrue("Assistent property should be optional", p.isNullable());
        // Test that Collections and Maps are nullable by default
        p = (Constrained)constraints.get("chapters");
        assertTrue("Chapters property should be optional", p.isNullable());
        p = (Constrained)constraints.get("remarks");
        assertTrue("Remarks property should be optional", p.isNullable());
    }
View Full Code Here

                        if (constrainedProperties.remove(p.getName()) != null) {
                            LOG.warn("Derived properties may not be constrained. Property [" + p.getName() + "] of domain class " + theClass.getName() + " will not be checked during validation.");
                        }
                    } else {
                        final String propertyName = p.getName();
                        Constrained cp = constrainedProperties.get(propertyName);
                        if (cp == null) {
                            ConstrainedProperty constrainedProperty = new ConstrainedProperty(p.getDomainClass().getClazz(), propertyName, p.getType());
                            cp = constrainedProperty;
                            constrainedProperty.setOrder(constrainedProperties.size() + 1);
                            constrainedProperties.put(propertyName, cp);
                        }
                        // Make sure all fields are required by default, unless
                        // specified otherwise by the constraints
                        // If the field is a Java entity annotated with @Entity skip this
                        applyDefaultConstraints(propertyName, p, cp, defaultConstraints);
                    }
                }
            }
        }

        if (properties == null || properties.length == 0) {
            final Set<Entry<String, Constrained>> entrySet = constrainedProperties.entrySet();
            for (Entry<String, Constrained> entry : entrySet) {
                final Constrained constrainedProperty = entry.getValue();
                if (!constrainedProperty.hasAppliedConstraint(ConstrainedProperty.NULLABLE_CONSTRAINT)) {
                    applyDefaultNullableConstraint(constrainedProperty, defaultNullable);
                }
            }
        }
View Full Code Here

    protected void applySharedConstraints(
            ConstrainedPropertyBuilder constrainedPropertyBuilder,
            Map<String, Constrained> constrainedProperties) {
        for (Map.Entry<String, Constrained> entry : constrainedProperties.entrySet()) {
            String propertyName = entry.getKey();
            Constrained constrainedProperty = entry.getValue();
            String sharedConstraintReference = constrainedPropertyBuilder.getSharedConstraint(propertyName);
            if (sharedConstraintReference != null && defaultConstraints !=  null) {
                Object o = defaultConstraints.get(sharedConstraintReference);
                if (o instanceof Map) {
                    @SuppressWarnings({ "unchecked", "rawtypes" })
                    Map<String, Object> constraintsWithinSharedConstraint = (Map) o;
                    for (Map.Entry<String, Object> e : constraintsWithinSharedConstraint.entrySet()) {
                        constrainedProperty.applyConstraint(e.getKey(), e.getValue());
                    }
                } else {
                    throw new GrailsConfigurationException("Property [" +
                            constrainedProperty.getOwner().getName() + '.' + propertyName +
                            "] references shared constraint [" + sharedConstraintReference +
                            ":" + o + "], which doesn't exist!");
                }
            }
        }
View Full Code Here

TOP

Related Classes of grails.validation.Constrained

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.