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.gl.batch.service.impl;
017    
018    import java.io.File;
019    import java.text.ParseException;
020    import java.text.SimpleDateFormat;
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    import org.kuali.kfs.coa.businessobject.OrganizationReversion;
025    import org.kuali.kfs.coa.service.OrganizationReversionService;
026    import org.kuali.kfs.coa.service.PriorYearAccountService;
027    import org.kuali.kfs.gl.GeneralLedgerConstants;
028    import org.kuali.kfs.gl.batch.service.OrganizationReversionCategoryLogic;
029    import org.kuali.kfs.gl.batch.service.OrganizationReversionProcess;
030    import org.kuali.kfs.gl.batch.service.OrganizationReversionProcessService;
031    import org.kuali.kfs.gl.batch.service.OrganizationReversionUnitOfWorkService;
032    import org.kuali.kfs.gl.service.BalanceService;
033    import org.kuali.kfs.gl.service.OriginEntryService;
034    import org.kuali.kfs.sys.KFSConstants;
035    import org.kuali.kfs.sys.KFSKeyConstants;
036    import org.kuali.kfs.sys.context.SpringContext;
037    import org.kuali.kfs.sys.service.ReportWriterService;
038    import org.kuali.kfs.sys.service.impl.KfsParameterConstants;
039    import org.kuali.rice.kns.service.DateTimeService;
040    import org.kuali.rice.kns.service.KualiConfigurationService;
041    import org.kuali.rice.kns.service.ParameterService;
042    import org.kuali.rice.kns.service.PersistenceService;
043    import org.springframework.transaction.annotation.Transactional;
044    
045    /**
046     * The base implementation of OrganizationReversionProcessService
047     */
048    @Transactional
049    public class OrganizationReversionProcessServiceImpl implements OrganizationReversionProcessService {
050        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OrganizationReversionProcessServiceImpl.class);
051        
052        private static final String ORGANIZATION_REVERSION_PRIOR_YEAR_ACCOUNT_PROCESS_BEAN_NAME = "glOrganizationReversionPriorYearAccountProcess";
053        private static final String ORGANIZATION_REVERSION_CURRENT_YEAR_ACCOUNT_PROCESS_BEAN_NAME = "glOrganizationReversionCurrentYearAccountProcess";
054    
055        private static final String DATE_FORMAT = "yyyy-MM-dd";
056    
057        private ReportWriterService organizationReversionReportWriterService;
058        private ParameterService parameterService;
059        private KualiConfigurationService configurationService;
060    
061        /**
062         * Gets the organizationReversionReportWriterService attribute. 
063         * @return Returns the organizationReversionReportWriterService.
064         */
065        public ReportWriterService getOrganizationReversionReportWriterService() {
066            return organizationReversionReportWriterService;
067        }
068    
069        /**
070         * Sets the organizationReversionReportWriterService attribute value.
071         * @param organizationReversionReportWriterService The organizationReversionReportWriterService to set.
072         */
073        public void setOrganizationReversionReportWriterService(ReportWriterService organizationReversionReportWriterService) {
074            this.organizationReversionReportWriterService = organizationReversionReportWriterService;
075        }
076    
077        /**
078         * Runs the Organization Reversion Year End Process for the end of a fiscal year (ie, a process that
079         * runs before the fiscal year end, and thus uses current account, etc.)
080         * 
081         * @param outputGroup the origin entry group that this process should save entries to
082         * @param jobParameters the parameters used in the process
083         * @param organizationReversionCounts a Map of named statistics generated by running the process
084         * @see org.kuali.kfs.gl.batch.service.OrganizationReversionProcessService#organizationReversionProcessEndOfYear(org.kuali.kfs.gl.businessobject.OriginEntryGroup, java.util.Map, java.util.Map)
085         */
086        public void organizationReversionPriorYearAccountProcess(Map jobParameters, Map<String, Integer> organizationReversionCounts) {
087            if (LOG.isDebugEnabled()) {
088                LOG.debug("organizationReversionProcessEndOfYear() started");
089            }
090            OrganizationReversionProcess orp = SpringContext.getBean(OrganizationReversionProcess.class,OrganizationReversionProcessServiceImpl.ORGANIZATION_REVERSION_PRIOR_YEAR_ACCOUNT_PROCESS_BEAN_NAME);
091    
092            orp.organizationReversionProcess(jobParameters, organizationReversionCounts);
093            
094            writeReports(orp, jobParameters, organizationReversionCounts);
095        }
096    
097        /**
098         * Organization Reversion Year End Process for the beginning of a fiscal year (ie, the process as it runs
099         * after the fiscal year end, thus using prior year account, etc.)
100         * 
101         * @param jobParameters the parameters used in the process
102         * @param organizationReversionCounts a Map of named statistics generated by running the process
103         * @see org.kuali.kfs.gl.batch.service.OrganizationReversionProcessService#organizationReversionProcessBeginningOfYear(org.kuali.kfs.gl.businessobject.OriginEntryGroup, java.util.Map, java.util.Map)
104         */
105        public void organizationReversionCurrentYearAccountProcess(Map jobParameters, Map<String, Integer> organizationReversionCounts) {
106            if (LOG.isDebugEnabled()) {
107                LOG.debug("organizationReversionProcessEndOfYear() started");
108            }
109            OrganizationReversionProcess orp = SpringContext.getBean(OrganizationReversionProcess.class,OrganizationReversionProcessServiceImpl.ORGANIZATION_REVERSION_CURRENT_YEAR_ACCOUNT_PROCESS_BEAN_NAME);
110    
111            LOG.info("processing organization reversions for current year accounts");
112            orp.organizationReversionProcess(jobParameters, organizationReversionCounts);
113            
114            writeReports(orp, jobParameters, organizationReversionCounts);
115        }
116    
117        /**
118         * Returns a Map with the properly initialized parameters for an organization reversion job that is about to run
119         * @return a Map holding parameters for the job
120         * @see org.kuali.kfs.gl.batch.service.OrganizationReversionProcessService#getJobParameters()
121         */
122        public Map getJobParameters() {
123            // Get job parameters
124            Map jobParameters = new HashMap();
125            String strTransactionDate = getParameterService().getParameterValue(KfsParameterConstants.GENERAL_LEDGER_BATCH.class, GeneralLedgerConstants.ANNUAL_CLOSING_TRANSACTION_DATE_PARM);
126            jobParameters.put(KFSConstants.UNALLOC_OBJECT_CD, getParameterService().getParameterValue(OrganizationReversion.class, GeneralLedgerConstants.OrganizationReversionProcess.UNALLOC_OBJECT_CODE_PARM));
127            jobParameters.put(KFSConstants.BEG_BUD_CASH_OBJECT_CD, getParameterService().getParameterValue(OrganizationReversion.class, GeneralLedgerConstants.OrganizationReversionProcess.CARRY_FORWARD_OBJECT_CODE));
128            jobParameters.put(KFSConstants.FUND_BAL_OBJECT_CD, getParameterService().getParameterValue(KfsParameterConstants.GENERAL_LEDGER_BATCH.class, GeneralLedgerConstants.ANNUAL_CLOSING_FUND_BALANCE_OBJECT_CODE_PARM));
129            String strUniversityFiscalYear = getParameterService().getParameterValue(KfsParameterConstants.GENERAL_LEDGER_BATCH.class, GeneralLedgerConstants.ANNUAL_CLOSING_FISCAL_YEAR_PARM);
130    
131            try {
132                SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
133                java.util.Date jud = sdf.parse(strTransactionDate);
134                jobParameters.put(KFSConstants.TRANSACTION_DT, new java.sql.Date(jud.getTime()));
135            }
136            catch (ParseException e) {
137                throw new IllegalArgumentException("TRANSACTION_DT is an invalid date");
138            }
139            try {
140                jobParameters.put(KFSConstants.UNIV_FISCAL_YR, new Integer(strUniversityFiscalYear));
141            }
142            catch (NumberFormatException nfe) {
143                throw new IllegalArgumentException("UNIV_FISCAL_YR is an invalid year");
144            }
145            return jobParameters;
146        }
147        
148        /**
149         * 
150         * @param organizationReversionProcess
151         * @param jobParameters
152         * @param counts
153         */
154        public void writeReports(OrganizationReversionProcess organizationReversionProcess, Map jobParameters, Map<String, Integer> counts) {
155            // write job parameters
156            for (Object jobParameterKeyAsObject : jobParameters.keySet()) {
157                if (jobParameterKeyAsObject != null) {
158                    final String jobParameterKey = jobParameterKeyAsObject.toString();
159                    getOrganizationReversionReportWriterService().writeParameterLine("%32s %10s", jobParameterKey, jobParameters.get(jobParameterKey));
160                }
161            }
162            
163            // write statistics
164            getOrganizationReversionReportWriterService().writeStatisticLine("NUMBER OF GLBL RECORDS READ....: %10d", counts.get("balancesRead"));
165            getOrganizationReversionReportWriterService().writeStatisticLine("NUMBER OF GLBL RECORDS SELECTED: %10d", counts.get("balancesSelected"));
166            getOrganizationReversionReportWriterService().writeStatisticLine("NUMBER OF SEQ RECORDS WRITTEN..: %10d", counts.get("recordsWritten"));
167            getOrganizationReversionReportWriterService().pageBreak();
168            
169            // write ledger report
170            getOrganizationReversionReportWriterService().writeSubTitle(getConfigurationService().getPropertyString(KFSKeyConstants.MESSAGE_REPORT_YEAR_END_ORGANIZATION_REVERSION_LEDGER_TITLE_LINE));
171            organizationReversionProcess.writeLedgerSummaryReport(getOrganizationReversionReportWriterService());
172        }
173    
174        /**
175         * Sets the implementation of ParameterService to use
176         * @param parameterService an implementation of ParameterService
177         */
178        public void setParameterService(ParameterService parameterService) {
179            this.parameterService = parameterService;
180        }
181    
182        /**
183         * Sets the implementation of the KualiConfigurationService to use
184         * @param configurationService an implementation of the KualiConfigurationService
185         */
186        public void setConfigurationService(KualiConfigurationService configurationService) {
187            this.configurationService = configurationService;
188        }
189    
190        /**
191         * Gets the parameterService attribute. 
192         * @return Returns the parameterService.
193         */
194        public ParameterService getParameterService() {
195            return parameterService;
196        }
197    
198        /**
199         * Gets the configurationService attribute. 
200         * @return Returns the configurationService.
201         */
202        public KualiConfigurationService getConfigurationService() {
203            return configurationService;
204        }
205    }