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.timeblock.service;
017    
018    import java.util.Collections;
019    import java.util.Comparator;
020    import java.util.Iterator;
021    import java.util.List;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.kuali.hr.time.timeblock.TimeBlockHistory;
025    import org.kuali.rice.krad.bo.BusinessObject;
026    
027    public class TimeBlockHistoryLookupableHelperServiceImpl extends TimeBlockLookupableHelperServiceImpl {
028    
029        /**
030         *
031         */
032        private static final long serialVersionUID = 1L;
033    
034        @SuppressWarnings("unchecked")
035            @Override
036        public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) {
037    
038            String docStatus = "", beginDateString = "";
039    
040            if (fieldValues.containsKey(DOC_STATUS_ID)) {
041                docStatus = fieldValues.get(DOC_STATUS_ID);
042                fieldValues.remove(DOC_STATUS_ID);
043            }
044            if (fieldValues.containsKey(BEGIN_DATE_ID)) {
045                beginDateString = fieldValues.get(BEGIN_DATE_ID);
046                fieldValues.remove(BEGIN_DATE_ID);
047            }
048            List<TimeBlockHistory> objectList = (List<TimeBlockHistory>) super.getSearchResults(fieldValues);
049    
050            if (!objectList.isEmpty()) {
051                Iterator<TimeBlockHistory> itr = objectList.iterator();
052                while (itr.hasNext()) {
053                    TimeBlockHistory tb = itr.next();
054    
055                    if (StringUtils.isNotEmpty(docStatus)) {
056                        if (tb.getTimesheetDocumentHeader() == null) {
057                            itr.remove();
058                            continue;
059                        } else {
060                            if (tb.getTimesheetDocumentHeader().getDocumentStatus() != null) {
061                                if (!tb.getTimesheetDocumentHeader().getDocumentStatus().equals(docStatus)) {
062                                    itr.remove();
063                                    continue;
064                                }
065                            } else {
066                                itr.remove();
067                                continue;
068                            }
069                        }
070                    }
071                    if(StringUtils.isNotEmpty(beginDateString)) {
072                                            if(tb.getBeginDate() != null) {
073                                                    if(!this.checkDate(tb, tb.getBeginDate(), beginDateString)) {
074                                                            itr.remove();
075                                                            continue;
076                                                    } 
077                                            } else {
078                                                    itr.remove();
079                                                    continue;
080                                            }
081                                    }
082                }
083            }
084    
085            sortByTimeBlockId(objectList);
086    
087            return objectList;
088        }
089    
090        private void sortByTimeBlockId(List<TimeBlockHistory> objectList) {
091            Collections.sort(objectList, new Comparator<TimeBlockHistory>() { // Sort the Time Blocks
092                @Override
093                public int compare(TimeBlockHistory timeBlockHistory, TimeBlockHistory timeBlockHistory1) {
094                    return timeBlockHistory.getTkTimeBlockId().compareTo(timeBlockHistory1.getTkTimeBlockId());
095                }
096            });
097        }
098    }