View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.hr.time;
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.kuali.hr.time.util.TKUtils;
29  import org.kuali.rice.kns.lookup.HtmlData;
30  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
31  import org.kuali.rice.kns.lookup.LookupUtils;
32  import org.kuali.rice.krad.bo.BusinessObject;
33  import org.kuali.rice.krad.lookup.CollectionIncomplete;
34  
35  public abstract class HrEffectiveDateActiveLookupableHelper extends KualiLookupableHelperServiceImpl{
36  
37  	/**
38  	 * 
39  	 */
40  	private static final long serialVersionUID = 1L;
41  
42  	@SuppressWarnings("unchecked")
43  	@Override
44  	/**
45  	 * Core HR Effective dating lookup logic
46  	 * alter at your own risk
47  	 */
48  	public List<? extends BusinessObject> getSearchResults(
49  			Map<String, String> fieldValues) {
50  		if (fieldValues.containsKey("workArea")
51  				&& StringUtils.equals(fieldValues.get("workArea"), "%")) {
52  			fieldValues.put("workArea", "");
53  		}
54  		if (fieldValues.containsKey("jobNumber")
55  				&& StringUtils.equals(fieldValues.get("jobNumber"), "%")) {
56  			fieldValues.put("jobNumber", "");
57  		}
58  		if (fieldValues.containsKey("dept")
59  				&& StringUtils.equals(fieldValues.get("dept"), "%")) {
60  			fieldValues.put("dept", "");
61  		}
62  		if (fieldValues.containsKey("principalId")
63  				&& StringUtils.equals(fieldValues.get("principalId"), "%")) {
64  			fieldValues.put("principalId", "");
65  		}		
66  		String showHistory = "Y";
67  		if (fieldValues.containsKey("history")) {
68  			showHistory = fieldValues.get("history");
69  			fieldValues.remove("history");
70  		}
71  		String active = "";
72  		if(fieldValues.containsKey("active")){
73  			active = fieldValues.get("active");
74  			fieldValues.put("active", "");
75  		}
76  		
77  		List<HrBusinessObject> hrObjList = (List<HrBusinessObject>)super.getSearchResults(fieldValues);
78  		//Create a principalId+jobNumber map as this is the unique key for results
79  		Map<String,List<HrBusinessObject>> hrBusinessMap = new HashMap<String,List<HrBusinessObject>>();
80  		
81  		for(HrBusinessObject hrObj : hrObjList){
82  			String key = hrObj.getUniqueKey();
83  			
84  			//If no key exists for this business object return full collection
85  			if(StringUtils.isEmpty(key)){
86  				return hrObjList;
87  			}
88  			if(hrBusinessMap.get(key)!= null){
89  				List<HrBusinessObject> lstHrBusinessList = hrBusinessMap.get(key);
90  				lstHrBusinessList.add(hrObj);
91  			} else {
92  				List<HrBusinessObject> lstHrBusinessObj = new ArrayList<HrBusinessObject>();
93  				lstHrBusinessObj.add(hrObj);
94  				hrBusinessMap.put(key, lstHrBusinessObj);
95  			}
96  		}
97  		
98  		List<BusinessObject> finalBusinessObjectList = new ArrayList<BusinessObject>();
99  		
100 		for(List<HrBusinessObject> lstHrBusinessObj: hrBusinessMap.values()){
101 			Collections.sort(lstHrBusinessObj, new EffectiveDateTimestampCompare());
102 			Collections.reverse(lstHrBusinessObj);
103 		}
104 		
105 		
106 		Date currDate = TKUtils.getCurrentDate();
107 		//Active = Both and Show History = Yes
108 		//return all results
109 		if(StringUtils.isEmpty(active) && StringUtils.equals("Y", showHistory)){
110 			return hrObjList;
111 		} 
112 		//Active = Both and show history = No
113 		//return the most effective results from today and any future rows
114 		else if(StringUtils.isEmpty(active) && StringUtils.equals("N", showHistory)){
115 			for(List<HrBusinessObject> lstHrBusiness : hrBusinessMap.values()){
116 				for(HrBusinessObject hrBus : lstHrBusiness){
117 					if(hrBus.getEffectiveDate().before(currDate)){
118 						finalBusinessObjectList.add(hrBus);
119 						break;
120 					} else {
121 						finalBusinessObjectList.add(hrBus);
122 					}
123 				}
124 			}
125 		}
126 		//Active = Yes and Show History = No
127 		//return all active records as of today and any active future rows
128 		//if there is an inactive record before the active one then do not show the results as this record is inactive
129 		else if(StringUtils.equals(active, "Y") && StringUtils.equals("N", showHistory)){
130 			for(List<HrBusinessObject> lstHrBus : hrBusinessMap.values()){
131 				for(HrBusinessObject hrBusinessObject : lstHrBus){
132 					if(!hrBusinessObject.isActive() && hrBusinessObject.getEffectiveDate().before(currDate)){
133 						break;
134 					}
135 					else {
136 						if(hrBusinessObject.getEffectiveDate().before(currDate)){
137 							finalBusinessObjectList.add(hrBusinessObject);
138 							break;
139 						} else {
140 							if(hrBusinessObject.isActive()){
141 								finalBusinessObjectList.add(hrBusinessObject);
142 							}
143 						}
144 					}
145 				}
146 			}			
147 		}
148 		//Active = Yes and Show History = Yes
149 		//return all active records from database
150 		//if there is an inactive record before the active one then do not show the results as this record is inactive
151 		else if(StringUtils.equals(active, "Y") && StringUtils.equals("Y", showHistory)){
152 			for(List<HrBusinessObject> lstHrBus : hrBusinessMap.values()){
153 				for(HrBusinessObject hrBus : lstHrBus){
154 					if(!hrBus.isActive() && hrBus.getEffectiveDate().before(currDate)){
155 						break;
156 					}
157 					else if(hrBus.isActive()){
158 						finalBusinessObjectList.add(hrBus);			
159 					}
160 				}
161 			}
162 		}
163 		//Active = No and Show History = Yes
164 		//return all inactive records in the database
165 		else if(StringUtils.equals(active, "N") && StringUtils.equals(showHistory, "Y")){
166 			for(List<HrBusinessObject> lstHrBus : hrBusinessMap.values()){
167 				for(HrBusinessObject hrBus : lstHrBus){
168 					if(!hrBus.isActive()){
169 						finalBusinessObjectList.add(hrBus);	
170 					}
171 				}
172 			}
173 		}
174 		//Active = No and Show History = No
175 		//return the most effective inactive rows if there are no active rows <= the curr date
176 		else if(StringUtils.equals(active, "N") && StringUtils.equals(showHistory, "N")){
177 			for(List<HrBusinessObject> lstHrBusiness : hrBusinessMap.values()){
178 				for(HrBusinessObject hrBus : lstHrBusiness){
179 					if(hrBus.getEffectiveDate().before(currDate)){
180 						if(!hrBus.isActive()){
181 							finalBusinessObjectList.add(hrBus);
182 						} 
183 						break;
184 					} else {
185 						if(!hrBus.isActive()){
186 							finalBusinessObjectList.add(hrBus);
187 						}
188 					}
189 				}
190 			}
191 		}
192 		
193 		Integer searchResultsLimit = LookupUtils.getSearchResultsLimit(businessObjectClass);
194 
195 		Long matchingResultsCount = Long.valueOf(finalBusinessObjectList.size());
196 
197 		if (matchingResultsCount.intValue() <= searchResultsLimit.intValue()) {
198 
199 		matchingResultsCount = Long.valueOf(0);
200 
201 		}
202 
203 		return new CollectionIncomplete(finalBusinessObjectList, matchingResultsCount);
204 
205 	}
206 	@SuppressWarnings("rawtypes")
207 	public static class EffectiveDateTimestampCompare implements Comparator, Serializable {
208 
209 		@Override
210 		public int compare(Object arg0, Object arg1) {
211             HrBusinessObject hrBusinessObject = (HrBusinessObject)arg0;
212 			HrBusinessObject hrBusinessObject2 = (HrBusinessObject)arg1;
213 
214             java.sql.Date effDate1 = hrBusinessObject.getEffectiveDate();
215             java.sql.Date effDate2 = hrBusinessObject2.getEffectiveDate();
216             if (effDate1 == null ^ effDate2 == null) {
217                 return (effDate1 == null) ? -1 : 1;
218             }
219             if (effDate1 == null && effDate2 == null) {
220                 return 0;
221             }
222 			int result = hrBusinessObject.getEffectiveDate().compareTo(hrBusinessObject2.getEffectiveDate());
223 			if(result==0){
224 				return hrBusinessObject.getTimestamp().compareTo(hrBusinessObject2.getTimestamp());
225 			}
226 			return result;
227 		}
228 		
229 	}
230 	@Override
231 	public List<HtmlData> getCustomActionUrls(BusinessObject businessObject,
232 			@SuppressWarnings("rawtypes") List pkNames) {
233 		List<HtmlData> customActionUrls = super.getCustomActionUrls(businessObject, pkNames);
234 		List<HtmlData> overrideUrls = new ArrayList<HtmlData>();
235 		for(HtmlData actionUrl : customActionUrls){
236 			if(!StringUtils.equals(actionUrl.getMethodToCall(), "copy")){
237 				overrideUrls.add(actionUrl);
238 			}
239 
240 		}
241 		return overrideUrls;
242 	}
243 	
244 	
245 	
246  
247 }