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.coa.document;
17  
18  import java.util.Map;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.apache.log4j.Logger;
22  import org.kuali.ole.coa.businessobject.A21SubAccount;
23  import org.kuali.ole.coa.businessobject.Account;
24  import org.kuali.ole.coa.businessobject.SubAccount;
25  import org.kuali.ole.coa.service.A21SubAccountService;
26  import org.kuali.ole.coa.service.AccountService;
27  import org.kuali.ole.sys.OLEConstants;
28  import org.kuali.ole.sys.OLEPropertyConstants;
29  import org.kuali.ole.sys.context.SpringContext;
30  import org.kuali.ole.sys.document.FinancialSystemMaintainable;
31  import org.kuali.rice.kim.api.identity.Person;
32  import org.kuali.rice.kns.document.MaintenanceDocument;
33  import org.kuali.rice.kns.document.authorization.MaintenanceDocumentRestrictions;
34  import org.kuali.rice.krad.bo.PersistableBusinessObject;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  import org.kuali.rice.krad.util.ObjectUtils;
37  
38  /**
39   * This class...
40   */
41  public class SubAccountMaintainableImpl extends FinancialSystemMaintainable {
42      private static final Logger LOG = Logger.getLogger(SubAccountMaintainableImpl.class);
43  
44      // account fields that are PKs of nested reference accounts but don't exist in the Sub-Account BO as FKs.
45      public static final String[] COA_CODE_NAMES = {        
46          OLEPropertyConstants.A21_SUB_ACCOUNT + "." + OLEPropertyConstants.COST_SHARE_SOURCE_CHART_OF_ACCOUNTS_CODE, 
47      };
48      public static final String[] ACCOUNT_NUMBER_NAMES = {        
49          OLEPropertyConstants.A21_SUB_ACCOUNT + "." + OLEPropertyConstants.COST_SHARE_SOURCE_ACCOUNT_NUMBER, 
50      };
51  
52      /**
53       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#refresh(java.lang.String, java.util.Map,
54       *      org.kuali.rice.kns.document.MaintenanceDocument)
55       */
56      @Override
57      public void refresh(String refreshCaller, Map fieldValues, MaintenanceDocument document) {
58          super.refresh(refreshCaller, fieldValues, document);
59  
60          Person person = GlobalVariables.getUserSession().getPerson();
61          MaintenanceDocumentRestrictions restrictions = getBusinessObjectAuthorizationService().getMaintenanceDocumentRestrictions(document, person);
62          boolean canEdit = !restrictions.isHiddenSectionId(OLEConstants.SUB_ACCOUNT_EDIT_CG_ICR_SECTION_ID) && !restrictions.isReadOnlySectionId(OLEConstants.SUB_ACCOUNT_EDIT_CG_ICR_SECTION_ID);
63          
64          // after account lookup, refresh the CG ICR account fields
65          if (refreshCaller != null && (refreshCaller.toUpperCase().endsWith(OLEConstants.LOOKUPABLE_SUFFIX.toUpperCase())) && fieldValues.containsKey("document.newMaintainableObject.accountNumber") && canEdit) {
66              SubAccount subAccount = (SubAccount) this.getBusinessObject();
67              this.populateCGIcrFields(subAccount);
68          }
69      }
70  
71      // populate the CG ICR fields if any
72      private void populateCGIcrFields(SubAccount subAccount) {
73          A21SubAccount a21SubAccount = subAccount.getA21SubAccount();
74          String chartOfAccountsCode = subAccount.getChartOfAccountsCode();
75          String accountNumber = subAccount.getAccountNumber();
76          
77          //populate ICR fields if subAccount COA or Acct does not match a21SubAccount COA/Acct
78          if (ObjectUtils.isNotNull(a21SubAccount) && (!StringUtils.equals(chartOfAccountsCode, a21SubAccount.getChartOfAccountsCode()) || !StringUtils.equals(accountNumber, a21SubAccount.getAccountNumber()))) {                  
79              A21SubAccountService a21SubAccountService = SpringContext.getBean(A21SubAccountService.class);
80              a21SubAccountService.populateCgIcrAccount(a21SubAccount, chartOfAccountsCode, accountNumber);
81          }
82      }
83      
84      /**
85       * @see org.kuali.ole.sys.document.FinancialSystemMaintainable#populateChartOfAccountsCodeFields()
86       * 
87       * Special treatment is needed to populate chart code fields from account number fields 
88       * in nested reference accounts a21SubAccount.costShareAccount 
89       * when accounts can't cross charts.
90       */
91      @Override
92      protected void populateChartOfAccountsCodeFields() {
93          super.populateChartOfAccountsCodeFields();
94          PersistableBusinessObject bo = getBusinessObject();        
95          AccountService acctService = SpringContext.getBean(AccountService.class);
96  
97          for (int i=0; i<COA_CODE_NAMES.length; i++) {
98              String coaCodeName = COA_CODE_NAMES[i];            
99              String acctNumName = ACCOUNT_NUMBER_NAMES[i];        
100             String accountNumber = (String)ObjectUtils.getPropertyValue(bo, acctNumName);
101             String coaCode = null;
102             Account account = acctService.getUniqueAccountForAccountNumber(accountNumber);            
103             if (ObjectUtils.isNotNull(account)) {
104                 coaCode = account.getChartOfAccountsCode();
105             }
106             try {
107                 ObjectUtils.setObjectProperty(bo, coaCodeName, coaCode); 
108             }
109             catch (Exception e) {
110                 LOG.error("Error in setting property value for " + coaCodeName);
111             } 
112         }
113     }
114     
115 }