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    @Override
057    public String getName() {
058        return name;
059    }
060
061    /**
062     * Returns true if the enabled element was set on the descriptor, useful for merging.
063     */
064    public boolean isEnableSet() {
065        return enabled != null;
066    }
067
068    @Override
069    public boolean isEnabled() {
070        return enabled == null || Boolean.TRUE.equals(enabled);
071    }
072
073    @Override
074    public List<String> getTypes() {
075        List<String> types = new ArrayList<>();
076        if (type != null) {
077            types.add(type);
078        }
079        types.addAll(this.types);
080        return types;
081    }
082
083    @Override
084    public int getOrder() {
085        return order;
086    }
087
088    @Override
089    public Class<?> getTargetProcessorClass() {
090        return klass;
091    }
092
093    @Override
094    public int compareTo(Processor o) {
095        int cmp = order - o.getOrder();
096        if (cmp == 0) {
097            // make sure we have a deterministic sort
098            cmp = name.compareTo(o.getName());
099        }
100        return cmp;
101    }
102
103    @Override
104    public ProcessorDescriptor clone() {
105        ProcessorDescriptor clone = new ProcessorDescriptor();
106        clone.name = name;
107        clone.enabled = enabled;
108        clone.klass = klass;
109        clone.order = order;
110        clone.type = type;
111        clone.appendTypes = appendTypes;
112        if (types != null) {
113            clone.types = new ArrayList<>(types);
114        }
115        return clone;
116    }
117
118    public void merge(ProcessorDescriptor other) {
119        if (other == null) {
120            return;
121        }
122        if (other.isEnableSet()) {
123            enabled = other.enabled;
124        }
125    }
126
127}