1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
43
44 public class TaxRegionRule extends KfsMaintenanceDocumentRuleBase {
45
46
47
48
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
72
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
88
89
90
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
107
108
109
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
125
126
127
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
145
146
147
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
165
166
167
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 }