]> git.argeo.org Git - gpl/argeo-slc.git/blob - cms/org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/utils/DistNodeViewerComparator.java
14119969f7a2c4d5372968921b2a30a4f18115a1
[gpl/argeo-slc.git] / cms / org.argeo.slc.client.ui.dist / src / org / argeo / slc / client / ui / dist / utils / DistNodeViewerComparator.java
1 package org.argeo.slc.client.ui.dist.utils;
2
3 import java.math.BigDecimal;
4 import java.util.Calendar;
5 import java.util.List;
6
7 import javax.jcr.Node;
8 import javax.jcr.PropertyType;
9 import javax.jcr.RepositoryException;
10 import javax.jcr.Value;
11 import javax.jcr.ValueFormatException;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.argeo.eclipse.ui.GenericTableComparator;
16 import org.argeo.slc.SlcException;
17 import org.eclipse.jface.viewers.Viewer;
18
19 /** Add ability to order by name version and version */
20 public class DistNodeViewerComparator extends GenericTableComparator {
21 private static final long serialVersionUID = -5966120108210992211L;
22
23 private final static Log log = LogFactory
24 .getLog(DistNodeViewerComparator.class);
25
26 // Jcr property type goes to 12
27 public final static int NAME_VERSION_TYPE = 100;
28 public final static int VERSION_TYPE = 101;
29
30 protected List<String> propertiesList;
31 protected List<Integer> propertyTypesList;
32 protected Integer propertyType;
33 protected String property;
34
35 private NameVersionComparator nvc = new NameVersionComparator();
36 private VersionComparator vc = new VersionComparator();
37
38 public DistNodeViewerComparator(int defaultColIndex, int defaultDirection,
39 List<String> propertiesList, List<Integer> propertyTypesList) {
40 super(defaultColIndex, defaultDirection);
41 this.propertiesList = propertiesList;
42 this.propertyTypesList = propertyTypesList;
43 this.propertyIndex = defaultColIndex;
44 this.propertyType = propertyTypesList.get(defaultColIndex);
45 this.property = propertiesList.get(defaultColIndex);
46 setColumn(defaultColIndex);
47 }
48
49 @Override
50 public int compare(Viewer viewer, Object e1, Object e2) {
51 int rc = 0;
52 long lc = 0;
53
54 try {
55 Node n1 = (Node) e1;
56 Node n2 = (Node) e2;
57
58 Value v1 = null;
59 Value v2 = null;
60 if (n1.hasProperty(property))
61 v1 = n1.getProperty(property).getValue();
62 if (n2.hasProperty(property))
63 v2 = n2.getProperty(property).getValue();
64
65 if (v2 == null && v1 == null)
66 return 0;
67 else if (v2 == null)
68 return -1;
69 else if (v1 == null)
70 return 1;
71
72 switch (propertyType) {
73 case NAME_VERSION_TYPE:
74 rc = nvc.compare(viewer, v1.getString(), v2.getString());
75 break;
76 case VERSION_TYPE:
77 rc = vc.compare(viewer, v1.getString(), v2.getString());
78 break;
79 case PropertyType.STRING:
80 rc = v1.getString().compareTo(v2.getString());
81 break;
82 case PropertyType.BOOLEAN:
83 boolean b1 = v1.getBoolean();
84 boolean b2 = v2.getBoolean();
85 if (b1 == b2)
86 rc = 0;
87 else
88 // we assume true is greater than false
89 rc = b1 ? 1 : -1;
90 break;
91 case PropertyType.DATE:
92 Calendar c1 = v1.getDate();
93 Calendar c2 = v2.getDate();
94 if (c1 == null || c2 == null)
95 log.trace("undefined date");
96 lc = c1.getTimeInMillis() - c2.getTimeInMillis();
97 if (lc < Integer.MIN_VALUE)
98 // rc = Integer.MIN_VALUE;
99 rc = -1;
100 else if (lc > Integer.MAX_VALUE)
101 // rc = Integer.MAX_VALUE;
102 rc = 1;
103 else
104 rc = (int) lc;
105 break;
106 case PropertyType.LONG:
107 long l1;
108 long l2;
109 // FIXME sometimes an empty string is set instead of the id
110 try {
111 l1 = v1.getLong();
112 } catch (ValueFormatException ve) {
113 l1 = 0;
114 }
115 try {
116 l2 = v2.getLong();
117 } catch (ValueFormatException ve) {
118 l2 = 0;
119 }
120
121 lc = l1 - l2;
122 if (lc < Integer.MIN_VALUE)
123 // rc = Integer.MIN_VALUE;
124 rc = -1;
125 else if (lc > Integer.MAX_VALUE)
126 // rc = Integer.MAX_VALUE;
127 rc = 1;
128 else
129 rc = (int) lc;
130 break;
131 case PropertyType.DECIMAL:
132 BigDecimal bd1 = v1.getDecimal();
133 BigDecimal bd2 = v2.getDecimal();
134 rc = bd1.compareTo(bd2);
135 break;
136 default:
137 throw new SlcException(
138 "Unimplemented comparaison for PropertyType "
139 + propertyType);
140 }
141
142 // If descending order, flip the direction
143 if (direction == DESCENDING) {
144 rc = -rc;
145 }
146
147 } catch (RepositoryException re) {
148 throw new SlcException("Unexpected error "
149 + "while comparing nodes", re);
150 }
151 return rc;
152 }
153
154 @Override
155 public void setColumn(int column) {
156 if (column == this.propertyIndex) {
157 // Same column as last sort; toggle the direction
158 direction = 1 - direction;
159 } else {
160 // New column; do a descending sort
161 this.propertyIndex = column;
162 this.propertyType = propertyTypesList.get(column);
163 this.property = propertiesList.get(column);
164 direction = ASCENDING;
165 }
166 }
167 }