001/**
002 * Copyright 2005-2016 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 org.apache.commons.collections.CollectionUtils;
019import org.kuali.rice.core.api.util.ConcreteKeyValue;
020import org.kuali.rice.core.api.util.KeyValue;
021import org.kuali.rice.coreservice.impl.namespace.NamespaceBo;
022import org.kuali.rice.krad.service.KRADServiceLocator;
023import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase;
024import org.kuali.rice.krad.uif.view.ViewModel;
025
026import java.util.ArrayList;
027import java.util.Collection;
028import java.util.Collections;
029import java.util.HashMap;
030import java.util.List;
031import java.util.Map;
032
033/**
034 * Helper class that returns all namespaces that have contexts associated w/ them.
035 */
036public class AgendaNamespaceValuesFinder extends UifKeyValuesFinderBase {
037
038    @Override
039    public List<KeyValue> getKeyValues(ViewModel model) {
040        List<KeyValue> keyValues = new ArrayList<KeyValue>();
041
042        // TODO: this is not efficient -- do a smart 'select distinct' and make sure we have a good index!
043
044        Collection<ContextBo> contexts = KRADServiceLocator.getBusinessObjectService().findAll(ContextBo.class);
045
046        Collection<NamespaceBo> namespaceBos = KRADServiceLocator.getBusinessObjectService().findAll(NamespaceBo.class);
047        Map<String, String> namespaceCodeToName = new HashMap<String, String>();
048        if (!CollectionUtils.isEmpty(namespaceBos)) for (NamespaceBo namespaceBo : namespaceBos) {
049            namespaceCodeToName.put(namespaceBo.getCode(), namespaceBo.getName());
050        }
051
052        List<String> namespaceCodes = new ArrayList<String>();
053
054        if (!CollectionUtils.isEmpty(contexts)) for (ContextBo context : contexts) {
055            if (!namespaceCodes.contains(context.getNamespace())) {
056                // add if not already there
057                namespaceCodes.add(context.getNamespace());
058            }
059        }
060
061        Collections.sort(namespaceCodes);
062
063        for (String namespaceCode : namespaceCodes) {
064            String namespaceName = namespaceCode;
065            if (namespaceCodeToName.containsKey(namespaceCode)) { namespaceName = namespaceCodeToName.get(namespaceCode); }
066            keyValues.add(new ConcreteKeyValue(namespaceCode, namespaceName));
067        }
068
069        return keyValues;
070    }
071}