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