View Javadoc

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