package org.gcube.application.geoportal.common.model.legacy.report; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; @Getter @Setter @Slf4j public class ConstraintCheck { public static ConstraintCheck defaultFor(T toCheck,T defaultValue){ return new ConstraintCheck(toCheck).withDefault(defaultValue); } private T theObject; private String fieldLabel; private String message; private boolean error=false; private boolean mandatory; private T theDefault; private List additionalChecks=new ArrayList(); public ConstraintCheck(T theObject,Check...checks){ this.theObject=theObject; for(Check c:checks) additionalChecks.add(c); check(); } public ConstraintCheck withDefault(T theDefault){ this.theDefault=theDefault; return this; } public ConstraintCheck addChecks(Check...checks){ for(Check c:checks) additionalChecks.add(c); return this; } public ConstraintCheck(T theObject,String fieldLabel,Check...checks){ this.theObject=theObject; this.fieldLabel=fieldLabel; for(Check c:checks) additionalChecks.add(c); check(); } public T evaluate() { check(); T result=theObject; if(isError()) { result=theDefault; log.debug("Applying default {} for current value {}",theDefault,theObject); if(theDefault!=null && theDefault instanceof Collection) { Collection defaultCollection=(Collection) theDefault; ArrayList target=new ArrayList(defaultCollection); result=(T) target; } } return result; } private void check() { if(theObject==null) { this.setMessage(fieldLabel+" è un campo obbligatorio."); this.setError(true); }else if((theObject instanceof String) && ((String) theObject).isEmpty()) { this.setMessage(fieldLabel+" non può essere vuoto."); this.setError(true); }else if((theObject instanceof Collection)) { Collection toCheckCollection=(Collection) theObject; if(toCheckCollection.isEmpty()) { this.setMessage("La lista "+fieldLabel+" non può essere vuota."); this.setError(true); } Boolean containsError=false; Object[] array=toCheckCollection.toArray(new Object[toCheckCollection.size()]); for(int i=0;i