001/*
002 * (C) Copyright 2015 Nuxeo SA (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 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.web.resources.core;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.nuxeo.common.xmap.annotation.XNode;
023import org.nuxeo.common.xmap.annotation.XNodeList;
024import org.nuxeo.common.xmap.annotation.XObject;
025import org.nuxeo.ecm.web.resources.api.Processor;
026
027/**
028 * @since 7.3
029 */
030@XObject("processor")
031public class ProcessorDescriptor implements Processor {
032
033    @XNode("@name")
034    public String name;
035
036    @XNode("@enabled")
037    protected Boolean enabled;
038
039    @XNode("class")
040    Class<?> klass;
041
042    @XNode("@order")
043    protected int order = 0;
044
045    @XNode("@type")
046    protected String type;
047
048    @XNode(value = "types@append")
049    protected Boolean appendTypes;
050
051    @XNodeList(value = "types/type", type = ArrayList.class, componentType = String.class)
052    protected List<String> types;
053
054    public String getName() {
055        return name;
056    }
057
058    /**
059     * Returns true if the enabled element was set on the descriptor, useful for merging.
060     */
061    public boolean isEnableSet() {
062        return enabled != null;
063    }
064
065    public boolean isEnabled() {
066        return enabled == null || Boolean.TRUE.equals(enabled);
067    }
068
069    @Override
070    public List<String> getTypes() {
071        List<String> types = new ArrayList<String>();
072        if (type != null) {
073            types.add(type);
074        }
075        types.addAll(this.types);
076        return types;
077    }
078
079    @Override
080    public int getOrder() {
081        return order;
082    }
083
084    public Class<?> getTargetProcessorClass() {
085        return klass;
086    }
087
088    @Override
089    public int compareTo(Processor o) {
090        int cmp = order - o.getOrder();
091        if (cmp == 0) {
092            // make sure we have a deterministic sort
093            cmp = name.compareTo(o.getName());
094        }
095        return cmp;
096    }
097
098    @Override
099    public ProcessorDescriptor clone() {
100        ProcessorDescriptor clone = new ProcessorDescriptor();
101        clone.name = name;
102        clone.enabled = enabled;
103        clone.klass = klass;
104        clone.order = order;
105        clone.type = type;
106        clone.appendTypes = appendTypes;
107        if (types != null) {
108            clone.types = new ArrayList<String>(types);
109        }
110        return clone;
111    }
112
113    public void merge(ProcessorDescriptor other) {
114        if (other == null) {
115            return;
116        }
117        if (other.isEnableSet()) {
118            enabled = other.enabled;
119        }
120    }
121
122}