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