001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.webapp.tree.nav;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024
025import org.nuxeo.ecm.webapp.directory.DirectoryTreeDescriptor;
026import org.nuxeo.ecm.webapp.directory.DirectoryTreeService;
027import org.nuxeo.runtime.model.SimpleContributionRegistry;
028
029/**
030 * Registry for nav trees.
031 * <p>
032 * Moved from module nuxeo-platform-virtual-navigation-web, originally added in 5.6.
033 *
034 * @since 6.0
035 */
036public class NavTreeRegistry extends SimpleContributionRegistry<NavTreeDescriptor> {
037
038    @Override
039    public String getContributionId(NavTreeDescriptor contrib) {
040        return contrib.getTreeId();
041    }
042
043    @Override
044    public void contributionUpdated(String id, NavTreeDescriptor contrib, NavTreeDescriptor newOrigContrib) {
045        if (currentContribs.containsKey(id)) {
046            currentContribs.remove(id);
047        }
048        if (contrib.isEnabled()) {
049            currentContribs.put(id, contrib);
050        }
051    }
052
053    // API
054
055    public List<NavTreeDescriptor> getTreeDescriptors(DirectoryTreeService directoryTreeService) {
056        List<NavTreeDescriptor> allTrees = new ArrayList<NavTreeDescriptor>();
057        allTrees.addAll(currentContribs.values());
058        List<NavTreeDescriptor> directoryTrees = getDirectoryTrees(directoryTreeService);
059        if (directoryTrees != null) {
060            allTrees.addAll(directoryTrees);
061        }
062        Collections.sort(allTrees);
063        return allTrees;
064    }
065
066    protected List<NavTreeDescriptor> getDirectoryTrees(DirectoryTreeService directoryTreeService) {
067        if (directoryTreeService == null) {
068            return null;
069        }
070        List<String> treeNames = directoryTreeService.getNavigationDirectoryTrees();
071        List<NavTreeDescriptor> trees = new ArrayList<NavTreeDescriptor>();
072        for (String dTreeName : treeNames) {
073            DirectoryTreeDescriptor desc = directoryTreeService.getDirectoryTreeDescriptor(dTreeName);
074            trees.add(new NavTreeDescriptor(dTreeName, desc.getLabel(), true));
075        }
076        return trees;
077    }
078
079}