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