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