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