001/*
002 * (C) Copyright 2010-2015 Nuxeo SAS (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 *     Thomas Roger
016 *     Thierry Delprat
017 *     Florent Guillaume
018 *     ron1
019 */
020package org.nuxeo.ecm.platform.rendition.service;
021
022import static java.lang.Boolean.FALSE;
023import static java.lang.Boolean.TRUE;
024
025import java.util.ArrayList;
026import java.util.List;
027
028import org.apache.commons.lang3.StringUtils;
029import org.nuxeo.common.xmap.annotation.XNode;
030import org.nuxeo.common.xmap.annotation.XNodeList;
031import org.nuxeo.common.xmap.annotation.XObject;
032import org.nuxeo.ecm.platform.rendition.extension.RenditionProvider;
033
034/**
035 * Definition of a rendition.
036 *
037 * @since 5.4.1
038 */
039@XObject("renditionDefinition")
040public class RenditionDefinition {
041
042    public static final String DEFAULT_SOURCE_DOCUMENT_MODIFICATION_DATE_PROPERTY_NAME = "dc:modified";
043
044    /** True if the boolean is null or TRUE, false otherwise. */
045    private static boolean defaultTrue(Boolean bool) {
046        return !FALSE.equals(bool);
047    }
048
049    /** False if the boolean is null or FALSE, true otherwise. */
050    private static boolean defaultFalse(Boolean bool) {
051        return TRUE.equals(bool);
052    }
053
054    @XNode("@name")
055    protected String name;
056
057    public String getName() {
058        return name;
059    }
060
061    public void setName(String name) {
062        this.name = name;
063    }
064
065    /**
066     * @since 7.3
067     */
068    @XNode("@cmisName")
069    protected String cmisName;
070
071    public String getCmisName() {
072        return cmisName;
073    }
074
075    public void setCmisName(String cmisName) {
076        this.cmisName = cmisName;
077    }
078
079    @XNode("@enabled")
080    protected Boolean enabled;
081
082    public boolean isEnabled() {
083        return defaultTrue(enabled);
084    }
085
086    public void setEnabled(boolean enabled) {
087        this.enabled = Boolean.valueOf(enabled);
088    }
089
090    @XNode("label")
091    protected String label;
092
093    public String getLabel() {
094        return label;
095    }
096
097    public void setLabel(String label) {
098        this.label = label;
099    }
100
101    @XNode("icon")
102    protected String icon;
103
104    public String getIcon() {
105        return icon;
106    }
107
108    public void setIcon(String icon) {
109        this.icon = icon;
110    }
111
112    @XNode("kind")
113    protected String kind;
114
115    public String getKind() {
116        return kind;
117    }
118
119    public void setKind(String kind) {
120        this.kind = kind;
121    }
122
123    @XNode("operationChain")
124    protected String operationChain;
125
126    public String getOperationChain() {
127        return operationChain;
128    }
129
130    public void setOperationChain(String operationChain) {
131        this.operationChain = operationChain;
132    }
133
134    /**
135     * @since 6.0
136     */
137    @XNode("allowEmptyBlob")
138    protected Boolean allowEmptyBlob;
139
140    /**
141     * @since 7.3
142     */
143    public boolean isEmptyBlobAllowed() {
144        return defaultFalse(allowEmptyBlob);
145    }
146
147    public void setAllowEmptyBlob(boolean allowEmptyBlob) {
148        this.allowEmptyBlob = Boolean.valueOf(allowEmptyBlob);
149    }
150
151    /**
152     * @since 6.0
153     */
154    @XNode("@visible")
155    protected Boolean visible;
156
157    public boolean isVisible() {
158        return defaultTrue(visible);
159    }
160
161    public void setVisible(boolean visible) {
162        this.visible = Boolean.valueOf(visible);
163    }
164
165    @XNode("@class")
166    protected Class<? extends RenditionProvider> providerClass;
167
168    public Class<? extends RenditionProvider> getProviderClass() {
169        return providerClass;
170    }
171
172    public void setProviderClass(Class<? extends RenditionProvider> providerClass) {
173        this.providerClass = providerClass;
174    }
175
176    // computed from providerClass
177    protected RenditionProvider provider;
178
179    public RenditionProvider getProvider() {
180        return provider;
181    }
182
183    public String getProviderType() {
184        RenditionProvider provider = getProvider();
185        if (provider == null) {
186            return null;
187        }
188        return provider.getClass().getSimpleName();
189    }
190
191    public void setProvider(RenditionProvider provider) {
192        this.provider = provider;
193    }
194
195    @XNode("contentType")
196    protected String contentType;
197
198    public String getContentType() {
199        return contentType;
200    }
201
202    public void setContentType(String contentType) {
203        this.contentType = contentType;
204    }
205
206    /**
207     * @since 7.2
208     */
209    @XNodeList(value = "filters/filter-id", type = ArrayList.class, componentType = String.class)
210    protected List<String> filterIds;
211
212    public List<String> getFilterIds() {
213        return filterIds;
214    }
215
216    public void setFilterIds(List<String> filterIds) {
217        this.filterIds = filterIds;
218    }
219
220    /**
221     * @since 7.10
222     */
223    @XNode("sourceDocumentModificationDatePropertyName")
224    protected String sourceDocumentModificationDatePropertyName;
225
226    /**
227     * @since 7.10
228     */
229    public String getSourceDocumentModificationDatePropertyName() {
230        return StringUtils.defaultString(sourceDocumentModificationDatePropertyName,
231                DEFAULT_SOURCE_DOCUMENT_MODIFICATION_DATE_PROPERTY_NAME);
232    }
233
234    /**
235     * @since 7.10
236     */
237    public void setSourceDocumentModificationDatePropertyName(String sourceDocumentModificationDatePropertyName) {
238        this.sourceDocumentModificationDatePropertyName = sourceDocumentModificationDatePropertyName;
239    }
240
241    /**
242     * @since 7.10
243     */
244    @XNode("storeByDefault")
245    protected Boolean storeByDefault;
246
247    /**
248     * @since 7.10
249     */
250    public boolean isStoreByDefault() {
251        return defaultFalse(storeByDefault);
252    }
253
254    /**
255     * @since 7.10
256     */
257    public void setStoreByDefault(boolean storeByDefault) {
258        this.storeByDefault = Boolean.valueOf(storeByDefault);
259    }
260
261    /** Empty constructor. */
262    public RenditionDefinition() {
263    }
264
265    /**
266     * Copy constructor.
267     *
268     * @since 7.10
269     */
270    public RenditionDefinition(RenditionDefinition other) {
271        name = other.name;
272        cmisName = other.cmisName;
273        enabled = other.enabled;
274        label = other.label;
275        icon = other.icon;
276        kind = other.kind;
277        operationChain = other.operationChain;
278        allowEmptyBlob = other.allowEmptyBlob;
279        visible = other.visible;
280        providerClass = other.providerClass;
281        contentType = other.contentType;
282        filterIds = other.filterIds == null ? null : new ArrayList<>(other.filterIds);
283        sourceDocumentModificationDatePropertyName = other.sourceDocumentModificationDatePropertyName;
284        storeByDefault = other.storeByDefault;
285    }
286
287    /** @since 7.10 */
288    public void merge(RenditionDefinition other) {
289        if (other.cmisName != null) {
290            cmisName = other.cmisName;
291        }
292        if (other.enabled != null) {
293            enabled = other.enabled;
294        }
295        if (other.label != null) {
296            label = other.label;
297        }
298        if (other.icon != null) {
299            icon = other.icon;
300        }
301        if (other.operationChain != null) {
302            operationChain = other.operationChain;
303        }
304        if (other.allowEmptyBlob != null) {
305            allowEmptyBlob = other.allowEmptyBlob;
306        }
307        if (other.visible != null) {
308            visible = other.visible;
309        }
310        if (other.providerClass != null) {
311            providerClass = other.providerClass;
312        }
313        if (other.contentType != null) {
314            contentType = other.contentType;
315        }
316        if (other.filterIds != null) {
317            if (filterIds == null) {
318                filterIds = new ArrayList<>();
319            }
320            filterIds.addAll(other.filterIds);
321        }
322        if (other.sourceDocumentModificationDatePropertyName != null) {
323            sourceDocumentModificationDatePropertyName = other.sourceDocumentModificationDatePropertyName;
324        }
325        if (other.storeByDefault != null) {
326            storeByDefault = other.storeByDefault;
327        }
328    }
329
330}