001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Anahide Tchertchian 016 */ 017package org.nuxeo.theme.styling.service.descriptors; 018 019import java.util.ArrayList; 020import java.util.Collections; 021import java.util.List; 022 023import org.apache.commons.lang.builder.EqualsBuilder; 024import org.nuxeo.common.xmap.annotation.XNode; 025import org.nuxeo.common.xmap.annotation.XNodeList; 026import org.nuxeo.common.xmap.annotation.XObject; 027 028/** 029 * Descriptor for contributed negotiators. 030 * 031 * @since 7.4 032 */ 033@XObject("negotiation") 034public class NegotiationDescriptor { 035 036 @XNode("@target") 037 protected String target; 038 039 @XNode("@append") 040 protected boolean append = false; 041 042 @XNodeList(value = "negotiator", type = ArrayList.class, componentType = NegotiatorDescriptor.class) 043 List<NegotiatorDescriptor> negotiators; 044 045 public String getTarget() { 046 return target; 047 } 048 049 public void setTarget(String target) { 050 this.target = target; 051 } 052 053 public boolean isAppend() { 054 return append; 055 } 056 057 public void setAppend(boolean append) { 058 this.append = append; 059 } 060 061 public List<NegotiatorDescriptor> getNegotiators() { 062 List<NegotiatorDescriptor> res = new ArrayList<NegotiatorDescriptor>(); 063 if (negotiators != null) { 064 res.addAll(negotiators); 065 } 066 Collections.sort(res); 067 return res; 068 } 069 070 public void setNegotiators(List<NegotiatorDescriptor> negotiators) { 071 this.negotiators = negotiators; 072 } 073 074 public void merge(NegotiationDescriptor src) { 075 List<NegotiatorDescriptor> negotiators = src.negotiators; 076 if (negotiators != null) { 077 List<NegotiatorDescriptor> merged = new ArrayList<NegotiatorDescriptor>(); 078 merged.addAll(negotiators); 079 boolean keepOld = src.isAppend() || (negotiators.isEmpty() && !src.isAppend()); 080 if (keepOld) { 081 // add back old contributions 082 List<NegotiatorDescriptor> oldNegotiators = this.negotiators; 083 if (oldNegotiators != null) { 084 merged.addAll(0, oldNegotiators); 085 } 086 } 087 setNegotiators(merged); 088 } 089 } 090 091 @Override 092 public NegotiationDescriptor clone() { 093 NegotiationDescriptor clone = new NegotiationDescriptor(); 094 clone.setTarget(getTarget()); 095 clone.setAppend(isAppend()); 096 List<NegotiatorDescriptor> negotiators = this.negotiators; 097 if (negotiators != null) { 098 List<NegotiatorDescriptor> cnegociators = new ArrayList<NegotiatorDescriptor>(); 099 for (NegotiatorDescriptor neg : negotiators) { 100 cnegociators.add(neg.clone()); 101 } 102 clone.setNegotiators(cnegociators); 103 } 104 return clone; 105 } 106 107 @Override 108 public boolean equals(Object obj) { 109 if (!(obj instanceof NegotiationDescriptor)) { 110 return false; 111 } 112 if (obj == this) { 113 return true; 114 } 115 NegotiationDescriptor p = (NegotiationDescriptor) obj; 116 return new EqualsBuilder().append(target, p.target).append(append, p.append).append(negotiators, p.negotiators).isEquals(); 117 } 118}