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