View Javadoc

1   /**
2    * Copyright 2005-2012 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.rice.location.impl.country;
17  
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
21  import org.kuali.rice.core.api.exception.RiceIllegalStateException;
22  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
23  import org.kuali.rice.kns.service.KNSServiceLocator;
24  import org.kuali.rice.kns.util.KNSConstants;
25  import org.kuali.rice.krad.service.BusinessObjectService;
26  import org.kuali.rice.krad.util.KRADConstants;
27  import org.kuali.rice.krad.util.KRADPropertyConstants;
28  import org.kuali.rice.location.api.country.Country;
29  import org.kuali.rice.location.api.country.CountryService;
30  
31  import java.util.ArrayList;
32  import java.util.Collection;
33  import java.util.Collections;
34  import java.util.HashMap;
35  import java.util.List;
36  import java.util.Map;
37  
38  public final class CountryServiceImpl implements CountryService {
39  
40      private BusinessObjectService businessObjectService;
41      private ParameterService parameterService;
42  
43      @Override
44      public Country getCountry(final String code) {
45          if (StringUtils.isBlank(code)) {
46              throw new RiceIllegalArgumentException("code is blank");
47          }
48  
49          CountryBo countryBo = businessObjectService.findByPrimaryKey(CountryBo.class, Collections.singletonMap(
50                  KRADPropertyConstants.POSTAL_COUNTRY_CODE, code));
51  
52          return CountryBo.to(countryBo);
53      }
54  
55      @Override
56      public Country getCountryByAlternateCode(final String alternateCode) {
57          if (StringUtils.isBlank(alternateCode)) {
58              throw new RiceIllegalArgumentException("alt code is blank");
59          }
60  
61          Collection<CountryBo> countryList = businessObjectService.findMatching(CountryBo.class, Collections.singletonMap(
62                  KRADPropertyConstants.ALTERNATE_POSTAL_COUNTRY_CODE, alternateCode));
63          if (countryList == null || countryList.isEmpty()) {
64              return null;
65          } else if (countryList.size() == 1) {
66              return CountryBo.to(countryList.iterator().next());
67          } else throw new RiceIllegalStateException("Multiple countries found with same alternateCode");
68      }
69  
70      @Override
71      public List<Country> findAllCountriesNotRestricted() {
72          List<String> criteriaValues = new ArrayList<String>();
73          criteriaValues.add(null);
74          criteriaValues.add("N");
75  
76          final Map<String, Object> map = new HashMap<String, Object>();
77          map.put(KRADPropertyConstants.POSTAL_COUNTRY_RESTRICTED_INDICATOR, criteriaValues);
78          map.put("active", Boolean.TRUE);
79  
80          Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map));
81  
82          return convertListOfBosToImmutables(countryBos);
83      }
84  
85      @Override
86      public List<Country> findAllCountries() {
87          final Map<String, Object> map = new HashMap<String, Object>();
88          map.put("active", Boolean.TRUE);
89  
90          Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map));
91          return convertListOfBosToImmutables(countryBos);
92      }
93  
94      @Override
95      public Country getDefaultCountry() {
96          String defaultCountryCode = parameterService.getParameterValueAsString(KRADConstants.KNS_NAMESPACE,
97                  KRADConstants.DetailTypes.ALL_DETAIL_TYPE, KRADConstants.SystemGroupParameterNames.DEFAULT_COUNTRY);
98          if (StringUtils.isBlank(defaultCountryCode)) {
99              return null;
100         }
101         return getCountry(defaultCountryCode);
102     }
103 
104     public ParameterService getParameterService() {
105         return parameterService;
106     }
107 
108     public void setParameterService(ParameterService parameterService) {
109         this.parameterService = parameterService;
110     }
111 
112     /**
113      * Sets the businessObjectServiceMockFor attribute value.
114      *
115      * @param businessObjectService The businessObjectServiceMockFor to set.
116      */
117     public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
118         this.businessObjectService = businessObjectService;
119     }
120 
121     /**
122      * Converts a List<CountryBo> to an Unmodifiable List<Country>
123      *
124      * @param countryBos a mutable List<CountryBo> to made completely immutable.
125      * @return An unmodifiable List<Country>
126      */
127     List<Country> convertListOfBosToImmutables(final Collection<CountryBo> countryBos) {
128         ArrayList<Country> countries = new ArrayList<Country>();
129         for (CountryBo bo : countryBos) {
130             Country country = CountryBo.to(bo);
131             countries.add(country);
132         }
133         return Collections.unmodifiableList(countries);
134     }
135 }