Search in sources :

Example 56 with Collection

use of java.util.Collection in project grails-core by grails.

the class DefaultGrailsPlugin method addExcludeRuleInternal.

@SuppressWarnings("unchecked")
private void addExcludeRuleInternal(Map map, Object o) {
    Collection excludes = (Collection) map.get(EXCLUDES);
    if (excludes == null) {
        excludes = new ArrayList();
        map.put(EXCLUDES, excludes);
    }
    Collection includes = (Collection) map.get(INCLUDES);
    if (includes != null)
        includes.remove(o);
    excludes.add(o);
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection)

Example 57 with Collection

use of java.util.Collection in project grails-core by grails.

the class DefaultUrlCreator method appendRequestParams.

/*
     * Appends all the request parameters to the URI buffer
     */
private void appendRequestParams(FastStringWriter actualUriBuf, Map<Object, Object> params, String encoding) {
    boolean querySeparator = false;
    for (Map.Entry<Object, Object> entry : params.entrySet()) {
        Object name = entry.getKey();
        if (name.equals(GrailsControllerClass.CONTROLLER) || name.equals(GrailsControllerClass.ACTION) || name.equals(ARGUMENT_ID)) {
            continue;
        }
        if (!querySeparator) {
            actualUriBuf.append('?');
            querySeparator = true;
        } else {
            actualUriBuf.append(ENTITY_AMPERSAND);
        }
        Object value = entry.getValue();
        if (value instanceof Collection) {
            Collection values = (Collection) value;
            Iterator valueIterator = values.iterator();
            while (valueIterator.hasNext()) {
                Object currentValue = valueIterator.next();
                appendRequestParam(actualUriBuf, name, currentValue, encoding);
                if (valueIterator.hasNext()) {
                    actualUriBuf.append(ENTITY_AMPERSAND);
                }
            }
        } else if (value != null && value.getClass().isArray()) {
            Object[] array = (Object[]) value;
            for (int j = 0; j < array.length; j++) {
                Object currentValue = array[j];
                appendRequestParam(actualUriBuf, name, currentValue, encoding);
                if (j < (array.length - 1)) {
                    actualUriBuf.append(ENTITY_AMPERSAND);
                }
            }
        } else {
            appendRequestParam(actualUriBuf, name, value, encoding);
        }
    }
}
Also used : Iterator(java.util.Iterator) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 58 with Collection

use of java.util.Collection in project grails-core by grails.

the class DefaultUrlMappingsHolder method lookupMapping.

/**
     * Performs a match uses reverse mappings to looks up a mapping from the
     * controller, action and params. This is refactored to use a list of mappings
     * identified by only controller and action and then matches the mapping to select
     * the mapping that best matches the params (most possible matches).
     *
     *
     * @param controller The controller name
     * @param action The action name
     * @param httpMethod The HTTP method
     * @param version
     * @param params The params  @return A UrlMapping instance or null
     */
@SuppressWarnings("unchecked")
protected UrlMapping lookupMapping(String controller, String action, String namespace, String pluginName, String httpMethod, String version, Map params) {
    final UrlMappingsListKey lookupKey = new UrlMappingsListKey(controller, action, namespace, pluginName, httpMethod, version);
    Collection mappingKeysSet = mappingsListLookup.get(lookupKey);
    final String actionName = lookupKey.action;
    boolean secondAttempt = false;
    final boolean isIndexAction = GrailsControllerClass.INDEX_ACTION.equals(actionName);
    if (null == mappingKeysSet) {
        lookupKey.httpMethod = UrlMapping.ANY_HTTP_METHOD;
        mappingKeysSet = mappingsListLookup.get(lookupKey);
    }
    if (null == mappingKeysSet && actionName != null) {
        lookupKey.action = null;
        mappingKeysSet = mappingsListLookup.get(lookupKey);
        secondAttempt = true;
    }
    if (null == mappingKeysSet)
        return null;
    Set<String> lookupParams = new HashSet<String>(params.keySet());
    if (secondAttempt) {
        lookupParams.removeAll(DEFAULT_ACTION_PARAMS);
        lookupParams.addAll(DEFAULT_ACTION_PARAMS);
    }
    UrlMappingKey[] mappingKeys = (UrlMappingKey[]) mappingKeysSet.toArray(new UrlMappingKey[mappingKeysSet.size()]);
    for (int i = mappingKeys.length; i > 0; i--) {
        UrlMappingKey mappingKey = mappingKeys[i - 1];
        if (lookupParams.containsAll(mappingKey.paramNames)) {
            final UrlMapping mapping = mappingsLookup.get(mappingKey);
            if (canInferAction(actionName, secondAttempt, isIndexAction, mapping)) {
                return mapping;
            }
            if (!secondAttempt) {
                return mapping;
            }
        }
    }
    return null;
}
Also used : UrlMapping(grails.web.mapping.UrlMapping) Collection(java.util.Collection) HashSet(java.util.HashSet)

