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.endow.batch.service.impl;
017    
018    import java.util.Collection;
019    import java.util.Date;
020    
021    import org.kuali.kfs.module.endow.batch.service.UpdateHistoryCashService;
022    import org.kuali.kfs.module.endow.businessobject.KemidCurrentCash;
023    import org.kuali.kfs.module.endow.businessobject.KemidHistoricalCash;
024    import org.kuali.kfs.module.endow.businessobject.MonthEndDate;
025    import org.kuali.kfs.module.endow.document.service.KEMService;
026    import org.kuali.kfs.module.endow.document.service.MonthEndDateService;
027    import org.kuali.kfs.module.endow.util.ValidateLastDayOfMonth;
028    import org.kuali.rice.kns.service.BusinessObjectService;
029    import org.kuali.rice.kns.util.KualiInteger;
030    
031    
032    public class UpdateHistoryCashServiceImpl implements UpdateHistoryCashService {
033        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UpdateHistoryCashServiceImpl.class);
034    
035        private KEMService kemService;
036        private BusinessObjectService businessObjectService;
037        private MonthEndDateService monthEndDateService;
038        
039        /**
040         * @see org.kuali.kfs.module.endow.batch.service.UpdateHistoryCashService#updateHistoryCash()
041         */
042        public boolean updateHistoryCash() {
043            Date currentDate = kemService.getCurrentDate();
044            if (ValidateLastDayOfMonth.validateLastDayOfMonth(currentDate)){
045                //Append a new record to the END_ME_DT_T TABLE
046                KualiInteger monthEndDateId = appendNewMonthEndDate(currentDate);
047                LOG.info("UpdateHistoryCash batch process has appended "+monthEndDateId+":"+currentDate+" to END_ME_DT_T table.");
048                //Append the records in the END_CRNT_CSH_T table to the END_HIST_CSH_T table
049                return appendNewHistoricalCashRecords(monthEndDateId); 
050            }
051            else {
052                LOG.info(currentDate+" is NOT the last day of the month. Update History Cash batch process will do nothing.");
053                return true;
054            }        
055        }    
056    
057        /**
058         * calculates the next month end date id using the month end date service and uses
059         * that value to insert a new record into the month end date table.
060         * 
061         * @param monthEndDate current date
062         * @return KualiInteger
063         */
064        protected KualiInteger appendNewMonthEndDate(Date monthEndDate){
065             KualiInteger newMonthEndDateId = monthEndDateService.getNextMonthEndIdForNewRecord();
066             MonthEndDate newMonthEndDate = new MonthEndDate();
067             newMonthEndDate.setMonthEndDateId(newMonthEndDateId);
068             newMonthEndDate.setMonthEndDate(new java.sql.Date(monthEndDate.getTime()));         
069             businessObjectService.save(newMonthEndDate);
070    
071             return newMonthEndDateId;
072        }
073        
074        /**
075         * For each current cash records, creates a record in historical cash table
076         * 
077         * @param monthEndDateId
078         * @return true
079         */
080        protected boolean appendNewHistoricalCashRecords(KualiInteger monthEndDateId){
081            Collection<KemidCurrentCash> kemidCurrentCashRecords = 
082                    businessObjectService.findAll(KemidCurrentCash.class);
083    
084            int counter = 0;
085            for (KemidCurrentCash kemidCurrentCashRecord : kemidCurrentCashRecords) {
086                KemidHistoricalCash newKemidHistoricalCash = new KemidHistoricalCash();            
087                newKemidHistoricalCash.setMonthEndDateId(monthEndDateId);
088                newKemidHistoricalCash.setKemid(kemidCurrentCashRecord.getKemid());
089                newKemidHistoricalCash.setHistoricalIncomeCash(kemidCurrentCashRecord.getCurrentIncomeCash());
090                newKemidHistoricalCash.setHistoricalPrincipalCash(kemidCurrentCashRecord.getCurrentPrincipalCash());        
091                businessObjectService.save(newKemidHistoricalCash);
092                counter++;            
093            }
094            LOG.info ("UpdateHistoryCash batch process has appended "+counter+" records to the END_HIST_CSH_T table.");
095    
096            return true;
097        }
098        
099        /**
100         * Sets the monthEndDateService.
101         * 
102         * @param monthEndDateService
103         */
104        public void setMonthEndDateService(MonthEndDateService monthEndDateService) {
105            this.monthEndDateService = monthEndDateService;
106        }
107        
108        /**
109         * Sets the kemService.
110         * 
111         * @param kemService
112         */
113        public void setKemService(KEMService kemService) {
114            this.kemService = kemService;
115        }
116        
117        /**
118         * Sets the businessObjectService.
119         * 
120         * @param businessObjectService
121         */
122        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
123            this.businessObjectService = businessObjectService;
124        }
125        
126        /**
127         * gets kemService
128         * @return kemService
129         */
130        protected KEMService getKemService() {
131            return kemService;
132        }
133    
134        /**
135         * gets businessObjectService
136         */
137        protected BusinessObjectService getBusinessObjectService() {
138            return businessObjectService;
139        }
140    
141        /**
142         * gets monthEndDateService
143         * @return monthEndDateService
144         */
145        protected MonthEndDateService getMonthEndDateService() {
146            return monthEndDateService;
147        }
148    }