001    /**
002     * Copyright 2004-2012 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.util.ArrayList;
019    import java.util.Collections;
020    import java.util.Comparator;
021    import java.util.Date;
022    import java.util.HashMap;
023    import java.util.List;
024    import java.util.Map;
025    
026    import org.apache.commons.lang.StringUtils;
027    import org.kuali.hr.time.util.TKUtils;
028    import org.kuali.rice.kns.lookup.HtmlData;
029    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
030    import org.kuali.rice.kns.lookup.LookupUtils;
031    import org.kuali.rice.krad.bo.BusinessObject;
032    import org.kuali.rice.krad.lookup.CollectionIncomplete;
033    
034    public abstract class HrEffectiveDateActiveLookupableHelper extends KualiLookupableHelperServiceImpl{
035    
036            /**
037             * 
038             */
039            private static final long serialVersionUID = 1L;
040    
041            @SuppressWarnings("unchecked")
042            @Override
043            /**
044             * Core HR Effective dating lookup logic
045             * alter at your own risk
046             */
047            public List<? extends BusinessObject> getSearchResults(
048                            Map<String, String> fieldValues) {
049                    if (fieldValues.containsKey("workArea")
050                                    && StringUtils.equals(fieldValues.get("workArea"), "%")) {
051                            fieldValues.put("workArea", "");
052                    }
053                    if (fieldValues.containsKey("jobNumber")
054                                    && StringUtils.equals(fieldValues.get("jobNumber"), "%")) {
055                            fieldValues.put("jobNumber", "");
056                    }
057                    if (fieldValues.containsKey("dept")
058                                    && StringUtils.equals(fieldValues.get("dept"), "%")) {
059                            fieldValues.put("dept", "");
060                    }
061                    if (fieldValues.containsKey("principalId")
062                                    && StringUtils.equals(fieldValues.get("principalId"), "%")) {
063                            fieldValues.put("principalId", "");
064                    }               
065                    String showHistory = "Y";
066                    if (fieldValues.containsKey("history")) {
067                            showHistory = fieldValues.get("history");
068                            fieldValues.remove("history");
069                    }
070                    String active = "";
071                    if(fieldValues.containsKey("active")){
072                            active = fieldValues.get("active");
073                            fieldValues.put("active", "");
074                    }
075                    
076                    List<HrBusinessObject> hrObjList = (List<HrBusinessObject>)super.getSearchResults(fieldValues);
077                    //Create a principalId+jobNumber map as this is the unique key for results
078                    Map<String,List<HrBusinessObject>> hrBusinessMap = new HashMap<String,List<HrBusinessObject>>();
079                    
080                    for(HrBusinessObject hrObj : hrObjList){
081                            String key = hrObj.getUniqueKey();
082                            
083                            //If no key exists for this business object return full collection
084                            if(StringUtils.isEmpty(key)){
085                                    return hrObjList;
086                            }
087                            if(hrBusinessMap.get(key)!= null){
088                                    List<HrBusinessObject> lstHrBusinessList = hrBusinessMap.get(key);
089                                    lstHrBusinessList.add(hrObj);
090                            } else {
091                                    List<HrBusinessObject> lstHrBusinessObj = new ArrayList<HrBusinessObject>();
092                                    lstHrBusinessObj.add(hrObj);
093                                    hrBusinessMap.put(key, lstHrBusinessObj);
094                            }
095                    }
096                    
097                    List<BusinessObject> finalBusinessObjectList = new ArrayList<BusinessObject>();
098                    
099                    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    }