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.directory;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.runtime.model.ContributionFragmentRegistry;
030
031/**
032 * Registry for directory tree descriptors
033 *
034 * @since 5.6
035 */
036public class DirectoryTreeRegistry extends ContributionFragmentRegistry<DirectoryTreeDescriptor> {
037
038    private static final Log log = LogFactory.getLog(DirectoryTreeRegistry.class);
039
040    protected Map<String, DirectoryTreeDescriptor> registry = new HashMap<String, DirectoryTreeDescriptor>();
041
042    @Override
043    public String getContributionId(DirectoryTreeDescriptor contrib) {
044        return contrib.getName();
045    }
046
047    @Override
048    public void contributionUpdated(String id, DirectoryTreeDescriptor contrib, DirectoryTreeDescriptor newOrigContrib) {
049        if (registry.containsKey(contrib.getName())) {
050            DirectoryTreeDescriptor existing_descriptor = registry.get(contrib.getName());
051            existing_descriptor.merge(contrib);
052            log.debug("merged DirectoryTreeDescriptor: " + contrib.getName());
053        } else {
054            registry.put(contrib.getName(), contrib);
055            log.debug("registered DirectoryTreeDescriptor: " + contrib.getName());
056        }
057    }
058
059    @Override
060    public void contributionRemoved(String id, DirectoryTreeDescriptor contrib) {
061        registry.remove(contrib.getName());
062        log.debug("unregistered DirectoryTreeDescriptor: " + contrib.getName());
063    }
064
065    @Override
066    public DirectoryTreeDescriptor clone(DirectoryTreeDescriptor orig) {
067        return orig.clone();
068    }
069
070    @Override
071    public void merge(DirectoryTreeDescriptor src, DirectoryTreeDescriptor dst) {
072        dst.merge(src);
073    }
074
075    // API
076
077    public List<String> getDirectoryTrees() {
078        List<String> directoryTrees = new ArrayList<String>();
079        for (DirectoryTreeDescriptor desc : registry.values()) {
080            if (Boolean.TRUE.equals(desc.getEnabled())) {
081                directoryTrees.add(desc.getName());
082            }
083        }
084        Collections.sort(directoryTrees);
085        return directoryTrees;
086    }
087
088    public DirectoryTreeDescriptor getDirectoryTreeDescriptor(String treeName) {
089        DirectoryTreeDescriptor desc = registry.get(treeName);
090        if (desc != null && Boolean.TRUE.equals(desc.getEnabled())) {
091            return desc;
092        } else {
093            return null;
094        }
095    }
096
097    /**
098     * Returns only the enabled Directory Trees marked as being also Navigation Trees.
099     */
100    public List<String> getNavigationDirectoryTrees() {
101        List<String> directoryTrees = new ArrayList<String>();
102        for (DirectoryTreeDescriptor desc : registry.values()) {
103            if (Boolean.TRUE.equals(desc.getEnabled()) && desc.isNavigationTree()) {
104                directoryTrees.add(desc.getName());
105            }
106        }
107        Collections.sort(directoryTrees);
108        return directoryTrees;
109    }
110
111}