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);
}
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);
}
}
}
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;
}
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());
}
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();
}
}
}
Aggregations