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