1 25 package org.ofbiz.shark.compare; 26 27 import java.io.Serializable ; 28 import java.util.Comparator ; 29 30 import org.enhydra.shark.api.client.wfmodel.WfAssignment; 31 import org.enhydra.shark.api.client.wfbase.BaseException; 32 33 40 public class FlexibleAssignmentComparator implements Comparator , Serializable { 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 62 public boolean equals(Object obj) { 63 return obj.equals(this); 64 } 65 66 69 public int compare(Object obj1, Object 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 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 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 a1 = null; 133 String a2 = null; 134 135 try { 136 a1 = as1.activity().key(); 137 a2 = as2.activity().key(); 138 } catch (BaseException e) { 139 throw new IllegalArgumentException ("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 r1 = null; 151 String r2 = null; 152 try { 153 r1 = as1.assignee().resource_key(); 154 r2 = as2.assignee().resource_key(); 155 } catch (BaseException e) { 156 throw new IllegalArgumentException ("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 ("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 ("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 ("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 |