1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.gl.service.impl;
17
18 import java.util.Comparator;
19 import java.util.Map;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.kuali.ole.coa.businessobject.Account;
23 import org.kuali.ole.coa.businessobject.SubFundGroup;
24 import org.kuali.ole.gl.batch.service.AccountingCycleCachingService;
25 import org.kuali.ole.gl.businessobject.OriginEntryInformation;
26 import org.kuali.ole.gl.businessobject.PosterOutputSummaryAmountHolder;
27 import org.kuali.ole.gl.businessobject.PosterOutputSummaryEntry;
28 import org.kuali.ole.gl.businessobject.Transaction;
29 import org.kuali.ole.gl.service.PosterOutputSummaryService;
30 import org.kuali.rice.core.api.util.type.KualiDecimal;
31
32
33
34
35 public class PosterOutputSummaryServiceImpl implements PosterOutputSummaryService {
36 private AccountingCycleCachingService accountingCycleCachingService;
37
38
39
40
41
42 public void addAmountToAmountHolder(OriginEntryInformation originEntry, PosterOutputSummaryAmountHolder amountHolder) {
43 final String debitCreditCode = originEntry.getTransactionDebitCreditCode();
44 final KualiDecimal amount = originEntry.getTransactionLedgerEntryAmount();
45 final String objectTypeCode = originEntry.getFinancialObjectTypeCode();
46
47 amountHolder.addAmount(debitCreditCode, objectTypeCode, amount);
48 }
49
50
51
52
53
54 public void addAmountToAmountHolder(Transaction transaction, PosterOutputSummaryAmountHolder amountHolder) {
55 final String debitCreditCode = transaction.getTransactionDebitCreditCode();
56 final KualiDecimal amount = transaction.getTransactionLedgerEntryAmount();
57 final String objectTypeCode = transaction.getFinancialObjectTypeCode();
58
59 amountHolder.addAmount(debitCreditCode, objectTypeCode, amount);
60 }
61
62
63
64
65 public Comparator<PosterOutputSummaryEntry> getEntryComparator() {
66 return new Comparator<PosterOutputSummaryEntry>() {
67
68
69
70
71
72
73
74
75 public int compare(PosterOutputSummaryEntry vladimir, PosterOutputSummaryEntry estragon) {
76 if (shouldCompare(vladimir.getBalanceTypeCode(), estragon.getBalanceTypeCode())) {
77 return vladimir.getBalanceTypeCode().toUpperCase().compareTo(estragon.getBalanceTypeCode().toUpperCase());
78 } else if (shouldCompare(vladimir.getUniversityFiscalYear(), estragon.getUniversityFiscalYear())) {
79 return vladimir.getUniversityFiscalYear().compareTo(estragon.getUniversityFiscalYear());
80 } else if (shouldCompare(vladimir.getFiscalPeriodCode(), estragon.getFiscalPeriodCode())) {
81 return vladimir.getFiscalPeriodCode().toUpperCase().compareTo(estragon.getFiscalPeriodCode().toUpperCase());
82 } else if (shouldCompare(vladimir.getFundGroup(), estragon.getFundGroup())) {
83 return vladimir.getFundGroup().toUpperCase().compareTo(estragon.getFundGroup().toUpperCase());
84 } else {
85 return 0;
86 }
87 }
88
89
90
91
92
93
94
95 protected boolean shouldCompare(String s1, String s2) {
96 return !StringUtils.isBlank(s1) && !StringUtils.isBlank(s2) && !s1.equalsIgnoreCase(s2);
97 }
98
99
100
101
102
103
104
105 protected boolean shouldCompare(Integer i1, Integer i2) {
106 return i1 != null && i2 != null && !i1.equals(i2);
107 }
108 };
109 }
110
111
112
113
114 protected String getPosterOutputSummaryEntryMapKey(OriginEntryInformation originEntry) {
115 return buildKey(originEntry.getFinancialBalanceTypeCode(), originEntry.getUniversityFiscalYear(), originEntry.getUniversityFiscalPeriodCode(), originEntry.getChartOfAccountsCode(), originEntry.getAccountNumber());
116 }
117
118
119
120
121 protected String getPosterOutputSummaryEntryMapKey(Transaction transaction) {
122 return buildKey(transaction.getFinancialBalanceTypeCode(), transaction.getUniversityFiscalYear(), transaction.getUniversityFiscalPeriodCode(), transaction.getChartOfAccountsCode(), transaction.getAccountNumber());
123 }
124
125
126
127
128
129
130
131
132
133 protected String buildKey(String balanceTypeCode, Integer universityFiscalYear, String fiscalPeriodCode, String chartOfAccountsCode, String accountNumber) {
134 return StringUtils.join(new String[] {balanceTypeCode, universityFiscalYear == null ? "" : universityFiscalYear.toString(), fiscalPeriodCode, getFundGroupCodeForAccount(chartOfAccountsCode, accountNumber)}, ':');
135 }
136
137
138
139
140
141
142 protected String getFundGroupCodeForAccount(String chartOfAccountsCode, String accountNumber) {
143 final Account account = this.getAccountingCycleCachingService().getAccount(chartOfAccountsCode, accountNumber);
144 if (account != null) {
145 final SubFundGroup subFundGroup = this.getAccountingCycleCachingService().getSubFundGroup(account.getSubFundGroupCode());
146 if (subFundGroup != null) {
147 return subFundGroup.getFundGroupCode();
148 }
149 }
150 return "";
151 }
152
153
154
155
156 public void summarize(OriginEntryInformation originEntry, Map<String, PosterOutputSummaryEntry> entries) {
157 final String key = getPosterOutputSummaryEntryMapKey(originEntry);
158 PosterOutputSummaryEntry entry = entries.get(key);
159 if (entry == null) {
160 entry = new PosterOutputSummaryEntry(originEntry.getFinancialBalanceTypeCode(), originEntry.getUniversityFiscalYear(), originEntry.getUniversityFiscalPeriodCode(), getFundGroupCodeForAccount(originEntry.getChartOfAccountsCode(), originEntry.getAccountNumber()));
161 entries.put(key, entry);
162 }
163 addAmountToAmountHolder(originEntry, entry);
164 }
165
166
167
168
169 public void summarize(Transaction transaction, Map<String, PosterOutputSummaryEntry> entries) {
170 final String key = getPosterOutputSummaryEntryMapKey(transaction);
171 PosterOutputSummaryEntry entry = entries.get(key);
172 if (entry == null) {
173 entry = new PosterOutputSummaryEntry(transaction.getFinancialBalanceTypeCode(), transaction.getUniversityFiscalYear(), transaction.getUniversityFiscalPeriodCode(), getFundGroupCodeForAccount(transaction.getChartOfAccountsCode(), transaction.getAccountNumber()));
174 entries.put(key, entry);
175 }
176 addAmountToAmountHolder(transaction, entry);
177 }
178
179
180
181
182
183 public AccountingCycleCachingService getAccountingCycleCachingService() {
184 return accountingCycleCachingService;
185 }
186
187
188
189
190
191 public void setAccountingCycleCachingService(AccountingCycleCachingService accountingCycleCachingService) {
192 this.accountingCycleCachingService = accountingCycleCachingService;
193 }
194 }