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 *     Anahide Tchertchian
018 */
019package org.nuxeo.theme.styling.service.descriptors;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024
025import org.apache.commons.lang.builder.EqualsBuilder;
026import org.nuxeo.common.xmap.annotation.XNode;
027import org.nuxeo.common.xmap.annotation.XNodeList;
028import org.nuxeo.common.xmap.annotation.XObject;
029
030/**
031 * Descriptor for contributed negotiators.
032 *
033 * @since 7.4
034 */
035@XObject("negotiation")
036public class NegotiationDescriptor {
037
038    @XNode("@target")
039    protected String target;
040
041    @XNode("@append")
042    protected boolean append = false;
043
044    @XNodeList(value = "negotiator", type = ArrayList.class, componentType = NegotiatorDescriptor.class)
045    List<NegotiatorDescriptor> negotiators;
046
047    public String getTarget() {
048        return target;
049    }
050
051    public void setTarget(String target) {
052        this.target = target;
053    }
054
055    public boolean isAppend() {
056        return append;
057    }
058
059    public void setAppend(boolean append) {
060        this.append = append;
061    }
062
063    public List<NegotiatorDescriptor> getNegotiators() {
064        List<NegotiatorDescriptor> res = new ArrayList<NegotiatorDescriptor>();
065        if (negotiators != null) {
066            res.addAll(negotiators);
067        }
068        Collections.sort(res);
069        return res;
070    }
071
072    public void setNegotiators(List<NegotiatorDescriptor> negotiators) {
073        this.negotiators = negotiators;
074    }
075
076    public void merge(NegotiationDescriptor src) {
077        List<NegotiatorDescriptor> negotiators = src.negotiators;
078        if (negotiators != null) {
079            List<NegotiatorDescriptor> merged = new ArrayList<NegotiatorDescriptor>();
080            merged.addAll(negotiators);
081            boolean keepOld = src.isAppend() || (negotiators.isEmpty() && !src.isAppend());
082            if (keepOld) {
083                // add back old contributions
084                List<NegotiatorDescriptor> oldNegotiators = this.negotiators;
085                if (oldNegotiators != null) {
086                    merged.addAll(0, oldNegotiators);
087                }
088            }
089            setNegotiators(merged);
090        }
091    }
092
093    @Override
094    public NegotiationDescriptor clone() {
095        NegotiationDescriptor clone = new NegotiationDescriptor();
096        clone.setTarget(getTarget());
097        clone.setAppend(isAppend());
098        List<NegotiatorDescriptor> negotiators = this.negotiators;
099        if (negotiators != null) {
100            List<NegotiatorDescriptor> cnegociators = new ArrayList<NegotiatorDescriptor>();
101            for (NegotiatorDescriptor neg : negotiators) {
102                cnegociators.add(neg.clone());
103            }
104            clone.setNegotiators(cnegociators);
105        }
106        return clone;
107    }
108
109    @Override
110    public boolean equals(Object obj) {
111        if (!(obj instanceof NegotiationDescriptor)) {
112            return false;
113        }
114        if (obj == this) {
115            return true;
116        }
117        NegotiationDescriptor p = (NegotiationDescriptor) obj;
118        return new EqualsBuilder().append(target, p.target).append(append, p.append).append(negotiators, p.negotiators).isEquals();
119    }
120}