View Javadoc
1   /*
2    * Copyright 2006 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.coa.service.impl;
17  
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.apache.log4j.Logger;
28  import org.kuali.ole.coa.batch.AddPriorYearAccountsStep;
29  import org.kuali.ole.coa.businessobject.Account;
30  import org.kuali.ole.coa.businessobject.IndirectCostRecoveryAccount;
31  import org.kuali.ole.coa.businessobject.PriorYearAccount;
32  import org.kuali.ole.coa.businessobject.PriorYearIndirectCostRecoveryAccount;
33  import org.kuali.ole.coa.dataaccess.PriorYearAccountDao;
34  import org.kuali.ole.coa.service.AccountService;
35  import org.kuali.ole.coa.service.PriorYearAccountService;
36  import org.kuali.ole.sys.OLEPropertyConstants;
37  import org.kuali.ole.sys.OLEConstants.ChartApcParms;
38  import org.kuali.ole.sys.service.ReportWriterService;
39  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
40  import org.kuali.rice.krad.service.BusinessObjectService;
41  import org.kuali.rice.krad.service.PersistenceService;
42  import org.kuali.rice.krad.service.PersistenceStructureService;
43  import org.kuali.rice.krad.util.ObjectUtils;
44  import org.springframework.transaction.annotation.Transactional;
45  
46  /**
47   * This class implements the PriorYearAccountService interface.
48   */
49  @Transactional
50  public class PriorYearAccountServiceImpl implements PriorYearAccountService {
51      private static final Logger LOG = Logger.getLogger(PriorYearAccountServiceImpl.class);
52  
53      protected PriorYearAccountDao priorYearAccountDao;
54      protected AccountService accountService;
55      protected ReportWriterService reportWriterService;
56      protected PersistenceStructureService persistenceStructureService;
57      protected PersistenceService persistenceServiceOjb;
58      protected BusinessObjectService businessObjectService;
59      protected ParameterService parameterService;
60  
61      /**
62       * @see org.kuali.ole.coa.service.PriorYearAccountService#getByPrimaryKey(java.lang.String, java.lang.String)
63       */
64      @Override
65      public PriorYearAccount getByPrimaryKey(String chartCode, String accountNumber) {
66          Map<String, Object> keys = new HashMap<String, Object>();
67          keys.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
68          keys.put(OLEPropertyConstants.ACCOUNT_NUMBER, accountNumber);
69          return businessObjectService.findByPrimaryKey(PriorYearAccount.class, keys);
70      }
71  
72      /**
73       * @see org.kuali.ole.coa.service.PriorYearAccountService#populatePriorYearAccountsFromCurrent()
74       */
75      @Override
76      public void populatePriorYearAccountsFromCurrent() {
77          final String priorYrAcctTableName = persistenceStructureService.getTableName(PriorYearAccount.class);
78          int purgedCount = priorYearAccountDao.purgePriorYearAccounts(priorYrAcctTableName);
79          if (LOG.isInfoEnabled()) {
80              LOG.info("number of prior year accounts purged : " + purgedCount);
81          }
82  
83          final String acctTableName = persistenceStructureService.getTableName(Account.class);
84          int copiedCount = priorYearAccountDao.copyCurrentAccountsToPriorYearTable(priorYrAcctTableName, acctTableName);
85          if (LOG.isInfoEnabled()) {
86              LOG.info("number of current year accounts copied to prior year : " + copiedCount);
87          }
88          
89          //copy prior year ICR accounts
90          final String priorYrIcrAcctTableName = persistenceStructureService.getTableName(PriorYearIndirectCostRecoveryAccount.class);
91          purgedCount = priorYearAccountDao.purgePriorYearAccounts(priorYrIcrAcctTableName);
92          if (LOG.isInfoEnabled()) {
93              LOG.info("number of prior year indirect cost recovery accounts purged : " + purgedCount);
94          }
95  
96          final String icrAcctTableName = persistenceStructureService.getTableName(IndirectCostRecoveryAccount.class);
97          copiedCount = priorYearAccountDao.copyCurrentICRAccountsToPriorYearTable(priorYrIcrAcctTableName, icrAcctTableName);
98          if (LOG.isInfoEnabled()) {
99              LOG.info("number of current year indirect cost recovery accounts copied to prior year : " + copiedCount);
100         }
101 
102     }
103 
104     /**
105      * @see org.kuali.ole.coa.service.PriorYearAccountService#addPriorYearAccountsFromParameter()
106      */
107     public void addPriorYearAccountsFromParameter() {        
108         /*
109         Collection<String> accountsColl = new ArrayList<String>();
110         accountsColl.add("BL-9923234");
111         accountsColl.add("BL-1024600");
112         accountsColl.add("0000000");
113         accountsColl.add("BL-0000000");
114         accountsColl.add("UA-2131401");      
115         accountsColl.add("BA-6044909");
116         accountsColl.add("BA-6044901");
117         accountsColl.add("UA-7014960");
118         */
119         
120         // clear cache so that batch job will be reading most up-to-date data from Account and PriorYearAccount tables
121         persistenceServiceOjb.clearCache();
122 
123         String param = ChartApcParms.PRIOR_YEAR_ACCOUNTS_TO_BE_ADDED;
124         Collection<String> accountsColl = parameterService.getParameterValuesAsString(AddPriorYearAccountsStep.class, param);
125         Iterator<String> accountsIter = accountsColl.iterator();
126         List<PriorYearAccount> priorAccounts = new ArrayList<PriorYearAccount>();
127         int countError = 0;
128         String errmsg = "";
129         String failmsg = "Failed to add account ";
130         
131         LOG.info("Adding Accounts to Prior Year Account table from parameter " + param);
132         reportWriterService.writeSubTitle("Accounts failed to be added to Prior Year Account table from parameter " + param);
133 
134         while (accountsIter.hasNext()) {
135             // retrieve chart code and account number from parameter
136             String accountStr = accountsIter.next();
137             String chartCode = StringUtils.substringBefore(accountStr, "-");
138             String accountNumber = StringUtils.substringAfter(accountStr, "-");       
139             
140             // if account format is invalid, report error      
141             if (StringUtils.isEmpty(chartCode) || StringUtils.isEmpty(accountNumber)) {
142                 countError++;                
143                 errmsg = accountStr + " : invalid format. Correct account format: coaCode-accountNumber."; 
144                 reportWriterService.writeFormattedMessageLine("%s", errmsg);
145                 LOG.error(failmsg + errmsg);
146                 continue;
147             }
148 
149             // check whether account exists, report error if not   
150             // TODO switch back to accountService.getByPrimaryId when cache issue is fixed
151             // not using accountService.getByPrimaryId here because there is an issue with Account cache
152             // using businessObjectService instead will skip cache issue
153             // Account account = accountService.getByPrimaryId(chartCode, accountNumber);
154             Map<String, Object> keys = new HashMap<String, Object>(2);
155             keys.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
156             keys.put(OLEPropertyConstants.ACCOUNT_NUMBER, accountNumber);
157             Account account = businessObjectService.findByPrimaryKey(Account.class, keys);
158             
159             if (ObjectUtils.isNull(account)) {
160                 countError++;
161                 errmsg = accountStr + " : doesn't exist in Account table."; 
162                 reportWriterService.writeFormattedMessageLine("%s", errmsg);
163                 LOG.error(failmsg + errmsg);
164             }            
165             // check whether account already exists in prior year, report error if yes 
166             else if (ObjectUtils.isNotNull(getByPrimaryKey(chartCode, accountNumber))) {
167                 countError++;
168                 errmsg = accountStr + " : already exists in Prior Year Account table."; 
169                 reportWriterService.writeFormattedMessageLine("%s", errmsg);
170                 LOG.error(failmsg + errmsg);
171             }
172             // otherwise, add account to prior year table 
173             else {
174                 PriorYearAccount priorAccount = new PriorYearAccount(account);
175                 businessObjectService.save(priorAccount);                
176                 priorAccounts.add(priorAccount);
177                 LOG.info("Successfully added account " + accountStr);
178             }
179         }
180         
181         String totalSuccessMsg = "Total number of accounts successfully added to prior year: " + priorAccounts.size();
182         String totalFailureMsg = "Total number of accounts failed to be added to prior year: " + countError;
183         reportWriterService.writeSubTitle("Accounts successfully added to Prior Year Account table:");
184         reportWriterService.writeTable(priorAccounts, true, false);
185         reportWriterService.writeStatisticLine("%s", totalSuccessMsg);
186         reportWriterService.writeStatisticLine("%s", totalFailureMsg);
187         LOG.info(totalSuccessMsg);
188         LOG.info(totalFailureMsg);
189     }
190     
191     public void setPriorYearAccountDao(PriorYearAccountDao priorYearAccountDao) {
192         this.priorYearAccountDao = priorYearAccountDao;
193     }
194     
195     public void setAccountService(AccountService accountService) {
196         this.accountService = accountService;
197     }
198 
199     public void setReportWriterService(ReportWriterService reportWriterService) {
200         this.reportWriterService = reportWriterService;
201     }
202 
203     public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) {
204         this.persistenceStructureService = persistenceStructureService;
205     }
206     
207     public void setPersistenceServiceOjb(PersistenceService persistenceServiceOjb) {
208         this.persistenceServiceOjb = persistenceServiceOjb;
209     }
210 
211     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
212         this.businessObjectService = businessObjectService;
213     }
214     
215     public void setParameterService(ParameterService parameterService) {
216         this.parameterService = parameterService;
217     }
218     
219 }