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