View Javadoc
1   /*
2    * Copyright 2006 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.validation.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.coa.businessobject.A21IndirectCostRecoveryAccount;
20  import org.kuali.ole.coa.businessobject.A21SubAccount;
21  import org.kuali.ole.coa.businessobject.Account;
22  import org.kuali.ole.coa.businessobject.IndirectCostRecoveryAccount;
23  import org.kuali.ole.coa.businessobject.SubAccount;
24  import org.kuali.ole.sys.OLEConstants;
25  import org.kuali.ole.sys.context.SpringContext;
26  import org.kuali.rice.kim.api.identity.Person;
27  import org.kuali.rice.kns.document.MaintenanceDocument;
28  import org.kuali.rice.kns.document.authorization.MaintenanceDocumentRestrictions;
29  import org.kuali.rice.kns.service.BusinessObjectAuthorizationService;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  import org.kuali.rice.krad.util.ObjectUtils;
32  
33  /**
34   * PreRules checks for the {@link SubAccount} that needs to occur while still in the Struts processing. This includes defaults, confirmations,
35   * etc.
36   */
37  public class SubAccountPreRules extends MaintenancePreRulesBase {
38  
39      protected SubAccount newSubAccount;
40  
41      // protected SubAccount copyAccount;
42  
43      public SubAccountPreRules() {
44  
45      }
46  
47      /**
48       * This checks to see if a continuation account is necessary and then copies the ICR data from the Account 
49       * associated with this SubAccount (if necessary)
50       * @see org.kuali.ole.coa.document.validation.impl.MaintenancePreRulesBase#doCustomPreRules(org.kuali.rice.kns.document.MaintenanceDocument)
51       */
52      protected boolean doCustomPreRules(MaintenanceDocument document) {
53          setupConvenienceObjects(document);
54          checkForContinuationAccounts(document.getNewMaintainableObject().getMaintenanceAction()); // run this first to avoid side
55                                                                                                      // effects
56  
57          LOG.debug("done with continuation account, proceeeding with remaining pre rules");
58  
59          copyICRFromAccount(document);
60  
61          return true;
62      }
63  
64      /**
65       * 
66       * This looks for the SubAccount's account number and then sets the values to the continuation account value if it exists
67       * @param maintenanceAction
68       */
69      protected void checkForContinuationAccounts(String maintenanceAction) {
70          LOG.debug("entering checkForContinuationAccounts()");
71  
72          /*
73           * KULCOA-734 - The check for continuation account for main Account Number on sub-account has been modified to only occur
74           * for a New and Copy Action. This cannot happen on an Edit as the primary key will change.
75           */
76          if (OLEConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction) || OLEConstants.MAINTENANCE_COPY_ACTION.equals(maintenanceAction)) {
77  
78              if (StringUtils.isNotBlank(newSubAccount.getAccountNumber())) {
79                  Account account = checkForContinuationAccount("Account Number", newSubAccount.getChartOfAccountsCode(), newSubAccount.getAccountNumber(), "");
80                  if (ObjectUtils.isNotNull(account)) { // override old user inputs
81                      newSubAccount.setAccountNumber(account.getAccountNumber());
82                      newSubAccount.setChartOfAccountsCode(account.getChartOfAccountsCode());
83                  }
84              }
85          }
86      }
87  
88      /**
89       * This method sets the convenience objects like newSubAccount, so you have short and easy handles to the new and
90       * old objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load
91       * all sub-objects from the DB by their primary keys, if available.
92       * @param document
93       */
94      protected void setupConvenienceObjects(MaintenanceDocument document) {
95  
96          // setup newAccount convenience objects, make sure all possible sub-objects are populated
97          newSubAccount = (SubAccount) document.getNewMaintainableObject().getBusinessObject();
98          // copyAccount = (SubAccount) ObjectUtils.deepCopy(newAccount);
99          // copyAccount.refresh();
100     }
101 
102     /**
103      * 
104      * This copies the Indirect Cost Rate (ICR) from the account if the SubAccount is a specific type - determined 
105      * as "EX" from {@link SubAccountRule#CG_A21_TYPE_ICR}
106      * <p>
107      * If it is "EX" it will then copy over the ICR information from the Account specified for this SubAccount
108      * @param document
109      */
110     protected void copyICRFromAccount(MaintenanceDocument document) {
111         Person user = GlobalVariables.getUserSession().getPerson();
112 
113         // get a new instance of MaintenanceDocumentAuthorizations for this context
114         MaintenanceDocumentRestrictions auths = SpringContext.getBean(BusinessObjectAuthorizationService.class).getMaintenanceDocumentRestrictions(document, user);
115 
116         // don't need to copy if the user does not have the authority to edit the fields
117         if (!auths.getFieldRestriction("a21SubAccount.financialIcrSeriesIdentifier").isReadOnly()) {
118             // only need to do this of the account sub type is EX
119             A21SubAccount a21SubAccount = newSubAccount.getA21SubAccount();
120             Account account = newSubAccount.getAccount();
121             if (OLEConstants.SubAccountType.EXPENSE.equals(a21SubAccount.getSubAccountTypeCode())) {
122                 if (ObjectUtils.isNull(account) || StringUtils.isBlank(account.getAccountNumber())) {
123                     account = getAccountService().getByPrimaryId(newSubAccount.getChartOfAccountsCode(), newSubAccount.getAccountNumber());
124                     if (ObjectUtils.isNotNull(account)) {
125                         if (a21SubAccount.getA21ActiveIndirectCostRecoveryAccounts().isEmpty()) {
126                             for (IndirectCostRecoveryAccount icrAccount : account.getActiveIndirectCostRecoveryAccounts()){
127                                 a21SubAccount.getA21IndirectCostRecoveryAccounts().add(A21IndirectCostRecoveryAccount.copyICRAccount(icrAccount));
128                             }
129                         }
130                         if (StringUtils.isBlank(a21SubAccount.getFinancialIcrSeriesIdentifier())) {
131                             a21SubAccount.setFinancialIcrSeriesIdentifier(account.getFinancialIcrSeriesIdentifier());
132                             a21SubAccount.setOffCampusCode(account.isAccountOffCampusIndicator());
133                         }
134                         if (StringUtils.isBlank(a21SubAccount.getIndirectCostRecoveryTypeCode())) {
135                             a21SubAccount.setIndirectCostRecoveryTypeCode(account.getAcctIndirectCostRcvyTypeCd());
136                         }
137                     }
138                 }
139             }
140         }
141     }
142 }