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