View Javadoc

1   /**
2    * Copyright 2005-2014 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.coreservice.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.coreservice.api.CoreServiceApiServiceLocator;
21  import org.kuali.rice.coreservice.api.namespace.Namespace;
22  import org.kuali.rice.krad.keyvalues.KeyValuesBase;
23  
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.Comparator;
27  import java.util.List;
28  
29  public class NamespaceValuesFinder extends KeyValuesBase {
30  
31      @Override
32  	public List<KeyValue> getKeyValues() {
33  
34          // get a list of all Namespaces
35          List<Namespace> namespaces = CoreServiceApiServiceLocator.getNamespaceService().findAllNamespaces();
36          // copy the list of codes before sorting, since we can't modify the results from this method
37          namespaces = namespaces == null ? new ArrayList<Namespace>(0) : new ArrayList<Namespace>( namespaces );
38  
39          // sort using comparator.
40          Collections.sort(namespaces, NamespaceComparator.INSTANCE);
41  
42          // create a new list (code, descriptive-name)
43          List<KeyValue> labels = new ArrayList<KeyValue>( namespaces.size() );
44          labels.add(new ConcreteKeyValue("", ""));
45          for ( Namespace namespace : namespaces ) {
46              labels.add( new ConcreteKeyValue(namespace.getCode(), namespace.getCode() + " - " + namespace.getName() ) );
47          }
48          return labels;
49      }
50  
51      private static class NamespaceComparator implements Comparator<Namespace> {
52          public static final Comparator<Namespace> INSTANCE = new NamespaceComparator();
53  
54          @Override
55          public int compare(Namespace o1, Namespace o2) {
56              return o1.getCode().compareTo( o2.getCode() );
57          }
58      }
59  }