001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time;
017    
018    import java.io.Serializable;
019    import java.util.ArrayList;
020    import java.util.Collections;
021    import java.util.Comparator;
022    import java.util.Date;
023    import java.util.HashMap;
024    import java.util.List;
025    import java.util.Map;
026    
027    import org.apache.commons.lang.StringUtils;
028    import org.kuali.hr.time.util.TKUtils;
029    import org.kuali.rice.kns.lookup.HtmlData;
030    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
031    import org.kuali.rice.kns.lookup.LookupUtils;
032    import org.kuali.rice.krad.bo.BusinessObject;
033    import org.kuali.rice.krad.lookup.CollectionIncomplete;
034    
035    public abstract class HrEffectiveDateActiveLookupableHelper extends KualiLookupableHelperServiceImpl{
036    
037            /**
038             * 
039             */
040            private static final long serialVersionUID = 1L;
041    
042            @SuppressWarnings("unchecked")
043            @Override
044            /**
045             * Core HR Effective dating lookup logic
046             * alter at your own risk
047             */
048            public List<? extends BusinessObject> getSearchResults(
049                            Map<String, String> fieldValues) {
050                    if (fieldValues.containsKey("workArea")
051                                    && StringUtils.equals(fieldValues.get("workArea"), "%")) {
052                            fieldValues.put("workArea", "");
053                    }
054                    if (fieldValues.containsKey("jobNumber")
055                                    && StringUtils.equals(fieldValues.get("jobNumber"), "%")) {
056                            fieldValues.put("jobNumber", "");
057                    }
058                    if (fieldValues.containsKey("dept")
059                                    && StringUtils.equals(fieldValues.get("dept"), "%")) {
060                            fieldValues.put("dept", "");
061                    }
062                    if (fieldValues.containsKey("principalId")
063                                    && StringUtils.equals(fieldValues.get("principalId"), "%")) {
064                            fieldValues.put("principalId", "");
065                    }               
066                    String showHistory = "Y";
067                    if (fieldValues.containsKey("history")) {
068                            showHistory = fieldValues.get("history");
069                            fieldValues.remove("history");
070                    }
071                    String active = "";
072                    if(fieldValues.containsKey("active")){
073                            active = fieldValues.get("active");
074                            fieldValues.put("active", "");
075                    }
076                    
077                    List<HrBusinessObject> hrObjList = (List<HrBusinessObject>)super.getSearchResults(fieldValues);
078                    //Create a principalId+jobNumber map as this is the unique key for results
079                    Map<String,List<HrBusinessObject>> hrBusinessMap = new HashMap<String,List<HrBusinessObject>>();
080                    
081                    for(HrBusinessObject hrObj : hrObjList){
082                            String key = hrObj.getUniqueKey();
083                            
084                            //If no key exists for this business object return full collection
085                            if(StringUtils.isEmpty(key)){
086                                    return hrObjList;
087                            }
088                            if(hrBusinessMap.get(key)!= null){
089                                    List<HrBusinessObject> lstHrBusinessList = hrBusinessMap.get(key);
090                                    lstHrBusinessList.add(hrObj);
091                            } else {
092                                    List<HrBusinessObject> lstHrBusinessObj = new ArrayList<HrBusinessObject>();
093                                    lstHrBusinessObj.add(hrObj);
094                                    hrBusinessMap.put(key, lstHrBusinessObj);
095                            }
096                    }
097                    
098                    List<BusinessObject> finalBusinessObjectList = new ArrayList<BusinessObject>();
099                    
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    }