View Javadoc

1   /**
2    * Copyright 2005-2011 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  
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.Comparator;
27  import java.util.List;
28  
29  /**
30   * This is a description of what this class does - wliang don't forget to fill this in.
31   *
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   */
34  public abstract class AbstractCountryValuesFinderBase extends KeyValuesBase {
35  
36      /**
37       * Returns a list of countries that will be added to the result of {@link #getKeyValues()}.  Note that the result may
38       * be filtered by active status
39       *
40       * @return
41       */
42      protected abstract List<Country> retrieveCountriesForValuesFinder();
43  
44      @Override
45      public List<KeyValue> getKeyValues() {
46          List<Country> boList = new ArrayList<Country>(retrieveCountriesForValuesFinder());
47          List<KeyValue> labels = new ArrayList<KeyValue>(boList.size() + 1);
48  
49          labels.add(new ConcreteKeyValue("", ""));
50  
51          Collections.sort(boList, new Comparator<Country>() {
52              @Override
53              public int compare(Country o1, Country o2) {
54                  // some institutions may prefix the country name with an asterisk if the country no longer exists
55                  // the country names will be compared without the asterisk
56                  String sortValue1 = StringUtils.trim(StringUtils.removeStart(o1.getName(), "*"));
57                  String sortValue2 = StringUtils.trim(StringUtils.removeStart(o2.getName(), "*"));
58                  return sortValue1.compareToIgnoreCase(sortValue2);
59              }
60  
61          });
62  
63          for (Country country : boList) {
64              if (country.isActive()) {
65                  labels.add(new ConcreteKeyValue(country.getCode(), country.getName()));
66              }
67          }
68          return labels;
69      }
70  }