1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.shareddata.impl.country; |
18 | |
|
19 | |
|
20 | |
import org.apache.commons.lang.StringUtils; |
21 | |
import org.kuali.rice.kns.service.BusinessObjectService; |
22 | |
import org.kuali.rice.kns.util.KNSPropertyConstants; |
23 | |
import org.kuali.rice.shareddata.api.country.Country; |
24 | |
import org.kuali.rice.shareddata.api.country.CountryService; |
25 | |
|
26 | |
import java.util.ArrayList; |
27 | |
import java.util.Collection; |
28 | |
import java.util.Collections; |
29 | |
import java.util.HashMap; |
30 | |
import java.util.List; |
31 | |
import java.util.Map; |
32 | |
|
33 | 0 | public final class CountryServiceImpl implements CountryService { |
34 | |
|
35 | |
private BusinessObjectService businessObjectService; |
36 | |
|
37 | |
@Override |
38 | |
public Country getCountry(final String code) { |
39 | 0 | if (StringUtils.isBlank(code)) { |
40 | 0 | throw new IllegalArgumentException("code is blank"); |
41 | |
} |
42 | |
|
43 | 0 | CountryBo countryBo = businessObjectService.findByPrimaryKey(CountryBo.class, Collections.singletonMap(KNSPropertyConstants.POSTAL_COUNTRY_CODE, code)); |
44 | |
|
45 | 0 | return CountryBo.to(countryBo); |
46 | |
} |
47 | |
|
48 | |
@Override |
49 | |
public Country getCountryByAlternateCode(final String alternateCode) { |
50 | 0 | if (StringUtils.isBlank(alternateCode)) { |
51 | 0 | throw new IllegalArgumentException("alt code is blank"); |
52 | |
} |
53 | |
|
54 | 0 | Collection<CountryBo> countryList = businessObjectService.findMatching(CountryBo.class, Collections.singletonMap(KNSPropertyConstants.ALTERNATE_POSTAL_COUNTRY_CODE, alternateCode)); |
55 | 0 | if (countryList == null || countryList.isEmpty()) { |
56 | 0 | return null; |
57 | 0 | } else if (countryList.size() == 1) { |
58 | 0 | return CountryBo.to(countryList.iterator().next()); |
59 | 0 | } else throw new IllegalStateException("Multiple countries found with same alternateCode"); |
60 | |
} |
61 | |
|
62 | |
@Override |
63 | |
public List<Country> findAllCountriesNotRestricted() { |
64 | 0 | List<String> criteriaValues = new ArrayList<String>(); |
65 | 0 | criteriaValues.add(null); |
66 | 0 | criteriaValues.add("N"); |
67 | |
|
68 | 0 | final Map<String, Object> map = new HashMap<String, Object>(); |
69 | 0 | map.put(KNSPropertyConstants.POSTAL_COUNTRY_RESTRICTED_INDICATOR, criteriaValues); |
70 | 0 | map.put("active", Boolean.TRUE); |
71 | |
|
72 | 0 | Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map)); |
73 | |
|
74 | 0 | return convertListOfBosToImmutables(countryBos); |
75 | |
} |
76 | |
|
77 | |
@Override |
78 | |
public List<Country> findAllCountries() { |
79 | 0 | final Map<String, Object> map = new HashMap<String, Object>(); |
80 | 0 | map.put("active", Boolean.TRUE); |
81 | |
|
82 | 0 | Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map)); |
83 | 0 | return convertListOfBosToImmutables(countryBos); |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public void setBusinessObjectService(final BusinessObjectService businessObjectService) { |
92 | 0 | this.businessObjectService = businessObjectService; |
93 | 0 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
List<Country> convertListOfBosToImmutables(final Collection<CountryBo> countryBos) { |
102 | 0 | ArrayList<Country> countries = new ArrayList<Country>(); |
103 | 0 | for (CountryBo bo : countryBos) { |
104 | 0 | Country country = CountryBo.to(bo); |
105 | 0 | countries.add(country); |
106 | 0 | } |
107 | 0 | return Collections.unmodifiableList(countries); |
108 | |
} |
109 | |
} |