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