1   /*
2    * Created by IntelliJ IDEA.
3    * User: Mike
4    * Date: Aug 22, 2001
5    * Time: 3:37:34 PM
6    * To change template for new class use
7    * Source Code | Class Templates options (Tools | IDE Options).
8    */
9   package com.atlassian.core.ofbiz.comparators;
10  
11  import org.ofbiz.core.entity.GenericValue;
12  import org.apache.log4j.Category;
13  
14  public class OFBizFieldComparator implements java.util.Comparator
15  {
16      private static final Category log = Category.getInstance(OFBizFieldComparator.class);
17  
18      String fieldname;
19  
20      public OFBizFieldComparator(String fieldname)
21      {
22          this.fieldname = fieldname;
23      }
24  
25      public int compare(Object o1, Object o2)
26      {
27          try
28          {
29              GenericValue i1 = (GenericValue) o1;
30              GenericValue i2 = (GenericValue) o2;
31  
32              if (i1 == null && i2 == null)
33                  return 0;
34              else if (i2 == null) // any value is less than null
35                  return -1;
36              else if (i1 == null) // null is greater than any value
37                  return 1;
38              
39              String s1 = i1.getString(fieldname);
40              String s2 = i2.getString(fieldname);
41  
42              if (s1 == null && s2 == null)
43                  return 0;
44              else if (s2 == null) // any value is less than null
45                  return -1;
46              else if (s1 == null) // null is greater than any value
47                  return 1;
48              else
49                  return s1.compareToIgnoreCase(s2);
50          }
51          catch (Exception e)
52          {
53              log.error("Exception: " + e, e);
54          }
55          return 0;
56      }
57  }