1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.Comparator;
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.kuali.hr.time.util.TKUtils;
28 import org.kuali.rice.kns.lookup.HtmlData;
29 import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
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 KualiLookupableHelperServiceImpl{
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 Date currDate = TKUtils.getCurrentDate();
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.getEffectiveDate().before(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.getEffectiveDate().before(currDate)){
132 break;
133 }
134 else {
135 if(hrBusinessObject.getEffectiveDate().before(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.getEffectiveDate().before(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.getEffectiveDate().before(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 = new Long(0);
199
200 }
201
202 return new CollectionIncomplete(finalBusinessObjectList, matchingResultsCount);
203
204 }
205 @SuppressWarnings("rawtypes")
206 public class EffectiveDateTimestampCompare implements Comparator{
207
208 @Override
209 public int compare(Object arg0, Object arg1) {
210 HrBusinessObject hrBusinessObject = (HrBusinessObject)arg0;
211 HrBusinessObject hrBusinessObject2 = (HrBusinessObject)arg1;
212
213 java.sql.Date effDate1 = hrBusinessObject.getEffectiveDate();
214 java.sql.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 @Override
230 public List<HtmlData> getCustomActionUrls(BusinessObject businessObject,
231 @SuppressWarnings("rawtypes") List pkNames) {
232 List<HtmlData> customActionUrls = super.getCustomActionUrls(businessObject, pkNames);
233 List<HtmlData> overrideUrls = new ArrayList<HtmlData>();
234 for(HtmlData actionUrl : customActionUrls){
235 if(!StringUtils.equals(actionUrl.getMethodToCall(), "copy")){
236 overrideUrls.add(actionUrl);
237 }
238
239 }
240 return overrideUrls;
241 }
242
243
244
245
246 }