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