001/*
002 * (C) Copyright 2015 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-2.1.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 *      Nelson Silva
016 */
017
018package org.nuxeo.ecm.media.publishing;
019
020import org.nuxeo.runtime.model.ContributionFragmentRegistry;
021
022import java.util.HashMap;
023import java.util.HashSet;
024import java.util.Map;
025import java.util.Set;
026
027/**
028 * @since 7.3
029 */
030public class MediaPublishingProviderRegistry extends ContributionFragmentRegistry<MediaPublishingProviderDescriptor> {
031
032    protected final Map<String, MediaPublishingProviderDescriptor> providers = new HashMap<>();
033
034    @Override
035    public MediaPublishingProviderDescriptor clone(MediaPublishingProviderDescriptor source) {
036        MediaPublishingProviderDescriptor copy = new MediaPublishingProviderDescriptor();
037        // TODO
038        return copy;
039    }
040
041    @Override
042    public void contributionRemoved(String name, MediaPublishingProviderDescriptor origContrib) {
043        providers.remove(name);
044    }
045
046    @Override
047    public void contributionUpdated(String name, MediaPublishingProviderDescriptor contrib,
048        MediaPublishingProviderDescriptor newOrigContrib) {
049        if (newOrigContrib.isEnabled()) {
050            providers.put(name, newOrigContrib);
051        } else {
052            providers.remove(name);
053        }
054    }
055
056    @Override
057    public String getContributionId(MediaPublishingProviderDescriptor contrib) {
058        return contrib.getId();
059    }
060
061    @Override
062    public void merge(MediaPublishingProviderDescriptor src, MediaPublishingProviderDescriptor dst) {
063        // TODO
064    }
065
066    public Set<String> getServices() {
067        Set<String> services = new HashSet<>();
068        for (String provider : providers.keySet()) {
069            if (lookup(provider).isEnabled()) {
070                services.add(provider);
071            }
072        }
073        return services;
074    }
075
076    public MediaPublishingProviderDescriptor lookup(String provider) {
077        return providers.get(provider);
078    }
079}
080