Search in sources :

Example 1 with MergedValidationModel

use of org.apache.sling.validation.impl.model.MergedValidationModel in project sling by apache.

the class ValidationModelRetrieverImpl method getValidationModel.

/*
     * (non-Javadoc)
     * 
     * @see org.apache.sling.validation.impl.ValidationModelRetriever#getModels(java.lang.String, java.lang.String)
     */
@CheckForNull
public ValidationModel getValidationModel(@Nonnull String resourceType, String resourcePath, boolean considerResourceSuperTypeModels) {
    // first get model for exactly the requested resource type
    ValidationModel baseModel = getModel(resourceType, resourcePath);
    String currentResourceType = resourceType;
    if (considerResourceSuperTypeModels) {
        Collection<ValidationModel> modelsToMerge = new ArrayList<ValidationModel>();
        ResourceResolver resourceResolver = null;
        try {
            resourceResolver = resourceResolverFactory.getServiceResourceResolver(null);
            while ((currentResourceType = resourceResolver.getParentResourceType(currentResourceType)) != null) {
                LOG.debug("Retrieving validation models for resource super type {}...", currentResourceType);
                ValidationModel modelToMerge = getModel(currentResourceType, resourcePath);
                if (modelToMerge != null) {
                    if (baseModel == null) {
                        baseModel = modelToMerge;
                    } else {
                        modelsToMerge.add(modelToMerge);
                    }
                }
            }
            if (!modelsToMerge.isEmpty()) {
                return new MergedValidationModel(baseModel, modelsToMerge.toArray(new ValidationModel[modelsToMerge.size()]));
            }
        } catch (LoginException e) {
            throw new IllegalStateException("Could not get service resource resolver", e);
        } finally {
            if (resourceResolver != null) {
                resourceResolver.close();
            }
        }
    }
    return baseModel;
}
Also used : MergedValidationModel(org.apache.sling.validation.impl.model.MergedValidationModel) ValidationModel(org.apache.sling.validation.model.ValidationModel) MergedValidationModel(org.apache.sling.validation.impl.model.MergedValidationModel) ArrayList(java.util.ArrayList) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoginException(org.apache.sling.api.resource.LoginException) CheckForNull(javax.annotation.CheckForNull)

Aggregations

ArrayList (java.util.ArrayList)1 CheckForNull (javax.annotation.CheckForNull)1 LoginException (org.apache.sling.api.resource.LoginException)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 MergedValidationModel (org.apache.sling.validation.impl.model.MergedValidationModel)1 ValidationModel (org.apache.sling.validation.model.ValidationModel)1