fr.ird.observe.validation.validators.IdDtoReferentialDisabledFieldValidationSupport Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-validation Show documentation
Show all versions of common-validation Show documentation
ObServe Toolkit Common Validation module
package fr.ird.observe.validation.validators;
/*
* #%L
* ObServe Toolkit :: Common Validation
* %%
* Copyright (C) 2017 - 2019 IRD, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* .
* #L%
*/
import com.opensymphony.xwork2.validator.ValidationException;
import fr.ird.observe.dto.decoration.I18nDecoratorHelper;
import fr.ird.observe.dto.reference.ReferentialDtoReference;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nuiton.validator.NuitonValidatorScope;
import org.nuiton.validator.xwork2.field.NuitonFieldValidatorSupport;
import java.util.Collection;
import java.util.Objects;
import static io.ultreia.java4all.i18n.I18n.n;
import static io.ultreia.java4all.i18n.I18n.t;
/**
* Created by tchemit on 17/10/2018.
*
* @author Tony Chemit - [email protected]
*/
public abstract class IdDtoReferentialDisabledFieldValidationSupport extends NuitonFieldValidatorSupport {
private static final Logger log = LogManager.getLogger(IdDtoReferentialDisabledFieldValidationSupport.class);
private final NuitonValidatorScope scope;
private String referenceType;
private String message;
IdDtoReferentialDisabledFieldValidationSupport(NuitonValidatorScope scope) {
this.scope = scope;
super.setSkip("validationDisabledReferentialScope != \"" + Objects.requireNonNull(scope).name() + "\"");
}
@Override
public void setSkip(String skip) {
throw new IllegalStateException("Can't use this parameter!");
}
@Override
protected void validateWhenNotSkip(Object object) throws ValidationException {
String fieldName = Objects.requireNonNull(getFieldName());
Object fieldValue = getFieldValue(fieldName, object);
if (fieldValue == null) {
return;
}
if (fieldValue instanceof Collection) {
if (message == null) {
super.setDefaultMessage(message = n("observe.validation.referential.multiple.disabled") + "##${referenceType}");
}
for (Object o : (Collection) fieldValue) {
validateOne(object, fieldName, o);
}
} else {
if (message == null) {
super.setDefaultMessage(message = n("observe.validation.referential.single.disabled") + "##${referenceType}");
}
validateOne(object, fieldName, fieldValue);
}
}
private void validateOne(Object object, String fieldName, Object fieldValue) {
if (!(fieldValue instanceof ReferentialDtoReference)) {
throw new IllegalStateException(String.format("Can't validate field %s which is not a referential reference (%s)", fieldName, fieldValue));
}
ReferentialDtoReference referentialDtoReference = (ReferentialDtoReference) fieldValue;
if (referentialDtoReference.isEnabled()) {
return;
}
if (referenceType == null) {
referenceType = t(I18nDecoratorHelper.getTypeI18nKey(referentialDtoReference.getReferenceType()));
}
log.debug(String.format("[%s] Found a referential (%s) disabled.", scope, referentialDtoReference));
addFieldError(fieldName, object);
}
public String getReferenceType() {
return referenceType;
}
@Override
public String getValidatorType() {
return "idDtoReferentialDisabledFieldValidation";
}
}