View Javadoc
1   /*
2    * Copyright 2008 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.sys.document;
17  
18  import java.util.Iterator;
19  import java.util.Map;
20  
21  import org.apache.log4j.Logger;
22  import org.kuali.ole.coa.businessobject.Account;
23  import org.kuali.ole.coa.service.AccountPersistenceStructureService;
24  import org.kuali.ole.coa.service.AccountService;
25  import org.kuali.ole.sys.OLEPropertyConstants;
26  import org.kuali.ole.sys.context.SpringContext;
27  import org.kuali.rice.kns.document.MaintenanceDocument;
28  import org.kuali.rice.kns.maintenance.KualiGlobalMaintainableImpl;
29  import org.kuali.rice.krad.bo.BusinessObject;
30  import org.kuali.rice.krad.bo.PersistableBusinessObject;
31  import org.kuali.rice.krad.util.ObjectUtils;
32  
33  /**
34   * This class...
35   */
36  public abstract class FinancialSystemGlobalMaintainable extends KualiGlobalMaintainableImpl {
37      private static final Logger LOG = Logger.getLogger(FinancialSystemGlobalMaintainable.class);
38  
39      protected boolean answerSplitNodeQuestion(String nodeName) throws UnsupportedOperationException {
40          throw new UnsupportedOperationException("FinancialSystemGlobalMaintainable does not implement the answerSplitNodeQuestion method. Node name specified was: " + nodeName); 
41      }
42  
43      /**
44       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#refreshReferences(String)
45       */
46      @Override
47      protected void refreshReferences(String referencesToRefresh) {
48          // if accounts can't cross charts, populate chart code fields according to corresponding account number fields
49          if (!SpringContext.getBean(AccountService.class).accountsCanCrossCharts()) {
50              populateChartOfAccountsCodeFields();            
51          }
52          
53          super.refreshReferences(referencesToRefresh);
54      }
55      
56      /**
57       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#processAfterAddLine(String)
58       */
59      @Override
60      public void processBeforeAddLine(String colName, Class colClass, BusinessObject bo) {
61          super.processBeforeAddLine(colName, colClass, bo);
62  
63          // if accounts can't cross charts, populate chart code fields according to corresponding account number fields
64          if (!SpringContext.getBean(AccountService.class).accountsCanCrossCharts()) {
65              populateChartOfAccountsCodeFields();            
66          }
67      }
68  
69      /**
70       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#processAfterPost(String)
71       */
72      @Override
73      public void processAfterPost(MaintenanceDocument document, Map<String, String[]> parameters) {
74          super.processAfterPost(document, parameters);
75  
76          // if accounts can't cross charts, populate chart code fields according to corresponding account number fields
77          if (!SpringContext.getBean(AccountService.class).accountsCanCrossCharts()) {
78              populateChartOfAccountsCodeFields();            
79          }
80      }
81      
82      /**
83       * Populates all chartOfAccountsCode fields according to corresponding accountNumber fields in this BO.  
84       * The chartOfAccountsCode-accountNumber pairs are (part of) the FKs for the reference accounts in this BO.    
85       */
86      protected void populateChartOfAccountsCodeFields() {
87          AccountService acctService = SpringContext.getBean(AccountService.class);
88          AccountPersistenceStructureService apsService = SpringContext.getBean(AccountPersistenceStructureService.class);
89   
90          // non-collection reference accounts 
91          PersistableBusinessObject bo = getBusinessObject();        
92          Iterator<Map.Entry<String, String>> chartAccountPairs = apsService.listChartCodeAccountNumberPairs(bo).entrySet().iterator();        
93          while (chartAccountPairs.hasNext()) {
94              Map.Entry<String, String> entry = (Map.Entry<String, String>)chartAccountPairs.next();
95              String coaCodeName = entry.getKey();            
96              String acctNumName = entry.getValue(); 
97              String accountNumber = (String)ObjectUtils.getPropertyValue(bo, acctNumName);
98              String coaCode = null;
99              Account account = acctService.getUniqueAccountForAccountNumber(accountNumber);            
100             if (ObjectUtils.isNotNull(account)) {
101                 coaCode = account.getChartOfAccountsCode();
102             }
103             try {
104                 ObjectUtils.setObjectProperty(bo, coaCodeName, coaCode); 
105             }
106             catch (Exception e) {
107                 LOG.error("Error in setting property value for " + coaCodeName, e);
108             }
109         }
110         
111         // collection reference accounts         
112         Iterator<Map.Entry<String, Class>> accountColls = apsService.listCollectionAccountFields(bo).entrySet().iterator();        
113         while (accountColls.hasNext()) {
114             Map.Entry<String, Class> entry = (Map.Entry<String, Class>)accountColls.next();
115             String accountCollName = entry.getKey();
116             PersistableBusinessObject newAccount = getNewCollectionLine(accountCollName);
117             
118             // here we can use hard-coded chartOfAccountsCode and accountNumber field name 
119             // since all reference account types do follow the standard naming pattern        
120             String accountNumber = (String)ObjectUtils.getPropertyValue(newAccount, OLEPropertyConstants.ACCOUNT_NUMBER);            
121             String coaCode = null;
122             Account account = acctService.getUniqueAccountForAccountNumber(accountNumber);            
123             if (ObjectUtils.isNotNull(account)) {
124                 coaCode = account.getChartOfAccountsCode();
125                 try {
126                     ObjectUtils.setObjectProperty(newAccount, OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, coaCode); 
127                 }
128                 catch (Exception e) {
129                     LOG.error("Error in setting chartOfAccountsCode property value in account collection " + accountCollName, e);
130                 }
131             }
132         }
133     }
134     
135 }