Coverage Report - org.kuali.rice.krad.util.BeanPropertyComparator
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanPropertyComparator
87%
41/47
100%
22/22
6.2
BeanPropertyComparator$1
100%
9/9
100%
4/4
6.2
BeanPropertyComparator$BeanComparisonException
100%
2/2
N/A
6.2
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.krad.util;
 17  
 
 18  
 import org.apache.commons.beanutils.PropertyUtils;
 19  
 import org.apache.commons.collections.comparators.ComparableComparator;
 20  
 import org.kuali.rice.core.api.exception.KualiException;
 21  
 import org.kuali.rice.core.api.util.type.TypeUtils;
 22  
 
 23  
 import java.beans.PropertyDescriptor;
 24  
 import java.io.Serializable;
 25  
 import java.lang.reflect.InvocationTargetException;
 26  
 import java.util.Collections;
 27  
 import java.util.Comparator;
 28  
 import java.util.Iterator;
 29  
 import java.util.List;
 30  
 
 31  
 /**
 32  
  * This class compares the two beans using multiple property names.
 33  
  * 
 34  
  * 
 35  
  */
 36  
 public class BeanPropertyComparator implements Comparator, Serializable {
 37  
     private static final long serialVersionUID = -2675700473766186018L;
 38  
     boolean ignoreCase;
 39  
     private List propertyNames;
 40  
     private Comparator stringComparator;
 41  
     private Comparator booleanComparator;
 42  
     private Comparator genericComparator;
 43  
 
 44  
     /**
 45  
      * Constructs a PropertyComparator for comparing beans using the properties named in the given List; if the List is null, the
 46  
      * beans will be compared directly (by Properties will be compared in the order in which they are listed. Case will be ignored
 47  
      * in String comparisons.
 48  
      * 
 49  
      * @param propertyNames List of property names (as Strings) used to compare beans
 50  
      */
 51  
     public BeanPropertyComparator(List propertyNames) {
 52  14
         this(propertyNames, true);
 53  12
     }
 54  
 
 55  
     /**
 56  
      * Constructs a PropertyComparator for comparing beans using the properties named in the given List. Properties will be compared
 57  
      * in the order in which they are listed. Case will be ignored if ignoreCase is true.
 58  
      * 
 59  
      * @param propertyNames List of property names (as Strings) used to compare beans
 60  
      * @param ignoreCase if true, case will be ignored during String comparisons
 61  
      */
 62  16
     public BeanPropertyComparator(List propertyNames, boolean ignoreCase) {
 63  16
         if (propertyNames == null) {
 64  1
             throw new IllegalArgumentException("invalid (null) propertyNames list");
 65  
         }
 66  15
         if (propertyNames.size() == 0) {
 67  1
             throw new IllegalArgumentException("invalid (empty) propertyNames list");
 68  
         }
 69  14
         this.propertyNames = Collections.unmodifiableList(propertyNames);
 70  14
         this.ignoreCase = ignoreCase;
 71  
 
 72  14
         if (ignoreCase) {
 73  13
             this.stringComparator = String.CASE_INSENSITIVE_ORDER;
 74  
         }
 75  
         else {
 76  1
             this.stringComparator = ComparableComparator.getInstance();
 77  
         }
 78  14
         this.booleanComparator = new Comparator() {
 79  
             public int compare(Object o1, Object o2) {
 80  9
                 int compared = 0;
 81  
 
 82  9
                 Boolean b1 = (Boolean) o1;
 83  9
                 Boolean b2 = (Boolean) o2;
 84  
 
 85  9
                 if (!b1.equals(b2)) {
 86  4
                     if (b1.equals(Boolean.FALSE)) {
 87  2
                         compared = -1;
 88  
                     }
 89  
                     else {
 90  2
                         compared = 1;
 91  
                     }
 92  
                 }
 93  
 
 94  9
                 return compared;
 95  
             }
 96  
 
 97  
         };
 98  14
         this.genericComparator = ComparableComparator.getInstance();
 99  14
     }
 100  
 
 101  
 
 102  
     /**
 103  
      * Compare two JavaBeans by the properties given to the constructor. If no propertues
 104  
      * 
 105  
      * @param o1 Object The first bean to get data from to compare against
 106  
      * @param o2 Object The second bean to get data from to compare
 107  
      * @return int negative or positive based on order
 108  
      */
 109  
     public int compare(Object o1, Object o2) {
 110  35
         int compared = 0;
 111  
 
 112  
         try {
 113  35
             for (Iterator i = propertyNames.iterator(); (compared == 0) && i.hasNext();) {
 114  49
                 String currentProperty = i.next().toString();
 115  
 
 116  
                 // choose appropriate comparator
 117  49
                 Comparator currentComparator = null;
 118  
                 try {
 119  49
                     PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(o1, currentProperty);
 120  49
                     Class propertyClass = propertyDescriptor.getPropertyType();
 121  47
                     if (propertyClass.equals(String.class)) {
 122  14
                         currentComparator = this.stringComparator;
 123  
                     }
 124  33
                     else if (TypeUtils.isBooleanClass(propertyClass)) {
 125  9
                         currentComparator = this.booleanComparator;
 126  
                     }
 127  
                     else {
 128  24
                         currentComparator = this.genericComparator;
 129  
                     }
 130  
                 }
 131  2
                 catch (NullPointerException e) {
 132  2
                     throw new BeanComparisonException("unable to find property '" + o1.getClass().getName() + "." + currentProperty + "'", e);
 133  47
                 }
 134  
 
 135  
                 // compare the values
 136  47
                 Object value1 = PropertyUtils.getProperty(o1, currentProperty);
 137  47
                 Object value2 = PropertyUtils.getProperty(o2, currentProperty);
 138  
                 /* Fix for KULRICE-5170 : BeanPropertyComparator throws exception when a null value is found in sortable non-string data type column */
 139  47
                 if ( value1 == null && value2 == null)
 140  2
                     return 0;
 141  45
                 else if ( value1 == null)
 142  2
                     return -1;
 143  43
                 else if ( value2 == null )
 144  2
                     return 1;
 145  
                 /* End KULRICE-5170 Fix*/
 146  41
                 compared = currentComparator.compare(value1, value2);
 147  40
             }
 148  
         }
 149  0
         catch (IllegalAccessException e) {
 150  0
             throw new BeanComparisonException("unable to compare property values", e);
 151  
         }
 152  0
         catch (NoSuchMethodException e) {
 153  0
             throw new BeanComparisonException("unable to compare property values", e);
 154  
         }
 155  0
         catch (InvocationTargetException e) {
 156  0
             throw new BeanComparisonException("unable to compare property values", e);
 157  26
         }
 158  
 
 159  26
         return compared;
 160  
     }
 161  
     
 162  
     public static class BeanComparisonException extends KualiException {
 163  
         private static final long serialVersionUID = 2622379680100640029L;
 164  
 
 165  
         /**
 166  
          * @param message
 167  
          * @param t
 168  
          */
 169  
         public BeanComparisonException(String message, Throwable t) {
 170  2
             super(message, t);
 171  2
         }
 172  
     }
 173  
 }