View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.location.framework.country;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.util.ConcreteKeyValue;
20  import org.kuali.rice.core.api.util.KeyValue;
21  import org.kuali.rice.krad.keyvalues.KeyValuesBase;
22  import org.kuali.rice.location.api.country.Country;
23  import org.kuali.rice.location.api.services.LocationApiServiceLocator;
24  
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.Comparator;
28  import java.util.List;
29  
30  /**
31   * An abstract KeyValuesBase for defining a values finder which produces a list of Countries.  Sub-classes should
32   * extend this class and override {@link #retrieveCountriesForValuesFinder()} in order to produce a list of
33   * countries to include.
34   *
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  public abstract class AbstractCountryValuesFinderBase extends KeyValuesBase {
38  
39      @Override
40      public List<KeyValue> getKeyValues() {
41          Country defaultCountry = getDefaultCountry();
42          List<Country> countries = new ArrayList<Country>(retrieveCountriesForValuesFinder());
43  
44          List<KeyValue> values = new ArrayList<KeyValue>(countries.size() + 1);
45          values.add(new ConcreteKeyValue("", ""));
46          if (defaultCountry != null) {
47              values.add(new ConcreteKeyValue(defaultCountry.getCode(), defaultCountry.getName()));
48          }
49  
50          Collections.sort(countries, new Comparator<Country>() {
51              @Override
52              public int compare(Country country1, Country country2) {
53                  // some institutions may prefix the country name with an asterisk if the country no longer exists
54                  // the country names will be compared without the asterisk
55                  String sortValue1 = StringUtils.trim(StringUtils.removeStart(country1.getName(), "*"));
56                  String sortValue2 = StringUtils.trim(StringUtils.removeStart(country2.getName(), "*"));
57                  return sortValue1.compareToIgnoreCase(sortValue2);
58              }
59  
60          });
61  
62          // the default country may show up twice, but that's fine
63          for (Country country : countries) {
64              if (country.isActive()) {
65                  values.add(new ConcreteKeyValue(country.getCode(), country.getName()));
66              }
67          }
68          return values;
69      }
70  
71      /**
72       * Returns a list of countries that will be added to the result of {@link #getKeyValues()}.  Note that the result
73       * may be filtered by active status
74       *
75       * @return a List of countries to include in the values returned by this finder
76       */
77      protected abstract List<Country> retrieveCountriesForValuesFinder();
78  
79      /**
80       * Returns the default country to use for this values finder.  If no default country is returned, none will be
81       * used.  The default implementation of this method will defer to {@link org.kuali.rice.location.api.country.CountryService#getDefaultCountry()}.
82       *
83       * @return the default country to use for this values finder, or null if no default country should be used
84       */
85      protected Country getDefaultCountry() {
86          return LocationApiServiceLocator.getCountryService().getDefaultCountry();
87      }
88  
89  }