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.service.impl;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.ole.sys.OLEConstants;
25  import org.kuali.ole.sys.businessobject.TaxRegion;
26  import org.kuali.ole.sys.businessobject.TaxRegionCounty;
27  import org.kuali.ole.sys.businessobject.TaxRegionPostalCode;
28  import org.kuali.ole.sys.businessobject.TaxRegionState;
29  import org.kuali.ole.sys.service.TaxRegionService;
30  import org.kuali.rice.krad.service.BusinessObjectService;
31  import org.kuali.rice.krad.util.ObjectUtils;
32  import org.kuali.rice.location.api.postalcode.PostalCode;
33  import org.kuali.rice.location.api.postalcode.PostalCodeService;
34  import org.springframework.transaction.annotation.Transactional;
35  
36  @Transactional
37  public class TaxRegionServiceImpl implements TaxRegionService {
38  
39      protected BusinessObjectService businessObjectService;
40      protected PostalCodeService postalCodeService;
41  
42      /**
43       * @see org.kuali.ole.sys.service.TaxRegionService#getSalesTaxRegions(java.lang.String)
44       */
45      @Override
46      public List<TaxRegion> getSalesTaxRegions(String postalCode) {
47  
48          List<TaxRegion> salesTaxRegions = new ArrayList<TaxRegion>();
49          if ( StringUtils.isNotBlank(postalCode) ) {
50              PostalCode postalCodeObj = postalCodeService.getPostalCode( OLEConstants.COUNTRY_CODE_UNITED_STATES, postalCode );
51          if(ObjectUtils.isNotNull(postalCodeObj)) {
52              salesTaxRegions.addAll(getPostalCodeTaxRegions(postalCodeObj.getCode(), postalCodeObj.getCountryCode(), false));
53              salesTaxRegions.addAll(getStateTaxRegions(postalCodeObj.getStateCode(), postalCodeObj.getCountryCode(), false));
54              salesTaxRegions.addAll(getCountyTaxRegions(postalCodeObj.getCountyCode(), postalCodeObj.getStateCode(), postalCodeObj.getCountryCode(), false));
55          }
56          }
57  
58          return salesTaxRegions;
59      }
60  
61      /**
62       * @see org.kuali.ole.sys.service.TaxRegionService#getUseTaxRegions(java.lang.String)
63       */
64      @Override
65      public List<TaxRegion> getUseTaxRegions(String postalCode) {
66  
67          List<TaxRegion> useTaxRegions = new ArrayList<TaxRegion>();
68  
69          if ( StringUtils.isNotBlank(postalCode) ) {
70              PostalCode postalCodeObj = postalCodeService.getPostalCode( OLEConstants.COUNTRY_CODE_UNITED_STATES, postalCode );
71          useTaxRegions.addAll(getPostalCodeTaxRegions(postalCodeObj.getCode(), postalCodeObj.getCountryCode(), true));
72          useTaxRegions.addAll(getStateTaxRegions(postalCodeObj.getStateCode(), postalCodeObj.getCountryCode(), true));
73          useTaxRegions.addAll(getCountyTaxRegions(postalCodeObj.getCountyCode(), postalCodeObj.getStateCode(), postalCodeObj.getCountryCode(), true));
74          }
75          return useTaxRegions;
76      }
77  
78      /**
79       * This method returns a list of tax regions that match postal code and country code.
80       * 
81       * @param postalCode postal code
82       * @param postalCountryCode country code
83       * @param useTaxOnly determines if only (use tax = true) tax regions are returned 
84       * @return
85       */
86      protected List<TaxRegion> getPostalCodeTaxRegions(String postalCode, String postalCountryCode, boolean useTaxOnly) {
87  
88          List<TaxRegion> postalCodeTaxRegions = new ArrayList<TaxRegion>();
89  
90          if (StringUtils.isNotEmpty(postalCode)) {
91              Map<String, Object> criteria = new HashMap<String, Object>();
92              criteria.put("postalCode", postalCode);
93              criteria.put("postalCountryCode", postalCountryCode);
94              criteria.put("active", true);
95              if (useTaxOnly) {
96                  criteria.put("taxRegion.taxRegionUseTaxIndicator", useTaxOnly);
97              }
98  
99              List<TaxRegionPostalCode> taxRegionPostalCodes = (List<TaxRegionPostalCode>) businessObjectService.findMatching(TaxRegionPostalCode.class, criteria);
100             for (TaxRegionPostalCode taxRegionPostalCode : taxRegionPostalCodes) {
101                 postalCodeTaxRegions.add(taxRegionPostalCode.getTaxRegion());
102             }
103         }
104         return postalCodeTaxRegions;
105     }
106 
107     /**
108      * This method returns a list of tax regions that match state code and country code.
109      * 
110      * @param stateCode state code
111      * @param postalCountryCode country code
112      * @param useTaxOnly determines if only (use tax = true) tax regions are returned
113      * @return
114      */
115     protected List<TaxRegion> getStateTaxRegions(String stateCode, String postalCountryCode, boolean useTaxOnly) {
116 
117         List<TaxRegion> stateTaxRegions = new ArrayList<TaxRegion>();
118 
119         if (StringUtils.isNotEmpty(stateCode)) {
120 
121             Map<String, Object> criteria = new HashMap<String, Object>();
122             criteria.put("stateCode", stateCode);
123             criteria.put("postalCountryCode", postalCountryCode);
124             criteria.put("active", true);
125             if (useTaxOnly) {
126                 criteria.put("taxRegion.taxRegionUseTaxIndicator", useTaxOnly);
127             }
128 
129             List<TaxRegionState> taxRegionStates = (List<TaxRegionState>) businessObjectService.findMatching(TaxRegionState.class, criteria);
130             for (TaxRegionState taxRegionState : taxRegionStates) {
131                 stateTaxRegions.add(taxRegionState.getTaxRegion());
132             }
133         }
134         return stateTaxRegions;
135     }
136 
137     /**
138      * This method returns a list of tax regions that match county code, state code, and country code
139      * @param countyCode county code
140      * @param stateCode state code
141      * @param postalCountryCode country code
142      * @param useTaxOnly determines if only (use tax = true) tax regions are returned
143      * @return
144      */
145     protected List<TaxRegion> getCountyTaxRegions(String countyCode, String stateCode, String postalCountryCode, boolean useTaxOnly) {
146 
147         List<TaxRegion> countyTaxRegions = new ArrayList<TaxRegion>();
148         if (StringUtils.isNotEmpty(countyCode)) {
149             Map<String, Object> criteria = new HashMap<String, Object>();
150             criteria.put("countyCode", countyCode);
151             criteria.put("stateCode", stateCode);
152             criteria.put("postalCountryCode", postalCountryCode);
153             criteria.put("active", true);
154             if (useTaxOnly) {
155                 criteria.put("taxRegion.taxRegionUseTaxIndicator", useTaxOnly);
156             }
157 
158             List<TaxRegionCounty> taxRegionCounties = (List<TaxRegionCounty>) businessObjectService.findMatching(TaxRegionCounty.class, criteria);
159             for (TaxRegionCounty taxRegionCounty : taxRegionCounties) {
160                 countyTaxRegions.add(taxRegionCounty.getTaxRegion());
161             }
162         }
163         return countyTaxRegions;
164     }
165 
166     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
167         this.businessObjectService = businessObjectService;
168     }
169 
170     public void setPostalCodeService(PostalCodeService postalCodeService) {
171         this.postalCodeService = postalCodeService;
172     }
173 }