1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
35  
36  
37  public class SubAccountPreRules extends MaintenancePreRulesBase {
38  
39      protected SubAccount newSubAccount;
40  
41      
42  
43      public SubAccountPreRules() {
44  
45      }
46  
47      
48  
49  
50  
51  
52      protected boolean doCustomPreRules(MaintenanceDocument document) {
53          setupConvenienceObjects(document);
54          checkForContinuationAccounts(document.getNewMaintainableObject().getMaintenanceAction()); 
55                                                                                                      
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  
67  
68  
69      protected void checkForContinuationAccounts(String maintenanceAction) {
70          LOG.debug("entering checkForContinuationAccounts()");
71  
72          
73  
74  
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)) { 
81                      newSubAccount.setAccountNumber(account.getAccountNumber());
82                      newSubAccount.setChartOfAccountsCode(account.getChartOfAccountsCode());
83                  }
84              }
85          }
86      }
87  
88      
89  
90  
91  
92  
93  
94      protected void setupConvenienceObjects(MaintenanceDocument document) {
95  
96          
97          newSubAccount = (SubAccount) document.getNewMaintainableObject().getBusinessObject();
98          
99          
100     }
101 
102     
103 
104 
105 
106 
107 
108 
109 
110     protected void copyICRFromAccount(MaintenanceDocument document) {
111         Person user = GlobalVariables.getUserSession().getPerson();
112 
113         
114         MaintenanceDocumentRestrictions auths = SpringContext.getBean(BusinessObjectAuthorizationService.class).getMaintenanceDocumentRestrictions(document, user);
115 
116         
117         if (!auths.getFieldRestriction("a21SubAccount.financialIcrSeriesIdentifier").isReadOnly()) {
118             
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 }