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.Account;
20  import org.kuali.ole.coa.service.AccountService;
21  import org.kuali.ole.sys.OLEKeyConstants;
22  import org.kuali.ole.sys.context.SpringContext;
23  import org.kuali.rice.core.api.config.property.ConfigurationService;
24  import org.kuali.rice.kns.document.MaintenanceDocument;
25  import org.kuali.rice.kns.rules.PromptBeforeValidationBase;
26  import org.kuali.rice.krad.document.Document;
27  import org.kuali.rice.krad.util.ObjectUtils;
28  
29  /**
30   * General PreRules checks for all Maintenance docs that needs to occur while still in the Struts processing.
31   */
32  public class MaintenancePreRulesBase extends PromptBeforeValidationBase {
33  
34      protected ConfigurationService configService;
35      protected AccountService accountService;
36  
37      /**
38       * Constructs a MaintenancePreRulesBase class and injects some services through setters
39       * 
40       * @TODO: should be fixed in the future to use Spring to inject these services
41       */
42      public MaintenancePreRulesBase() {
43          // Pseudo-inject some services.
44          //
45          // This approach is being used to make it simpler to convert the Rule classes
46          // to spring-managed with these services injected by Spring at some later date.
47          // When this happens, just remove these calls to the setters with
48          // SpringContext, and configure the bean defs for spring.
49          setAccountService(SpringContext.getBean(AccountService.class));
50          setConfigService(SpringContext.getBean(ConfigurationService.class));
51      }
52  
53      public void setAccountService(AccountService accountService) {
54          this.accountService = accountService;
55      }
56  
57      public void setConfigService(ConfigurationService configService) {
58          this.configService = configService;
59      }
60  
61      /**
62       * This is called from the rules service to execute our rules A hook is provided here for sub-classes to override the
63       * {@link MaintenancePreRulesBase#doCustomPreRules(MaintenanceDocument)}
64       * 
65       * @see org.kuali.rice.kns.rules.PromptBeforeValidationBase#doRules(org.kuali.rice.krad.document.Document)
66       */
67      @Override
68      public boolean doPrompts(Document document) {
69          MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
70          return doCustomPreRules(maintenanceDocument);
71      }
72  
73      /**
74       * This is a hook for sub-classes to implement their own pre-rules. Override to get hooked into main class
75       * 
76       * @param maintenanceDocument
77       * @return true if rules pass
78       */
79      protected boolean doCustomPreRules(MaintenanceDocument maintenanceDocument) {
80          return true;
81      }
82  
83      /**
84       * This method checks for continuation accounts, returns the continuation account if it is found, null otherwise
85       * 
86       * @param accName
87       * @param chart
88       * @param accountNumber
89       * @param accountName
90       * @param allowExpiredAccount
91       * @return the continuation account if it is found, null otherwise
92       */
93      protected Account checkForContinuationAccount(String accName, String chart, String accountNumber, String accountName, boolean allowExpiredAccount) {
94          Account result = checkForContinuationAccount(accName, chart, accountNumber, accountName);
95          if (!allowExpiredAccount) {
96              if (result.isExpired()) {
97                  return null;
98              }
99          }
100         return result;
101     }
102 
103     /**
104      * This method checks for continuation accounts and presents the user with a question regarding their use on this account.
105      * 
106      * @param accName
107      * @param chart
108      * @param accountNumber
109      * @param accountName
110      * @return
111      */
112     protected Account checkForContinuationAccount(String accName, String chart, String accountNumber, String accountName) {
113         if (LOG.isDebugEnabled()) {
114             LOG.debug("entering checkForContinuationAccounts(" + accountNumber + ")");
115         }
116         if (StringUtils.isBlank(accountNumber) || StringUtils.isBlank(chart))
117             return null;
118 
119         Account account = accountService.getByPrimaryId(chart, accountNumber);
120 
121         if (ObjectUtils.isNotNull(account) && !account.isExpired()) { // no need for a continuation account
122             return null;
123         }
124 
125         boolean useContinuationAccount = true;
126 
127         while (ObjectUtils.isNotNull(account) && account.isExpired() && useContinuationAccount) {
128             if (LOG.isDebugEnabled()) {
129                 LOG.debug("Expired account: " + accountNumber);    
130             }
131             String continuationAccountNumber = account.getContinuationAccountNumber();
132 
133             useContinuationAccount = askOrAnalyzeYesNoQuestion("ContinuationAccount" + accName + accountNumber, buildContinuationConfirmationQuestion(accName, accountNumber, continuationAccountNumber));
134             if (useContinuationAccount) {
135                 String continuationChart = account.getContinuationFinChrtOfAcctCd();
136                 account = accountService.getByPrimaryId(continuationChart, continuationAccountNumber);
137 
138                 if (ObjectUtils.isNotNull(account)) {
139                     accountNumber = account.getAccountNumber();
140                 }
141 
142                 if (LOG.isDebugEnabled()) {
143                     LOG.debug("Selected continuation account: " + account);
144                 }
145             }
146         }
147         return account;
148 
149     }
150 
151 
152     /**
153      * This method builds up the continuation account confirmation question that will be presented to the user
154      * 
155      * @param accName
156      * @param expiredAccount
157      * @param continuationAccount
158      * @return the question to the user about the continuation account
159      */
160     protected String buildContinuationConfirmationQuestion(String accName, String expiredAccount, String continuationAccount) {
161         String result = configService.getPropertyValueAsString(OLEKeyConstants.QUESTION_CONTINUATION_ACCOUNT_SELECTION);
162         result = StringUtils.replace(result, "{0}", accName);
163         result = StringUtils.replace(result, "{1}", expiredAccount);
164         result = StringUtils.replace(result, "{2}", continuationAccount);
165         return result;
166     }
167 
168     public AccountService getAccountService() {
169         return accountService;
170     }
171 
172     public ConfigurationService getConfigService() {
173         return configService;
174     }
175 
176 }