View Javadoc
1   /**
2    * Copyright 2005-2015 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.state;
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  import org.kuali.rice.location.api.state.State;
25  
26  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.Comparator;
29  import java.util.List;
30  
31  public class StateValuesFinder extends KeyValuesBase {
32  
33      private String countryCode = "";
34  
35      @Override
36  	public List<KeyValue> getKeyValues() {
37      	List<KeyValue> labels = new ArrayList<KeyValue>();
38          if (StringUtils.isEmpty(this.countryCode)) {
39              Country defaultCountry = LocationApiServiceLocator.getCountryService().getDefaultCountry();
40              if (defaultCountry != null) {
41                  countryCode = defaultCountry.getCode();
42              }
43          }
44          List<State> baseCodes = LocationApiServiceLocator.getStateService().findAllStatesInCountry(countryCode);
45          List<State> codes = new ArrayList<State>( baseCodes );
46          Collections.sort(codes, new Comparator<State> () {
47              @Override
48              public int compare(State o1, State o2) {
49                  return o1.getName().compareTo(o2.getName());
50              }
51          });
52  
53          List<KeyValue> newLabels = new ArrayList<KeyValue>();
54          newLabels.add(new ConcreteKeyValue("", ""));
55          for (State state : codes) {
56              if(state.isActive()) {
57                  newLabels.add(new ConcreteKeyValue(state.getCode(), state.getName()));
58              }
59          }
60          labels = newLabels;
61  
62          return labels;
63      }
64  
65      public void setCountryCode(String countryCode) {
66          this.countryCode = countryCode;
67      }
68  
69  
70      @Override
71      public void clearInternalCache() {
72      }
73  }