fr.ird.observe.validation.validators.CommentNeededDtoValidator Maven / Gradle / Ivy
Show all versions of common-validation Show documentation
package fr.ird.observe.validation.validators;
/*-
* #%L
* ObServe Toolkit :: Common Validation
* %%
* Copyright (C) 2017 - 2020 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.google.common.collect.ImmutableSet;
import com.opensymphony.xwork2.validator.ValidationException;
import fr.ird.observe.dto.decoration.I18nDecoratorHelper;
import fr.ird.observe.dto.reference.ReferentialDtoReferenceAware;
import fr.ird.observe.dto.referential.ReferentialDto;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nuiton.validator.xwork2.field.NuitonFieldValidatorSupport;
import java.util.Collection;
import java.util.Objects;
import java.util.Set;
import static io.ultreia.java4all.i18n.I18n.n;
import static io.ultreia.java4all.i18n.I18n.t;
/**
* Ask a comment if some of referential used require one (see {@link ReferentialDto#isNeedComment()}.
*
* Created by tchemit on 18/10/2018.
*
* @author Tony Chemit - [email protected]
*/
public class CommentNeededDtoValidator extends NuitonFieldValidatorSupport {
private static final Logger log = LogManager.getLogger(CommentNeededDtoValidator.class);
private Set propertyNames;
private String bean;
public void setPropertyNames(String propertyNames) {
this.propertyNames = ImmutableSet.copyOf(Objects.requireNonNull(propertyNames).split(","));
}
public void setBean(String bean) {
this.bean = bean;
}
@Override
protected void validateWhenNotSkip(Object object) throws ValidationException {
String fieldName = Objects.requireNonNull(getFieldName());
String commentValue = (String) getFieldValue(fieldName, object);
if (commentValue != null && commentValue.length() > 0) {
return;
}
Object bean = object;
if (this.bean != null) {
bean = getFieldValue(this.bean, object);
if (bean == null) {
return;
}
}
boolean withMessage = getDefaultMessage() != null;
for (String propertyName : propertyNames) {
Object fieldValue = getFieldValue(propertyName, bean);
if (fieldValue instanceof Collection) {
// setDefaultMessage("");
boolean needComment = false;
for (Object o : (Collection) fieldValue) {
needComment = validateOne((ReferentialDtoReferenceAware) o);
if (needComment) {
break;
}
}
if (needComment) {
addError(fieldName, bean, n("observe.validation.comment.needed.for.multiple"), propertyName, withMessage);
}
} else {
boolean needComment = validateOne((ReferentialDtoReferenceAware) fieldValue);
if (needComment) {
addError(fieldName, bean, n("observe.validation.comment.needed.for.single"), propertyName, withMessage);
}
}
}
}
private boolean validateOne(ReferentialDtoReferenceAware fieldValue) {
return fieldValue != null && fieldValue.isNeedComment();
}
private void addError(String fieldName, Object object, String messageKey, String propertyName, boolean withMessage) {
if (withMessage) {
addFieldError(fieldName, object);
return;
}
String propertyLabel = t(I18nDecoratorHelper.getPropertyI18nKey(object.getClass(), propertyName));
setDefaultMessage(messageKey + "##" + propertyLabel);
log.info(String.format("[%s] Need a comment (%s).", fieldName, propertyLabel));
try {
addFieldError(fieldName, object);
} finally {
setDefaultMessage(null);
}
}
@Override
public String getValidatorType() {
return "commentNeeded";
}
}