001/*
002 * (C) Copyright 2012 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.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 *     Thomas Roger (troger@nuxeo.com)
016 */
017
018package org.nuxeo.ecm.platform.picture.api;
019
020import java.util.ArrayList;
021import java.util.List;
022
023import org.apache.commons.lang.builder.EqualsBuilder;
024import org.apache.commons.lang.builder.HashCodeBuilder;
025import org.nuxeo.common.xmap.annotation.XNode;
026import org.nuxeo.common.xmap.annotation.XNodeList;
027import org.nuxeo.common.xmap.annotation.XObject;
028
029/**
030 * Object to store the definition of a picture conversion, to be used when computing views for a given image.
031 *
032 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
033 * @since 7.1
034 */
035@XObject("pictureConversion")
036public class PictureConversion implements Comparable<PictureConversion> {
037
038    @XNode("@id")
039    protected String id;
040
041    @XNode("@order")
042    protected Integer order;
043
044    @XNode("@description")
045    protected String description;
046
047    @XNode("@enabled")
048    protected Boolean enabled;
049
050    @XNode("@default")
051    protected Boolean isDefault;
052
053    @XNode("@chainId")
054    protected String chainId;
055
056    @XNode("@tag")
057    protected String tag;
058
059    @XNode("@maxSize")
060    protected Integer maxSize;
061
062    @XNodeList(value = "filters/filter-id", type = ArrayList.class, componentType = String.class)
063    protected List<String> filterIds;
064
065    /**
066     * @since 7.2
067     */
068    @XNode("@rendition")
069    protected Boolean rendition;
070
071    /**
072     * @since 7.2
073     */
074    @XNode("@renditionVisible")
075    protected Boolean renditionVisible;
076
077    public PictureConversion() {
078        super();
079    }
080
081    public PictureConversion(String id, String description, String tag, Integer maxSize) {
082        this.id = id;
083        this.description = description;
084        this.tag = tag;
085        this.maxSize = maxSize;
086    }
087
088    public String getId() {
089        return id;
090    }
091
092    public Integer getOrder() {
093        return order == null ? 0 : order;
094    }
095
096    public String getDescription() {
097        return description;
098    }
099
100    public String getTag() {
101        return tag;
102    }
103
104    public boolean isEnabled() {
105        return enabled == null || enabled;
106    }
107
108    public boolean isEnabledSet() {
109        return enabled != null;
110    }
111
112    public boolean isDefault() {
113        return isDefault == null ? false : isDefault;
114    }
115
116    public boolean isDefaultSet() {
117        return isDefault != null;
118    }
119
120    public String getChainId() {
121        return chainId;
122    }
123
124    /**
125     * For compat with {@link org.nuxeo.ecm.platform.picture.api.PictureTemplate}.
126     *
127     * @deprecated since 7.1. Use {@link #getId()}.
128     */
129    @Deprecated
130    public String getTitle() {
131        return id;
132    }
133
134    public Integer getMaxSize() {
135        return maxSize;
136    }
137
138    public List<String> getFilterIds() {
139        return filterIds;
140    }
141
142    public void setOrder(Integer order) {
143        this.order = order;
144    }
145
146    public void setDescription(String description) {
147        this.description = description;
148    }
149
150    public void setEnabled(boolean enabled) {
151        this.enabled = enabled;
152    }
153
154    public void setDefault(boolean isDefault) {
155        this.isDefault = isDefault;
156    }
157
158    public void setChainId(String chainId) {
159        this.chainId = chainId;
160    }
161
162    public void setTag(String tag) {
163        this.tag = tag;
164    }
165
166    public void setMaxSize(Integer maxSize) {
167        this.maxSize = maxSize;
168    }
169
170    public void setFilterIds(List<String> filterIds) {
171        this.filterIds = filterIds;
172    }
173
174    public boolean isRenditionVisible() {
175        return renditionVisible == null || renditionVisible;
176    }
177
178    public boolean isRenditionVisibleSet() {
179        return renditionVisible != null;
180    }
181
182    public boolean isRendition() {
183        return rendition == null || rendition;
184    }
185
186    public boolean isRenditionSet() {
187        return rendition != null;
188    }
189
190    public void setRendition(Boolean rendition) {
191        this.rendition = rendition;
192    }
193
194    public void setRenditionVisible(Boolean renditionVisible) {
195        this.renditionVisible = renditionVisible;
196    }
197
198    @Override
199    public int hashCode() {
200        return HashCodeBuilder.reflectionHashCode(this);
201    }
202
203    @Override
204    public boolean equals(Object obj) {
205        return EqualsBuilder.reflectionEquals(this, obj);
206    }
207
208    @Override
209    public int compareTo(PictureConversion other) {
210        return Integer.compare(getOrder(), other.getOrder());
211    }
212
213    @Override
214    public PictureConversion clone() {
215        PictureConversion clone = new PictureConversion();
216        clone.id = id;
217        clone.description = description;
218        clone.tag = tag;
219        clone.maxSize = maxSize;
220        clone.order = order;
221        clone.chainId = chainId;
222        clone.enabled = enabled;
223        clone.isDefault = isDefault;
224        if (filterIds != null) {
225            clone.filterIds = new ArrayList<>();
226            clone.filterIds.addAll(filterIds);
227        }
228        clone.rendition = rendition;
229        clone.renditionVisible = renditionVisible;
230        return clone;
231    }
232
233    @Override
234    public String toString() {
235        return String.format(
236                "PictureConversion [id=%s, description=%s, tag=%s, maxSize=%d, order=%d, chainId=%s, enabled=%s, default=%s]",
237                id, description, tag, maxSize, order, chainId, enabled, isDefault);
238    }
239}