All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.kestros.commons.structuredslingmodels.validation.CommonValidators Maven / Gradle / Ivy

Go to download

Foundational classes for that can be used across any Sling instance. Includes base model types and model interaction utilities.

There is a newer version: 0.2.5
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one

 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package io.kestros.commons.structuredslingmodels.validation;

import static io.kestros.commons.structuredslingmodels.validation.ModelValidationMessageType.ERROR;
import static io.kestros.commons.structuredslingmodels.validation.ModelValidationMessageType.WARNING;

import io.kestros.commons.structuredslingmodels.BaseResource;
import io.kestros.commons.structuredslingmodels.exceptions.ChildResourceNotFoundException;
import io.kestros.commons.structuredslingmodels.exceptions.ModelAdaptionException;
import io.kestros.commons.structuredslingmodels.utils.SlingModelUtils;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import org.apache.commons.lang3.StringUtils;

/**
 * Utility Class which holds static validators which are commonly used.
 */
public class CommonValidators {

  /**
   * Validator that checks if the current Resource has a title value.
   *
   * @param model Model to add the validator to.
   * @param  Generic model type.
   * @return Validator that checks if the current Resource has a title value.
   */
  public static  ModelValidator hasTitle(final T model) {
    return new ModelValidator() {
      @Override
      public boolean isValid() {
        return !model.getName().equals(model.getTitle());
      }

      @Override
      public String getMessage() {
        return "Title is configured.";
      }

      @Override
      public ModelValidationMessageType getType() {
        return ERROR;
      }
    };
  }

  /**
   * Validator that checks if the current Resource has a description value.
   *
   * @param model Model to add the validator to.
   * @param messageType Message type to return failed validation as.
   * @param  Generic model type.
   * @return Validator that checks if the current Resource has a description value.
   */
  public static  ModelValidator hasDescription(final T model,
      final ModelValidationMessageType messageType) {
    return new ModelValidator() {
      @Override
      public boolean isValid() {
        return StringUtils.isNotEmpty(model.getDescription());
      }

      @Override
      public String getMessage() {
        return "Description is configured.";
      }

      @Override
      public ModelValidationMessageType getType() {
        return messageType;
      }
    };
  }

  /**
   * Validator that checks if the Resource's name ends with the file extension String.
   *
   * @param extension String to match the Resource's name against.
   * @param model Model to add the validator to.
   * @param messageType Message type to return failed validation as.
   * @param  Generic model type.
   * @return Validator that checks if the Resource's name ends with the specified String.
   */
  public static  ModelValidator hasFileExtension(final String extension,
      final T model, final ModelValidationMessageType messageType) {
    return new ModelValidator() {
      @Override
      public boolean isValid() {
        return model.getName().endsWith(extension);
      }

      @Override
      public String getMessage() {
        return "Resource name ends with " + extension + " extension.";
      }

      @Override
      public ModelValidationMessageType getType() {
        return messageType;
      }
    };
  }

  /**
   * Validator that checks if a specified child Resource exists.
   *
   * @param childName name of the child Resource to check for.
   * @param model Model to add the validator to.
   * @param  Generic model type.
   * @return Validator that checks if a specified child Resource exists.
   */
  public static  ModelValidator hasChildResource(final String childName,
      final T model) {
    return new ModelValidator() {
      @Override
      public boolean isValid() {
        try {
          SlingModelUtils.getChildAsBaseResource(childName, model);
        } catch (final ChildResourceNotFoundException exception) {
          return false;
        }
        return true;
      }

      @Override
      public String getMessage() {
        return String.format("Has child resource '%s'.", childName);
      }

      @Override
      public ModelValidationMessageType getType() {
        return ERROR;
      }
    };
  }

