001 /*
002 * Copyright 2006-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.kuali.rice.shareddata.impl.country;
018
019
020 import org.apache.commons.lang.StringUtils;
021 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
022 import org.kuali.rice.core.api.exception.RiceIllegalStateException;
023 import org.kuali.rice.krad.service.BusinessObjectService;
024 import org.kuali.rice.krad.util.KRADPropertyConstants;
025 import org.kuali.rice.shareddata.api.country.Country;
026 import org.kuali.rice.shareddata.api.country.CountryService;
027
028 import java.util.ArrayList;
029 import java.util.Collection;
030 import java.util.Collections;
031 import java.util.HashMap;
032 import java.util.List;
033 import java.util.Map;
034
035 public final class CountryServiceImpl implements CountryService {
036
037 private BusinessObjectService businessObjectService;
038
039 @Override
040 public Country getCountry(final String code) {
041 if (StringUtils.isBlank(code)) {
042 throw new RiceIllegalArgumentException("code is blank");
043 }
044
045 CountryBo countryBo = businessObjectService.findByPrimaryKey(CountryBo.class, Collections.singletonMap(
046 KRADPropertyConstants.POSTAL_COUNTRY_CODE, code));
047
048 return CountryBo.to(countryBo);
049 }
050
051 @Override
052 public Country getCountryByAlternateCode(final String alternateCode) {
053 if (StringUtils.isBlank(alternateCode)) {
054 throw new RiceIllegalArgumentException("alt code is blank");
055 }
056
057 Collection<CountryBo> countryList = businessObjectService.findMatching(CountryBo.class, Collections.singletonMap(
058 KRADPropertyConstants.ALTERNATE_POSTAL_COUNTRY_CODE, alternateCode));
059 if (countryList == null || countryList.isEmpty()) {
060 return null;
061 } else if (countryList.size() == 1) {
062 return CountryBo.to(countryList.iterator().next());
063 } else throw new RiceIllegalStateException("Multiple countries found with same alternateCode");
064 }
065
066 @Override
067 public List<Country> findAllCountriesNotRestricted() {
068 List<String> criteriaValues = new ArrayList<String>();
069 criteriaValues.add(null);
070 criteriaValues.add("N");
071
072 final Map<String, Object> map = new HashMap<String, Object>();
073 map.put(KRADPropertyConstants.POSTAL_COUNTRY_RESTRICTED_INDICATOR, criteriaValues);
074 map.put("active", Boolean.TRUE);
075
076 Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map));
077
078 return convertListOfBosToImmutables(countryBos);
079 }
080
081 @Override
082 public List<Country> findAllCountries() {
083 final Map<String, Object> map = new HashMap<String, Object>();
084 map.put("active", Boolean.TRUE);
085
086 Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map));
087 return convertListOfBosToImmutables(countryBos);
088 }
089
090 /**
091 * Sets the businessObjectServiceMockFor attribute value.
092 *
093 * @param businessObjectService The businessObjectServiceMockFor to set.
094 */
095 public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
096 this.businessObjectService = businessObjectService;
097 }
098
099 /**
100 * Converts a List<CountryBo> to an Unmodifiable List<Country>
101 *
102 * @param countryBos a mutable List<CountryBo> to made completely immutable.
103 * @return An unmodifiable List<Country>
104 */
105 List<Country> convertListOfBosToImmutables(final Collection<CountryBo> countryBos) {
106 ArrayList<Country> countries = new ArrayList<Country>();
107 for (CountryBo bo : countryBos) {
108 Country country = CountryBo.to(bo);
109 countries.add(country);
110 }
111 return Collections.unmodifiableList(countries);
112 }
113 }