Author: tchemit Date: 2010-02-26 19:39:10 +0100 (Fri, 26 Feb 2010) New Revision: 1751 Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/field/CollectionFieldExpressionValidator.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/field/CollectionUniqueKeyValidator.java Log: reformat code + add logs Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/field/CollectionFieldExpressionValidator.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/field/CollectionFieldExpressionValidator.java 2010-02-21 10:26:26 UTC (rev 1750) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/field/CollectionFieldExpressionValidator.java 2010-02-26 18:39:10 UTC (rev 1751) @@ -29,7 +29,8 @@ import org.apache.commons.lang.builder.HashCodeBuilder; /** - * Un validateur basé sur {@link FieldExpressionValidator} qui valide sur une collection de propriéte. + * Un validateur basé sur {@link FieldExpressionValidator} qui valide sur une + * collection de propriéte. * * @author chemit */ @@ -152,10 +153,12 @@ useLast = expressionForLast != null && !expressionForLast.trim().isEmpty(); if (useFirst && mode != Mode.ALL) { - throw new ValidationException("can only use expressionForFirst in mode ALL but was " + mode); + throw new ValidationException("can only use expressionForFirst in " + + "mode ALL but was " + mode); } if (useLast && mode != Mode.ALL) { - throw new ValidationException("can only use expressionForLast in mode ALL but was " + mode); + throw new ValidationException("can only use expressionForLast in " + + "mode ALL but was " + mode); } String fieldName = getFieldName(); 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-02-21 10:26:26 UTC (rev 1750) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/field/CollectionUniqueKeyValidator.java 2010-02-26 18:39:10 UTC (rev 1751) @@ -32,7 +32,8 @@ * Un validateur basé sur {@link FieldExpressionValidator} qui valide une clef * unique sur une collection. * <p/> - * Le {@link #fieldName} sert à récupérer la propriété de type de collection du bean. + * Le {@link #fieldName} sert à récupérer la propriété de type de collection du + * bean. * * @author chemit */ @@ -40,12 +41,13 @@ /** * pour indiquer la propriété qui contient la liste à valider. - * + * <p/> * Si cette prorpiété n'est pas renseignée alors on utilise la * {@link #getFieldName()} pour obtenir la collection. - * + * <p/> * Cela permet d'effectuer une validation si une collection mais portant * en fait sur un autre champs + * * @since 1.5 */ protected String collectionFieldName; @@ -62,8 +64,8 @@ protected String againstProperty; /** * 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 - * à exclure lors de la recherche de la violation de clef unique. + * 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. */ protected String againstIndexExpression; @@ -80,7 +82,7 @@ } public void setKeys(String[] keys) { - if (keys != null && keys.length == 1 && keys[0].indexOf(",") != -1) { + if (keys != null && keys.length == 1 && keys[0].indexOf(',') != -1) { this.keys = keys[0].split(","); } else { this.keys = keys; @@ -114,9 +116,17 @@ Collection<?> col = getCollection(object); - Object againstBean = againstProperty == null ? null : getFieldValue(againstProperty, object); + if (log.isDebugEnabled()) { + log.debug("collection found : " + col); + } + Object againstBean = againstProperty == null ? null : + getFieldValue(againstProperty, object); - Integer againstIndex = (Integer) (againstIndexExpression == null ? -1 : getFieldValue(againstIndexExpression, object)); + if (log.isDebugEnabled()) { + log.debug("againtBean = " + againstBean); + } + Integer againstIndex = (Integer) (againstIndexExpression == null ? + -1 : getFieldValue(againstIndexExpression, object)); if (againstIndex == null) { againstIndex = -1; } @@ -127,31 +137,42 @@ boolean answer = true; - Integer againstHashCode = againstBean == null ? null : getUniqueKeyHashCode(againstBean); - + Integer againstHashCode = againstBean == null ? + null : getUniqueKeyHashCode(againstBean); + if (log.isDebugEnabled()) { + log.debug("hash for new key " + againstHashCode); + } 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 (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)) { + 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)) { + if (!againstBean.equals(o) && + hash.equals(againstHashCode)) { // on a deja rencontre cette clef unique, // donc la validation a echouee answer = false; @@ -173,11 +194,13 @@ /** * Calcule pour une entrée donné, le hash de la clef unique * - * @param o l'entree de la collection dont on va calculer le hash de la clef unique + * @param o l'entree de la collection dont on va calculer le hash de + * la clef unique * @return le hashCode calclé de la clef unique sur l'entrée donné * @throws ValidationException if any pb to retreave properties values */ - protected Integer getUniqueKeyHashCode(Object o) throws ValidationException { + protected Integer getUniqueKeyHashCode(Object o) + throws ValidationException { // calcul du hash à la volée HashCodeBuilder builder = new HashCodeBuilder(); for (String key : this.keys) { @@ -189,10 +212,12 @@ /** * @param object the incoming object containing the collection to test - * @return the collection of the incoming object given by the fieldName property + * @return the collection of the incoming object given by the fieldName + * property * @throws ValidationException if any pb to retreave the collection */ - protected Collection<?> getCollection(Object object) throws ValidationException { + protected Collection<?> getCollection(Object object) + throws ValidationException { String fieldName = getCollectionFieldName(); if (fieldName == null || fieldName.trim().isEmpty()) { // on travaille directement sur le fieldName @@ -216,7 +241,8 @@ } if (!Collection.class.isInstance(obj)) { - throw new ValidationException("field " + fieldName + " is not a collection type! (" + obj.getClass() + ")"); + throw new ValidationException("field " + fieldName + + " is not a collection type! (" + obj.getClass() + ')'); } return (Collection<?>) obj; }