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