Search in sources :

Example 1 with Constrained

use of grails.gorm.validation.Constrained in project grails-core by grails.

the class DefaultUrlMappingsHolder method initialize.

public void initialize() {
    sortMappings();
    cachedMatches = new ConcurrentLinkedHashMap.Builder<String, UrlMappingInfo>().maximumWeightedCapacity(maxWeightedCacheCapacity).build();
    cachedListMatches = new ConcurrentLinkedHashMap.Builder<UriToUrlMappingKey, List<UrlMappingInfo>>().maximumWeightedCapacity(maxWeightedCacheCapacity).weigher(CustomListWeigher.INSTANCE).build();
    if (urlCreatorMaxWeightedCacheCapacity > 0) {
        urlCreatorCache = new UrlCreatorCache(urlCreatorMaxWeightedCacheCapacity);
    }
    mappings = urlMappings.toArray(new UrlMapping[urlMappings.size()]);
    for (UrlMapping mapping : mappings) {
        String mappingName = mapping.getMappingName();
        if (mappingName != null) {
            namedMappings.put(mappingName, mapping);
        }
        String controllerName = mapping.getControllerName() instanceof String ? mapping.getControllerName().toString() : null;
        String actionName = mapping.getActionName() instanceof String ? mapping.getActionName().toString() : null;
        String pluginName = mapping.getPluginName() instanceof String ? mapping.getPluginName().toString() : null;
        String httpMethod = mapping.getHttpMethod();
        String version = mapping.getVersion();
        String namespace = mapping.getNamespace() instanceof String ? mapping.getNamespace().toString() : null;
        Constrained[] params = mapping.getConstraints();
        Set<String> requiredParams = new HashSet<String>();
        int optionalIndex = -1;
        for (int j = 0; j < params.length; j++) {
            Constrained param = params[j];
            if (param instanceof ConstrainedProperty) {
                if (!param.isNullable()) {
                    requiredParams.add(((ConstrainedProperty) param).getPropertyName());
                } else {
                    optionalIndex = j;
                    break;
                }
            }
        }
        UrlMappingKey key = new UrlMappingKey(controllerName, actionName, namespace, pluginName, httpMethod, version, requiredParams);
        mappingsLookup.put(key, mapping);
        UrlMappingsListKey listKey = new UrlMappingsListKey(controllerName, actionName, namespace, pluginName, httpMethod, version);
        mappingsListLookup.put(listKey, key);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Reverse mapping: " + key + " -> " + mapping);
        }
        Set<String> requiredParamsAndOptionals = new HashSet<String>(requiredParams);
        if (optionalIndex > -1) {
            for (int j = optionalIndex; j < params.length; j++) {
                Constrained constrained = params[j];
                if (constrained instanceof ConstrainedProperty) {
                    ConstrainedProperty param = (ConstrainedProperty) constrained;
                    requiredParamsAndOptionals.add(param.getPropertyName());
                    key = new UrlMappingKey(controllerName, actionName, namespace, pluginName, httpMethod, version, new HashSet<>(requiredParamsAndOptionals));
                    mappingsLookup.put(key, mapping);
                    listKey = new UrlMappingsListKey(controllerName, actionName, namespace, pluginName, httpMethod, version);
                    mappingsListLookup.put(listKey, key);
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Reverse mapping: " + key + " -> " + mapping);
                    }
                }
            }
        }
    }
}
Also used : UrlMapping(grails.web.mapping.UrlMapping) ConcurrentLinkedHashMap(com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap) Constrained(grails.gorm.validation.Constrained) ConstrainedProperty(grails.gorm.validation.ConstrainedProperty) UrlMappingInfo(grails.web.mapping.UrlMappingInfo) HashSet(java.util.HashSet)

Example 2 with Constrained

use of grails.gorm.validation.Constrained in project grails-core by grails.

the class ConstraintsEvaluatingPropertyTests method testNullableConstraint.

/**
     * Test that static constraints work
     */
@SuppressWarnings("rawtypes")
public void testNullableConstraint() throws Exception {
    GroovyClassLoader gcl = new GroovyClassLoader();
    gcl.parseClass("package org.grails.validation\n" + "@grails.persistence.Entity class Book {\n" + "   Long id\n" + "   Long version\n" + "   String title\n" + "   String description\n" + "   Author author\n" + "   Author assistent\n" + "   Set chapters\n" + "   Map remarks\n" + "   static hasMany = [chapters:Chapter]\n" + "   static constraints = {\n" + "      description(nullable: true)\n" + "      assistent(nullable: true)\n" + "   }\n" + "}\n" + "@grails.persistence.Entity class Author {\n" + "   Long id\n" + "   Long version\n" + "   String name\n" + "}\n" + "@grails.persistence.Entity class Chapter {\n" + "   Long id\n" + "   Long version\n" + "   String text\n" + "}");
    GrailsApplication ga = new DefaultGrailsApplication(gcl.getLoadedClasses());
    ga.initialise();
    new MappingContextBuilder(ga).build(gcl.getLoadedClasses());
    GrailsDomainClass bookClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "org.grails.validation.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());
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) Constrained(grails.gorm.validation.Constrained) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GrailsDomainClass(grails.core.GrailsDomainClass) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) Map(java.util.Map)

Aggregations

Constrained (grails.gorm.validation.Constrained)2 ConcurrentLinkedHashMap (com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap)1 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)1 GrailsApplication (grails.core.GrailsApplication)1 GrailsDomainClass (grails.core.GrailsDomainClass)1 ConstrainedProperty (grails.gorm.validation.ConstrainedProperty)1 UrlMapping (grails.web.mapping.UrlMapping)1 UrlMappingInfo (grails.web.mapping.UrlMappingInfo)1 GroovyClassLoader (groovy.lang.GroovyClassLoader)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1