001: /*
002: * Copyright 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.purap.rules;
017:
018: import org.apache.commons.lang.StringUtils;
019: import org.kuali.core.document.Document;
020: import org.kuali.core.service.KualiConfigurationService;
021: import org.kuali.kfs.KFSConstants;
022: import org.kuali.kfs.context.SpringContext;
023: import org.kuali.module.purap.PurapConstants;
024: import org.kuali.module.purap.PurapKeyConstants;
025: import org.kuali.module.purap.PurapConstants.PREQDocumentsStrings;
026: import org.kuali.module.purap.document.PaymentRequestDocument;
027: import org.kuali.module.purap.service.PurapService;
028:
029: /**
030: * Business pre rule(s) applicable to Payment Request documents.
031: */
032: public class PaymentRequestDocumentPreRules extends
033: AccountsPayableDocumentPreRulesBase {
034:
035: /**
036: * Default Constructor
037: */
038: public PaymentRequestDocumentPreRules() {
039: super ();
040: }
041:
042: /**
043: * Main hook point to perform rules check.
044: *
045: * @see org.kuali.core.rules.PreRulesContinuationBase#doRules(org.kuali.core.document.Document)
046: */
047: @Override
048: public boolean doRules(Document document) {
049: boolean preRulesOK = true;
050:
051: PaymentRequestDocument preq = (PaymentRequestDocument) document;
052: if ((!SpringContext.getBean(PurapService.class)
053: .isFullDocumentEntryCompleted(preq))
054: || (StringUtils
055: .equals(
056: preq.getStatusCode(),
057: PurapConstants.PaymentRequestStatuses.AWAITING_ACCOUNTS_PAYABLE_REVIEW))) {
058: if (!confirmPayDayNotOverThresholdDaysAway(preq)) {
059: return false;
060: }
061: }
062: preRulesOK &= super .doRules(document);
063: return preRulesOK;
064: }
065:
066: /**
067: * Prompts user to confirm with a Yes or No to a question being asked.
068: *
069: * @param questionType - type of question
070: * @param messageConstant - key to retrieve message
071: * @return - true if overriding, false otherwise
072: */
073: private boolean askForConfirmation(String questionType,
074: String messageConstant) {
075:
076: String questionText = SpringContext.getBean(
077: KualiConfigurationService.class).getPropertyString(
078: messageConstant);
079: if (questionText.contains("{")) {
080: questionText = prepareQuestionText(questionType,
081: questionText);
082: }
083:
084: boolean confirmOverride = super .askOrAnalyzeYesNoQuestion(
085: questionType, questionText);
086:
087: if (!confirmOverride) {
088: event.setActionForwardName(KFSConstants.MAPPING_BASIC);
089: return false;
090: }
091: return true;
092: }
093:
094: /**
095: * Creates the actual text of the question, replacing place holders like pay date threshold with an actual constant value.
096: *
097: * @param questionType - type of question
098: * @param questionText - actual text of question pulled from resource file
099: * @return - question text with place holders replaced
100: */
101: private String prepareQuestionText(String questionType,
102: String questionText) {
103: if (StringUtils.equals(questionType,
104: PREQDocumentsStrings.THRESHOLD_DAYS_OVERRIDE_QUESTION)) {
105: questionText = StringUtils
106: .replace(
107: questionText,
108: "{0}",
109: new Integer(
110: PurapConstants.PREQ_PAY_DATE_DAYS_BEFORE_WARNING)
111: .toString());
112: }
113: return questionText;
114: }
115:
116: /**
117: * Validates if the pay date threshold has not been passed, if so confirmation is required by the user to
118: * exceed the threshold.
119: *
120: * @param preq - payment request document
121: * @return - true if threshold has not been surpassed or if user confirmed ok to override, false otherwise
122: */
123: public boolean confirmPayDayNotOverThresholdDaysAway(
124: PaymentRequestDocument preq) {
125: // If the pay date is more than the threshold number of days in the future, ask for confirmation.
126: PaymentRequestDocumentRule rule = new PaymentRequestDocumentRule();
127: if (!rule.validatePayDateNotOverThresholdDaysAway(preq)) {
128: return askForConfirmation(
129: PREQDocumentsStrings.THRESHOLD_DAYS_OVERRIDE_QUESTION,
130: PurapKeyConstants.MESSAGE_PAYMENT_REQUEST_PAYDATE_OVER_THRESHOLD_DAYS);
131: }
132: return true;
133: }
134:
135: /**
136: * @see org.kuali.module.purap.rules.AccountsPayableDocumentPreRulesBase#getDocumentName()
137: */
138: @Override
139: public String getDocumentName() {
140: return "Payment Request";
141: }
142: }
|