001/** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krms.impl.repository; 017 018import java.util.ArrayList; 019import java.util.Collection; 020import java.util.Collections; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024 025import org.apache.commons.collections.CollectionUtils; 026import org.kuali.rice.core.api.criteria.QueryByCriteria; 027import org.kuali.rice.core.api.criteria.QueryResults; 028import org.kuali.rice.core.api.util.ConcreteKeyValue; 029import org.kuali.rice.core.api.util.KeyValue; 030import org.kuali.rice.coreservice.impl.namespace.NamespaceBo; 031import org.kuali.rice.kns.service.KNSServiceLocator; 032import org.kuali.rice.krad.data.KradDataServiceLocator; 033import org.kuali.rice.krad.service.KRADServiceLocator; 034import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase; 035import org.kuali.rice.krad.uif.view.ViewModel; 036 037/** 038 * Helper class that returns all namespaces that have contexts associated w/ them. 039 */ 040public class AgendaNamespaceValuesFinder extends UifKeyValuesFinderBase { 041 042 @Override 043 public List<KeyValue> getKeyValues(ViewModel model) { 044 List<KeyValue> keyValues = new ArrayList<KeyValue>(); 045 046 // TODO: this is not efficient -- do a smart 'select distinct' and make sure we have a good index! 047 048 QueryByCriteria contextCrit = QueryByCriteria.Builder.create().build(); 049 QueryResults<ContextBo> contexts = KRADServiceLocator.getDataObjectService().findMatching(ContextBo.class, contextCrit); 050 051 QueryResults<NamespaceBo> namespaceBos = KradDataServiceLocator.getDataObjectService().findMatching(NamespaceBo.class, 052 QueryByCriteria.Builder.create().build()); 053 Map<String, String> namespaceCodeToName = new HashMap<String, String>(); 054 if (!namespaceBos.getResults().isEmpty()) { 055 for (NamespaceBo namespaceBo : namespaceBos.getResults()) { 056 namespaceCodeToName.put(namespaceBo.getCode(), namespaceBo.getName()); 057 } 058 } 059 060 List<String> namespaceCodes = new ArrayList<String>(); 061 062 if (!CollectionUtils.isEmpty(contexts.getResults())) { 063 for (ContextBo context : contexts.getResults()) { 064 if (!namespaceCodes.contains(context.getNamespace())) { 065 // add if not already there 066 namespaceCodes.add(context.getNamespace()); 067 } 068 } 069 } 070 071 Collections.sort(namespaceCodes); 072 073 for (String namespaceCode : namespaceCodes) { 074 String namespaceName = namespaceCode; 075 if (namespaceCodeToName.containsKey(namespaceCode)) { 076 namespaceName = namespaceCodeToName.get(namespaceCode); 077 } 078 keyValues.add(new ConcreteKeyValue(namespaceCode, namespaceName)); 079 } 080 081 return keyValues; 082 } 083}