Author: tchemit Date: 2013-02-25 16:51:17 +0100 (Mon, 25 Feb 2013) New Revision: 466 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/466 Log: fixes #2008: [PROTOCOLE] Nom de protocole mal contr?\195?\180l?\195?\169 Added: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/TuttiCollectionUniqueKeyValidator.java Modified: trunk/tutti-ui-swing/src/main/resources/validators.xml Added: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/TuttiCollectionUniqueKeyValidator.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/TuttiCollectionUniqueKeyValidator.java (rev 0) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/TuttiCollectionUniqueKeyValidator.java 2013-02-25 15:51:17 UTC (rev 466) @@ -0,0 +1,132 @@ +package fr.ifremer.tutti.ui.swing.util; + +import com.opensymphony.xwork2.validator.ValidationException; +import org.nuiton.validator.xwork2.field.CollectionUniqueKeyValidator; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * To fix a bug (will be moved back to nuiton-validator) (see + * http://nuiton.org/issues/2545). + * + * TODO Remove this when using nuiton-validator 2.6.10 and reuse the basic validator. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0.1 + */ +public class TuttiCollectionUniqueKeyValidator extends CollectionUniqueKeyValidator { + + @Override + public void validateWhenNotSkip(Object object) throws ValidationException { + + if (keys == null || keys.length == 0) { + throw new ValidationException("no unique keys defined"); + } + + String fieldName = getFieldName(); + + Collection<?> col = getCollection(object); + + if (log.isDebugEnabled()) { + log.debug("collection found : " + col); + } + Object againstBean = againstProperty == null ? null : + getFieldValue(againstProperty, object); + + if (log.isDebugEnabled()) { + log.debug("againtBean = " + againstBean); + } + Integer againstIndex = (Integer) (againstIndexExpression == null ? + -1 : + getFieldValue(againstIndexExpression, object)); + if (againstIndex == null) { + againstIndex = -1; + } + + if (!againstMe && againstBean == null && col.size() < 2) { + // la liste ne contient pas deux entrées donc c'est valide + return; + } + + if (againstMe) { + // try on this object + againstBean = object; + if (log.isDebugEnabled()) { + log.debug("againtBean from me = " + againstBean); + } + } + + + boolean answer = true; + + Integer againstHashCode = againstBean == null ? + null : getUniqueKeyHashCode(againstBean); + if (log.isDebugEnabled()) { + log.debug("hash for new key " + againstHashCode); + } + + if (againstHashCode == null && nullValueSkipped) { + + // clef nulle, donc pas d'erreur vu que le flag a ete positionne + return; + } + List<Integer> hashCodes = new ArrayList<Integer>(); + + int index = 0; + for (Object o : col) { + Integer hash = getUniqueKeyHashCode(o); + + if (log.isDebugEnabled()) { + log.debug("hash for object " + o + " = " + hash); + } + + if (hash == null && nullValueSkipped) { + + // clef nulle, donc pas d'erreur vu que le flag a ete positionne + continue; + } + + if (againstBean == null) { + if (hashCodes.contains(hash)) { + + // on a deja rencontre cette clef unique, + // donc la validation a echouee + answer = false; + if (log.isDebugEnabled()) { + log.debug("Found same hashcode, not unique!"); + } + break; + } + } else { + // utilisation de againstBean + if (againstIndex != -1) { + if (index != againstIndex && + hash.equals(againstHashCode)) { + // on a deja rencontre cette clef unique, + // donc la validation a echouee + answer = false; + break; + } + } else { + if (!againstBean.equals(o) && + hash.equals(againstHashCode)) { + // on a deja rencontre cette clef unique, + // donc la validation a echouee + answer = false; + break; + } + } + } + // nouveau hashcode enregistre + hashCodes.add(hash); + // index suivant + index++; + } + + if (!answer) { + addFieldError(fieldName, object); + } + } +} Property changes on: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/TuttiCollectionUniqueKeyValidator.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/tutti-ui-swing/src/main/resources/validators.xml =================================================================== --- trunk/tutti-ui-swing/src/main/resources/validators.xml 2013-02-25 15:16:54 UTC (rev 465) +++ trunk/tutti-ui-swing/src/main/resources/validators.xml 2013-02-25 15:51:17 UTC (rev 466) @@ -42,7 +42,9 @@ <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/> <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/> - <validator name="collectionUniqueKey" class="org.nuiton.validator.xwork2.field.CollectionUniqueKeyValidator"/> + <!--TODO Remove this when using nuiton-validator 2.6.10 and reuse the basic validator.--> + <!--<validator name="collectionUniqueKey" class="org.nuiton.validator.xwork2.field.CollectionUniqueKeyValidator"/>--> + <validator name="collectionUniqueKey" class="fr.ifremer.tutti.ui.swing.util.TuttiCollectionUniqueKeyValidator"/> <validator name="fieldexpressionwithparams" class="org.nuiton.validator.xwork2.field.FieldExpressionWithParamsValidator"/> </validators>