View Javadoc

1   /**
2    * Copyright 2004-2012 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.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  	 * Core HR Effective dating lookup logic
45  	 * alter at your own risk
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  		//Create a principalId+jobNumber map as this is the unique key for results
78  		Map<String,List<HrBusinessObject>> hrBusinessMap = new HashMap<String,List<HrBusinessObject>>();
79  		
80  		for(HrBusinessObject hrObj : hrObjList){
81  			String key = hrObj.getUniqueKey();
82  			
83  			//If no key exists for this business object return full collection
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 		//Active = Both and Show History = Yes
107 		//return all results
108 		if(StringUtils.isEmpty(active) && StringUtils.equals("Y", showHistory)){
109 			return hrObjList;
110 		} 
111 		//Active = Both and show history = No
112 		//return the most effective results from today and any future rows
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 		//Active = Yes and Show History = No
126 		//return all active records as of today and any active future rows
127 		//if there is an inactive record before the active one then do not show the results as this record is inactive
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 		//Active = Yes and Show History = Yes
148 		//return all active records from database
149 		//if there is an inactive record before the active one then do not show the results as this record is inactive
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 		//Active = No and Show History = Yes
163 		//return all inactive records in the database
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 		//Active = No and Show History = No
174 		//return the most effective inactive rows if there are no active rows <= the curr date
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 }