View Javadoc
1   /*
2    * Copyright 2009 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.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   * The default implementation of the PosterOutputSummaryService
34   */
35  public class PosterOutputSummaryServiceImpl implements PosterOutputSummaryService {
36      private AccountingCycleCachingService accountingCycleCachingService;
37  
38      /**
39       * Default implementation
40       * @see org.kuali.ole.gl.service.PosterOutputSummaryService#addOriginEntryAmountToAmountHolder(org.kuali.ole.gl.businessobject.OriginEntryInformation, org.kuali.ole.gl.businessobject.PosterOutputSummaryAmountHolder)
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       * Default implementation
52       * @see org.kuali.ole.gl.service.PosterOutputSummaryService#addTransactionAmountToAmountHolder(org.kuali.ole.gl.businessobject.Transaction, org.kuali.ole.gl.businessobject.PosterOutputSummaryAmountHolder)
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       * @see org.kuali.ole.gl.service.PosterOutputSummaryService#getEntryComparator()
64       */
65      public Comparator<PosterOutputSummaryEntry> getEntryComparator() {
66          return new Comparator<PosterOutputSummaryEntry>() {
67              
68              /**
69               * Compares the first PosterOutputSummaryEntry given to the second, based on - in order - balance type code,
70               * university fiscal year, fiscal period code, and finally fund group
71               * @param vladimir the first PosterOutputSummaryEntry to compare
72               * @param estragon the second PosterOutputSummaryEntry to compare
73               * @return the standard result of a compare operation: 0 if there's equality, less than 0 if vladimir is "less" than estragon, and more than 0 if vladimir is "greater" than estragon
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               * Determines if it's safe to compare two Strings
91               * @param s1 the first String we may compare
92               * @param s2 the second String we may compare
93               * @return true if comparison of these two Strings would be meaningful
94               */
95              protected boolean shouldCompare(String s1, String s2) {
96                  return !StringUtils.isBlank(s1) && !StringUtils.isBlank(s2) && !s1.equalsIgnoreCase(s2);
97              }
98              
99              /**
100              * Determine if it's safe to compare two Integers
101              * @param i1 the first Integer we may compare
102              * @param i2 the second Integer we may compare
103              * @return true if comparison of the two Integers would be meaningful
104              */
105             protected boolean shouldCompare(Integer i1, Integer i2) {
106                 return i1 != null && i2 != null && !i1.equals(i2);
107             }
108         };
109     }
110 
111     /**
112      * @see org.kuali.ole.gl.service.PosterOutputSummaryService#getPosterOutputSummaryEntryMapKey(org.kuali.ole.gl.businessobject.OriginEntryInformation)
113      */
114     protected String getPosterOutputSummaryEntryMapKey(OriginEntryInformation originEntry) {
115         return buildKey(originEntry.getFinancialBalanceTypeCode(), originEntry.getUniversityFiscalYear(), originEntry.getUniversityFiscalPeriodCode(), originEntry.getChartOfAccountsCode(), originEntry.getAccountNumber());
116     }
117 
118     /**
119      * @see org.kuali.ole.gl.service.PosterOutputSummaryService#getPosterOutputSummaryEntryMapKey(org.kuali.ole.gl.businessobject.Transaction)
120      */
121     protected String getPosterOutputSummaryEntryMapKey(Transaction transaction) {
122         return buildKey(transaction.getFinancialBalanceTypeCode(), transaction.getUniversityFiscalYear(), transaction.getUniversityFiscalPeriodCode(), transaction.getChartOfAccountsCode(), transaction.getAccountNumber());
123     }
124 
125     /**
126      * Builds a map key based on the given information
127      * @param balanceTypeCode the balance type code to put in the key
128      * @param universityFiscalYear the fiscal year to put in the key
129      * @param fiscalPeriodCode the period code to put in the key
130      * @param subFundGroupCode the sub fund group code to put in the key
131      * @return a key build from the various attributes
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      * Returns the sub fund group for the given origin entry
139      * @param originEntry the origin entry to find the sub fund group for, from its account
140      * @return the sub fund group code related to the account used by this origin entry
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      * @see org.kuali.ole.gl.service.PosterOutputSummaryService#summarizeOriginEntry(org.kuali.ole.gl.businessobject.OriginEntryInformation, java.util.Map)
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      * @see org.kuali.ole.gl.service.PosterOutputSummaryService#summarizeTransaction(org.kuali.ole.gl.businessobject.Transaction, java.util.Map)
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      * Gets the accountingCycleCachingService attribute. 
181      * @return Returns the accountingCycleCachingService.
182      */
183     public AccountingCycleCachingService getAccountingCycleCachingService() {
184         return accountingCycleCachingService;
185     }
186 
187     /**
188      * Sets the accountingCycleCachingService attribute value.
189      * @param accountingCycleCachingService The accountingCycleCachingService to set.
190      */
191     public void setAccountingCycleCachingService(AccountingCycleCachingService accountingCycleCachingService) {
192         this.accountingCycleCachingService = accountingCycleCachingService;
193     }
194 }