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