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 | 0 | public final class CountryServiceImpl implements CountryService { |
35 | |
|
36 | |
private BusinessObjectService businessObjectService; |
37 | |
|
38 | |
@Override |
39 | |
public Country getCountry(final String code) { |
40 | 0 | if (StringUtils.isBlank(code)) { |
41 | 0 | throw new RiceIllegalArgumentException("code is blank"); |
42 | |
} |
43 | |
|
44 | 0 | CountryBo countryBo = businessObjectService.findByPrimaryKey(CountryBo.class, Collections.singletonMap( |
45 | |
KRADPropertyConstants.POSTAL_COUNTRY_CODE, code)); |
46 | |
|
47 | 0 | return CountryBo.to(countryBo); |
48 | |
} |
49 | |
|
50 | |
@Override |
51 | |
public Country getCountryByAlternateCode(final String alternateCode) { |
52 | 0 | if (StringUtils.isBlank(alternateCode)) { |
53 | 0 | throw new RiceIllegalArgumentException("alt code is blank"); |
54 | |
} |
55 | |
|
56 | 0 | Collection<CountryBo> countryList = businessObjectService.findMatching(CountryBo.class, Collections.singletonMap( |
57 | |
KRADPropertyConstants.ALTERNATE_POSTAL_COUNTRY_CODE, alternateCode)); |
58 | 0 | if (countryList == null || countryList.isEmpty()) { |
59 | 0 | return null; |
60 | 0 | } else if (countryList.size() == 1) { |
61 | 0 | return CountryBo.to(countryList.iterator().next()); |
62 | 0 | } else throw new RiceIllegalStateException("Multiple countries found with same alternateCode"); |
63 | |
} |
64 | |
|
65 | |
@Override |
66 | |
public List<Country> findAllCountriesNotRestricted() { |
67 | 0 | List<String> criteriaValues = new ArrayList<String>(); |
68 | 0 | criteriaValues.add(null); |
69 | 0 | criteriaValues.add("N"); |
70 | |
|
71 | 0 | final Map<String, Object> map = new HashMap<String, Object>(); |
72 | 0 | map.put(KRADPropertyConstants.POSTAL_COUNTRY_RESTRICTED_INDICATOR, criteriaValues); |
73 | 0 | map.put("active", Boolean.TRUE); |
74 | |
|
75 | 0 | Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map)); |
76 | |
|
77 | 0 | return convertListOfBosToImmutables(countryBos); |
78 | |
} |
79 | |
|
80 | |
@Override |
81 | |
public List<Country> findAllCountries() { |
82 | 0 | final Map<String, Object> map = new HashMap<String, Object>(); |
83 | 0 | map.put("active", Boolean.TRUE); |
84 | |
|
85 | 0 | Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map)); |
86 | 0 | return convertListOfBosToImmutables(countryBos); |
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public void setBusinessObjectService(final BusinessObjectService businessObjectService) { |
95 | 0 | this.businessObjectService = businessObjectService; |
96 | 0 | } |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
List<Country> convertListOfBosToImmutables(final Collection<CountryBo> countryBos) { |
105 | 0 | ArrayList<Country> countries = new ArrayList<Country>(); |
106 | 0 | for (CountryBo bo : countryBos) { |
107 | 0 | Country country = CountryBo.to(bo); |
108 | 0 | countries.add(country); |
109 | 0 | } |
110 | 0 | return Collections.unmodifiableList(countries); |
111 | |
} |
112 | |
} |