001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.chart.rules;
017:
018: import org.apache.commons.lang.StringUtils;
019: import org.kuali.core.document.Document;
020: import org.kuali.core.document.MaintenanceDocument;
021: import org.kuali.core.rules.PreRulesContinuationBase;
022: import org.kuali.core.service.DocumentAuthorizationService;
023: import org.kuali.core.service.KualiConfigurationService;
024: import org.kuali.core.util.ObjectUtils;
025: import org.kuali.kfs.KFSKeyConstants;
026: import org.kuali.kfs.context.SpringContext;
027: import org.kuali.module.chart.bo.Account;
028: import org.kuali.module.chart.service.AccountService;
029:
030: /**
031: * General PreRules checks for all Maintenance docs that needs to occur while still in the Struts processing.
032: */
033: public class MaintenancePreRulesBase extends PreRulesContinuationBase {
034:
035: private KualiConfigurationService configService;
036: private AccountService accountService;
037: private DocumentAuthorizationService documentAuthorizationService;
038:
039: /**
040: * Constructs a MaintenancePreRulesBase class and injects some services through setters
041: *
042: * @TODO: should be fixed in the future to use Spring to inject these services
043: */
044: public MaintenancePreRulesBase() {
045: // Pseudo-inject some services.
046: //
047: // This approach is being used to make it simpler to convert the Rule classes
048: // to spring-managed with these services injected by Spring at some later date.
049: // When this happens, just remove these calls to the setters with
050: // SpringContext, and configure the bean defs for spring.
051: setAccountService(SpringContext.getBean(AccountService.class));
052: setConfigService(SpringContext
053: .getBean(KualiConfigurationService.class));
054: setDocumentAuthorizationService(SpringContext
055: .getBean(DocumentAuthorizationService.class));
056: }
057:
058: public void setAccountService(AccountService accountService) {
059: this .accountService = accountService;
060: }
061:
062: public void setConfigService(KualiConfigurationService configService) {
063: this .configService = configService;
064: }
065:
066: /**
067: * This is called from the rules service to execute our rules A hook is provided here for sub-classes to override the
068: * {@link MaintenancePreRulesBase#doCustomPreRules(MaintenanceDocument)}
069: *
070: * @see org.kuali.core.rules.PreRulesContinuationBase#doRules(org.kuali.core.document.Document)
071: */
072: public boolean doRules(Document document) {
073: MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
074: return doCustomPreRules(maintenanceDocument);
075: }
076:
077: /**
078: * This is a hook for sub-classes to implement their own pre-rules. Override to get hooked into main class
079: *
080: * @param maintenanceDocument
081: * @return true if rules pass
082: */
083: protected boolean doCustomPreRules(
084: MaintenanceDocument maintenanceDocument) {
085: return true;
086: }
087:
088: /**
089: * This method checks for continuation accounts, returns the continuation account if it is found, null otherwise
090: *
091: * @param accName
092: * @param chart
093: * @param accountNumber
094: * @param accountName
095: * @param allowExpiredAccount
096: * @return the continuation account if it is found, null otherwise
097: */
098: protected Account checkForContinuationAccount(String accName,
099: String chart, String accountNumber, String accountName,
100: boolean allowExpiredAccount) {
101: Account result = checkForContinuationAccount(accName, chart,
102: accountNumber, accountName);
103: if (!allowExpiredAccount) {
104: if (result.isExpired()) {
105: return null;
106: }
107: }
108: return result;
109: }
110:
111: /**
112: * This method checks for continuation accounts and presents the user with a question regarding their use on this account.
113: *
114: * @param accName
115: * @param chart
116: * @param accountNumber
117: * @param accountName
118: * @return
119: */
120: protected Account checkForContinuationAccount(String accName,
121: String chart, String accountNumber, String accountName) {
122: if (LOG.isDebugEnabled()) {
123: LOG.debug("entering checkForContinuationAccounts("
124: + accountNumber + ")");
125: }
126: if (StringUtils.isBlank(accountNumber)
127: || StringUtils.isBlank(chart))
128: return null;
129:
130: Account account = accountService.getByPrimaryId(chart,
131: accountNumber);
132:
133: if (ObjectUtils.isNotNull(account) && !account.isExpired()) { // no need for a continuation account
134: return null;
135: }
136:
137: boolean useContinuationAccount = true;
138:
139: while (ObjectUtils.isNotNull(account) && account.isExpired()
140: && useContinuationAccount) {
141: LOG.debug("Expired account: " + accountNumber);
142: String continuationAccountNumber = account
143: .getContinuationAccountNumber();
144:
145: useContinuationAccount = askOrAnalyzeYesNoQuestion(
146: "ContinuationAccount" + accName + accountNumber,
147: buildContinuationConfirmationQuestion(accName,
148: accountNumber, continuationAccountNumber));
149: if (useContinuationAccount) {
150: String continuationChart = account
151: .getContinuationFinChrtOfAcctCd();
152: account = accountService.getByPrimaryId(
153: continuationChart, continuationAccountNumber);
154:
155: if (ObjectUtils.isNotNull(account)) {
156: accountNumber = account.getAccountNumber();
157: }
158:
159: if (LOG.isDebugEnabled()) {
160: LOG.debug("Selected continuation account: "
161: + account);
162: }
163: }
164: }
165: return account;
166:
167: }
168:
169: /**
170: * This method builds up the continuation account confirmation question that will be presented to the user
171: *
172: * @param accName
173: * @param expiredAccount
174: * @param continuationAccount
175: * @return the question to the user about the continuation account
176: */
177: protected String buildContinuationConfirmationQuestion(
178: String accName, String expiredAccount,
179: String continuationAccount) {
180: String result = configService
181: .getPropertyString(KFSKeyConstants.QUESTION_CONTINUATION_ACCOUNT_SELECTION);
182: result = StringUtils.replace(result, "{0}", accName);
183: result = StringUtils.replace(result, "{1}", expiredAccount);
184: result = StringUtils
185: .replace(result, "{2}", continuationAccount);
186: return result;
187: }
188:
189: public AccountService getAccountService() {
190: return accountService;
191: }
192:
193: public KualiConfigurationService getConfigService() {
194: return configService;
195: }
196:
197: /**
198: * Gets the documentAuthorizationService attribute.
199: *
200: * @return Returns the documentAuthorizationService.
201: */
202: protected final DocumentAuthorizationService getDocumentAuthorizationService() {
203: return documentAuthorizationService;
204: }
205:
206: /**
207: * Sets the documentAuthorizationService attribute value.
208: *
209: * @param documentAuthorizationService The documentAuthorizationService to set.
210: */
211: public final void setDocumentAuthorizationService(
212: DocumentAuthorizationService documentAuthorizationService) {
213: this.documentAuthorizationService = documentAuthorizationService;
214: }
215:
216: }
|