KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > shark > compare > FlexibleAssignmentComparator


1 /*
2  * $Id: FlexibleAssignmentComparator.java 5720 2005-09-13 03:10:59Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.shark.compare;
26
27 import java.io.Serializable JavaDoc;
28 import java.util.Comparator JavaDoc;
29
30 import org.enhydra.shark.api.client.wfmodel.WfAssignment;
31 import org.enhydra.shark.api.client.wfbase.BaseException;
32
33 /**
34  * Flexible WfAssignment Comparator - Sorting assignments by common fields
35  *
36  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
37  * @version $Rev: 5720 $
38  * @since 3.1
39  */

40 public class FlexibleAssignmentComparator implements Comparator JavaDoc, Serializable JavaDoc {
41
42     public static final int ASCENDING_ORDER = 0;
43     public static final int DESCENDING_ORDER = 1;
44
45     public static final int ORDER_BY_RESOURCE = 0;
46     public static final int ORDER_BY_PRIORITY = 1;
47     public static final int ORDER_BY_TIME = 2;
48     public static final int ORDER_BY_ACCEPT = 3;
49     public static final int ORDER_BY_ACTIVITY = 4;
50
51     protected int[] sort = { 0, 3, 1, 2, 4 };
52     protected int order = 0;
53
54     public FlexibleAssignmentComparator(int[] sort, int order) {
55         this.sort = sort;
56         this.order = order;
57     }
58
59     /**
60      * @see java.lang.Object#equals(java.lang.Object)
61      */

62     public boolean equals(Object JavaDoc obj) {
63         return obj.equals(this);
64     }
65
66     /**
67      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
68      */

69     public int compare(Object JavaDoc obj1, Object JavaDoc obj2) {
70         try {
71             WfAssignment as1 = (WfAssignment) obj1;
72             WfAssignment as2 = (WfAssignment) obj2;
73             if (as1 == null && as2 != null) {
74                 return (order == ASCENDING_ORDER ? -1 : 1);
75             }
76             if (as2 == null && as1 != null) {
77                 return (order == ASCENDING_ORDER ? 1 : -1);
78             }
79             if (as1 == null && as2 == null) {
80                 return 0;
81             }
82
83             for (int i = 0; i < sort.length; i++) {
84                 int compare = 0;
85                 switch (sort[i]) {
86                     case ORDER_BY_RESOURCE :
87                         compare = compareResource(as1, as2);
88                         break;
89                     case ORDER_BY_PRIORITY :
90                         compare = comparePriority(as1, as2);
91                         break;
92                     case ORDER_BY_TIME :
93                         compare = compareTime(as1, as2);
94                         break;
95                     case ORDER_BY_ACCEPT :
96                         compare = compareAccepted(as1, as2);
97                         break;
98                 }
99                 if (compare != 0) {
100                     return compare;
101                 }
102             }
103
104             // some default compares
105
if (!containsSort(ORDER_BY_RESOURCE)) {
106                 int compare = compareResource(as1, as2);
107                 if (compare != 0) {
108                     return compare;
109                 }
110             }
111             if (!containsSort(ORDER_BY_ACTIVITY)) {
112                 int compare = compareActivity(as1, as2);
113                 if (compare != 0) {
114                     return compare;
115                 }
116             }
117         } catch (Exception JavaDoc e) {
118             return 0;
119         }
120
121         return 0;
122     }
123
124     private boolean containsSort(int field) {
125         for (int i = 0; i < sort.length; i++) {
126             if (sort[i] == field) return true;
127         }
128         return false;
129     }
130
131     private int compareActivity(WfAssignment as1, WfAssignment as2) {
132         String JavaDoc a1 = null;
133         String JavaDoc a2 = null;
134
135         try {
136             a1 = as1.activity().key();
137             a2 = as2.activity().key();
138         } catch (BaseException e) {
139             throw new IllegalArgumentException JavaDoc("Unable to obtain activity from assignment");
140         }
141
142         if (order == ASCENDING_ORDER) {
143             return a1.compareTo(a2);
144         } else {
145             return a2.compareTo(a1);
146         }
147     }
148
149     private int compareResource(WfAssignment as1, WfAssignment as2) {
150         String JavaDoc r1 = null;
151         String JavaDoc r2 = null;
152         try {
153             r1 = as1.assignee().resource_key();
154             r2 = as2.assignee().resource_key();
155         } catch (BaseException e) {
156             throw new IllegalArgumentException JavaDoc("Unable to obtain resource from assignment");
157         }
158
159         if (order == ASCENDING_ORDER) {
160             return r1.compareTo(r2);
161         } else {
162             return r2.compareTo(r1);
163         }
164     }
165
166     private int comparePriority(WfAssignment as1, WfAssignment as2) {
167         short p1 = 0;
168         short p2 = 0;
169         try {
170             p1 = as1.activity().priority();
171             p2 = as2.activity().priority();
172         } catch (BaseException e) {
173             throw new IllegalArgumentException JavaDoc("Unable to obtain activity from assignment");
174         }
175
176         if (order == ASCENDING_ORDER) {
177             return (p1 < p2 ? -1 : (p1 == p2 ? 0 : 1));
178         } else {
179             return (p2 < p1 ? -1 : (p2 == p1 ? 0 : 1));
180         }
181     }
182
183     private int compareTime(WfAssignment as1, WfAssignment as2) {
184         long t1 = 0;
185         long t2 = 0;
186         try {
187             if ("open.not_running.not_started".equals(as1.activity().state())) {
188                 t1 = 0;
189             } else {
190                 t1 = as1.activity().last_state_time().time;
191             }
192             if ("open.not_running.not_started".equals(as2.activity().state())) {
193                 t2 = 0;
194             } else {
195                 t2 = as2.activity().last_state_time().time;
196             }
197         } catch (BaseException e) {
198             throw new IllegalArgumentException JavaDoc("Unable to obtain activity from assignment");
199         }
200
201         if (order == ASCENDING_ORDER) {
202             return (t1 < t2 ? -1 : (t1 == t2 ? 0 : 1));
203         } else {
204             return (t2 < t1 ? -1 : (t2 == t1 ? 0 : 1));
205         }
206     }
207
208     private int compareAccepted(WfAssignment as1, WfAssignment as2) {
209         boolean a1 = false;
210         boolean a2 = false;
211         try {
212             a1 = as1.get_accepted_status();
213             a2 = as2.get_accepted_status();
214         } catch (BaseException e) {
215             throw new IllegalArgumentException JavaDoc("Unable to get accepted status from assignment");
216         }
217
218         if (order == ASCENDING_ORDER) {
219             return (a1 && !a2 ? -1 : (a1 && a2 ? 0 : 1));
220         } else {
221             return (a2 && !a1 ? -1 : (a2 && a1 ? 0 : 1));
222         }
223     }
224 }
225
Popular Tags