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.UpdateHoldingHistoryService;
022    import org.kuali.kfs.module.endow.businessobject.CurrentTaxLotBalance;
023    import org.kuali.kfs.module.endow.businessobject.HoldingHistory;
024    import org.kuali.kfs.module.endow.document.service.KEMService;
025    import org.kuali.kfs.module.endow.document.service.MonthEndDateService;
026    import org.kuali.kfs.module.endow.util.ValidateLastDayOfMonth;
027    import org.kuali.rice.kns.service.BusinessObjectService;
028    import org.kuali.rice.kns.util.KualiInteger;
029    import org.kuali.rice.kns.util.ObjectUtils;
030    
031    public class UpdateHoldingHistoryServiceImpl implements UpdateHoldingHistoryService {
032    
033        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UpdateHistoryCashServiceImpl.class);
034        private KEMService kemService;
035        private BusinessObjectService businessObjectService;
036        private MonthEndDateService monthEndDateService;
037    
038        /**
039         * @see org.kuali.kfs.module.endow.batch.service.UpdateHoldingHistoryService#updateHoldingHistory()
040         */
041        public boolean updateHoldingHistory() {
042            Date currentDate = kemService.getCurrentDate();
043            if (ValidateLastDayOfMonth.validateLastDayOfMonth(currentDate)){
044                KualiInteger monthEndDateId = monthEndDateService.getMonthEndId(currentDate);
045                if (ObjectUtils.isNotNull(monthEndDateId)){
046                    //Append the records in the END_CRNT_TAX_LOT_BAL_T table to the END_HLDG_HIST_T table
047                    return appendNewHoldingHistoryRecords(monthEndDateId);
048                }else {
049                    LOG.error(currentDate+" does not exist in END_MD_DT_T table. "+
050                            "Can NOT append the records in the END_CRNT_TAX_LOT_BAL_T table to the END_HLDG_HIST_T table ");
051                    return false;
052                }
053            }
054            else {            
055                LOG.info(currentDate+" is NOT the last day of the month. Update Holding History batch process will do nothing.");
056                return true;
057            }  
058        }
059        
060        
061        private boolean appendNewHoldingHistoryRecords(KualiInteger monthEndDateId){
062            Collection<CurrentTaxLotBalance> currentTaxLotBalanceRecords = 
063                    businessObjectService.findAll(CurrentTaxLotBalance.class);
064            int counter = 0;
065            for (CurrentTaxLotBalance currentTaxLotBalanceRecord : currentTaxLotBalanceRecords) {
066                HoldingHistory newHoldingHistory = new HoldingHistory();            
067                newHoldingHistory.setMonthEndDateId(monthEndDateId);
068                newHoldingHistory.setKemid(currentTaxLotBalanceRecord.getKemid());
069                newHoldingHistory.setSecurityId(currentTaxLotBalanceRecord.getSecurityId());
070                newHoldingHistory.setRegistrationCode(currentTaxLotBalanceRecord.getRegistrationCode());
071                newHoldingHistory.setLotNumber(currentTaxLotBalanceRecord.getLotNumber());
072                newHoldingHistory.setIncomePrincipalIndicator(currentTaxLotBalanceRecord.getIncomePrincipalIndicator());
073                newHoldingHistory.setUnits(currentTaxLotBalanceRecord.getUnits());
074                newHoldingHistory.setCost(currentTaxLotBalanceRecord.getCost());
075                newHoldingHistory.setEstimatedIncome(currentTaxLotBalanceRecord.getAnnualEstimatedIncome());
076                newHoldingHistory.setRemainderOfFYEstimatedIncome(currentTaxLotBalanceRecord.getRemainderOfFYEstimatedIncome());
077                newHoldingHistory.setNextFYEstimatedIncome(currentTaxLotBalanceRecord.getNextFYEstimatedIncome());
078                newHoldingHistory.setSecurityUnitVal(currentTaxLotBalanceRecord.getSecurityUnitVal());
079                newHoldingHistory.setAcquiredDate(currentTaxLotBalanceRecord.getAcquiredDate());
080                newHoldingHistory.setPriorAccrual(currentTaxLotBalanceRecord.getPriorAccrual());
081                newHoldingHistory.setCurrentAccrual(currentTaxLotBalanceRecord.getCurrentAccrual());
082                newHoldingHistory.setLastTransactionDate(currentTaxLotBalanceRecord.getLastTransactionDate());
083                newHoldingHistory.setMarketValue(currentTaxLotBalanceRecord.getMarketValue());
084                businessObjectService.save(newHoldingHistory);
085                counter++;            
086            }
087            LOG.info ("UpdateHoldingHistory batch process has appended "+counter+" records to the END_HLDG_HIST_T table.");
088            return true;
089      
090        }
091        
092        /**
093         * Sets the monthEndDateService.
094         * 
095         * @param monthEndDateService
096         */
097        public void setMonthEndDateService(MonthEndDateService monthEndDateService) {
098            this.monthEndDateService = monthEndDateService;
099        }
100        
101        /**
102         * Sets the kemService.
103         * 
104         * @param kemService
105         */
106        public void setKemService(KEMService kemService) {
107            this.kemService = kemService;
108        }
109        
110        /**
111         * Sets the businessObjectService.
112         * 
113         * @param businessObjectService
114         */
115        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
116            this.businessObjectService = businessObjectService;
117        }
118        
119    
120    }