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.directory.registry;
018
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.nuxeo.runtime.model.ContributionFragmentRegistry;
025
026/**
027 * Tracks what factory should be used depending on the directory registration.
028 *
029 * @since 5.6
030 */
031public class DirectoryFactoryMapperRegistry extends ContributionFragmentRegistry<DirectoryFactoryMapper> {
032
033    protected Map<String, List<String>> factoriesByDir = new HashMap<String, List<String>>();
034
035    @Override
036    public String getContributionId(DirectoryFactoryMapper contrib) {
037        return contrib.getDirectoryName();
038    }
039
040    @Override
041    public void contributionUpdated(String id, DirectoryFactoryMapper contrib, DirectoryFactoryMapper newOrigContrib) {
042        factoriesByDir.put(id, contrib.getFactories());
043    }
044
045    @Override
046    public void contributionRemoved(String id, DirectoryFactoryMapper origContrib) {
047        List<String> factories = factoriesByDir.get(id);
048        if (factories != null) {
049            List<String> toRemove = origContrib.getFactories();
050            if (toRemove != null) {
051                factories.removeAll(toRemove);
052            }
053        }
054    }
055
056    @Override
057    public DirectoryFactoryMapper clone(DirectoryFactoryMapper orig) {
058        return orig.clone();
059    }
060
061    @Override
062    public void merge(DirectoryFactoryMapper src, DirectoryFactoryMapper dst) {
063        // new factories given by src should be added on dst, taking precedence
064        // over previous available factories
065        List<String> oldFacts = dst.getFactories();
066        List<String> updatedFacts = new ArrayList<String>();
067        if (oldFacts != null) {
068            updatedFacts.addAll(oldFacts);
069        }
070        List<String> newFacts = src.getFactories();
071        if (newFacts != null) {
072            for (String newFact : newFacts) {
073                // remove if already there
074                updatedFacts.remove(newFact);
075                // add at the beginning
076                updatedFacts.add(0, newFact);
077            }
078        }
079        dst.setFactories(updatedFacts);
080    }
081
082    // API
083
084    public List<String> getFactoriesForDirectory(String name) {
085        return factoriesByDir.get(name);
086    }
087
088}