001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.webapp.tree.nav;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.List;
022
023import org.nuxeo.ecm.webapp.directory.DirectoryTreeDescriptor;
024import org.nuxeo.ecm.webapp.directory.DirectoryTreeService;
025import org.nuxeo.runtime.model.SimpleContributionRegistry;
026
027/**
028 * Registry for nav trees.
029 * <p>
030 * Moved from module nuxeo-platform-virtual-navigation-web, originally added in 5.6.
031 *
032 * @since 6.0
033 */
034public class NavTreeRegistry extends SimpleContributionRegistry<NavTreeDescriptor> {
035
036    @Override
037    public String getContributionId(NavTreeDescriptor contrib) {
038        return contrib.getTreeId();
039    }
040
041    @Override
042    public void contributionUpdated(String id, NavTreeDescriptor contrib, NavTreeDescriptor newOrigContrib) {
043        if (currentContribs.containsKey(id)) {
044            currentContribs.remove(id);
045        }
046        if (contrib.isEnabled()) {
047            currentContribs.put(id, contrib);
048        }
049    }
050
051    // API
052
053    public List<NavTreeDescriptor> getTreeDescriptors(DirectoryTreeService directoryTreeService) {
054        List<NavTreeDescriptor> allTrees = new ArrayList<NavTreeDescriptor>();
055        allTrees.addAll(currentContribs.values());
056        List<NavTreeDescriptor> directoryTrees = getDirectoryTrees(directoryTreeService);
057        if (directoryTrees != null) {
058            allTrees.addAll(directoryTrees);
059        }
060        Collections.sort(allTrees);
061        return allTrees;
062    }
063
064    protected List<NavTreeDescriptor> getDirectoryTrees(DirectoryTreeService directoryTreeService) {
065        if (directoryTreeService == null) {
066            return null;
067        }
068        List<String> treeNames = directoryTreeService.getNavigationDirectoryTrees();
069        List<NavTreeDescriptor> trees = new ArrayList<NavTreeDescriptor>();
070        for (String dTreeName : treeNames) {
071            DirectoryTreeDescriptor desc = directoryTreeService.getDirectoryTreeDescriptor(dTreeName);
072            trees.add(new NavTreeDescriptor(dTreeName, desc.getLabel(), true));
073        }
074        return trees;
075    }
076
077}