001/*
002 * (C) Copyright 2014 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.targetplatforms.core.service;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import org.nuxeo.runtime.model.SimpleContributionRegistry;
025import org.nuxeo.targetplatforms.core.descriptors.TargetPackageDescriptor;
026
027/**
028 * Registry for target package contributions, handling merge on "enabled" attribute only.
029 *
030 * @since 5.7.1
031 */
032public class TargetPackageRegistry extends SimpleContributionRegistry<TargetPackageDescriptor> {
033
034    @Override
035    public String getContributionId(TargetPackageDescriptor contrib) {
036        return contrib.getId();
037    }
038
039    @Override
040    public void contributionUpdated(String id, TargetPackageDescriptor contrib, TargetPackageDescriptor newOrigContrib) {
041        if (currentContribs.containsKey(id)) {
042            currentContribs.remove(id);
043        }
044        currentContribs.put(id, contrib);
045    }
046
047    @Override
048    public boolean isSupportingMerge() {
049        return true;
050    }
051
052    @Override
053    public TargetPackageDescriptor clone(TargetPackageDescriptor orig) {
054        return orig.clone();
055    }
056
057    @Override
058    public void merge(TargetPackageDescriptor src, TargetPackageDescriptor dst) {
059        // support merge only for enabled boolean
060        if (src.isEnableSet() && src.isEnabled() != dst.isEnabled()) {
061            dst.setEnabled(src.isEnabled());
062        }
063    }
064
065    // API
066
067    public TargetPackageDescriptor getTargetPackage(String id) {
068        return currentContribs.get(id);
069    }
070
071    public List<TargetPackageDescriptor> getTargetPackages() {
072        List<TargetPackageDescriptor> all = new ArrayList<>();
073        all.addAll(currentContribs.values());
074        return all;
075    }
076
077    public List<TargetPackageDescriptor> getTargetPackages(String targetPlatform) {
078        List<TargetPackageDescriptor> tps = new ArrayList<>();
079        for (TargetPackageDescriptor desc : currentContribs.values()) {
080            List<String> tts = desc.getTargetPlatforms();
081            if (tts != null && tts.contains(targetPlatform)) {
082                tps.add(desc);
083            }
084        }
085        return tps;
086    }
087
088}