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