View Javadoc

1   /**
2    * Copyright 2005-2013 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.krms.impl.repository;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.kuali.rice.core.api.util.ConcreteKeyValue;
20  import org.kuali.rice.core.api.util.KeyValue;
21  import org.kuali.rice.coreservice.impl.namespace.NamespaceBo;
22  import org.kuali.rice.krad.service.KRADServiceLocator;
23  import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase;
24  import org.kuali.rice.krad.uif.view.ViewModel;
25  
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * Helper class that returns all namespaces that have contexts associated w/ them.
35   */
36  public class AgendaNamespaceValuesFinder extends UifKeyValuesFinderBase {
37  
38      @Override
39      public List<KeyValue> getKeyValues(ViewModel model) {
40          List<KeyValue> keyValues = new ArrayList<KeyValue>();
41  
42          // TODO: this is not efficient -- do a smart 'select distinct' and make sure we have a good index!
43  
44          Collection<ContextBo> contexts = KRADServiceLocator.getBusinessObjectService().findAll(ContextBo.class);
45  
46          Collection<NamespaceBo> namespaceBos = KRADServiceLocator.getBusinessObjectService().findAll(NamespaceBo.class);
47          Map<String, String> namespaceCodeToName = new HashMap<String, String>();
48          if (!CollectionUtils.isEmpty(namespaceBos)) for (NamespaceBo namespaceBo : namespaceBos) {
49              namespaceCodeToName.put(namespaceBo.getCode(), namespaceBo.getName());
50          }
51  
52          List<String> namespaceCodes = new ArrayList<String>();
53  
54          if (!CollectionUtils.isEmpty(contexts)) for (ContextBo context : contexts) {
55              if (!namespaceCodes.contains(context.getNamespace())) {
56                  // add if not already there
57                  namespaceCodes.add(context.getNamespace());
58              }
59          }
60  
61          Collections.sort(namespaceCodes);
62  
63          for (String namespaceCode : namespaceCodes) {
64              String namespaceName = namespaceCode;
65              if (namespaceCodeToName.containsKey(namespaceCode)) { namespaceName = namespaceCodeToName.get(namespaceCode); }
66              keyValues.add(new ConcreteKeyValue(namespaceCode, namespaceName));
67          }
68  
69          return keyValues;
70      }
71  }