1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
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
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
106
107 public void addPriorYearAccountsFromParameter() {
108
109
110
111
112
113
114
115
116
117
118
119
120
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
136 String accountStr = accountsIter.next();
137 String chartCode = StringUtils.substringBefore(accountStr, "-");
138 String accountNumber = StringUtils.substringAfter(accountStr, "-");
139
140
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
150
151
152
153
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
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
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 }