001    /*
002     * Copyright 2011 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.kfs.module.ec.businessobject.lookup;
017    
018    import java.util.ArrayList;
019    import java.util.HashMap;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.kuali.kfs.module.ec.businessobject.DuplicateCertificationsReport;
024    import org.kuali.kfs.module.ec.businessobject.EffortCertificationDocumentBuild;
025    import org.kuali.kfs.sys.context.SpringContext;
026    import org.kuali.rice.kns.authorization.BusinessObjectRestrictions;
027    import org.kuali.rice.kns.bo.BusinessObject;
028    import org.kuali.rice.kns.lookup.CollectionIncomplete;
029    import org.kuali.rice.kns.lookup.HtmlData;
030    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
031    import org.kuali.rice.kns.service.LookupService;
032    import org.kuali.rice.kns.util.KNSConstants;
033    import org.kuali.rice.kns.web.struts.form.LookupForm;
034    
035    /**
036     * Searches for documents that are not approved.
037     */
038    public class DuplicateCertificationsLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
039    
040        /**
041         * @see org.kuali.rice.kns.lookup.LookupableHelperService#getSearchResults(java.util.Map)
042         */
043        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
044            LookupService lookupService = SpringContext.getBean(LookupService.class);
045            List<EffortCertificationDocumentBuild> reportList = new ArrayList<EffortCertificationDocumentBuild>(lookupService.findCollectionBySearch(EffortCertificationDocumentBuild.class, fieldValues));
046            
047            setBackLocation(fieldValues.get(KNSConstants.BACK_LOCATION));
048            setDocFormKey(fieldValues.get(KNSConstants.DOC_FORM_KEY));
049            setReferencesToRefresh(fieldValues.get(KNSConstants.REFERENCES_TO_REFRESH));
050            
051            return findEmployeesWithPayOnMoreThanOneEffortCertificationReport(reportList);
052        }
053        
054        @Override
055        public HtmlData getReturnUrl(BusinessObject businessObject, LookupForm lookupForm, List pkNames, BusinessObjectRestrictions businessObjectRestrictions) {
056            return getEmptyAnchorHtmlData();
057        }
058        
059        /**
060         * Returns the records for employees who appear on more than one report.
061         * 
062         * @param reportList
063         * @return
064         */
065        private List<DuplicateCertificationsReport> findEmployeesWithPayOnMoreThanOneEffortCertificationReport(List<EffortCertificationDocumentBuild> reportList) {
066            ArrayList<DuplicateCertificationsReport> returnResults = new ArrayList<DuplicateCertificationsReport>();
067            HashMap<String, List<String>> employeeIdReportNumberMap = findDuplicatesMap(reportList);
068            
069            //return only records for employees that appear on more than one report
070            for (EffortCertificationDocumentBuild effortCertificationDocumentBuild : reportList) {
071                String reportNumber = effortCertificationDocumentBuild.getEffortCertificationReportNumber();
072                String emplid = effortCertificationDocumentBuild.getEmplid();
073                
074                if ( employeeIdReportNumberMap.get(emplid).size() >1 ) {
075                    DuplicateCertificationsReport temp = new DuplicateCertificationsReport();
076                    
077                    temp.setEffortCertificationReportNumber(effortCertificationDocumentBuild.getEffortCertificationReportNumber());
078                    temp.setUniversityFiscalYear(effortCertificationDocumentBuild.getUniversityFiscalYear());
079                    temp.setEmplid(effortCertificationDocumentBuild.getEmplid());
080                    
081                    returnResults.add(temp);
082                }
083                
084            }
085            
086            return new CollectionIncomplete(returnResults, new Long(0));
087        }
088        
089        /**
090         * Returns a map of emplids and the effort certification report numbers they are associated with. To be used to determine which employees appear in more than one report.
091         * 
092         * @param reportList
093         * @return
094         */
095        private HashMap<String, List<String>> findDuplicatesMap(List<EffortCertificationDocumentBuild> reportList) {
096            HashMap<String, List<String>> employeeIdReportNumberMap = new HashMap<String, List<String>>();
097            
098            //build up map of emplid/ report number list 
099            for (EffortCertificationDocumentBuild effortCertificationDocumentBuild : reportList) {
100                String reportNumber = effortCertificationDocumentBuild.getEffortCertificationReportNumber();
101                String emplid = effortCertificationDocumentBuild.getEmplid();
102                
103                if (employeeIdReportNumberMap.containsKey(emplid)) {
104                    List<String> reportNumbers = employeeIdReportNumberMap.get(emplid);
105                    if (!reportNumbers.contains(reportNumber)) {
106                        reportNumbers.add(reportNumber);
107                    }
108                } else {
109                    List<String> reportNumbers = new ArrayList<String>();
110                    reportNumbers.add(reportNumber);
111                    employeeIdReportNumberMap.put(emplid, reportNumbers);
112                }
113            }
114            
115            return employeeIdReportNumberMap;
116        }
117    }
118    
119