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.List;
019    
020    import org.kuali.kfs.module.endow.EndowParameterKeyConstants;
021    import org.kuali.kfs.module.endow.batch.CurrentTaxLotBalanceUpdateStep;
022    import org.kuali.kfs.module.endow.batch.service.CurrentTaxLotBalanceUpdateService;
023    import org.kuali.kfs.module.endow.businessobject.CurrentTaxLotBalance;
024    import org.kuali.kfs.module.endow.businessobject.HoldingTaxLot;
025    import org.kuali.kfs.module.endow.document.service.CurrentTaxLotService;
026    import org.kuali.kfs.module.endow.document.service.HoldingTaxLotService;
027    import org.kuali.kfs.sys.service.impl.KfsParameterConstants;
028    import org.kuali.rice.kns.service.ParameterService;
029    import org.springframework.transaction.annotation.Transactional;
030    
031    /**
032     * This class implements the CurrentTaxLotBalanceUpdateService batch job.
033     */
034    @Transactional
035    public class CurrentTaxLotBalanceUpdateServiceImpl implements CurrentTaxLotBalanceUpdateService {
036        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CurrentTaxLotBalanceUpdateServiceImpl.class);
037        
038        protected ParameterService parameterService;
039        protected HoldingTaxLotService holdingTaxLotService;
040        protected CurrentTaxLotService currentTaxLotService;
041    
042        /**
043         * Constructs a CurrentTaxLotBalanceUpdateServiceImpl instance
044         */
045        public CurrentTaxLotBalanceUpdateServiceImpl() {
046            
047        }
048    
049        /**
050         * the system will compile the information in the Holding Tax Lot Table along with 
051         * calculations of market value and estimated income into a new table called the 
052         * Current Tax Lot Balances 
053         */
054        public boolean updateCurrentTaxLotBalances() {
055            LOG.info("updateCurrentTaxLotBalances() - Processed all HoldingTaxLot records started."); 
056            
057            //Step 1: remove all the records from END_CURR_TAX_LOT_BAL_T table
058            clearAllCurrentTaxLotRecords();
059               
060            //Step 2: Retrieve all HoldingTaxLot records
061            List<HoldingTaxLot> holdingTaxLots = holdingTaxLotService.getAllTaxLots();
062                
063            //process the records in the list
064            for (HoldingTaxLot holdingTaxLot : holdingTaxLots) {
065                CurrentTaxLotBalance currentTaxLotBalance = currentTaxLotService.copyHoldingTaxLotToCurrentTaxLotBalance(holdingTaxLot); 
066    
067                String securityId = currentTaxLotBalance.getSecurityId();
068    
069                currentTaxLotBalance.setHoldingMarketValue(currentTaxLotService.getHoldingMarketValue(holdingTaxLot, securityId));
070                currentTaxLotBalance.setSecurityUnitVal(currentTaxLotService.getCurrentTaxLotBalanceSecurityUnitValue(securityId));
071                currentTaxLotBalance.setAnnualEstimatedIncome(currentTaxLotService.getNextTwelveMonthsEstimatedValue(holdingTaxLot, securityId));
072                currentTaxLotBalance.setRemainderOfFYEstimatedIncome(currentTaxLotService.getRemainderOfFiscalYearEstimatedIncome(holdingTaxLot, securityId));
073                currentTaxLotBalance.setNextFYEstimatedIncome(currentTaxLotService.getNextFiscalYearInvestmentIncome(holdingTaxLot, securityId));
074                    
075                saveCurrentTaxLotRecord(currentTaxLotBalance);
076                LOG.info("Updated current tax lot balance for Security Id: " + securityId + " and kemid: " + holdingTaxLot.getKemid());
077            }
078            
079            LOG.info("updateCurrentTaxLotBalances() - Updated  Current Tax Lot Balances."); 
080            
081            return true;
082        }
083        
084        /**
085         * This method checks if the System parameter has been set up for this batch job.
086         * @result return true if the system parameter exists, else false
087         */
088        protected boolean systemParametersForCurrentTaxLotBalanceUpdateStepJob() {
089            LOG.info("systemParametersForCurrentTaxLotBalanceUpdateStepJob() started.");
090            
091            boolean systemParameterExists = true;
092            
093            // check to make sure the system parameter has been setup...
094            if (!getParameterService().parameterExists(KfsParameterConstants.ENDOWMENT_BATCH.class, EndowParameterKeyConstants.FISCAL_YEAR_END_DAY_AND_MONTH)) {
095              LOG.warn("FISCAL_YEAR_END_DAY_AND_MONTH System parameter does not exist in the parameters list.  The job can not continue without this parameter");
096              return false;
097            }
098            
099            LOG.info("systemParametersForCurrentTaxLotBalanceUpdateStepJob() ended.");
100            
101            return systemParameterExists;
102        }
103        
104        /**
105         * @see org.kuali.kfs.module.endow.batch.service.CurrentTaxLotBalanceUpdateService#clearAllCurrentTaxLotRecords()
106         * Method to clear all the records in the currentTaxLotBalance table
107         */
108        public void clearAllCurrentTaxLotRecords() {
109            currentTaxLotService.clearAllCurrentTaxLotRecords();
110        }
111        
112        /**
113         * @see org.kuali.kfs.module.endow.batch.service.CurrentTaxLotBalanceUpdateService#saveCurrentTaxLotRecord(CurrentTaxLotBalance)
114         * Saves the CurrentTaxLot to the table (END_CURR_TAX_LOT_BAL_T).
115         */
116        public void saveCurrentTaxLotRecord(CurrentTaxLotBalance currentTaxLotBalance) {
117            currentTaxLotService.updateCurrentTaxLotBalance(currentTaxLotBalance);
118        }
119        
120        /**
121         * Gets the parameterService attribute.
122         * 
123         * @return Returns the parameterService.
124         */    
125        protected ParameterService getParameterService() {
126            return parameterService;
127        }
128    
129        /**
130         * Sets the parameterService attribute value.
131         * 
132         * @param parameterService The parameterService to set.
133         */    
134        public void setParameterService(ParameterService parameterService) {
135            this.parameterService = parameterService;
136        }
137    
138        /**
139         * gets the holdingTaxLotService
140         * 
141         * @param holdingTaxLotService The holdingTaxLotService to get.
142         */    
143        protected HoldingTaxLotService getHoldingTaxLotService() {
144            return holdingTaxLotService;
145        }
146    
147        /**
148         * Sets the holdingTaxLotService
149         * 
150         * @param holdingTaxLotService The holdingTaxLotService to set.
151         */
152        public void setHoldingTaxLotService(HoldingTaxLotService holdingTaxLotService) {
153            this.holdingTaxLotService = holdingTaxLotService;
154        }
155        
156        /**
157         * gets the currentTaxLotService
158         * 
159         * @param currentTaxLotService The currentTaxLotService to get.
160         */    
161        public CurrentTaxLotService getCurrentTaxLotService() {
162            return currentTaxLotService;
163        }
164    
165        /**
166         * gets the currentTaxLotService
167         * 
168         * @param currentTaxLotService The currentTaxLotService to get.
169         */    
170        public void setCurrentTaxLotService(CurrentTaxLotService currentTaxLotService) {
171            this.currentTaxLotService = currentTaxLotService;
172        }
173    }