001/*
002 * (C) Copyright 2015 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-2.1.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 *     Florent Guillaume
016 */
017package org.nuxeo.ecm.platform.ui.web.component.file;
018
019import java.lang.reflect.Constructor;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.common.xmap.annotation.XNode;
024import org.nuxeo.common.xmap.annotation.XObject;
025
026/**
027 * Descriptor for a JSF blob uploader.
028 */
029@XObject("uploader")
030public class JSFBlobUploaderDescriptor implements Comparable<JSFBlobUploaderDescriptor> {
031
032    private static final Log log = LogFactory.getLog(JSFBlobUploaderDescriptor.class);
033
034    @XNode("@id")
035    public String id;
036
037    @XNode("@order")
038    public Integer order;
039
040    protected int getOrder() {
041        return order == null ? 0 : order.intValue();
042    }
043
044    @Override
045    public int compareTo(JSFBlobUploaderDescriptor other) {
046        return getOrder() - other.getOrder();
047    }
048
049    @XNode("@class")
050    public Class<JSFBlobUploader> klass;
051
052    private transient JSFBlobUploader instance;
053
054    public JSFBlobUploader getJSFBlobUploader() {
055        if (instance != null) {
056            return instance;
057        }
058        if (klass == null) {
059            return null;
060        }
061        try {
062            Constructor<JSFBlobUploader> ctor = klass.getDeclaredConstructor(String.class);
063            instance = ctor.newInstance(id);
064            return instance;
065        } catch (ReflectiveOperationException e) {
066            throw new RuntimeException("Cannot instantiate class: " + klass, e);
067        } catch (IllegalStateException e) {
068            log.error("Cannot instantiate " + klass.getName() + ", " + e.getMessage());
069            log.debug(e, e);
070            return null;
071        }
072    }
073
074    /** Default constructor. */
075    public JSFBlobUploaderDescriptor() {
076    }
077
078    /** Copy constructor. */
079    public JSFBlobUploaderDescriptor(JSFBlobUploaderDescriptor other) {
080        id = other.id;
081        order = other.order;
082        klass = other.klass;
083    }
084
085    public void merge(JSFBlobUploaderDescriptor other) {
086        if (other.id != null) {
087            id = other.id;
088        }
089        if (other.order != null) {
090            order = other.order;
091        }
092        if (other.klass != null) {
093            klass = other.klass;
094        }
095    }
096
097}