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