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.sys.document.validation.impl;
017
018 import java.util.Date;
019 import java.util.HashMap;
020 import java.util.Map;
021
022 import org.apache.commons.lang.StringUtils;
023 import org.kuali.kfs.sys.KFSConstants;
024 import org.kuali.kfs.sys.KFSKeyConstants;
025 import org.kuali.rice.kns.bo.County;
026 import org.kuali.rice.kns.bo.PostalCode;
027 import org.kuali.rice.kns.bo.State;
028 import org.kuali.kfs.sys.businessobject.TaxRegion;
029 import org.kuali.kfs.sys.businessobject.TaxRegionCounty;
030 import org.kuali.kfs.sys.businessobject.TaxRegionPostalCode;
031 import org.kuali.kfs.sys.businessobject.TaxRegionRate;
032 import org.kuali.kfs.sys.businessobject.TaxRegionState;
033 import org.kuali.kfs.sys.context.SpringContext;
034 import org.kuali.rice.kns.service.CountyService;
035 import org.kuali.rice.kns.service.PostalCodeService;
036 import org.kuali.rice.kns.service.StateService;
037 import org.kuali.rice.kns.bo.PersistableBusinessObject;
038 import org.kuali.rice.kns.document.MaintenanceDocument;
039 import org.kuali.rice.kns.service.BusinessObjectService;
040 import org.kuali.rice.kns.service.DateTimeService;
041 import org.kuali.rice.kns.util.GlobalVariables;
042 import org.kuali.rice.kns.util.ObjectUtils;
043
044 /**
045 * This class implements add collection line business rule for tax district rate.
046 */
047 public class TaxRegionRule extends KfsMaintenanceDocumentRuleBase {
048
049 protected BusinessObjectService businessObjectService;
050
051 public TaxRegionRule() {
052 businessObjectService = SpringContext.getBean(BusinessObjectService.class);
053 }
054
055 /**
056 * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomAddCollectionLineBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument,
057 * java.lang.String, org.kuali.rice.kns.bo.PersistableBusinessObject)
058 */
059 @Override
060 public boolean processCustomAddCollectionLineBusinessRules(MaintenanceDocument document, String collectionName, PersistableBusinessObject bo) {
061
062 boolean success = true;
063 if (KFSConstants.TaxRegionConstants.TAX_REGION_RATES.equals(collectionName)) {
064 success &= isValidTaxRegionRate((TaxRegionRate) bo, null);
065 }
066 else if (KFSConstants.TaxRegionConstants.TAX_REGION_STATES.equals(collectionName)) {
067 success &= isValidTaxRegionState((TaxRegionState) bo);
068 }
069 else if (KFSConstants.TaxRegionConstants.TAX_REGION_COUNTIES.equals(collectionName)) {
070 success &= isValidTaxRegionCounty((TaxRegionCounty) bo);
071 }
072 else if (KFSConstants.TaxRegionConstants.TAX_REGION_POSTAL_CODES.equals(collectionName)) {
073 success &= isValidTaxRegionPostalCode((TaxRegionPostalCode) bo);
074 }
075
076 return success;
077 }
078
079 /**
080 * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomAddCollectionLineBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument,
081 * java.lang.String, org.kuali.rice.kns.bo.PersistableBusinessObject)
082 */
083 protected boolean isValidTaxRegionRate(TaxRegionRate taxRegionRate, TaxRegion taxRegion) {
084
085 boolean success = true;
086 if (ObjectUtils.isNotNull(taxRegionRate)) {
087 success &= isValidEffectiveDate(taxRegionRate);
088 success &= isValidTaxRate(taxRegionRate);
089 }
090
091 return success;
092 }
093
094
095 /**
096 * This method returns true if the effective date is not a date in the past or today's date.
097 *
098 * @param taxRegionRate
099 * @return
100 */
101 protected boolean isValidEffectiveDate(TaxRegionRate taxRegionRate) {
102 boolean success = true;
103 if (taxRegionRate.getEffectiveDate() != null) {
104 Date currentDate = SpringContext.getBean(DateTimeService.class).getCurrentDate();
105 int comparison = taxRegionRate.getEffectiveDate().compareTo(currentDate);
106 if (comparison == 0 || comparison < 0) {
107 GlobalVariables.getMessageMap().putError(KFSConstants.TaxRegionConstants.TAX_REGION_EFFECTIVE_DATE, KFSKeyConstants.ERROR_DOCUMENT_TAX_REGION_CANT_ADD_PAST_OR_CURRENT_DATE_FOR_TAX_DISTRICT);
108 success = false;
109 }
110 }
111 return success;
112 }
113
114 /**
115 * This method returns true if the tax rate is between 0 and 1.
116 *
117 * @param taxRegionRate
118 * @return
119 */
120 protected boolean isValidTaxRate(TaxRegionRate taxRegionRate) {
121 boolean success = true;
122 if (taxRegionRate.getTaxRate() != null) {
123 if (taxRegionRate.getTaxRate().intValue() > 1 || taxRegionRate.getTaxRate().intValue() < 0) {
124 GlobalVariables.getMessageMap().putError(KFSConstants.TaxRegionConstants.TAX_REGION_TAX_RATE, KFSKeyConstants.ERROR_DOCUMENT_TAX_REGION_TAX_RATE_BETWEEN0AND1);
125 success = false;
126 }
127 }
128
129 return success;
130 }
131
132 /**
133 * This method returns true if the state on tax region state object is valid.
134 *
135 * @param taxRegionState
136 * @return
137 */
138 protected boolean isValidTaxRegionState(TaxRegionState taxRegionState) {
139 boolean success = true;
140
141 State state = SpringContext.getBean(StateService.class).getByPrimaryId(taxRegionState.getStateCode());
142 if (ObjectUtils.isNull(state) || !state.isActive()) {
143 GlobalVariables.getMessageMap().putError(KFSConstants.TaxRegionConstants.TAX_REGION_STATE_CODE, KFSKeyConstants.ERROR_DOCUMENT_TAX_REGION_INVALID_STATE, taxRegionState.getStateCode());
144 success = false;
145 }
146
147 return success;
148 }
149
150 /**
151 * This method returns true if the state and county on the tax region county object is valid.
152 *
153 * @param taxRegionCounty
154 * @return
155 */
156 protected boolean isValidTaxRegionCounty(TaxRegionCounty taxRegionCounty) {
157 boolean success = true;
158
159 County county = SpringContext.getBean(CountyService.class).getByPrimaryId(taxRegionCounty.getStateCode(), taxRegionCounty.getCountyCode());
160 if (ObjectUtils.isNull(county) || !county.isActive()) {
161 GlobalVariables.getMessageMap().putError(KFSConstants.TaxRegionConstants.TAX_REGION_COUNTY_CODE, KFSKeyConstants.ERROR_DOCUMENT_TAX_REGION_INVALID_COUNTY, new String[] { taxRegionCounty.getCountyCode(), taxRegionCounty.getStateCode() });
162 success = false;
163 }
164
165 return success;
166 }
167
168 /**
169 * This method returns true if the postal code on the tax region postal code is valid.
170 *
171 * @param taxRegionPostalCode
172 * @return
173 */
174 protected boolean isValidTaxRegionPostalCode(TaxRegionPostalCode taxRegionPostalCode) {
175 boolean success = true;
176
177 PostalCode postalZipCode = SpringContext.getBean(PostalCodeService.class).getByPostalCodeInDefaultCountry(taxRegionPostalCode.getPostalCode());
178 if (ObjectUtils.isNull(postalZipCode) || !postalZipCode.isActive()) {
179 GlobalVariables.getMessageMap().putError(KFSConstants.TaxRegionConstants.TAX_REGION_POSTAL_CODE, KFSKeyConstants.ERROR_DOCUMENT_TAX_REGION_INVALID_POSTAL_CODE, taxRegionPostalCode.getPostalCode());
180 success = false;
181 }
182
183 return success;
184 }
185 }