001/*
002 * (C) Copyright 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 */
019
020package org.nuxeo.ecm.platform.rendition.service;
021
022import static org.apache.commons.logging.LogFactory.getLog;
023
024import java.util.ArrayList;
025import java.util.List;
026
027import org.apache.commons.logging.Log;
028import org.nuxeo.common.xmap.annotation.XNode;
029import org.nuxeo.common.xmap.annotation.XNodeList;
030import org.nuxeo.common.xmap.annotation.XObject;
031
032/**
033 * Descriptor contribution {@link RenditionDefinitionProvider}s.
034 *
035 * @since 7.2
036 */
037@XObject("renditionDefinitionProvider")
038public class RenditionDefinitionProviderDescriptor {
039
040    private static final Log log = getLog(RenditionDefinitionProviderDescriptor.class);
041
042    @XNode("@name")
043    protected String name;
044
045    @XNode("@enabled")
046    Boolean enabled;
047
048    @XNode("@class")
049    protected Class<? extends RenditionDefinitionProvider> providerClass;
050
051    protected RenditionDefinitionProvider provider;
052
053    @XNodeList(value = "filters/filter-id", type = ArrayList.class, componentType = String.class)
054    protected List<String> filterIds;
055
056    public String getName() {
057        return name;
058    }
059
060    public boolean isEnabled() {
061        return enabled == null || enabled;
062    }
063
064    public boolean isEnabledSet() {
065        return enabled != null;
066    }
067
068    public Class<? extends RenditionDefinitionProvider> getProviderClass() {
069        return providerClass;
070    }
071
072    public RenditionDefinitionProvider getProvider() {
073        if (provider == null && providerClass != null) {
074            try {
075                provider = providerClass.newInstance();
076            } catch (InstantiationException | IllegalAccessException e) {
077                log.error(String.format("Unable to instantiate RenditionDefinitionProvider for '%s'", getName()), e);
078            }
079        }
080        return provider;
081    }
082
083    public List<String> getFilterIds() {
084        return filterIds;
085    }
086
087    public void setEnabled(Boolean enabled) {
088        this.enabled = enabled;
089    }
090
091    public void setProviderClass(Class<? extends RenditionDefinitionProvider> providerClass) {
092        this.providerClass = providerClass;
093    }
094
095    public void setProvider(RenditionDefinitionProvider provider) {
096        this.provider = provider;
097    }
098
099    public void setFilterIds(List<String> filterIds) {
100        this.filterIds = filterIds;
101    }
102
103    @Override
104    public RenditionDefinitionProviderDescriptor clone() {
105        RenditionDefinitionProviderDescriptor clone = new RenditionDefinitionProviderDescriptor();
106        clone.name = name;
107        clone.enabled = enabled;
108        clone.providerClass = providerClass;
109        if (filterIds != null) {
110            clone.filterIds = new ArrayList<>();
111            clone.filterIds.addAll(filterIds);
112        }
113        return clone;
114    }
115}