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