001/*
002 * (C) Copyright 2009 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 *     Thierry Delprat
016 *     Florent Guillaume
017 */
018package org.nuxeo.ecm.webapp.tree.nav;
019
020import java.util.List;
021
022import org.nuxeo.ecm.platform.actions.ActionService;
023import org.nuxeo.ecm.webapp.directory.DirectoryTreeService;
024import org.nuxeo.runtime.api.Framework;
025import org.nuxeo.runtime.model.ComponentContext;
026import org.nuxeo.runtime.model.ComponentInstance;
027import org.nuxeo.runtime.model.DefaultComponent;
028
029/**
030 * Very simple component to manage Navigation tree registration.
031 * <p>
032 * Moved from module nuxeo-platform-virtual-navigation-web, originally added in 5.6.
033 *
034 * @author Thierry Delprat
035 * @author Thierry Martins
036 * @since 6.0
037 */
038public class NavTreeService extends DefaultComponent {
039
040    public static String NAVTREE_EP = "navigationTree";
041
042    protected NavTreeRegistry registry;
043
044    @Override
045    public void activate(ComponentContext context) {
046        registry = new NavTreeRegistry();
047    }
048
049    @Override
050    public void deactivate(ComponentContext context) {
051        registry = null;
052    }
053
054    @Override
055    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
056        if (NAVTREE_EP.equals(extensionPoint)) {
057            NavTreeDescriptor contrib = (NavTreeDescriptor) contribution;
058            registry.addContribution(contrib);
059            getActionService().getActionRegistry().addAction(contrib.getAction());
060        }
061    }
062
063    /**
064     * @since 5.6
065     */
066    @Override
067    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
068        if (NAVTREE_EP.equals(extensionPoint)) {
069            NavTreeDescriptor contrib = (NavTreeDescriptor) contribution;
070            registry.removeContribution(contrib);
071            getActionService().getActionRegistry().removeAction(contrib.getTreeId());
072        }
073    }
074
075    public List<NavTreeDescriptor> getTreeDescriptors() {
076        return registry.getTreeDescriptors(getDirectoryTreeService());
077    }
078
079    protected DirectoryTreeService getDirectoryTreeService() {
080        return (DirectoryTreeService) Framework.getRuntime().getComponent(DirectoryTreeService.NAME);
081    }
082
083    /**
084     * @since 6.0
085     */
086    protected ActionService getActionService() {
087        return (ActionService) Framework.getRuntime().getComponent(ActionService.ID);
088    }
089
090    /**
091     * Returns the last modified time of this service or of the {@link DirectoryTreeService}, whichever the greater
092     *
093     * @since 5.6
094     */
095    @Override
096    public Long getLastModified() {
097        Long res = super.getLastModified();
098        DirectoryTreeService treeService = getDirectoryTreeService();
099        if (treeService != null) {
100            Long other = treeService.getLastModified();
101            if (res == null || (other != null && other.compareTo(res) > 0)) {
102                res = other;
103            }
104        }
105        return res;
106    }
107}