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.batch.service.CacheService;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.rice.kns.document.MaintenanceDocument;
29  import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
30  import org.kuali.rice.krad.bo.BusinessObject;
31  import org.kuali.rice.krad.bo.PersistableBusinessObject;
32  import org.kuali.rice.krad.util.ObjectUtils;
33  
34  /**
35   * This class...
36   */
37  public class FinancialSystemMaintainable extends KualiMaintainableImpl {
38      private static final Logger LOG = Logger.getLogger(FinancialSystemMaintainable.class);
39      
40      /**
41       * Constructs a FinancialSystemMaintainable
42       */
43      public FinancialSystemMaintainable() {
44          super();
45      }
46      
47      /**
48       * Constructs a FinancialSystemMaintainable, allowing the PersistableBusinessObject from KualiMaintainableImpl
49       * to be inherited
50       * @param businessObject a business object to set
51       */
52      public FinancialSystemMaintainable(PersistableBusinessObject businessObject) {
53          super(businessObject);
54      }
55  
56      /**
57       * 
58       * @param nodeName
59       * @return
60       * @throws UnsupportedOperationException
61       */
62      protected boolean answerSplitNodeQuestion(String nodeName) throws UnsupportedOperationException {
63          throw new UnsupportedOperationException("FinancialSystemMaintainable does not implement the answerSplitNodeQuestion method. Node name specified was: " + nodeName);
64          
65      }
66      
67      /**
68       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#refreshReferences(String)
69       */
70      @Override
71      protected void refreshReferences(String referencesToRefresh) {
72          // if accounts can't cross charts, populate chart code fields according to corresponding account number fields
73          if (!SpringContext.getBean(AccountService.class).accountsCanCrossCharts()) {
74              populateChartOfAccountsCodeFields();            
75          }
76          
77          super.refreshReferences(referencesToRefresh);
78      }
79      
80      /**
81       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#processAfterAddLine(String)
82       */
83      @Override
84      //public void processAfterAddLine(String colName, Class colClass) {
85      public void processBeforeAddLine(String colName, Class colClass, BusinessObject bo) {
86          //super.processAfterAddLine(colName, colClass);
87          super.processBeforeAddLine(colName, colClass, bo);
88          
89          // if accounts can't cross charts, populate chart code fields according to corresponding account number fields
90          if (!SpringContext.getBean(AccountService.class).accountsCanCrossCharts()) {
91              populateChartOfAccountsCodeFields();            
92          }
93      }
94  
95      /**
96       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#processAfterPost(String)
97       */
98      @Override
99      public void processAfterPost(MaintenanceDocument document, Map<String, String[]> parameters) {
100         super.processAfterPost(document, parameters);
101 
102         // if accounts can't cross charts, populate chart code fields according to corresponding account number fields
103         if (!SpringContext.getBean(AccountService.class).accountsCanCrossCharts()) {
104             populateChartOfAccountsCodeFields();            
105         }
106     }
107     
108     /**
109      * Populates all chartOfAccountsCode fields according to corresponding accountNumber fields in this BO.  
110      * The chartOfAccountsCode-accountNumber pairs are (part of) the FKs for the reference accounts in this BO.    
111      */
112     protected void populateChartOfAccountsCodeFields() {
113         AccountService acctService = SpringContext.getBean(AccountService.class);
114         AccountPersistenceStructureService apsService = SpringContext.getBean(AccountPersistenceStructureService.class);
115  
116         // non-collection reference accounts 
117         PersistableBusinessObject bo = getBusinessObject();        
118         Iterator<Map.Entry<String, String>> chartAccountPairs = apsService.listChartCodeAccountNumberPairs(bo).entrySet().iterator();        
119         while (chartAccountPairs.hasNext()) {
120             Map.Entry<String, String> entry = chartAccountPairs.next();
121             String coaCodeName = entry.getKey();            
122             String acctNumName = entry.getValue(); 
123             String accountNumber = (String)ObjectUtils.getPropertyValue(bo, acctNumName);
124             String coaCode = null;
125             Account account = acctService.getUniqueAccountForAccountNumber(accountNumber);            
126             if (ObjectUtils.isNotNull(account)) {
127                 coaCode = account.getChartOfAccountsCode();
128             }
129             try {
130                 ObjectUtils.setObjectProperty(bo, coaCodeName, coaCode); 
131             }
132             catch (Exception e) {
133                 LOG.error("Error in setting property value for " + coaCodeName,e);
134             }
135         }
136         
137         // collection reference accounts         
138         Iterator<Map.Entry<String, Class>> accountColls = apsService.listCollectionAccountFields(bo).entrySet().iterator();        
139         while (accountColls.hasNext()) {
140             Map.Entry<String, Class> entry = accountColls.next();
141             String accountCollName = entry.getKey();
142             PersistableBusinessObject newAccount = getNewCollectionLine(accountCollName);
143             
144             // here we can use hard-coded chartOfAccountsCode and accountNumber field name 
145             // since all reference account types do follow the standard naming pattern        
146             String accountNumber = (String)ObjectUtils.getPropertyValue(newAccount, OLEPropertyConstants.ACCOUNT_NUMBER);            
147             String coaCode = null;
148             Account account = acctService.getUniqueAccountForAccountNumber(accountNumber);            
149             if (ObjectUtils.isNotNull(account)) {
150                 coaCode = account.getChartOfAccountsCode();
151                 try {
152                     ObjectUtils.setObjectProperty(newAccount, OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, coaCode); 
153                 }
154                 catch (Exception e) {
155                     LOG.error("Error in setting chartOfAccountsCode property value in account collection " + accountCollName,e);
156                 }
157             }
158         }
159     }
160     
161     @Override
162     public void saveBusinessObject() {
163         super.saveBusinessObject();
164         // clear any caches for the selected business object (as long as they are using the normal conventions)
165         SpringContext.getBean(CacheService.class).clearKfsBusinessObjectCache(getBoClass());
166     }
167 
168     /**
169      * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#getSections(MaintenanceDocument,Maintainable)
170      *
171     @Override
172     public List getSections(MaintenanceDocument document, Maintainable oldMaintainable) {
173         // if accounts can't cross charts, populate chart code fields according to corresponding account number fields
174         if (!SpringContext.getBean(AccountService.class).accountsCanCrossCharts()) {
175             populateChartOfAccountsCodeFields();            
176         }
177         
178         return super.getSections(document, oldMaintainable);
179     }
180     */
181         
182 }