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