View Javadoc
1   /*
2    * Copyright 2008 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.sys.document.validation.impl;
17  
18  import java.util.Date;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.ole.sys.OLEConstants;
22  import org.kuali.ole.sys.OLEKeyConstants;
23  import org.kuali.ole.sys.businessobject.TaxRegion;
24  import org.kuali.ole.sys.businessobject.TaxRegionCounty;
25  import org.kuali.ole.sys.businessobject.TaxRegionPostalCode;
26  import org.kuali.ole.sys.businessobject.TaxRegionRate;
27  import org.kuali.ole.sys.businessobject.TaxRegionState;
28  import org.kuali.ole.sys.context.SpringContext;
29  import org.kuali.rice.core.api.datetime.DateTimeService;
30  import org.kuali.rice.kns.document.MaintenanceDocument;
31  import org.kuali.rice.krad.bo.PersistableBusinessObject;
32  import org.kuali.rice.krad.util.GlobalVariables;
33  import org.kuali.rice.krad.util.ObjectUtils;
34  import org.kuali.rice.location.api.county.County;
35  import org.kuali.rice.location.api.county.CountyService;
36  import org.kuali.rice.location.api.postalcode.PostalCode;
37  import org.kuali.rice.location.api.postalcode.PostalCodeService;
38  import org.kuali.rice.location.api.state.State;
39  import org.kuali.rice.location.api.state.StateService;
40  
41  /**
42   * This class implements add collection line business rule for tax district rate.
43   */
44  public class TaxRegionRule extends KfsMaintenanceDocumentRuleBase {
45  
46      /**
47       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomAddCollectionLineBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument,
48       *      java.lang.String, org.kuali.rice.krad.bo.PersistableBusinessObject)
49       */
50      @Override
51      public boolean processCustomAddCollectionLineBusinessRules(MaintenanceDocument document, String collectionName, PersistableBusinessObject bo) {
52  
53          boolean success = true;
54          if (OLEConstants.TaxRegionConstants.TAX_REGION_RATES.equals(collectionName)) {
55              success &= isValidTaxRegionRate((TaxRegionRate) bo, null);
56          }
57          else if (OLEConstants.TaxRegionConstants.TAX_REGION_STATES.equals(collectionName)) {
58              success &= isValidTaxRegionState((TaxRegionState) bo);
59          }
60          else if (OLEConstants.TaxRegionConstants.TAX_REGION_COUNTIES.equals(collectionName)) {
61              success &= isValidTaxRegionCounty((TaxRegionCounty) bo);
62          }
63          else if (OLEConstants.TaxRegionConstants.TAX_REGION_POSTAL_CODES.equals(collectionName)) {
64              success &= isValidTaxRegionPostalCode((TaxRegionPostalCode) bo);
65          }
66  
67          return success;
68      }
69  
70      /**
71       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomAddCollectionLineBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument,
72       *      java.lang.String, org.kuali.rice.krad.bo.PersistableBusinessObject)
73       */
74      protected boolean isValidTaxRegionRate(TaxRegionRate taxRegionRate, TaxRegion taxRegion) {
75  
76          boolean success = true;
77          if (ObjectUtils.isNotNull(taxRegionRate)) {
78              success &= isValidEffectiveDate(taxRegionRate);
79              success &= isValidTaxRate(taxRegionRate);
80          }
81  
82          return success;
83      }
84  
85  
86      /**
87       * This method returns true if the effective date is not a date in the past or today's date.
88       *
89       * @param taxRegionRate
90       * @return
91       */
92      protected boolean isValidEffectiveDate(TaxRegionRate taxRegionRate) {
93          boolean success = true;
94          if (taxRegionRate.getEffectiveDate() != null) {
95              Date currentDate = SpringContext.getBean(DateTimeService.class).getCurrentDate();
96              int comparison = taxRegionRate.getEffectiveDate().compareTo(currentDate);
97              if (comparison == 0 || comparison < 0) {
98                  GlobalVariables.getMessageMap().putError(OLEConstants.TaxRegionConstants.TAX_REGION_EFFECTIVE_DATE, OLEKeyConstants.ERROR_DOCUMENT_TAX_REGION_CANT_ADD_PAST_OR_CURRENT_DATE_FOR_TAX_DISTRICT);
99                  success = false;
100             }
101         }
102         return success;
103     }
104 
105     /**
106      * This method returns true if the tax rate is between 0 and 1.
107      *
108      * @param taxRegionRate
109      * @return
110      */
111     protected boolean isValidTaxRate(TaxRegionRate taxRegionRate) {
112         boolean success = true;
113         if (taxRegionRate.getTaxRate() != null) {
114             if (taxRegionRate.getTaxRate().intValue() > 1 || taxRegionRate.getTaxRate().intValue() < 0) {
115                 GlobalVariables.getMessageMap().putError(OLEConstants.TaxRegionConstants.TAX_REGION_TAX_RATE, OLEKeyConstants.ERROR_DOCUMENT_TAX_REGION_TAX_RATE_BETWEEN0AND1);
116                 success = false;
117             }
118         }
119 
120         return success;
121     }
122 
123     /**
124      * This method returns true if the state on tax region state object is valid.
125      *
126      * @param taxRegionState
127      * @return
128      */
129     protected boolean isValidTaxRegionState(TaxRegionState taxRegionState) {
130         boolean success = true;
131 
132         if ( StringUtils.isNotBlank(taxRegionState.getPostalCountryCode()) && StringUtils.isNotBlank(taxRegionState.getStateCode()) ) {
133             State state = SpringContext.getBean(StateService.class).getState(taxRegionState.getPostalCountryCode(),taxRegionState.getStateCode());
134         if (ObjectUtils.isNull(state) || !state.isActive()) {
135             GlobalVariables.getMessageMap().putError(OLEConstants.TaxRegionConstants.TAX_REGION_STATE_CODE, OLEKeyConstants.ERROR_DOCUMENT_TAX_REGION_INVALID_STATE, taxRegionState.getStateCode());
136             success = false;
137         }
138         }
139 
140         return success;
141     }
142 
143     /**
144      * This method returns true if the state and county on the tax region county object is valid.
145      *
146      * @param taxRegionCounty
147      * @return
148      */
149     protected boolean isValidTaxRegionCounty(TaxRegionCounty taxRegionCounty) {
150         boolean success = true;
151 
152         if ( StringUtils.isNotBlank(taxRegionCounty.getPostalCountryCode()) && StringUtils.isNotBlank(taxRegionCounty.getStateCode()) && StringUtils.isNotBlank(taxRegionCounty.getCountyCode()) ) {
153             County county = SpringContext.getBean(CountyService.class).getCounty(taxRegionCounty.getPostalCountryCode(),taxRegionCounty.getStateCode(), taxRegionCounty.getCountyCode());
154         if (ObjectUtils.isNull(county) || !county.isActive()) {
155             GlobalVariables.getMessageMap().putError(OLEConstants.TaxRegionConstants.TAX_REGION_COUNTY_CODE, OLEKeyConstants.ERROR_DOCUMENT_TAX_REGION_INVALID_COUNTY, new String[] { taxRegionCounty.getCountyCode(), taxRegionCounty.getStateCode() });
156             success = false;
157         }
158         }
159 
160         return success;
161     }
162 
163     /**
164      * This method returns true if the postal code on the tax region postal code is valid.
165      *
166      * @param taxRegionPostalCode
167      * @return
168      */
169     protected boolean isValidTaxRegionPostalCode(TaxRegionPostalCode taxRegionPostalCode) {
170         boolean success = true;
171 
172         if ( StringUtils.isNotBlank(taxRegionPostalCode.getPostalCountryCode()) && StringUtils.isNotBlank(taxRegionPostalCode.getPostalCode()) ) {
173 
174             PostalCode postalZipCode = SpringContext.getBean(PostalCodeService.class).getPostalCode( taxRegionPostalCode.getPostalCountryCode(), taxRegionPostalCode.getPostalCode() );
175         if (ObjectUtils.isNull(postalZipCode) || !postalZipCode.isActive()) {
176             GlobalVariables.getMessageMap().putError(OLEConstants.TaxRegionConstants.TAX_REGION_POSTAL_CODE, OLEKeyConstants.ERROR_DOCUMENT_TAX_REGION_INVALID_POSTAL_CODE, taxRegionPostalCode.getPostalCode());
177             success = false;
178         }
179         }
180 
181         return success;
182     }
183 }