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.core.web.namespace;
17  
18  import org.kuali.rice.core.api.util.ConcreteKeyValue;
19  import org.kuali.rice.core.api.util.KeyValue;
20  import org.kuali.rice.core.impl.namespace.NamespaceBo;
21  import org.kuali.rice.krad.keyvalues.KeyValuesBase;
22  import org.kuali.rice.krad.service.KRADServiceLocator;
23  import org.kuali.rice.krad.service.KeyValuesService;
24  
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.Comparator;
28  import java.util.List;
29  
30  public class NamespaceValuesFinder extends KeyValuesBase {
31  
32      @Override
33  	public List<KeyValue> getKeyValues() {
34  
35          // get a list of all Namespaces
36          KeyValuesService boService = KRADServiceLocator.getKeyValuesService();
37          List<NamespaceBo> bos = (List<NamespaceBo>) boService.findAll(NamespaceBo.class);
38          // copy the list of codes before sorting, since we can't modify the results from this method
39          if ( bos == null ) {
40          	bos = new ArrayList<NamespaceBo>(0);
41          } else {
42          	bos = new ArrayList<NamespaceBo>( bos );
43          }
44  
45          // sort using comparator.
46          Collections.sort(bos, NamespaceComparator.INSTANCE); 
47  
48          // create a new list (code, descriptive-name)
49          List<KeyValue> labels = new ArrayList<KeyValue>( bos.size() );
50          labels.add(new ConcreteKeyValue("", ""));
51          for ( NamespaceBo bo : bos ) {
52              labels.add( new ConcreteKeyValue(bo.getCode(), bo.getName() ) );
53          }
54          return labels;
55      }
56  
57      private static class NamespaceComparator implements Comparator<NamespaceBo> {
58          public static final Comparator<NamespaceBo> INSTANCE = new NamespaceComparator();
59  
60          @Override
61          public int compare(NamespaceBo o1, NamespaceBo o2) {
62              return o1.getCode().compareTo( o2.getCode() );
63          }
64      }
65  }