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.batch.service.impl;
017    
018    import java.util.Collection;
019    import java.util.Map;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.kfs.module.ec.EffortConstants;
023    import org.kuali.kfs.module.ec.EffortKeyConstants;
024    import org.kuali.kfs.module.ec.EffortPropertyConstants;
025    import org.kuali.kfs.module.ec.batch.service.EffortCertificationCreateService;
026    import org.kuali.kfs.module.ec.businessobject.EffortCertificationDocumentBuild;
027    import org.kuali.kfs.module.ec.businessobject.EffortCertificationReportDefinition;
028    import org.kuali.kfs.module.ec.document.EffortCertificationDocument;
029    import org.kuali.kfs.module.ec.service.EffortCertificationDocumentService;
030    import org.kuali.kfs.module.ec.util.EffortCertificationParameterFinder;
031    import org.kuali.kfs.sys.KFSConstants;
032    import org.kuali.kfs.sys.KFSPropertyConstants;
033    import org.kuali.kfs.sys.MessageBuilder;
034    import org.kuali.rice.kns.service.BusinessObjectService;
035    import org.kuali.rice.kns.util.spring.Logged;
036    import org.springframework.transaction.annotation.Transactional;
037    
038    /**
039     * This Process creates effort certification documents from the temporary build table created by the batch process and routes effort
040     * certification documents to project directors, fiscal officers, and central workgroups. The process includes the following steps:
041     * 
042     * <li>construct an effort certification document from a temporary effort certification document; </li>
043     * <li>route each effort certification document; </li>
044     * <li>delete the temporary effort certification document after routing successfully. </li>
045     */
046    @Transactional
047    public class EffortCertificationCreateServiceImpl implements EffortCertificationCreateService {
048        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EffortCertificationCreateServiceImpl.class);
049    
050        private BusinessObjectService businessObjectService;
051        private EffortCertificationDocumentService effortCertificationDocumentService;
052    
053        /**
054         * @see org.kuali.kfs.module.ec.batch.service.EffortCertificationCreateService#create()
055         */
056        @Logged
057        public void create() {
058            Integer fiscalYear = EffortCertificationParameterFinder.getCreateReportFiscalYear();
059            String reportNumber = EffortCertificationParameterFinder.getCreateReportNumber();
060    
061            this.create(fiscalYear, reportNumber);
062        }
063    
064        /**
065         * @see org.kuali.kfs.module.ec.batch.service.EffortCertificationCreateService#create(java.lang.Integer, java.lang.String)
066         */
067        @Logged
068        public void create(Integer fiscalYear, String reportNumber) {
069            Map<String, String> fieldValues = EffortCertificationReportDefinition.buildKeyMap(fiscalYear, reportNumber);
070    
071            String errorMessage = this.validateReportDefintion(fieldValues);
072            if (StringUtils.isNotEmpty(errorMessage)) {
073                LOG.fatal(errorMessage);
074                throw new IllegalArgumentException(errorMessage);
075            }
076    
077            Collection<EffortCertificationDocumentBuild> documentsBuild = businessObjectService.findMatching(EffortCertificationDocumentBuild.class, fieldValues);
078            for (EffortCertificationDocumentBuild documentBuild : documentsBuild) {          
079                boolean isCreated = effortCertificationDocumentService.createAndRouteEffortCertificationDocument(documentBuild);
080                
081                if(isCreated) {
082                    businessObjectService.delete(documentBuild);
083                }
084            }
085        }
086    
087        /**
088         * check if a report has been defined and its docuemnts have not been generated. The combination of fiscal year and report
089         * number can determine a report definition.
090         * 
091         * @param fieldValues the map containing fiscalYear and report number
092         * @return a message if a report has not been defined or its documents have been gerenated; otherwise, return null
093         */
094        protected String validateReportDefintion(Map<String, String> fieldValues) {
095            String fiscalYear = fieldValues.get(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
096            String reportNumber = fieldValues.get(EffortPropertyConstants.EFFORT_CERTIFICATION_REPORT_NUMBER);
097            String combinedFieldValues = new StringBuilder(fiscalYear).append(EffortConstants.VALUE_SEPARATOR).append(reportNumber).toString();
098    
099            // Fiscal Year is required
100            if (StringUtils.isEmpty(fiscalYear)) {
101                return MessageBuilder.buildMessage(EffortKeyConstants.ERROR_FISCAL_YEAR_MISSING, null).getMessage();
102            }
103    
104            // Report Number is required
105            if (StringUtils.isEmpty(reportNumber)) {
106                return MessageBuilder.buildMessage(EffortKeyConstants.ERROR_REPORT_NUMBER_MISSING, null).getMessage();
107            }
108    
109            // check if a report has been defined
110            EffortCertificationReportDefinition reportDefinition = (EffortCertificationReportDefinition) businessObjectService.findByPrimaryKey(EffortCertificationReportDefinition.class, fieldValues);
111            if (reportDefinition == null) {
112                return MessageBuilder.buildMessage(EffortKeyConstants.ERROR_FISCAL_YEAR_OR_REPORT_NUMBER_INVALID, combinedFieldValues).getMessage();
113            }
114    
115            // check if the selected report definition is still active
116            if (!reportDefinition.isActive()) {
117                return MessageBuilder.buildMessage(EffortKeyConstants.ERROR_REPORT_DEFINITION_INACTIVE, combinedFieldValues).getMessage();
118            }
119               
120            // check if the report period of the selected report definition is open. If not, throws an error message
121            if (!KFSConstants.PeriodStatusCodes.OPEN.equals(reportDefinition.getEffortCertificationReportPeriodStatusCode())) {
122                return MessageBuilder.buildMessage(EffortKeyConstants.ERROR_REPORT_DEFINITION_PERIOD_NOT_OPENED, combinedFieldValues).getMessage();
123            }
124    
125            // check if any document has been generated for the selected report definition. If so, return with an error message
126            int countOfDocuments = businessObjectService.countMatching(EffortCertificationDocument.class, fieldValues);
127            if (countOfDocuments > 0) {
128                return MessageBuilder.buildMessageWithPlaceHolder(EffortKeyConstants.ERROR_REPORT_DOCUMENT_EXIST, reportNumber, fiscalYear).getMessage();
129            }
130    
131            // check if any document build has been generated for the selected report definition. If not, return with an error message
132            int countOfDocumentsBuild = businessObjectService.countMatching(EffortCertificationDocumentBuild.class, fieldValues);
133            if (countOfDocumentsBuild <= 0) {
134                return MessageBuilder.buildMessageWithPlaceHolder(EffortKeyConstants.ERROR_REPORT_DOCUMENT_BUILD_NOT_EXIST, reportNumber, fiscalYear).getMessage();
135            }
136    
137            return null;
138        }
139    
140        /**
141         * Sets the businessObjectService attribute value.
142         * 
143         * @param businessObjectService The businessObjectService to set.
144         */
145        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
146            this.businessObjectService = businessObjectService;
147        }
148    
149        /**
150         * Sets the effortCertificationDocumentService attribute value.
151         * @param effortCertificationDocumentService The effortCertificationDocumentService to set.
152         */
153        public void setEffortCertificationDocumentService(EffortCertificationDocumentService effortCertificationDocumentService) {
154            this.effortCertificationDocumentService = effortCertificationDocumentService;
155        }
156    }