001/*
002 * (C) Copyright 2006-2017 Nuxeo (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 *     Nuxeo - initial API and implementation
018 */
019package org.nuxeo.ecm.platform.content.template.service;
020
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025import java.util.stream.Collectors;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.common.xmap.annotation.XNode;
030import org.nuxeo.common.xmap.annotation.XNodeList;
031import org.nuxeo.common.xmap.annotation.XNodeMap;
032import org.nuxeo.common.xmap.annotation.XObject;
033
034/**
035 * Factory binding descriptor. Immutable.
036 */
037@XObject(value = "factoryBinding")
038public class FactoryBindingDescriptor {
039
040    private static final Log log = LogFactory.getLog(FactoryBindingDescriptor.class);
041
042    @XNode("@name")
043    private String name;
044
045    @XNode("@factoryName")
046    private String factoryName;
047
048    @XNode("@targetType")
049    private String targetType;
050
051    @XNode("@targetFacet")
052    private String targetFacet;
053
054    @XNode("@append")
055    private Boolean append = Boolean.FALSE;
056
057    @XNodeMap(value = "option", key = "@name", type = HashMap.class, componentType = String.class)
058    private Map<String, String> options;
059
060    @XNodeList(value = "template/templateItem", type = ArrayList.class, componentType = TemplateItemDescriptor.class)
061    private List<TemplateItemDescriptor> template;
062
063    // Declared as ArrayList to be serializable.
064    @XNodeList(value = "acl/ace", type = ArrayList.class, componentType = ACEDescriptor.class)
065    private List<ACEDescriptor> rootAcl;
066
067    public FactoryBindingDescriptor() {
068        // default constructor
069        this.options = new HashMap<>();
070        this.template = new ArrayList<>();
071        this.rootAcl = new ArrayList<>();
072    }
073
074    public FactoryBindingDescriptor(FactoryBindingDescriptor toCopy) {
075        this.name = toCopy.name;
076        this.factoryName = toCopy.factoryName;
077        this.targetType = toCopy.targetType;
078        this.targetFacet = toCopy.targetFacet;
079        this.options = new HashMap<>(toCopy.options);
080        this.template = toCopy.template.stream().map(TemplateItemDescriptor::new).collect(Collectors.toList());
081        this.rootAcl = toCopy.rootAcl.stream().map(ACEDescriptor::new).collect(Collectors.toList());
082    }
083
084    public String getFactoryName() {
085        return factoryName;
086    }
087
088    protected void setFactoryName(String factoryName) {
089        this.factoryName = factoryName;
090    }
091
092    public String getName() {
093        return name;
094    }
095
096    protected void setName(String name) {
097        this.name = name;
098    }
099
100    public Map<String, String> getOptions() {
101        return options;
102    }
103
104    public String getTargetType() {
105        return targetType;
106    }
107
108    protected void setTargetType(String targetType) {
109        this.targetType = targetType;
110    }
111
112    public String getTargetFacet() {
113        return targetFacet;
114    }
115
116    protected void setTargetFacet(String targetFacet) {
117        this.targetFacet = targetFacet;
118    }
119
120    public List<TemplateItemDescriptor> getTemplate() {
121        return template;
122    }
123
124    public List<ACEDescriptor> getRootAcl() {
125        return rootAcl;
126    }
127
128    public Boolean getAppend() {
129        return append;
130    }
131
132    public void setAppend(Boolean append) {
133        this.append = append;
134    }
135
136    public void merge(FactoryBindingDescriptor src) {
137        if (Boolean.TRUE.equals(src.getAppend())) {
138            if (log.isInfoEnabled()) {
139                log.info("FactoryBinding " + name + " is merging with " + src.getName());
140            }
141        } else {
142            // this needs to be overridden by src
143            factoryName = src.getFactoryName();
144            name = src.getName();
145            targetType = src.getTargetType();
146            targetFacet = src.getTargetFacet();
147            append = Boolean.FALSE;
148            options.clear();
149            rootAcl.clear();
150            template.clear();
151        }
152        options.putAll(src.getOptions());
153        rootAcl.addAll(src.getRootAcl());
154        template.addAll(src.getTemplate());
155    }
156
157}