
fr.ird.observe.validation.validators.CommentNeededDtoValidator Maven / Gradle / Ivy
package fr.ird.observe.validation.validators;
/*-
* #%L
* ObServe Toolkit :: Common Validation
* %%
* Copyright (C) 2017 - 2018 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;
public void setPropertyNames(String propertyNames) {
this.propertyNames = ImmutableSet.copyOf(Objects.requireNonNull(propertyNames).split(","));
}
@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;
}
for (String propertyName : propertyNames) {
Object fieldValue = getFieldValue(propertyName, object);
Class type = null;
if (fieldValue instanceof Collection) {
setDefaultMessage("");
boolean needComment = false;
for (Object o : (Collection) fieldValue) {
type = o.getClass();
needComment = validateOne((ReferentialDtoReferenceAware) o);
if (needComment) {
break;
}
}
if (needComment) {
addError(fieldName, object, n("observe.validation.comment.needed.for.multiple"), propertyName, type);
}
} else {
boolean needComment = validateOne((ReferentialDtoReferenceAware) fieldValue);
if (needComment) {
addError(fieldName, object, n("observe.validation.comment.needed.for.single"), propertyName, fieldValue.getClass());
}
}
}
}
private boolean validateOne(ReferentialDtoReferenceAware fieldValue) {
return fieldValue != null && fieldValue.isNeedComment();
}
private void addError(String fieldName, Object object, String messageKey, String propertyName, Class propertyType) {
String propertyLabel = t(I18nDecoratorHelper.getPropertyI18nKey(propertyType, 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";
}
}