View Javadoc

1   /**
2    * Copyright 2005-2011 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.krad.service.BusinessObjectService;
23  import org.kuali.rice.krad.util.KRADPropertyConstants;
24  import org.kuali.rice.location.api.country.Country;
25  import org.kuali.rice.location.api.country.CountryService;
26  
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.Collections;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  public final class CountryServiceImpl implements CountryService {
35  
36      private BusinessObjectService businessObjectService;
37  
38      @Override
39      public Country getCountry(final String code) {
40          if (StringUtils.isBlank(code)) {
41              throw new RiceIllegalArgumentException("code is blank");
42          }
43  
44          CountryBo countryBo = businessObjectService.findByPrimaryKey(CountryBo.class, Collections.singletonMap(
45                  KRADPropertyConstants.POSTAL_COUNTRY_CODE, code));
46  
47          return CountryBo.to(countryBo);
48      }
49  
50      @Override
51      public Country getCountryByAlternateCode(final String alternateCode) {
52          if (StringUtils.isBlank(alternateCode)) {
53              throw new RiceIllegalArgumentException("alt code is blank");
54          }
55  
56          Collection<CountryBo> countryList = businessObjectService.findMatching(CountryBo.class, Collections.singletonMap(
57                  KRADPropertyConstants.ALTERNATE_POSTAL_COUNTRY_CODE, alternateCode));
58          if (countryList == null || countryList.isEmpty()) {
59              return null;
60          } else if (countryList.size() == 1) {
61              return CountryBo.to(countryList.iterator().next());
62          } else throw new RiceIllegalStateException("Multiple countries found with same alternateCode");
63      }
64  
65      @Override
66      public List<Country> findAllCountriesNotRestricted() {
67          List<String> criteriaValues = new ArrayList<String>();
68          criteriaValues.add(null);
69          criteriaValues.add("N");
70  
71          final Map<String, Object> map = new HashMap<String, Object>();
72          map.put(KRADPropertyConstants.POSTAL_COUNTRY_RESTRICTED_INDICATOR, criteriaValues);
73          map.put("active", Boolean.TRUE);
74  
75          Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map));
76  
77          return convertListOfBosToImmutables(countryBos);
78      }
79  
80      @Override
81      public List<Country> findAllCountries() {
82          final Map<String, Object> map = new HashMap<String, Object>();
83          map.put("active", Boolean.TRUE);
84  
85          Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map));
86          return convertListOfBosToImmutables(countryBos);
87      }
88  
89      /**
90       * Sets the businessObjectServiceMockFor attribute value.
91       *
92       * @param businessObjectService The businessObjectServiceMockFor to set.
93       */
94      public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
95          this.businessObjectService = businessObjectService;
96      }
97  
98      /**
99       * Converts a List<CountryBo> to an Unmodifiable List<Country>
100      *
101      * @param countryBos a mutable List<CountryBo> to made completely immutable.
102      * @return An unmodifiable List<Country>
103      */
104     List<Country> convertListOfBosToImmutables(final Collection<CountryBo> countryBos) {
105         ArrayList<Country> countries = new ArrayList<Country>();
106         for (CountryBo bo : countryBos) {
107             Country country = CountryBo.to(bo);
108             countries.add(country);
109         }
110         return Collections.unmodifiableList(countries);
111     }
112 }