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.cg.rules;
017:
018: import java.sql.Date;
019:
020: import org.apache.commons.lang.StringUtils;
021: import org.kuali.core.document.MaintenanceDocument;
022: import org.kuali.core.service.DataDictionaryService;
023: import org.kuali.core.service.KualiConfigurationService;
024: import org.kuali.core.util.KualiDecimal;
025: import org.kuali.core.util.ObjectUtils;
026: import org.kuali.kfs.KFSKeyConstants;
027: import org.kuali.kfs.KFSPropertyConstants;
028: import org.kuali.kfs.context.SpringContext;
029: import org.kuali.module.cg.bo.Award;
030: import org.kuali.module.chart.rules.MaintenancePreRulesBase;
031:
032: /**
033: * PreRules checks for the Account that needs to occur while still in the Struts processing. This includes defaults, confirmations,
034: * etc.
035: */
036: public class AwardPreRules extends MaintenancePreRulesBase {
037:
038: protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
039: .getLogger(AwardPreRules.class);
040:
041: private KualiConfigurationService configService;
042: private DataDictionaryService dataDictionaryService;
043:
044: private Award newAward;
045:
046: /**
047: * Constructs a AwardPreRules.java.
048: */
049: public AwardPreRules() {
050: dataDictionaryService = SpringContext
051: .getBean(DataDictionaryService.class);
052: configService = SpringContext
053: .getBean(KualiConfigurationService.class);
054: }
055:
056: /**
057: * @see org.kuali.module.chart.rules.MaintenancePreRulesBase#doCustomPreRules(org.kuali.core.document.MaintenanceDocument)
058: */
059: @Override
060: protected boolean doCustomPreRules(MaintenanceDocument document) {
061: setupConvenienceObjects(document);
062: boolean proceed = true;
063: if (proceed) {
064: proceed = continueIfEntryDateBeforeBeginDate();
065: }
066: if (proceed) {
067: proceed = continueIfSubcontractorTotalGreaterThanAwardTotal();
068: }
069:
070: if (!proceed) {
071: abortRulesCheck();
072: }
073:
074: return true;
075: }
076:
077: /**
078: * Checks if the entry date is before the begin date. if so asks the user if they want to continue validation. if no is selected
079: * further validation is aborted and the user is returned to the award document.
080: *
081: * @return true if the user selects yes, false otherwise
082: */
083: private boolean continueIfEntryDateBeforeBeginDate() {
084: boolean proceed = true;
085: Date entryDate = newAward.getAwardEntryDate();
086: Date beginDate = newAward.getAwardBeginningDate();
087:
088: if (ObjectUtils.isNotNull(entryDate)
089: && ObjectUtils.isNotNull(beginDate)
090: && entryDate.before(beginDate)) {
091: String entryDateLabel = dataDictionaryService
092: .getAttributeErrorLabel(Award.class,
093: KFSPropertyConstants.AWARD_ENTRY_DATE);
094: String beginDateLabel = dataDictionaryService
095: .getAttributeErrorLabel(Award.class,
096: KFSPropertyConstants.AWARD_BEGINNING_DATE);
097: proceed = askOrAnalyzeYesNoQuestion(
098: "entryDateBeforeStartDate",
099: buildConfirmationQuestion(
100: KFSKeyConstants.WARNING_AWARD_ENTRY_BEFORE_START_DATE,
101: entryDateLabel, beginDateLabel));
102: }
103: return proceed;
104: }
105:
106: /**
107: * Checks if the {@link Subcontractor} total amount is greater than the award total. If so asks the user if they want to
108: * continue validation. if no is selected further validation is aborted and the user is returned to the award document.
109: *
110: * @return true if the user selects yes, false otherwise
111: */
112: private boolean continueIfSubcontractorTotalGreaterThanAwardTotal() {
113: boolean proceed = true;
114:
115: KualiDecimal awardTotal = newAward.getAwardTotalAmount();
116: KualiDecimal subcontractorTotal = newAward
117: .getAwardSubcontractorsTotalAmount();
118: if ((ObjectUtils.isNotNull(awardTotal) && subcontractorTotal
119: .isGreaterThan(awardTotal))
120: || (ObjectUtils.isNull(awardTotal) && subcontractorTotal
121: .isPositive())) {
122:
123: String subcontracorLabel = dataDictionaryService
124: .getCollectionLabel(Award.class,
125: KFSPropertyConstants.AWARD_SUBCONTRACTORS);
126: String awardLabel = dataDictionaryService
127: .getAttributeErrorLabel(Award.class,
128: KFSPropertyConstants.AWARD_TOTAL_AMOUNT);
129:
130: proceed = askOrAnalyzeYesNoQuestion(
131: "subcontractorTotalGreaterThanAwardTotal",
132: buildConfirmationQuestion(
133: KFSKeyConstants.WARNING_AWARD_SUBCONTRACTOR_TOTAL_GREATER_THAN_AWARD_TOTAL,
134: subcontracorLabel, awardLabel));
135: }
136:
137: return proceed;
138: }
139:
140: /**
141: * Builds out the confirmation question.
142: *
143: * @param messageKey
144: * @param parameters
145: * @return
146: */
147: protected String buildConfirmationQuestion(String messageKey,
148: String... parameters) {
149: String result = configService.getPropertyString(messageKey);
150: if (null != parameters) {
151: for (int i = 0; i < parameters.length; i++) {
152: result = StringUtils.replace(result, "{" + i + "}",
153: parameters[i]);
154: }
155: }
156: return result;
157: }
158:
159: /**
160: * @param document
161: */
162: private void setupConvenienceObjects(MaintenanceDocument document) {
163: // setup newAccount convenience objects, make sure all possible sub-objects are populated
164: newAward = (Award) document.getNewMaintainableObject()
165: .getBusinessObject();
166: }
167:
168: }
|