Search in sources :

Example 11 with ResourceProperty

use of org.apache.sling.validation.model.ResourceProperty in project sling by apache.

the class ResourceValidationModelProviderImplTest method createValidationModelProperties.

/**
     * Always uses the validator's class name as validator resource name.
     * @param model
     * @param properties
     * @throws PersistenceException
     */
private void createValidationModelProperties(Resource model, @Nonnull Collection<ResourceProperty> properties) throws PersistenceException {
    ResourceResolver rr = model.getResourceResolver();
    if (properties.isEmpty()) {
        return;
    }
    Resource propertiesResource = ResourceUtil.getOrCreateResource(rr, model.getPath() + "/" + ResourceValidationModelProviderImpl.PROPERTIES, JcrConstants.NT_UNSTRUCTURED, null, true);
    for (ResourceProperty property : properties) {
        Map<String, Object> modelPropertyJCRProperties = new HashMap<String, Object>();
        modelPropertyJCRProperties.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
        Resource propertyResource = ResourceUtil.getOrCreateResource(rr, propertiesResource.getPath() + "/" + property.getName(), modelPropertyJCRProperties, null, true);
        if (propertyResource != null) {
            ModifiableValueMap values = propertyResource.adaptTo(ModifiableValueMap.class);
            Pattern pattern = property.getNamePattern();
            if (pattern != null) {
                values.put(ResourceValidationModelProviderImpl.NAME_REGEX, pattern.pattern());
            }
            values.put(ResourceValidationModelProviderImpl.PROPERTY_MULTIPLE, property.isMultiple());
            values.put(ResourceValidationModelProviderImpl.OPTIONAL, !property.isRequired());
            Resource validators = ResourceUtil.getOrCreateResource(rr, propertyResource.getPath() + "/" + ResourceValidationModelProviderImpl.VALIDATORS, JcrConstants.NT_UNSTRUCTURED, null, true);
            if (validators != null) {
                for (ValidatorInvocation validatorIncovation : property.getValidatorInvocations()) {
                    Map<String, Object> validatorProperties = new HashMap<String, Object>();
                    validatorProperties.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
                    ValueMap parameters = validatorIncovation.getParameters();
                    if (!parameters.isEmpty()) {
                        // convert to right format
                        validatorProperties.put(ResourceValidationModelProviderImpl.VALIDATOR_ARGUMENTS, convertMapToJcrValidatorArguments(parameters));
                    }
                    Integer severity = validatorIncovation.getSeverity();
                    if (severity != null) {
                        validatorProperties.put(ResourceValidationModelProviderImpl.SEVERITY, severity);
                    }
                    ResourceUtil.getOrCreateResource(rr, validators.getPath() + "/" + validatorIncovation.getValidatorId(), validatorProperties, null, true);
                }
            }
        }
    }
}
Also used : ResourceProperty(org.apache.sling.validation.model.ResourceProperty) Pattern(java.util.regex.Pattern) ValidatorInvocation(org.apache.sling.validation.model.ValidatorInvocation) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Aggregations

ResourceProperty (org.apache.sling.validation.model.ResourceProperty)11 ChildResource (org.apache.sling.validation.model.ChildResource)9 Resource (org.apache.sling.api.resource.Resource)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)8 ChildResourceImpl (org.apache.sling.validation.impl.model.ChildResourceImpl)7 ValidationModel (org.apache.sling.validation.model.ValidationModel)7 Test (org.junit.Test)7 HashMap (java.util.HashMap)6 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)6 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)6 ValidationResult (org.apache.sling.validation.ValidationResult)6 DefaultValidationResult (org.apache.sling.validation.spi.support.DefaultValidationResult)6 DefaultValidationFailure (org.apache.sling.validation.spi.support.DefaultValidationFailure)4 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)3 ValueMap (org.apache.sling.api.resource.ValueMap)3 ResourcePropertyBuilder (org.apache.sling.validation.impl.model.ResourcePropertyBuilder)3 ArrayList (java.util.ArrayList)2 Pattern (java.util.regex.Pattern)2 List (java.util.List)1 Map (java.util.Map)1