001/*
002 * (C) Copyright 2012 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 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.drive.service.impl;
020
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.Map;
024import java.util.Objects;
025
026import org.nuxeo.common.xmap.annotation.XNode;
027import org.nuxeo.common.xmap.annotation.XNodeMap;
028import org.nuxeo.common.xmap.annotation.XObject;
029import org.nuxeo.drive.service.FileSystemItemAdapterService;
030import org.nuxeo.drive.service.FileSystemItemFactory;
031import org.nuxeo.ecm.core.api.NuxeoException;
032
033/**
034 * XMap descriptor for factories contributed to the {@code fileSystemItemFactory} extension point of the
035 * {@link FileSystemItemAdapterService}.
036 *
037 * @author Antoine Taillefer
038 */
039@XObject("fileSystemItemFactory")
040public class FileSystemItemFactoryDescriptor implements Serializable, Comparable<FileSystemItemFactoryDescriptor> {
041
042    private static final long serialVersionUID = 1L;
043
044    @XNode("@name")
045    protected String name;
046
047    @XNode("@order")
048    protected int order = 0;
049
050    @XNode("@docType")
051    protected String docType;
052
053    @XNode("@facet")
054    protected String facet;
055
056    @XNode("@class")
057    protected Class<? extends FileSystemItemFactory> factoryClass;
058
059    @XNodeMap(value = "parameters/parameter", key = "@name", type = HashMap.class, componentType = String.class)
060    protected Map<String, String> parameters = new HashMap<>(); // NOSONAR, serialization is actually performed by
061                                                                // SerializationUtils#clone during merge/clone
062
063    public String getName() {
064        return name;
065    }
066
067    public int getOrder() {
068        return order;
069    }
070
071    public void setOrder(int order) {
072        this.order = order;
073    }
074
075    public String getDocType() {
076        return docType;
077    }
078
079    public void setDocType(String docType) {
080        this.docType = docType;
081    }
082
083    public String getFacet() {
084        return facet;
085    }
086
087    public void setFacet(String facet) {
088        this.facet = facet;
089    }
090
091    public Class<? extends FileSystemItemFactory> getFactoryClass() {
092        return factoryClass;
093    }
094
095    public void setFactoryClass(Class<? extends FileSystemItemFactory> factoryClass) {
096        this.factoryClass = factoryClass;
097    }
098
099    public Map<String, String> getParameters() {
100        return parameters;
101    }
102
103    public String getParameter(String name) {
104        return parameters.get(name);
105    }
106
107    public void setParameters(Map<String, String> parameters) {
108        this.parameters = parameters;
109    }
110
111    public void setParameter(String name, String value) {
112        parameters.put(name, value);
113    }
114
115    public FileSystemItemFactory getFactory() {
116        FileSystemItemFactory factory;
117        try {
118            factory = factoryClass.getDeclaredConstructor().newInstance();
119        } catch (ReflectiveOperationException e) {
120            throw new NuxeoException(e);
121        }
122        factory.setName(name);
123        factory.handleParameters(parameters);
124        return factory;
125    }
126
127    @Override
128    public String toString() {
129        StringBuilder sb = new StringBuilder();
130        sb.append(getName());
131        sb.append("(");
132        sb.append(getOrder());
133        sb.append(")");
134        return sb.toString();
135    }
136
137    @Override
138    public boolean equals(Object obj) {
139        return Objects.equals(this, obj);
140    }
141
142    @Override
143    public int hashCode() {
144        return Objects.hashCode(this);
145    }
146
147    @Override
148    public int compareTo(FileSystemItemFactoryDescriptor other) {
149        if (other == null) {
150            return 1;
151        }
152        int orderDiff = getOrder() - other.getOrder();
153        if (orderDiff == 0) {
154            // Make sure we have a deterministic sort, use name
155            orderDiff = getName().compareTo(other.getName());
156        }
157        return orderDiff;
158    }
159
160}