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