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.AmountTotaling;
020: import org.kuali.core.document.Document;
021: import org.kuali.core.rules.PreRulesContinuationBase;
022: import org.kuali.core.service.KualiConfigurationService;
023: import org.kuali.core.util.GlobalVariables;
024: import org.kuali.core.util.KualiDecimal;
025: import org.kuali.core.util.ObjectUtils;
026: import org.kuali.kfs.KFSConstants;
027: import org.kuali.kfs.context.SpringContext;
028: import org.kuali.module.purap.PurapConstants;
029: import org.kuali.module.purap.PurapKeyConstants;
030: import org.kuali.module.purap.document.PurchaseOrderDocument;
031: import org.kuali.module.purap.document.PurchasingDocument;
032:
033: /**
034: * Business Prerules applicable to purchase order document.
035: */
036: public class PurchaseOrderDocumentPreRules extends
037: PreRulesContinuationBase {
038:
039: /**
040: * Overrides the method in PreRulesContinuationBase to also invoke the confirmNotToExceedOverride if the PreRulesCheckEvent is
041: * blank and the question matches with the OverrideNotToExceed
042: *
043: * @param document The purchase order document upon which we're performing the prerules logic.
044: * @return boolean true if it passes the pre rules conditions.
045: * @see org.kuali.core.rules.PreRulesContinuationBase#doRules(org.kuali.core.document.Document)
046: */
047: @Override
048: public boolean doRules(Document document) {
049:
050: boolean preRulesOK = true;
051:
052: PurchaseOrderDocument purchaseOrderDocument = (PurchaseOrderDocument) document;
053:
054: if (StringUtils.isBlank(event.getQuestionContext())
055: || StringUtils
056: .equals(
057: question,
058: PurapConstants.PO_OVERRIDE_NOT_TO_EXCEED_QUESTION)) {
059: preRulesOK &= confirmNotToExceedOverride(purchaseOrderDocument);
060: }
061:
062: return preRulesOK;
063: }
064:
065: /**
066: * Checks whether the 'Not-to-exceed' amount has been exceeded by the purchase order total dollar limit. If so, it
067: * prompts the user for confirmation.
068: *
069: * @param purchaseOrderDocument The current PurchaseOrderDocument
070: * @return True if the 'Not-to-exceed' amount is to be overridden or if the total dollar amount is less than the purchase order
071: * total dollar limit.
072: */
073: private boolean confirmNotToExceedOverride(
074: PurchaseOrderDocument purchaseOrderDocument) {
075:
076: // If the total exceeds the limit, ask for confirmation.
077: if (!validateTotalDollarAmountIsLessThanPurchaseOrderTotalLimit(purchaseOrderDocument)) {
078: String questionText = SpringContext
079: .getBean(KualiConfigurationService.class)
080: .getPropertyString(
081: PurapKeyConstants.PURCHASE_ORDER_QUESTION_OVERRIDE_NOT_TO_EXCEED);
082:
083: boolean confirmOverride = super .askOrAnalyzeYesNoQuestion(
084: PurapConstants.PO_OVERRIDE_NOT_TO_EXCEED_QUESTION,
085: questionText);
086:
087: // Set a marker to record that this method has been used.
088: if (confirmOverride
089: && StringUtils.isBlank(event.getQuestionContext())) {
090: event
091: .setQuestionContext(PurapConstants.PO_OVERRIDE_NOT_TO_EXCEED_QUESTION);
092: }
093:
094: if (!confirmOverride) {
095: event.setActionForwardName(KFSConstants.MAPPING_BASIC);
096:
097: return false;
098: }
099: }
100:
101: return true;
102: }
103:
104: /**
105: * Validate that if the PurchaseOrderTotalLimit is not null then the TotalDollarAmount cannot be greater than the
106: * PurchaseOrderTotalLimit.
107: *
108: * @param purDocument The purchase order document to be validated.
109: * @return True if the TotalDollarAmount is less than the PurchaseOrderTotalLimit. False otherwise.
110: */
111: public boolean validateTotalDollarAmountIsLessThanPurchaseOrderTotalLimit(
112: PurchasingDocument purDocument) {
113: boolean valid = true;
114: if (ObjectUtils.isNotNull(purDocument
115: .getPurchaseOrderTotalLimit())
116: && ObjectUtils.isNotNull(((AmountTotaling) purDocument)
117: .getTotalDollarAmount())) {
118: KualiDecimal totalAmount = ((AmountTotaling) purDocument)
119: .getTotalDollarAmount();
120: if (((AmountTotaling) purDocument).getTotalDollarAmount()
121: .isGreaterThan(
122: purDocument.getPurchaseOrderTotalLimit())) {
123: valid &= false;
124: GlobalVariables
125: .getMessageList()
126: .add(
127: PurapKeyConstants.PO_TOTAL_GREATER_THAN_PO_TOTAL_LIMIT);
128: }
129: }
130:
131: return valid;
132: }
133:
134: }
|