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.api.impl;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.LinkedHashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.nuxeo.targetplatforms.api.TargetPackage;
028import org.nuxeo.targetplatforms.api.TargetPlatform;
029import org.nuxeo.targetplatforms.api.TargetPlatformInstance;
030
031/**
032 * @since 5.7.1
033 */
034public class TargetPlatformInstanceImpl extends TargetImpl implements TargetPlatformInstance {
035
036    private static final long serialVersionUID = 1L;
037
038    protected TargetPlatform parent;
039
040    protected Map<String, TargetPackage> enabledPackages;
041
042    protected TargetPlatformInstanceImpl() {
043        super();
044    }
045
046    public TargetPlatformInstanceImpl(String id) {
047        super(id);
048    }
049
050    public TargetPlatformInstanceImpl(String id, String name, String version, String refVersion, String label) {
051        super(id, name, version, refVersion, label);
052    }
053
054    @Override
055    public List<String> getEnabledPackagesIds() {
056        if (enabledPackages == null) {
057            return Collections.emptyList();
058        }
059        return new ArrayList<>(enabledPackages.keySet());
060    }
061
062    @Override
063    public Map<String, TargetPackage> getEnabledPackages() {
064        if (enabledPackages == null) {
065            return Collections.emptyMap();
066        }
067        return enabledPackages;
068    }
069
070    public void addEnabledPackage(TargetPackage pack) {
071        if (pack == null) {
072            return;
073        }
074        if (enabledPackages == null) {
075            enabledPackages = new LinkedHashMap<>();
076        }
077        enabledPackages.put(pack.getId(), pack);
078    }
079
080    public void setEnabledPackages(Map<String, TargetPackage> packages) {
081        if (enabledPackages == null) {
082            enabledPackages = new LinkedHashMap<>();
083        } else {
084            enabledPackages.clear();
085        }
086        if (packages != null) {
087            enabledPackages.putAll(packages);
088        }
089    }
090
091    @Override
092    public boolean hasEnabledPackageWithName(String packageName) {
093        if (packageName == null || enabledPackages == null) {
094            return false;
095        }
096        for (TargetPackage pkg : enabledPackages.values()) {
097            if (pkg != null && packageName.equals(pkg.getName())) {
098                return true;
099            }
100        }
101        return false;
102    }
103
104    @Override
105    public TargetPlatform getParent() {
106        return parent;
107    }
108
109    public void setParent(TargetPlatform parent) {
110        this.parent = parent;
111    }
112
113}