1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
91
92
93
94 public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
95 this.businessObjectService = businessObjectService;
96 }
97
98
99
100
101
102
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 }