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.util.ArrayList;
020import java.util.Collections;
021import java.util.List;
022
023import org.nuxeo.runtime.model.ComponentInstance;
024import org.nuxeo.runtime.model.DefaultComponent;
025import org.nuxeo.runtime.model.SimpleContributionRegistry;
026
027/**
028 * Service holding the registered JSF Blob Uploaders.
029 *
030 * @since 7.2
031 */
032public class JSFBlobUploaderService extends DefaultComponent {
033
034    public static final String XP_UPLOADER = "uploader";
035
036    protected JSFBlobUploaderDescriptorRegistry registry = new JSFBlobUploaderDescriptorRegistry();
037
038    protected static class JSFBlobUploaderDescriptorRegistry extends
039            SimpleContributionRegistry<JSFBlobUploaderDescriptor> {
040
041        @Override
042        public String getContributionId(JSFBlobUploaderDescriptor contrib) {
043            return contrib.id;
044        }
045
046        @Override
047        public JSFBlobUploaderDescriptor clone(JSFBlobUploaderDescriptor orig) {
048            return new JSFBlobUploaderDescriptor(orig);
049        }
050
051        @Override
052        public void merge(JSFBlobUploaderDescriptor src, JSFBlobUploaderDescriptor dst) {
053            dst.merge(src);
054        }
055
056        @Override
057        public boolean isSupportingMerge() {
058            return true;
059        }
060
061        public List<JSFBlobUploader> getJSFBlobUploaders() {
062            List<JSFBlobUploader> uploaders = new ArrayList<>(currentContribs.size());
063            List<JSFBlobUploaderDescriptor> descs = new ArrayList<>(currentContribs.values());
064            Collections.sort(descs); // sort according to order
065            for (JSFBlobUploaderDescriptor desc : descs) {
066                JSFBlobUploader uploader = desc.getJSFBlobUploader();
067                if (uploader != null && uploader.isEnabled()) {
068                    uploaders.add(uploader);
069                }
070            }
071            return uploaders;
072        }
073
074        public JSFBlobUploader getJSFBlobUploader(String choice) {
075            for (JSFBlobUploaderDescriptor desc : currentContribs.values()) {
076                JSFBlobUploader uploader = desc.getJSFBlobUploader();
077                if (uploader != null && uploader.getChoice().equals(choice) && uploader.isEnabled()) {
078                    return uploader;
079                }
080            }
081            return null;
082        }
083    }
084
085    @Override
086    public void registerContribution(Object contrib, String xpoint, ComponentInstance contributor) {
087        if (XP_UPLOADER.equals(xpoint)) {
088            registry.addContribution((JSFBlobUploaderDescriptor) contrib);
089        } else {
090            throw new RuntimeException("Unknown extension point: " + xpoint);
091        }
092    }
093
094    @Override
095    public void unregisterContribution(Object contrib, String xpoint, ComponentInstance contributor) {
096        if (XP_UPLOADER.equals(xpoint)) {
097            registry.removeContribution((JSFBlobUploaderDescriptor) contrib);
098        } else {
099            throw new RuntimeException("Unknown extension point: " + xpoint);
100        }
101    }
102
103    public List<JSFBlobUploader> getJSFBlobUploaders() {
104        return registry.getJSFBlobUploaders();
105    }
106
107    public JSFBlobUploader getJSFBlobUploader(String choice) {
108        return registry.getJSFBlobUploader(choice);
109    }
110
111}