Example 59 with Collection

use of java.util.Collection in project grails-core by grails.

the class ConstrainedPropertyTests method testConstraintBuilder.

@SuppressWarnings("rawtypes")
public void testConstraintBuilder() throws Exception {
    GroovyClassLoader gcl = new GroovyClassLoader();
    Class groovyClass = gcl.parseClass("class TestClass {\n" + "Long id\n" + "Long version\n" + "String login\n" + "String other\n" + "String email\n" + "static constraints = {\n" + "login(size:5..15,nullable:false,blank:false)\n" + "other(blank:false,size:5..15,nullable:false)\n" + "email(email:true)\n" + "}\n" + "}");
    GrailsApplication ga = new DefaultGrailsApplication(groovyClass);
    ga.initialise();
    new MappingContextBuilder(ga).build(groovyClass);
    GrailsDomainClass domainClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "TestClass");
    Map constrainedProperties = domainClass.getConstrainedProperties();
    assert constrainedProperties.size() == 3;
    grails.gorm.validation.ConstrainedProperty loginConstraint = (grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("login");
    Collection appliedConstraints = loginConstraint.getAppliedConstraints();
    assertTrue(appliedConstraints.size() == 3);
    // Check the order of the constraints for the 'login' property...
    int index = 0;
    String[] constraintNames = new String[] { "size", "nullable", "blank" };
    for (Iterator iter = appliedConstraints.iterator(); iter.hasNext(); ) {
        Constraint c = (Constraint) iter.next();
        assertEquals(constraintNames[index], c.getName());
        index++;
    }
    // ...and for the 'other' property.
    appliedConstraints = ((grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("other")).getAppliedConstraints();
    index = 0;
    constraintNames = new String[] { "blank", "size", "nullable" };
    for (Iterator iter = appliedConstraints.iterator(); iter.hasNext(); ) {
        Constraint c = (Constraint) iter.next();
        assertEquals(constraintNames[index], c.getName());
        index++;
    }
    grails.gorm.validation.ConstrainedProperty emailConstraint = (grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("email");
    assertEquals(2, emailConstraint.getAppliedConstraints().size());
    GroovyObject go = (GroovyObject) groovyClass.newInstance();
    go.setProperty("email", "rubbish_email");
    Errors errors = new BindException(go, "TestClass");
    emailConstraint.validate(go, go.getProperty("email"), errors);
    assertTrue(errors.hasErrors());
    go.setProperty("email", "valid@email.com");
    errors = new BindException(go, "TestClass");
    emailConstraint.validate(go, go.getProperty("email"), errors);
    assertFalse(errors.hasErrors());
}
Also used : GrailsDomainClass(grails.core.GrailsDomainClass) BindException(org.springframework.validation.BindException) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GroovyObject(groovy.lang.GroovyObject) GroovyClassLoader(groovy.lang.GroovyClassLoader) Errors(org.springframework.validation.Errors) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GrailsApplication(grails.core.GrailsApplication) Iterator(java.util.Iterator) Collection(java.util.Collection) GrailsDomainClass(grails.core.GrailsDomainClass) Map(java.util.Map)

Example 60 with Collection

use of java.util.Collection in project grails-core by grails.

the class CollectionMarshaller method marshalObject.

public void marshalObject(Object object, XML xml) throws ConverterException {
    Collection col = (Collection) object;
    for (Object o : col) {
        if (o != null) {
            xml.startNode(xml.getElementName(o));
            xml.convertAnother(o);
            xml.end();
        } else {
            xml.startNode("null");
            xml.end();
        }
    }
}
Also used : Collection(java.util.Collection)

Aggregations

Collection (java.util.Collection)3392 ArrayList (java.util.ArrayList)983 Map (java.util.Map)732 Test (org.junit.Test)669 HashMap (java.util.HashMap)570 List (java.util.List)521 Iterator (java.util.Iterator)386 HashSet (java.util.HashSet)333 Set (java.util.Set)314 IOException (java.io.IOException)308 Collectors (java.util.stream.Collectors)155 File (java.io.File)135 LinkedHashMap (java.util.LinkedHashMap)112 Collections (java.util.Collections)100 LinkedList (java.util.LinkedList)96 Arrays (java.util.Arrays)84 UUID (java.util.UUID)80 Test (org.testng.annotations.Test)79 NotNull (org.jetbrains.annotations.NotNull)78 Field (java.lang.reflect.Field)77