io.kestros.commons.structuredslingmodels.services.impl.BaseValidationProviderService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kestros-structured-sling-models Show documentation
Show all versions of kestros-structured-sling-models Show documentation
Foundational classes for that can be used across any Sling instance. Includes base model types
and model interaction utilities.
/*
* 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.services.impl;
import io.kestros.commons.structuredslingmodels.BaseSlingModel;
import io.kestros.commons.structuredslingmodels.annotation.KestrosModel;
import io.kestros.commons.structuredslingmodels.services.ValidationProviderService;
import io.kestros.commons.structuredslingmodels.validation.ModelValidationMessageType;
import io.kestros.commons.structuredslingmodels.validation.ModelValidationService;
import io.kestros.commons.structuredslingmodels.validation.ModelValidator;
import io.kestros.commons.structuredslingmodels.validation.ModelValidatorBundle;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Provides uncached Model validation to {@link KestrosModel} instances.
*/
@Component(immediate = true,
service = ValidationProviderService.class,
property = "service.ranking:Integer=100")
public class BaseValidationProviderService implements ValidationProviderService {
private static final Logger LOG = LoggerFactory.getLogger(BaseValidationProviderService.class);
@Override
public List getValidators(@Nonnull final T model,
final ModelValidationService modelValidationService) {
if (modelValidationService != null) {
final List modelValidators = new ArrayList<>();
modelValidators.addAll(getBasicValidators(model, modelValidationService));
modelValidators.addAll(getDetailedValidators(model, modelValidationService));
return modelValidators;
}
return Collections.emptyList();
}
@Override
public List getBasicValidators(@Nonnull final T model,
final ModelValidationService modelValidationService) {
if (modelValidationService != null) {
if (modelValidationService.getBasicValidators().isEmpty()) {
modelValidationService.setModel(model);
modelValidationService.registerBasicValidators();
}
return modelValidationService.getBasicValidators();
}
return Collections.emptyList();
}
/**
* Retrieves detailed ModelValidators for a given Model. Model must extend {@link
* BaseSlingModel}.
*
* @param model Model to validate.
* @param modelValidationService ModelValidationService to pull ModelValidators from.
* @param Extends {@link BaseSlingModel}
* @return Detailed ModelValidators for a given Model. Model must extend {@link BaseSlingModel}.
*/
@Override
public List getDetailedValidators(
@Nonnull final T model, final ModelValidationService modelValidationService) {
if (modelValidationService != null) {
if (modelValidationService.getDetailedValidators().isEmpty()) {
modelValidationService.setModel(model);
modelValidationService.registerDetailedValidators();
}
return modelValidationService.getDetailedValidators();
}
return Collections.emptyList();
}
/**
* Performs basic validation for a specified Model.
*
* @param model Model to validate.
* @param modelValidationService ModelValidationService to retrieve {@link ModelValidator}
* registered as basic from.
* @param Extends {@link BaseSlingModel}.
*/
@Override
public void doBasicValidation(@Nonnull final T model,
final ModelValidationService modelValidationService) {
validateModelValidatorList(model, getBasicValidators(model, modelValidationService));
}
/**
* Performs detailed validation for a specified Model.
*
* @param model Model to validate.
* @param modelValidationService ModelValidationService to retrieve {@link ModelValidator}
* registered as basic and detailed from.
* @param Extends {@link BaseSlingModel}.
*/
@Override
public void doDetailedValidation(@Nonnull final T model,
final ModelValidationService modelValidationService) {
validateModelValidatorList(model, getBasicValidators(model, modelValidationService));
validateModelValidatorList(model, getDetailedValidators(model, modelValidationService));
}
/**
* Validates of list of validators against the current Model. If a validator bundle fails, child
* validators will not be performed.
*
* @param extends BaseSlingModel.
* @param model model to validate.
* @param validatorList List of validators to perform validation on.
*/
private void validateModelValidatorList(final T model,
final List validatorList) {
for (final ModelValidator validator : validatorList) {
if (validator instanceof ModelValidatorBundle) {
final ModelValidatorBundle validatorBundle = (ModelValidatorBundle) validator;
if (!validatorBundle.isValid()) {
addBasicValidatorMessagesToLists(model, validatorBundle);
}
} else if (!validator.isValid() && validator.getType() != null) {
addBasicValidatorMessagesToLists(model, validator);
}
}
}
@Override
@Nullable
@SuppressWarnings("unchecked")
public ModelValidationService getModelValidationService(
final T model) {
final Class extends BaseSlingModel> modelClass = model.getClass();
try {
if (modelClass.getAnnotation(KestrosModel.class) != null) {
return modelClass.getAnnotation(KestrosModel.class).validationService().newInstance();
} else {
if (modelClass.getSuperclass() != null) {
Class superClass = (Class) modelClass.getSuperclass();
return getModelValidationService(castModel(superClass));
}
}
} catch (final InstantiationException exception) {
LOG.warn("Unable to instantiate ModelValidationService {} for {}", modelClass.getAnnotation(
KestrosModel.class).validationService().getSimpleName(), modelClass.getSimpleName());
} catch (final IllegalAccessException exception) {
LOG.warn("Unable to retrieve ModelValidationService {} for {} due to IllegalAccessException",
modelClass.getAnnotation(KestrosModel.class).validationService().getSimpleName(),
modelClass.getSimpleName());
}
return null;
}
/**
* Performs a Model Validator, and adds the message to the appropriate message list. ( ERROR,
* WARNING, INFO).
*
* @param extends BaseSlingModel.
* @param model model to add basic validators to.
* @param validator Model validator to be performed.
*/
private void addBasicValidatorMessagesToLists(final T model,
final ModelValidator validator) {
String message = validator.getMessage();
final ModelValidationMessageType type = validator.getType();
if (validator instanceof ModelValidatorBundle) {
message = ((ModelValidatorBundle) validator).getBundleMessage();
}
switch (type) {
case ERROR:
model.addErrorMessage(message);
break;
case WARNING:
model.addWarningMessage(message);
break;
default:
model.addInfoMessage(message);
break;
}
}
private T castModel(Class clazz) throws IllegalAccessException, InstantiationException {
return clazz.newInstance();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy