001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.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/ecl2.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.kfs.module.cg.document.validation.impl;
017    
018    import java.sql.Date;
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.kuali.kfs.coa.document.validation.impl.MaintenancePreRulesBase;
022    import org.kuali.kfs.module.cg.businessobject.Award;
023    import org.kuali.kfs.sys.KFSKeyConstants;
024    import org.kuali.kfs.sys.KFSPropertyConstants;
025    import org.kuali.kfs.sys.context.SpringContext;
026    import org.kuali.rice.kns.document.MaintenanceDocument;
027    import org.kuali.rice.kns.service.DataDictionaryService;
028    import org.kuali.rice.kns.service.KualiConfigurationService;
029    import org.kuali.rice.kns.util.KualiDecimal;
030    import org.kuali.rice.kns.util.ObjectUtils;
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.getLogger(AwardPreRules.class);
039    
040        protected KualiConfigurationService configService;
041        protected DataDictionaryService dataDictionaryService;
042    
043        protected Award newAward;
044    
045        /**
046         * Constructs a AwardPreRules.java.
047         */
048        public AwardPreRules() {
049            dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
050            configService = SpringContext.getBean(KualiConfigurationService.class);
051        }
052    
053        /**
054         * @see org.kuali.kfs.coa.document.validation.impl.MaintenancePreRulesBase#doCustomPreRules(org.kuali.rice.kns.document.MaintenanceDocument)
055         */
056        @Override
057        protected boolean doCustomPreRules(MaintenanceDocument document) {
058            setupConvenienceObjects(document);
059            boolean proceed = true;
060            if (proceed) {
061                proceed = continueIfEntryDateBeforeBeginDate();
062            }
063            if (proceed) {
064                proceed = continueIfSubcontractorTotalGreaterThanAwardTotal();
065            }
066    
067            if (!proceed) {
068                abortRulesCheck();
069            }
070    
071            return true;
072        }
073    
074        /**
075         * 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
076         * further validation is aborted and the user is returned to the award document.
077         * 
078         * @return true if the user selects yes, false otherwise
079         */
080        protected boolean continueIfEntryDateBeforeBeginDate() {
081            boolean proceed = true;
082            Date entryDate = newAward.getAwardEntryDate();
083            Date beginDate = newAward.getAwardBeginningDate();
084    
085            if (ObjectUtils.isNotNull(entryDate) && ObjectUtils.isNotNull(beginDate) && entryDate.before(beginDate)) {
086                String entryDateLabel = dataDictionaryService.getAttributeErrorLabel(Award.class, KFSPropertyConstants.AWARD_ENTRY_DATE);
087                String beginDateLabel = dataDictionaryService.getAttributeErrorLabel(Award.class, KFSPropertyConstants.AWARD_BEGINNING_DATE);
088                proceed = askOrAnalyzeYesNoQuestion("entryDateBeforeStartDate", buildConfirmationQuestion(KFSKeyConstants.WARNING_AWARD_ENTRY_BEFORE_START_DATE, entryDateLabel, beginDateLabel));
089            }
090            return proceed;
091        }
092    
093        /**
094         * Checks if the {@link Subcontractor} total amount is greater than the award total. If so asks the user if they want to
095         * continue validation. if no is selected further validation is aborted and the user is returned to the award document.
096         * 
097         * @return true if the user selects yes, false otherwise
098         */
099        protected boolean continueIfSubcontractorTotalGreaterThanAwardTotal() {
100            boolean proceed = true;
101    
102            KualiDecimal awardTotal = newAward.getAwardTotalAmount();
103            KualiDecimal subcontractorTotal = newAward.getAwardSubcontractorsTotalAmount();
104            if ((ObjectUtils.isNotNull(awardTotal) && subcontractorTotal.isGreaterThan(awardTotal)) || (ObjectUtils.isNull(awardTotal) && subcontractorTotal.isPositive())) {
105    
106                String subcontracorLabel = dataDictionaryService.getCollectionLabel(Award.class, KFSPropertyConstants.AWARD_SUBCONTRACTORS);
107                String awardLabel = dataDictionaryService.getAttributeErrorLabel(Award.class, KFSPropertyConstants.AWARD_TOTAL_AMOUNT);
108    
109                proceed = askOrAnalyzeYesNoQuestion("subcontractorTotalGreaterThanAwardTotal", buildConfirmationQuestion(KFSKeyConstants.WARNING_AWARD_SUBCONTRACTOR_TOTAL_GREATER_THAN_AWARD_TOTAL, subcontracorLabel, awardLabel));
110            }
111    
112            return proceed;
113        }
114    
115        /**
116         * Builds out the confirmation question.
117         * 
118         * @param messageKey
119         * @param parameters
120         * @return
121         */
122        protected String buildConfirmationQuestion(String messageKey, String... parameters) {
123            String result = configService.getPropertyString(messageKey);
124            if (null != parameters) {
125                for (int i = 0; i < parameters.length; i++) {
126                    result = StringUtils.replace(result, "{" + i + "}", parameters[i]);
127                }
128            }
129            return result;
130        }
131    
132        /**
133         * @param document
134         */
135        protected void setupConvenienceObjects(MaintenanceDocument document) {
136            // setup newAccount convenience objects, make sure all possible sub-objects are populated
137            newAward = (Award) document.getNewMaintainableObject().getBusinessObject();
138        }
139    
140    }