View Javadoc

1   /*
2    * Copyright 2007-2009 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.kns.bo.options;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.Comparator;
21  import java.util.List;
22  
23  import org.kuali.rice.core.util.KeyLabelPair;
24  import org.kuali.rice.kns.bo.State;
25  import org.kuali.rice.kns.lookup.keyvalues.KeyValuesBase;
26  import org.kuali.rice.kns.service.KNSServiceLocator;
27  
28  /**
29   * This class...
30   */
31  public class StateValuesFinder extends KeyValuesBase {
32  
33  	private static List<KeyLabelPair> labels;
34      /*
35       * @see org.kuali.keyvalues.KeyValuesFinder#getKeyValues()
36       */
37      public List<KeyLabelPair> getKeyValues() {
38      	if ( labels == null ) {
39      		List<State> baseCodes = KNSServiceLocator.getStateService().findAllStates();
40      		List<State> codes = new ArrayList<State>( baseCodes );
41      		Collections.sort(codes, new Comparator<State> () {
42  				public int compare(State o1, State o2) {
43  					return o1.getPostalStateName().compareTo(o2.getPostalStateName());
44  				}
45      		});
46      		
47      		List<KeyLabelPair> newLabels = new ArrayList<KeyLabelPair>();
48  	        newLabels.add(new KeyLabelPair("", ""));
49  	        for (State state : codes) {
50  	            if(state.isActive()) {
51  	                newLabels.add(new KeyLabelPair(state.getPostalStateCode(), state.getPostalStateName()));
52  	            }
53  	        }
54  	        labels = newLabels;
55      	}
56  
57          return labels;
58      }
59  
60      @Override
61      public void clearInternalCache() {
62      	labels = null;
63      }
64  }