Author: tchemit Date: 2010-05-23 21:00:50 +0200 (Sun, 23 May 2010) New Revision: 1931 Url: http://nuiton.org/repositories/revision/jaxx/1931 Log: add againstMe property in validator Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/field/CollectionUniqueKeyValidator.java Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/field/CollectionUniqueKeyValidator.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/field/CollectionUniqueKeyValidator.java 2010-05-23 13:35:09 UTC (rev 1930) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/field/CollectionUniqueKeyValidator.java 2010-05-23 19:00:50 UTC (rev 1931) @@ -55,18 +55,28 @@ * @since 1.5 */ protected String collectionFieldName; + /** * la liste des propriétés d'une entrée de la collection qui définit la * clef unique. */ protected String[] keys; + /** * Une propriété optionnelle pour valider que l'objet reflétée par cette * propriété ne viole pas l'intégrité de la clef unique. * Cela permet de valider l'unicité sans que l'objet soit dans la collection */ protected String againstProperty; + /** + * Une propriété optionnelle pour utiliser l'objet en cours de validation pour + * valider que l'objet reflétée par cette propriété ne viole pas l'intégrité de la clef unique. + * Cela permet de valider l'unicité sans que l'objet soit dans la collection + */ + protected boolean againstMe; + + /** * Lors de l'utilisation de la againstProperty et qu'un ne peut pas utiliser * le equals sur l'objet, on peut spécifier une expression pour exclure des * tests lors de la recherche de la violation de clef unique. @@ -85,6 +95,10 @@ return keys; } + public boolean getAgainstMe() { + return againstMe; + } + public void setKeys(String[] keys) { if (keys != null && keys.length == 1 && keys[0].indexOf(',') != -1) { this.keys = keys[0].split(","); @@ -109,6 +123,10 @@ this.againstIndexExpression = againstIndexExpression; } + public void setAgainstMe(boolean againstMe) { + this.againstMe = againstMe; + } + @Override public void validate(Object object) throws ValidationException { @@ -124,13 +142,14 @@ log.debug("collection found : " + col); } Object againstBean = againstProperty == null ? null : - getFieldValue(againstProperty, object); + getFieldValue(againstProperty, object); if (log.isDebugEnabled()) { log.debug("againtBean = " + againstBean); } Integer againstIndex = (Integer) (againstIndexExpression == null ? - -1 : getFieldValue(againstIndexExpression, object)); + -1 : + getFieldValue(againstIndexExpression, object)); if (againstIndex == null) { againstIndex = -1; } @@ -139,10 +158,19 @@ 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); + null : getUniqueKeyHashCode(againstBean); if (log.isDebugEnabled()) { log.debug("hash for new key " + againstHashCode); } @@ -168,7 +196,7 @@ // utilisation de againstBean if (againstIndex != -1) { if (index != againstIndex && - hash.equals(againstHashCode)) { + hash.equals(againstHashCode)) { // on a deja rencontre cette clef unique, // donc la validation a echouee answer = false; @@ -176,7 +204,7 @@ } } else { if (!againstBean.equals(o) && - hash.equals(againstHashCode)) { + hash.equals(againstHashCode)) { // on a deja rencontre cette clef unique, // donc la validation a echouee answer = false; @@ -246,7 +274,7 @@ if (!Collection.class.isInstance(obj)) { throw new ValidationException("field " + fieldName + - " is not a collection type! (" + obj.getClass() + ')'); + " is not a collection type! (" + obj.getClass() + ')'); } return (Collection<?>) obj; }