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.service.impl;
017    
018    import java.util.ArrayList;
019    import java.util.Arrays;
020    import java.util.Collection;
021    import java.util.HashMap;
022    import java.util.HashSet;
023    import java.util.List;
024    import java.util.Map;
025    import java.util.Set;
026    
027    import org.apache.commons.lang.StringUtils;
028    import org.kuali.kfs.integration.ec.EffortCertificationReport;
029    import org.kuali.kfs.module.ec.EffortConstants;
030    import org.kuali.kfs.module.ec.EffortKeyConstants;
031    import org.kuali.kfs.module.ec.EffortPropertyConstants;
032    import org.kuali.kfs.module.ec.businessobject.EffortCertificationDocumentBuild;
033    import org.kuali.kfs.module.ec.businessobject.EffortCertificationReportDefinition;
034    import org.kuali.kfs.module.ec.businessobject.EffortCertificationReportEarnPaygroup;
035    import org.kuali.kfs.module.ec.businessobject.EffortCertificationReportPosition;
036    import org.kuali.kfs.module.ec.document.EffortCertificationDocument;
037    import org.kuali.kfs.module.ec.service.EffortCertificationReportDefinitionService;
038    import org.kuali.kfs.sys.KFSConstants;
039    import org.kuali.kfs.sys.KFSPropertyConstants;
040    import org.kuali.kfs.sys.MessageBuilder;
041    import org.kuali.rice.kns.service.BusinessObjectService;
042    import org.kuali.rice.kns.util.ObjectUtils;
043    
044    /**
045     * Provide the implementation of the service methods related to EffortCertificationReportDefinition
046     * 
047     * @see org.kuali.kfs.module.ec.businessobject.EffortCertificationReportDefinition
048     */
049    public class EffortCertificationReportDefinitionServiceImpl implements EffortCertificationReportDefinitionService {
050        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EffortCertificationReportDefinitionServiceImpl.class);
051    
052        private BusinessObjectService businessObjectService;
053    
054        /**
055         * @see org.kuali.kfs.module.ec.service.EffortCertificationReportDefinitionService#findReportDefinitionByPrimaryKey(java.util.Map)
056         */
057        public EffortCertificationReportDefinition findReportDefinitionByPrimaryKey(Map<String, String> fieldValues) {        
058            return (EffortCertificationReportDefinition) businessObjectService.findByPrimaryKey(EffortCertificationReportDefinition.class, fieldValues);
059        }
060        
061        /**
062         * @see org.kuali.kfs.module.ec.service.EffortCertificationReportDefinitionService#validateEffortCertificationReportDefinition(org.kuali.kfs.module.ec.businessobject.EffortCertificationReportDefinition)
063         */
064        public String validateEffortCertificationReportDefinition(EffortCertificationReportDefinition effortCertificationReportDefinition) {
065            Integer fiscalYear = effortCertificationReportDefinition.getUniversityFiscalYear();
066            String reportNumber = effortCertificationReportDefinition.getEffortCertificationReportNumber();
067    
068            // Fiscal Year is required
069            if (ObjectUtils.isNull(fiscalYear)) {
070                return MessageBuilder.buildMessage(EffortKeyConstants.ERROR_FISCAL_YEAR_MISSING, null).getMessage();
071            }
072    
073            // Report Number is required
074            if (StringUtils.isEmpty(reportNumber)) {
075                return MessageBuilder.buildMessage(EffortKeyConstants.ERROR_REPORT_NUMBER_MISSING, null).getMessage();
076            }
077            
078            String combinedFieldValues = new StringBuilder(fiscalYear.toString()).append(EffortConstants.VALUE_SEPARATOR).append(reportNumber).toString();
079    
080            // check if there exists the given report definition
081            effortCertificationReportDefinition = (EffortCertificationReportDefinition)businessObjectService.retrieve(effortCertificationReportDefinition);
082            if (effortCertificationReportDefinition == null) {
083                return MessageBuilder.buildMessage(EffortKeyConstants.ERROR_REPORT_DEFINITION_NOT_EXIST, combinedFieldValues).getMessage();
084            }
085    
086            // check if the given report definition is still active
087            if (!effortCertificationReportDefinition.isActive()) {
088                return MessageBuilder.buildMessage(EffortKeyConstants.ERROR_REPORT_DEFINITION_INACTIVE, combinedFieldValues).getMessage();
089            }
090    
091            return null;
092        }
093    
094        /**
095         * @see org.kuali.kfs.module.ec.service.EffortCertificationReportDefinitionService#findPositionObjectGroupCodes(org.kuali.kfs.module.ec.businessobject.EffortCertificationReportDefinition)
096         */
097        public List<String> findPositionObjectGroupCodes(EffortCertificationReportDefinition reportDefinition) {
098            Map<String, String> fieldValues = reportDefinition.buildKeyMapForCurrentReportDefinition();
099            fieldValues.put(KFSPropertyConstants.ACTIVE, Boolean.TRUE.toString());
100    
101            Collection<EffortCertificationReportPosition> reportPosition = businessObjectService.findMatching(EffortCertificationReportPosition.class, fieldValues);
102    
103            List<String> positionGroupCodes = new ArrayList<String>();
104            for (EffortCertificationReportPosition position : reportPosition) {
105                positionGroupCodes.add(position.getEffortCertificationReportPositionObjectGroupCode());
106            }
107    
108            return positionGroupCodes;
109        }
110    
111        /**
112         * @see org.kuali.kfs.module.ec.service.EffortCertificationReportDefinitionService#findReportEarnCodePayGroups(org.kuali.kfs.module.ec.businessobject.EffortCertificationReportDefinition)
113         */
114        public Map<String, Set<String>> findReportEarnCodePayGroups(EffortCertificationReportDefinition reportDefinition) {
115            Collection<EffortCertificationReportEarnPaygroup> reportEarnPay = this.findReportEarnPay(reportDefinition);
116            Map<String, Set<String>> earnCodePayGroups = new HashMap<String, Set<String>>();
117    
118            for (EffortCertificationReportEarnPaygroup earnPay : reportEarnPay) {
119                String payGroup = earnPay.getPayGroup();
120                String earnCode = earnPay.getEarnCode();
121    
122                if (earnCodePayGroups.containsKey(payGroup)) {
123                    Set<String> earnCodeSet = earnCodePayGroups.get(payGroup);
124                    earnCodeSet.add(earnCode);
125                }
126                else {
127                    Set<String> earnCodeSet = new HashSet<String>();
128                    earnCodeSet.add(earnCode);
129                    earnCodePayGroups.put(payGroup, earnCodeSet);
130                }
131            }
132    
133            return earnCodePayGroups;
134        }
135    
136        /**
137         * @see org.kuali.kfs.module.ec.service.EffortCertificationReportDefinitionService#findReportEarnPay(org.kuali.kfs.module.ec.businessobject.EffortCertificationReportDefinition)
138         */
139        public Collection<EffortCertificationReportEarnPaygroup> findReportEarnPay(EffortCertificationReportDefinition reportDefinition) {
140            Map<String, Object> fieldValues = new HashMap<String, Object>();
141            fieldValues.put(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, reportDefinition.getUniversityFiscalYear());
142            fieldValues.put(EffortPropertyConstants.EFFORT_CERTIFICATION_REPORT_TYPE_CODE, reportDefinition.getEffortCertificationReportTypeCode());
143            fieldValues.put(KFSPropertyConstants.ACTIVE, Boolean.TRUE.toString());
144    
145            return businessObjectService.findMatching(EffortCertificationReportEarnPaygroup.class, fieldValues);
146        }
147        
148        /**
149         * @see org.kuali.kfs.module.ec.service.EffortCertificationReportDefinitionService#existsPendingEffortCertification(org.kuali.kfs.module.ec.businessobject.EffortCertificationReportDefinition)
150         */
151        public boolean hasPendingEffortCertification(String emplid, EffortCertificationReportDefinition reportDefinition) {
152            Map<String, Object> fieldValues = new HashMap<String, Object>();
153            fieldValues.put(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, reportDefinition.getUniversityFiscalYear());
154            fieldValues.put(EffortPropertyConstants.EFFORT_CERTIFICATION_REPORT_NUMBER, reportDefinition.getEffortCertificationReportNumber());
155            
156            int numOfPendingDocuments = businessObjectService.countMatching(EffortCertificationDocumentBuild.class, fieldValues);
157            if(numOfPendingDocuments > 0) {
158                return true;
159            }
160            
161            List<String> pendingStatusCodes = Arrays.asList(KFSConstants.DocumentStatusCodes.ENROUTE);
162            fieldValues.put(KFSPropertyConstants.EMPLID, emplid);
163            fieldValues.put(KFSPropertyConstants.DOCUMENT_HEADER + "." + KFSPropertyConstants.FINANCIAL_DOCUMENT_STATUS_CODE, pendingStatusCodes);    
164            
165            return businessObjectService.countMatching(EffortCertificationDocument.class, fieldValues) > 0;
166        }
167        
168        /**
169         * @see org.kuali.kfs.module.ec.service.EffortCertificationReportDefinitionService#hasApprovedEffortCertification(java.lang.String, org.kuali.kfs.module.ec.businessobject.EffortCertificationReportDefinition)
170         */
171        public boolean hasApprovedEffortCertification(String emplid, EffortCertificationReportDefinition reportDefinition) {
172            Map<String, Object> fieldValues = new HashMap<String, Object>();        
173            fieldValues.put(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, reportDefinition.getUniversityFiscalYear());
174            fieldValues.put(EffortPropertyConstants.EFFORT_CERTIFICATION_REPORT_NUMBER, reportDefinition.getEffortCertificationReportNumber());
175            fieldValues.put(KFSPropertyConstants.EMPLID, emplid);
176            fieldValues.put(KFSPropertyConstants.DOCUMENT_HEADER + "." + KFSPropertyConstants.FINANCIAL_DOCUMENT_STATUS_CODE, KFSConstants.DocumentStatusCodes.APPROVED);    
177    
178            return businessObjectService.countMatching(EffortCertificationDocument.class, fieldValues) > 0;
179        }
180    
181        /**
182         * @see org.kuali.kfs.module.ec.service.EffortCertificationReportDefinitionService#hasBeenUsedForEffortCertificationGeneration(org.kuali.kfs.module.ec.businessobject.EffortCertificationReportDefinition)
183         */
184        public boolean hasBeenUsedForEffortCertificationGeneration(EffortCertificationReportDefinition reportDefinition) {
185            Map<String, Object> fieldValues = new HashMap<String, Object>();
186            fieldValues.put(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, reportDefinition.getUniversityFiscalYear());
187            fieldValues.put(EffortPropertyConstants.EFFORT_CERTIFICATION_REPORT_NUMBER, reportDefinition.getEffortCertificationReportNumber());
188    
189            return businessObjectService.countMatching(EffortCertificationDocument.class, fieldValues) > 0;
190        }
191        
192        /**
193         * @see org.kuali.kfs.module.ec.service.EffortCertificationReportDefinitionService#hasBeenUsedForEffortCertificationGeneration(java.lang.String, org.kuali.kfs.module.ec.businessobject.EffortCertificationReportDefinition)
194         */
195        public boolean hasBeenUsedForEffortCertificationGeneration(String emplid, EffortCertificationReport reportDefinition) {
196            Map<String, Object> fieldValues = new HashMap<String, Object>();
197            fieldValues.put(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, reportDefinition.getUniversityFiscalYear());
198            fieldValues.put(EffortPropertyConstants.EFFORT_CERTIFICATION_REPORT_NUMBER, reportDefinition.getEffortCertificationReportNumber());
199            fieldValues.put(KFSPropertyConstants.EMPLID, emplid);
200    
201            return businessObjectService.countMatching(EffortCertificationDocument.class, fieldValues) > 0;
202        }
203    
204        /**
205         * Sets the businessObjectService attribute value.
206         * 
207         * @param businessObjectService The businessObjectService to set.
208         */
209        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
210            this.businessObjectService = businessObjectService;
211        }
212    }