001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.location.impl.country;
017
018
019import org.apache.commons.lang.StringUtils;
020import org.kuali.rice.core.api.criteria.CriteriaLookupService;
021import org.kuali.rice.core.api.criteria.GenericQueryResults;
022import org.kuali.rice.core.api.criteria.QueryByCriteria;
023import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
024import org.kuali.rice.core.api.exception.RiceIllegalStateException;
025import org.kuali.rice.coreservice.framework.parameter.ParameterService;
026import org.kuali.rice.kns.service.KNSServiceLocator;
027import org.kuali.rice.kns.util.KNSConstants;
028import org.kuali.rice.krad.service.BusinessObjectService;
029import org.kuali.rice.krad.util.KRADConstants;
030import org.kuali.rice.krad.util.KRADPropertyConstants;
031import org.kuali.rice.location.api.campus.Campus;
032import org.kuali.rice.location.api.campus.CampusQueryResults;
033import org.kuali.rice.location.api.country.Country;
034import org.kuali.rice.location.api.country.CountryQueryResults;
035import org.kuali.rice.location.api.country.CountryService;
036import org.kuali.rice.location.impl.campus.CampusBo;
037
038import java.util.ArrayList;
039import java.util.Collection;
040import java.util.Collections;
041import java.util.HashMap;
042import java.util.List;
043import java.util.Map;
044
045public final class CountryServiceImpl implements CountryService {
046
047    private BusinessObjectService businessObjectService;
048    private ParameterService parameterService;
049    private CriteriaLookupService criteriaLookupService;
050
051    @Override
052    public Country getCountry(final String code) {
053        if (StringUtils.isBlank(code)) {
054            throw new RiceIllegalArgumentException("code is blank");
055        }
056
057        CountryBo countryBo = businessObjectService.findByPrimaryKey(CountryBo.class, Collections.singletonMap(
058                KRADPropertyConstants.POSTAL_COUNTRY_CODE, code));
059
060        return CountryBo.to(countryBo);
061    }
062
063    @Override
064    public Country getCountryByAlternateCode(final String alternateCode) {
065        if (StringUtils.isBlank(alternateCode)) {
066            throw new RiceIllegalArgumentException("alt code is blank");
067        }
068
069        Collection<CountryBo> countryList = businessObjectService.findMatching(CountryBo.class, Collections.singletonMap(
070                KRADPropertyConstants.ALTERNATE_POSTAL_COUNTRY_CODE, alternateCode));
071        if (countryList == null || countryList.isEmpty()) {
072            return null;
073        } else if (countryList.size() == 1) {
074            return CountryBo.to(countryList.iterator().next());
075        } else throw new RiceIllegalStateException("Multiple countries found with same alternateCode");
076    }
077
078    @Override
079    public List<Country> findAllCountriesNotRestricted() {
080        List<String> criteriaValues = new ArrayList<String>();
081        criteriaValues.add(null);
082        criteriaValues.add("N");
083
084        final Map<String, Object> map = new HashMap<String, Object>();
085        map.put(KRADPropertyConstants.POSTAL_COUNTRY_RESTRICTED_INDICATOR, criteriaValues);
086        map.put("active", Boolean.TRUE);
087
088        Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map));
089
090        return convertListOfBosToImmutables(countryBos);
091    }
092
093    @Override
094    public List<Country> findAllCountries() {
095        final Map<String, Object> map = new HashMap<String, Object>();
096        map.put("active", Boolean.TRUE);
097
098        Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map));
099        return convertListOfBosToImmutables(countryBos);
100    }
101
102    @Override
103    public Country getDefaultCountry() {
104        String defaultCountryCode = parameterService.getParameterValueAsString(KRADConstants.KNS_NAMESPACE,
105                KRADConstants.DetailTypes.ALL_DETAIL_TYPE, KRADConstants.SystemGroupParameterNames.DEFAULT_COUNTRY);
106        if (StringUtils.isBlank(defaultCountryCode)) {
107            return null;
108        }
109        return getCountry(defaultCountryCode);
110    }
111
112    @Override
113    public CountryQueryResults findCountries(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
114        incomingParamCheck(queryByCriteria, "queryByCriteria");
115
116        GenericQueryResults<CountryBo> results = criteriaLookupService.lookup(CountryBo.class, queryByCriteria);
117
118        CountryQueryResults.Builder builder = CountryQueryResults.Builder.create();
119        builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
120        builder.setTotalRowCount(results.getTotalRowCount());
121
122        final List<Country.Builder> ims = new ArrayList<Country.Builder>();
123        for (CountryBo bo : results.getResults()) {
124            ims.add(Country.Builder.create(bo));
125        }
126
127        builder.setResults(ims);
128        return builder.build();
129    }
130
131    public ParameterService getParameterService() {
132        return parameterService;
133    }
134
135    public void setParameterService(ParameterService parameterService) {
136        this.parameterService = parameterService;
137    }
138
139    /**
140     * Sets the businessObjectServiceMockFor attribute value.
141     *
142     * @param businessObjectService The businessObjectServiceMockFor to set.
143     */
144    public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
145        this.businessObjectService = businessObjectService;
146    }
147
148    /**
149     * Converts a List<CountryBo> to an Unmodifiable List<Country>
150     *
151     * @param countryBos a mutable List<CountryBo> to made completely immutable.
152     * @return An unmodifiable List<Country>
153     */
154    List<Country> convertListOfBosToImmutables(final Collection<CountryBo> countryBos) {
155        ArrayList<Country> countries = new ArrayList<Country>();
156        for (CountryBo bo : countryBos) {
157            Country country = CountryBo.to(bo);
158            countries.add(country);
159        }
160        return Collections.unmodifiableList(countries);
161    }
162
163    private void incomingParamCheck(Object object, String name) {
164        if (object == null) {
165            throw new RiceIllegalArgumentException(name + " was null");
166        } else if (object instanceof String
167                && StringUtils.isBlank((String) object)) {
168            throw new RiceIllegalArgumentException(name + " was blank");
169        }
170    }
171
172    /**
173     * Sets the criteriaLookupService attribute value.
174     *
175     * @param criteriaLookupService The criteriaLookupService to set.
176     */
177    public void setCriteriaLookupService(final CriteriaLookupService criteriaLookupService) {
178        this.criteriaLookupService = criteriaLookupService;
179    }
180}