  /**
   * Validator that checks if a specified child does not fail any ERROR type validators.
   *
   * @param childName name of the child Resource to check for.
   * @param childType type of Model to attempt to adapt the child to. Must extend BaseResource.
   * @param model Model to add the validator to.
   * @param  Generic model type.
   * @param  Generic model type.
   * @return Validator that checks if a specified child does not fail any ERROR type validators.
   */
  public static  ModelValidator
      isChildResourceValidResourceType(final String childName, final Class childType,
      final T model) {

    return new ModelValidator() {
      @Override
      public boolean isValid() {
        try {
          SlingModelUtils.getChildAsType(childName, model, childType);
        } catch (final ModelAdaptionException exception) {
          return false;
        }
        return true;
      }

      @Override
      public String getMessage() {
        return String.format("Has valid child resource '%s'.", childName);
      }

      @Override
      public ModelValidationMessageType getType() {
        return ERROR;
      }
    };
  }

  /**
   * Model Validator Bundle that checks if a specified child resource exists, and is valid.
   *
   * @param childName name of the child Resource to check for.
   * @param childType type of Model to attempt to adapt the child to. Must extend BaseResource.
   * @param model Model to add the validator to.
   * @param  Generic model type.
   * @param  Generic model type.
   * @return Model Validator Bundle that checks if a specified child resource exists, and is valid.
   */
  public static  ModelValidatorBundle hasValidChild(
      final String childName, final Class childType, final T model) {
    return new ModelValidatorBundle() {
      @Override
      public String getBundleMessage() {
        return "Has valid child " + childType.getSimpleName() + " '" + childName + "'";
      }

      @Override
      public void registerValidators() {
        addBasicValidator(hasChildResource(childName, model));
        addBasicValidator(isChildResourceValidResourceType(childName, childType, model));
      }

      @Override
      public ModelValidationMessageType getType() {
        return ERROR;
      }

      @Override
      public boolean isAllMustBeTrue() {
        return true;
      }
    };
  }

  /**
   * Builds a List of failed Error validators from a model.
   *
   * @param model model to build Validator List from.
   * @param  Generic type
   * @return List of failed Error validators from a model.
   */
  public static  List getFailedErrorValidators(
      @Nonnull final T model) {
    final List errorValidators = new ArrayList<>();
    for (final String errorMessage : model.getErrorMessages()) {
      final ModelValidator validator = new ModelValidator() {
        @Override
        public boolean isValid() {
          return false;
        }

        @Override
        public String getMessage() {
          return "Error validator failed for " + model.getPath() + ": " + errorMessage;
        }

        @Override
        public ModelValidationMessageType getType() {
          return ERROR;
        }
      };
      errorValidators.add(validator);
    }
    return errorValidators;
  }

  /**
   * Builds a List of failed Warning validators from a model.
   *
   * @param model model to build Validator List from.
   * @param  Generic type
   * @return List of failed Error Warning from a model.
   */
  public static  List getFailedWarningValidators(
      final T model) {
    final List warningValidators = new ArrayList<>();
    for (final String warningMessage : model.getWarningMessages()) {
      final ModelValidator validator = new ModelValidator() {
        @Override
        public boolean isValid() {
          return false;
        }

        @Override
        public String getMessage() {
          return "Warning validator failed for " + model.getPath() + ": " + warningMessage;
        }

        @Override
        public ModelValidationMessageType getType() {
          return WARNING;
        }
      };
      warningValidators.add(validator);
    }
    return warningValidators;
  }

  /**
   * Checks a list for null values.
   *
   * @param list List to check.
   * @param message Validation message.
   * @param type Validation error level.
   * @param  Generic type.
   * @return ModelValidator for null values in a specified list.
   */
  public static  ModelValidator listContainsNoNulls(@Nonnull final List list,
      @Nonnull final String message, @Nonnull final ModelValidationMessageType type) {
    return new ModelValidator() {
      @Override
      public boolean isValid() {
        for (final Object object : list) {
          if (object == null) {
            return false;
          }
        }
        return true;
      }

      @Override
      public String getMessage() {
        return message;
      }

      @Override
      public ModelValidationMessageType getType() {
        return type;
      }
    };
  }
}