View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.sys.document;
20  
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import org.apache.log4j.Logger;
25  import org.kuali.kfs.coa.businessobject.Account;
26  import org.kuali.kfs.coa.service.AccountPersistenceStructureService;
27  import org.kuali.kfs.coa.service.AccountService;
28  import org.kuali.kfs.sys.KFSPropertyConstants;
29  import org.kuali.kfs.sys.batch.service.CacheService;
30  import org.kuali.kfs.sys.context.SpringContext;
31  import org.kuali.rice.kns.document.MaintenanceDocument;
32  import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
33  import org.kuali.rice.krad.bo.BusinessObject;
34  import org.kuali.rice.krad.bo.PersistableBusinessObject;
35  import org.kuali.rice.krad.util.ObjectUtils;
36  
37  /**
38   * This class...
39   */
40  public class FinancialSystemMaintainable extends KualiMaintainableImpl {
41      private static final Logger LOG = Logger.getLogger(FinancialSystemMaintainable.class);
42  
43      /**
44       * Constructs a FinancialSystemMaintainable
45       */
46      public FinancialSystemMaintainable() {
47          super();
48      }
49  
50      /**
51       * Constructs a FinancialSystemMaintainable, allowing the PersistableBusinessObject from KualiMaintainableImpl
52       * to be inherited
53       * @param businessObject a business object to set
54       */
55      public FinancialSystemMaintainable(PersistableBusinessObject businessObject) {
56          super(businessObject);
57      }
58  
59      /**
60       *
61       * @param nodeName
62       * @return
63       * @throws UnsupportedOperationException
64       */
65      protected boolean answerSplitNodeQuestion(String nodeName) throws UnsupportedOperationException {
66          throw new UnsupportedOperationException("FinancialSystemMaintainable does not implement the answerSplitNodeQuestion method. Node name specified was: " + nodeName);
67  
68      }
69  
70      /**
71       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#refreshReferences(String)
72       */
73      @Override
74      protected void refreshReferences(String referencesToRefresh) {
75          // if accounts can't cross charts, populate chart code fields according to corresponding account number fields
76          if (!SpringContext.getBean(AccountService.class).accountsCanCrossCharts()) {
77              populateChartOfAccountsCodeFields();
78          }
79  
80          super.refreshReferences(referencesToRefresh);
81      }
82  
83      /**
84       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#processAfterAddLine(String)
85       */
86      @Override
87      //public void processAfterAddLine(String colName, Class colClass) {
88      public void processBeforeAddLine(String colName, Class colClass, BusinessObject bo) {
89          //super.processAfterAddLine(colName, colClass);
90          super.processBeforeAddLine(colName, colClass, bo);
91  
92          // if accounts can't cross charts, populate chart code fields according to corresponding account number fields
93          if (!SpringContext.getBean(AccountService.class).accountsCanCrossCharts()) {
94              populateChartOfAccountsCodeFields();
95          }
96      }
97  
98      /**
99       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#processAfterPost(String)
100      */
101     @Override
102     public void processAfterPost(MaintenanceDocument document, Map<String, String[]> parameters) {
103         super.processAfterPost(document, parameters);
104 
105         // if accounts can't cross charts, populate chart code fields according to corresponding account number fields
106         if (!SpringContext.getBean(AccountService.class).accountsCanCrossCharts()) {
107             populateChartOfAccountsCodeFields();
108         }
109     }
110 
111     /**
112      * Populates all chartOfAccountsCode fields according to corresponding accountNumber fields in this BO.
113      * The chartOfAccountsCode-accountNumber pairs are (part of) the FKs for the reference accounts in this BO.
114      */
115     protected void populateChartOfAccountsCodeFields() {
116         AccountService acctService = SpringContext.getBean(AccountService.class);
117         AccountPersistenceStructureService apsService = SpringContext.getBean(AccountPersistenceStructureService.class);
118 
119         // non-collection reference accounts
120         PersistableBusinessObject bo = getBusinessObject();
121         Iterator<Map.Entry<String, String>> chartAccountPairs = apsService.listChartCodeAccountNumberPairs(bo).entrySet().iterator();
122         while (chartAccountPairs.hasNext()) {
123             Map.Entry<String, String> entry = chartAccountPairs.next();
124             String coaCodeName = entry.getKey();
125             String acctNumName = entry.getValue();
126             String accountNumber = (String)ObjectUtils.getPropertyValue(bo, acctNumName);
127             String coaCode = null;
128             Account account = acctService.getUniqueAccountForAccountNumber(accountNumber);
129             if (ObjectUtils.isNotNull(account)) {
130                 coaCode = account.getChartOfAccountsCode();
131             }
132             try {
133                 ObjectUtils.setObjectProperty(bo, coaCodeName, coaCode);
134             }
135             catch (Exception e) {
136                 LOG.error("Error in setting property value for " + coaCodeName,e);
137             }
138         }
139 
140         // collection reference accounts
141         Iterator<Map.Entry<String, Class>> accountColls = apsService.listCollectionAccountFields(bo).entrySet().iterator();
142         while (accountColls.hasNext()) {
143             Map.Entry<String, Class> entry = accountColls.next();
144             String accountCollName = entry.getKey();
145             PersistableBusinessObject newAccount = getNewCollectionLine(accountCollName);
146 
147             // here we can use hard-coded chartOfAccountsCode and accountNumber field name
148             // since all reference account types do follow the standard naming pattern
149             String accountNumber = (String)ObjectUtils.getPropertyValue(newAccount, KFSPropertyConstants.ACCOUNT_NUMBER);
150             String coaCode = null;
151             Account account = acctService.getUniqueAccountForAccountNumber(accountNumber);
152             if (ObjectUtils.isNotNull(account)) {
153                 coaCode = account.getChartOfAccountsCode();
154                 try {
155                     ObjectUtils.setObjectProperty(newAccount, KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, coaCode);
156                 }
157                 catch (Exception e) {
158                     LOG.error("Error in setting chartOfAccountsCode property value in account collection " + accountCollName,e);
159                 }
160             }
161         }
162     }
163 
164     @Override
165     public void saveBusinessObject() {
166         super.saveBusinessObject();
167         // clear any caches for the selected business object (as long as they are using the normal conventions)
168         SpringContext.getBean(CacheService.class).clearKfsBusinessObjectCache(getBoClass());
169     }
170 
171     /**
172      * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#getSections(MaintenanceDocument,Maintainable)
173      *
174     @Override
175     public List getSections(MaintenanceDocument document, Maintainable oldMaintainable) {
176         // if accounts can't cross charts, populate chart code fields according to corresponding account number fields
177         if (!SpringContext.getBean(AccountService.class).accountsCanCrossCharts()) {
178             populateChartOfAccountsCodeFields();
179         }
180 
181         return super.getSections(document, oldMaintainable);
182     }
183     */
184 
185 }