Examples of ConstrainedProperty


Examples of grails.validation.ConstrainedProperty

            ConstrainedProperty[] params = mapping.getConstraints();
            Set<String> requiredParams = new HashSet<String>();
            int optionalIndex = -1;
            for (int j = 0; j < params.length; j++) {
                ConstrainedProperty param = params[j];
                if (!param.isNullable()) {
                    requiredParams.add(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++) {
                    ConstrainedProperty param = params[j];
                    requiredParamsAndOptionals.add(param.getPropertyName());
                    key = new UrlMappingKey(controllerName, actionName, namespace, pluginName,httpMethod, version,new HashSet<String>(requiredParamsAndOptionals));
                    mappingsLookup.put(key, mapping);

                    listKey = new UrlMappingsListKey(controllerName, actionName, namespace, pluginName,httpMethod, version);
                    mappingsListLookup.put(listKey, key);
View Full Code Here

Examples of grails.validation.ConstrainedProperty

                constraintUpperBound--;
                constraints[constraintUpperBound].setNullable(true);
            }

            for (int i = 0; i < constraintUpperBound; i++) {
                ConstrainedProperty constraint = constraints[i];
                if (currentToken > tokensLength) break;
                String token = tokens[currentToken];
                int shiftLength = 3;
                pos = token.indexOf(CAPTURED_WILDCARD, pos);
                while(pos == -1) {
                    boolean isLastToken = currentToken == tokensLength-1;
                    if (currentToken < tokensLength) {

                        token = tokens[++currentToken];
                        // special handling for last token to deal with optional extension
                        if (isLastToken) {
                            if (token.startsWith(CAPTURED_WILDCARD + '?') ) {
                                constraint.setNullable(true);
                            }
                            if (token.endsWith(OPTIONAL_EXTENSION_WILDCARD + '?')) {
                                constraints[constraints.length-1].setNullable(true);
                            }
                        }
                        else {
                            pos = token.indexOf(CAPTURED_WILDCARD, pos);
                        }
                    }
                    else {
                        break;
                    }
                }

                if (pos == -1) {
                    constraint.setNullable(true);
                }
                else if (pos + shiftLength < token.length() && token.charAt(pos + shiftLength) == '?') {
                    constraint.setNullable(true);
                }

                // Move on to the next place-holder.
                pos += shiftLength;
                if (token.indexOf(CAPTURED_WILDCARD, pos) == -1) {
View Full Code Here

Examples of grails.validation.ConstrainedProperty

            }
            Matcher m = OPTIONAL_EXTENSION_WILDCARD_PATTERN.matcher(token);
            if (m.find()) {

                if (token.startsWith(CAPTURED_WILDCARD)) {
                    ConstrainedProperty prop = constraints[paramIndex++];
                    String propName = prop.getPropertyName();

                    Object value = paramValues.get(propName);
                    usedParams.add(propName);

                    if (value != null) {
                        token = token.replaceFirst(DOUBLE_WILDCARD_PATTERN.pattern(), value.toString());
                    }
                    else if (prop.isNullable()) {
                        break;
                    }
                }
                uri.append(SLASH);
                ConstrainedProperty prop = constraints[paramIndex++];
                String propName = prop.getPropertyName();
                Object value = paramValues.get(propName);
                usedParams.add(propName);
                if (value != null) {
                    String ext = "." + value;
                    uri.append(token.replace(OPTIONAL_EXTENSION_WILDCARD+'?', ext).replace(OPTIONAL_EXTENSION_WILDCARD, ext));
                }
                else {
                    uri.append(token.replace(OPTIONAL_EXTENSION_WILDCARD+'?', "").replace(OPTIONAL_EXTENSION_WILDCARD, ""));
                }

                continue;
            }
            if (token.endsWith("?")) {
                token = token.substring(0,token.length()-1);
            }
            m = DOUBLE_WILDCARD_PATTERN.matcher(token);
            if (m.find()) {
                StringBuffer buf = new StringBuffer();
                do {
                    ConstrainedProperty prop = constraints[paramIndex++];
                    String propName = prop.getPropertyName();
                    Object value = paramValues.get(propName);
                    usedParams.add(propName);
                    if (value == null && !prop.isNullable()) {
                        throw new UrlMappingException("Unable to create URL for mapping [" + this +
                            "] and parameters [" + paramValues + "]. Parameter [" +
                            prop.getPropertyName() + "] is required, but was not specified!");
                    }
                    else if (value == null) {
                        m.appendReplacement(buf, "");
                    }
                    else {
View Full Code Here

Examples of grails.validation.ConstrainedProperty

        String lastGroup = null;
        for (int i = 0; i < groupCount; i++) {
            lastGroup = m.group(i + 1);
            // if null optional.. ignore
            if (i == groupCount - 1 && hasOptionalExtension) {
                ConstrainedProperty cp = constraints[constraints.length-1];
                cp.validate(this, lastGroup, errors);

                if (errors.hasErrors()) {
                    return null;
                }

                params.put(cp.getPropertyName(), lastGroup);
                break;
            }
            else {
                if (lastGroup == null) continue;
                int j = lastGroup.indexOf('?');
                if (j > -1) {
                    lastGroup = lastGroup.substring(0, j);
                }
                if (constraints.length > i) {
                    ConstrainedProperty cp = constraints[i];
                    cp.validate(this, lastGroup, errors);

                    if (errors.hasErrors()) {
                        return null;
                    }

                    params.put(cp.getPropertyName(), lastGroup);
                }
            }

        }
View Full Code Here

Examples of grails.validation.ConstrainedProperty

    protected Object createNode(Object name, Map attributes) {
        // we do this so that missing property exception is throw if it doesn't exist

        try {
            String property = (String)name;
            ConstrainedProperty cp;
            if (constrainedProperties.containsKey(property)) {
                cp = (ConstrainedProperty)constrainedProperties.get(property);
            }
            else {
                Class<?> propertyType = classPropertyFetcher.getPropertyType(property);
                if (propertyType == null) {
                    throw new MissingMethodException(property, targetClass, new Object[]{attributes}, true);
                }
                cp = new ConstrainedProperty(targetClass, property, propertyType);
                cp.setOrder(order++);
                constrainedProperties.put(property, cp);
            }

            if (cp.getPropertyType() == null) {
                if (!IMPORT_FROM_CONSTRAINT.equals(name)) {
                    GrailsUtil.warn("Property [" + cp.getPropertyName() + "] not found in domain class " +
                        targetClass.getName() + "; cannot apply constraints: " + attributes);
                }
                return cp;
            }

            for (Object o : attributes.keySet()) {
                String constraintName = (String) o;
                final Object value = attributes.get(constraintName);
                if (SHARED_CONSTRAINT.equals(constraintName)) {
                    if (value != null) {
                        sharedConstraints.put(property, value.toString());
                    }
                    continue;
                }
                if (cp.supportsContraint(constraintName)) {
                    cp.applyConstraint(constraintName, value);
                }
                else {
                    if (ConstrainedProperty.hasRegisteredConstraint(constraintName)) {
                        // constraint is registered but doesn't support this property's type
                        GrailsUtil.warn("Property [" + cp.getPropertyName() + "] of domain class " +
                                targetClass.getName() + " has type [" + cp.getPropertyType().getName() +
                                "] and doesn't support constraint [" + constraintName +
                                "]. This constraint will not be checked during validation.");
                    }
                    else {
                        // in the case where the constraint is not supported we still retain meta data
                        // about the constraint in case its needed for other things
                        cp.addMetaConstraint(constraintName, value);
                    }
                }
            }

            return cp;
View Full Code Here

Examples of grails.validation.ConstrainedProperty

        resultingPropertyNames.remove("class");
        resultingPropertyNames.remove("metaClass");

        for (String targetPropertyName : resultingPropertyNames) {
            ConstrainedProperty importFromConstrainedProperty =
                    (ConstrainedProperty) importFromConstrainedProperties.get(targetPropertyName);

            if (importFromConstrainedProperty != null) {
                // Map importFromConstrainedPropertyAttributes = importFromConstrainedProperty.getAttributes();
                // createNode(targetPropertyName, importFromConstrainedPropertyAttributes);
                Map importFromConstrainedPropertyAttributes = new HashMap();
                for (Constraint importFromAppliedConstraint : importFromConstrainedProperty.getAppliedConstraints()) {
                    String importFromAppliedConstraintName = importFromAppliedConstraint.getName();
                    Object importFromAppliedConstraintParameter = importFromAppliedConstraint.getParameter();
                    importFromConstrainedPropertyAttributes.put(
                            importFromAppliedConstraintName, importFromAppliedConstraintParameter);
                }
View Full Code Here

Examples of grails.validation.ConstrainedProperty

        DefaultGrailsDomainClass domainClass = new DefaultGrailsDomainClass(classes[classIndexToTest]);

        Map constraints = domainClass.getConstrainedProperties();

        ConstrainedProperty p = (ConstrainedProperty)constraints.get("name");
        Collection cons = p.getAppliedConstraints();

        assertEquals("Incorrect number of constraints extracted: " +constraints, constraintCount, cons.size());
    }
View Full Code Here

Examples of grails.validation.ConstrainedProperty

                        }
                    } 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

Examples of grails.validation.ConstrainedProperty

        else {
            constrainedPropertyName = propertyName;
        }
        FieldError fieldError = errors.getFieldError(constrainedPropertyName);
        if (fieldError == null) {
            ConstrainedProperty c = (ConstrainedProperty) constrainedProperties.get(constrainedPropertyName);
            c.setMessageSource(messageSource);
            c.validate(obj, bean.getPropertyValue(constrainedPropertyName), errors);
        }
    }
View Full Code Here

Examples of org.codehaus.groovy.grails.validation.ConstrainedProperty

    }

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

    ConstrainedProperty cp1 = (ConstrainedProperty)constrainedProperties.get(prop1.getName());
    ConstrainedProperty cp2 = (ConstrainedProperty)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
TOP
Copyright © 2018 www.massapi.com. 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.