View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.cg.document.validation.impl;
17  
18  import java.sql.Date;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.ole.coa.document.validation.impl.MaintenancePreRulesBase;
22  import org.kuali.ole.module.cg.businessobject.Award;
23  import org.kuali.ole.sys.OLEKeyConstants;
24  import org.kuali.ole.sys.OLEPropertyConstants;
25  import org.kuali.ole.sys.context.SpringContext;
26  import org.kuali.rice.core.api.config.property.ConfigurationService;
27  import org.kuali.rice.core.api.util.type.KualiDecimal;
28  import org.kuali.rice.kns.document.MaintenanceDocument;
29  import org.kuali.rice.kns.service.DataDictionaryService;
30  import org.kuali.rice.krad.util.ObjectUtils;
31  
32  /**
33   * PreRules checks for the Account that needs to occur while still in the Struts processing. This includes defaults, confirmations,
34   * etc.
35   */
36  public class AwardPreRules extends MaintenancePreRulesBase {
37  
38      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AwardPreRules.class);
39  
40      protected ConfigurationService configService;
41      protected DataDictionaryService dataDictionaryService;
42  
43      protected Award newAward;
44  
45      /**
46       * Constructs a AwardPreRules.java.
47       */
48      public AwardPreRules() {
49          dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
50          configService = SpringContext.getBean(ConfigurationService.class);
51      }
52  
53      /**
54       * @see org.kuali.ole.coa.document.validation.impl.MaintenancePreRulesBase#doCustomPreRules(org.kuali.rice.kns.document.MaintenanceDocument)
55       */
56      @Override
57      protected boolean doCustomPreRules(MaintenanceDocument document) {
58          setupConvenienceObjects(document);
59          boolean proceed = true;
60          if (proceed) {
61              proceed = continueIfEntryDateBeforeBeginDate();
62          }
63          if (proceed) {
64              proceed = continueIfSubcontractorTotalGreaterThanAwardTotal();
65          }
66  
67          if (!proceed) {
68              abortRulesCheck();
69          }
70  
71          return true;
72      }
73  
74      /**
75       * 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
76       * further validation is aborted and the user is returned to the award document.
77       * 
78       * @return true if the user selects yes, false otherwise
79       */
80      protected boolean continueIfEntryDateBeforeBeginDate() {
81          boolean proceed = true;
82          Date entryDate = newAward.getAwardEntryDate();
83          Date beginDate = newAward.getAwardBeginningDate();
84  
85          if (ObjectUtils.isNotNull(entryDate) && ObjectUtils.isNotNull(beginDate) && entryDate.before(beginDate)) {
86              String entryDateLabel = dataDictionaryService.getAttributeErrorLabel(Award.class, OLEPropertyConstants.AWARD_ENTRY_DATE);
87              String beginDateLabel = dataDictionaryService.getAttributeErrorLabel(Award.class, OLEPropertyConstants.AWARD_BEGINNING_DATE);
88              proceed = askOrAnalyzeYesNoQuestion("entryDateBeforeStartDate", buildConfirmationQuestion(OLEKeyConstants.WARNING_AWARD_ENTRY_BEFORE_START_DATE, entryDateLabel, beginDateLabel));
89          }
90          return proceed;
91      }
92  
93      /**
94       * Checks if the {@link Subcontractor} total amount is greater than the award total. If so asks the user if they want to
95       * continue validation. if no is selected further validation is aborted and the user is returned to the award document.
96       * 
97       * @return true if the user selects yes, false otherwise
98       */
99      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, OLEPropertyConstants.AWARD_SUBCONTRACTORS);
107             String awardLabel = dataDictionaryService.getAttributeErrorLabel(Award.class, OLEPropertyConstants.AWARD_TOTAL_AMOUNT);
108 
109             proceed = askOrAnalyzeYesNoQuestion("subcontractorTotalGreaterThanAwardTotal", buildConfirmationQuestion(OLEKeyConstants.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.getPropertyValueAsString(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 }