1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.core.bo;
17
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.joda.time.LocalDate;
29 import org.kuali.kpme.core.lookup.KPMELookupableHelper;
30 import org.kuali.rice.kns.lookup.LookupUtils;
31 import org.kuali.rice.krad.bo.BusinessObject;
32 import org.kuali.rice.krad.lookup.CollectionIncomplete;
33
34 public abstract class HrEffectiveDateActiveLookupableHelper extends KPMELookupableHelper {
35
36
37
38
39 private static final long serialVersionUID = 1L;
40
41 @SuppressWarnings("unchecked")
42 @Override
43
44
45
46
47 public List<? extends BusinessObject> getSearchResults(
48 Map<String, String> fieldValues) {
49 if (fieldValues.containsKey("workArea")
50 && StringUtils.equals(fieldValues.get("workArea"), "%")) {
51 fieldValues.put("workArea", "");
52 }
53 if (fieldValues.containsKey("jobNumber")
54 && StringUtils.equals(fieldValues.get("jobNumber"), "%")) {
55 fieldValues.put("jobNumber", "");
56 }
57 if (fieldValues.containsKey("dept")
58 && StringUtils.equals(fieldValues.get("dept"), "%")) {
59 fieldValues.put("dept", "");
60 }
61 if (fieldValues.containsKey("principalId")
62 && StringUtils.equals(fieldValues.get("principalId"), "%")) {
63 fieldValues.put("principalId", "");
64 }
65 String showHistory = "Y";
66 if (fieldValues.containsKey("history")) {
67 showHistory = fieldValues.get("history");
68 fieldValues.remove("history");
69 }
70 String active = "";
71 if(fieldValues.containsKey("active")){
72 active = fieldValues.get("active");
73 fieldValues.put("active", "");
74 }
75
76 List<HrBusinessObject> hrObjList = (List<HrBusinessObject>)super.getSearchResults(fieldValues);
77
78 Map<String,List<HrBusinessObject>> hrBusinessMap = new HashMap<String,List<HrBusinessObject>>();
79
80 for(HrBusinessObject hrObj : hrObjList){
81 String key = hrObj.getUniqueKey();
82
83
84 if(StringUtils.isEmpty(key)){
85 return hrObjList;
86 }
87 if(hrBusinessMap.get(key)!= null){
88 List<HrBusinessObject> lstHrBusinessList = hrBusinessMap.get(key);
89 lstHrBusinessList.add(hrObj);
90 } else {
91 List<HrBusinessObject> lstHrBusinessObj = new ArrayList<HrBusinessObject>();
92 lstHrBusinessObj.add(hrObj);
93 hrBusinessMap.put(key, lstHrBusinessObj);
94 }
95 }
96
97 List<BusinessObject> finalBusinessObjectList = new ArrayList<BusinessObject>();
98
99 for(List<HrBusinessObject> lstHrBusinessObj: hrBusinessMap.values()){
100 Collections.sort(lstHrBusinessObj, new EffectiveDateTimestampCompare());
101 Collections.reverse(lstHrBusinessObj);
102 }
103
104
105 LocalDate currDate = LocalDate.now();
106
107
108 if(StringUtils.isEmpty(active) && StringUtils.equals("Y", showHistory)){
109 return hrObjList;
110 }
111
112
113 else if(StringUtils.isEmpty(active) && StringUtils.equals("N", showHistory)){
114 for(List<HrBusinessObject> lstHrBusiness : hrBusinessMap.values()){
115 for(HrBusinessObject hrBus : lstHrBusiness){
116 if(hrBus.getEffectiveLocalDate().isBefore(currDate)){
117 finalBusinessObjectList.add(hrBus);
118 break;
119 } else {
120 finalBusinessObjectList.add(hrBus);
121 }
122 }
123 }
124 }
125
126
127
128 else if(StringUtils.equals(active, "Y") && StringUtils.equals("N", showHistory)){
129 for(List<HrBusinessObject> lstHrBus : hrBusinessMap.values()){
130 for(HrBusinessObject hrBusinessObject : lstHrBus){
131 if(!hrBusinessObject.isActive() && hrBusinessObject.getEffectiveLocalDate().isBefore(currDate)){
132 break;
133 }
134 else {
135 if(hrBusinessObject.getEffectiveLocalDate().isBefore(currDate)){
136 finalBusinessObjectList.add(hrBusinessObject);
137 break;
138 } else {
139 if(hrBusinessObject.isActive()){
140 finalBusinessObjectList.add(hrBusinessObject);
141 }
142 }
143 }
144 }
145 }
146 }
147
148
149
150 else if(StringUtils.equals(active, "Y") && StringUtils.equals("Y", showHistory)){
151 for(List<HrBusinessObject> lstHrBus : hrBusinessMap.values()){
152 for(HrBusinessObject hrBus : lstHrBus){
153 if(!hrBus.isActive() && hrBus.getEffectiveLocalDate().isBefore(currDate)){
154 break;
155 }
156 else if(hrBus.isActive()){
157 finalBusinessObjectList.add(hrBus);
158 }
159 }
160 }
161 }
162
163
164 else if(StringUtils.equals(active, "N") && StringUtils.equals(showHistory, "Y")){
165 for(List<HrBusinessObject> lstHrBus : hrBusinessMap.values()){
166 for(HrBusinessObject hrBus : lstHrBus){
167 if(!hrBus.isActive()){
168 finalBusinessObjectList.add(hrBus);
169 }
170 }
171 }
172 }
173
174
175 else if(StringUtils.equals(active, "N") && StringUtils.equals(showHistory, "N")){
176 for(List<HrBusinessObject> lstHrBusiness : hrBusinessMap.values()){
177 for(HrBusinessObject hrBus : lstHrBusiness){
178 if(hrBus.getEffectiveLocalDate().isBefore(currDate)){
179 if(!hrBus.isActive()){
180 finalBusinessObjectList.add(hrBus);
181 }
182 break;
183 } else {
184 if(!hrBus.isActive()){
185 finalBusinessObjectList.add(hrBus);
186 }
187 }
188 }
189 }
190 }
191
192 Integer searchResultsLimit = LookupUtils.getSearchResultsLimit(businessObjectClass);
193
194 Long matchingResultsCount = Long.valueOf(finalBusinessObjectList.size());
195
196 if (matchingResultsCount.intValue() <= searchResultsLimit.intValue()) {
197
198 matchingResultsCount = Long.valueOf(0);
199
200 }
201
202 return new CollectionIncomplete(finalBusinessObjectList, matchingResultsCount);
203
204 }
205 @SuppressWarnings("rawtypes")
206 public static class EffectiveDateTimestampCompare implements Comparator, Serializable {
207
208 @Override
209 public int compare(Object arg0, Object arg1) {
210 HrBusinessObject hrBusinessObject = (HrBusinessObject)arg0;
211 HrBusinessObject hrBusinessObject2 = (HrBusinessObject)arg1;
212
213 Date effDate1 = hrBusinessObject.getEffectiveDate();
214 Date effDate2 = hrBusinessObject2.getEffectiveDate();
215 if (effDate1 == null ^ effDate2 == null) {
216 return (effDate1 == null) ? -1 : 1;
217 }
218 if (effDate1 == null && effDate2 == null) {
219 return 0;
220 }
221 int result = hrBusinessObject.getEffectiveDate().compareTo(hrBusinessObject2.getEffectiveDate());
222 if(result==0){
223 return hrBusinessObject.getTimestamp().compareTo(hrBusinessObject2.getTimestamp());
224 }
225 return result;
226 }
227
228 }
229
230 }