001 /**
002 * Copyright 2005-2013 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 package org.kuali.rice.location.framework.country;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.kuali.rice.core.api.util.ConcreteKeyValue;
020 import org.kuali.rice.core.api.util.KeyValue;
021 import org.kuali.rice.krad.keyvalues.KeyValuesBase;
022 import org.kuali.rice.location.api.country.Country;
023 import org.kuali.rice.location.api.services.LocationApiServiceLocator;
024
025 import java.util.ArrayList;
026 import java.util.Collections;
027 import java.util.Comparator;
028 import java.util.List;
029
030 /**
031 * An abstract KeyValuesBase for defining a values finder which produces a list of Countries. Sub-classes should
032 * extend this class and override {@link #retrieveCountriesForValuesFinder()} in order to produce a list of
033 * countries to include.
034 *
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 */
037 public abstract class AbstractCountryValuesFinderBase extends KeyValuesBase {
038
039 @Override
040 public List<KeyValue> getKeyValues() {
041 Country defaultCountry = getDefaultCountry();
042 List<Country> countries = new ArrayList<Country>(retrieveCountriesForValuesFinder());
043
044 List<KeyValue> values = new ArrayList<KeyValue>(countries.size() + 1);
045 values.add(new ConcreteKeyValue("", ""));
046 if (defaultCountry != null) {
047 values.add(new ConcreteKeyValue(defaultCountry.getCode(), defaultCountry.getName()));
048 }
049
050 Collections.sort(countries, new Comparator<Country>() {
051 @Override
052 public int compare(Country country1, Country country2) {
053 // some institutions may prefix the country name with an asterisk if the country no longer exists
054 // the country names will be compared without the asterisk
055 String sortValue1 = StringUtils.trim(StringUtils.removeStart(country1.getName(), "*"));
056 String sortValue2 = StringUtils.trim(StringUtils.removeStart(country2.getName(), "*"));
057 return sortValue1.compareToIgnoreCase(sortValue2);
058 }
059
060 });
061
062 // the default country may show up twice, but that's fine
063 for (Country country : countries) {
064 if (country.isActive()) {
065 values.add(new ConcreteKeyValue(country.getCode(), country.getName()));
066 }
067 }
068 return values;
069 }
070
071 /**
072 * Returns a list of countries that will be added to the result of {@link #getKeyValues()}. Note that the result
073 * may be filtered by active status
074 *
075 * @return a List of countries to include in the values returned by this finder
076 */
077 protected abstract List<Country> retrieveCountriesForValuesFinder();
078
079 /**
080 * Returns the default country to use for this values finder. If no default country is returned, none will be
081 * used. The default implementation of this method will defer to {@link org.kuali.rice.location.api.country.CountryService#getDefaultCountry()}.
082 *
083 * @return the default country to use for this values finder, or null if no default country should be used
084 */
085 protected Country getDefaultCountry() {
086 return LocationApiServiceLocator.getCountryService().getDefaultCountry();
087 }
088
089 }