001/*
002 * Copyright (c) 2006-2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Vladimir Pasquier <vpasquier@nuxeo.com>
011 *     Antoine Taillefer <ataillefer@nuxeo.com>
012 *
013 */
014package org.nuxeo.ecm.core.api.thumbnail;
015
016import java.util.HashMap;
017import java.util.Map;
018import java.util.Set;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.nuxeo.ecm.core.api.Blob;
023import org.nuxeo.ecm.core.api.CoreSession;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.NuxeoException;
026import org.nuxeo.ecm.core.api.blobholder.BlobHolderAdapterComponent;
027import org.nuxeo.runtime.model.ComponentInstance;
028import org.nuxeo.runtime.model.DefaultComponent;
029
030/**
031 * Thumbnail service providing 3 kind of factories: by facet, by doctype, and thumbnail default one
032 *
033 * @since 5.7
034 */
035public class ThumbnailServiceImpl extends DefaultComponent implements ThumbnailService {
036
037    private static final Log log = LogFactory.getLog(BlobHolderAdapterComponent.class);
038
039    public static final String THUMBNAILFACTORY_EP = "thumbnailFactory";
040
041    protected static ThumbnailFactory defaultFactory;
042
043    protected static final Map<String, ThumbnailFactory> factoriesByDocType = new HashMap<String, ThumbnailFactory>();
044
045    protected static final Map<String, ThumbnailFactory> factoriesByFacets = new HashMap<String, ThumbnailFactory>();
046
047    @Override
048    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
049        if (THUMBNAILFACTORY_EP.equals(extensionPoint)) {
050            ThumbnailFactoryDescriptor desc = (ThumbnailFactoryDescriptor) contribution;
051            String docType = desc.getDocType();
052            if (docType != null) {
053                factoriesByDocType.put(docType, desc.getFactory());
054            }
055            String facet = desc.getFacet();
056            if (facet != null) {
057                factoriesByFacets.put(facet, desc.getFactory());
058            }
059            if (docType == null && facet == null) {
060                defaultFactory = desc.getFactory();
061            }
062        } else {
063            log.error("Unknown extension point " + extensionPoint);
064        }
065    }
066
067    @Override
068    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
069    }
070
071    public Set<String> getFactoryByDocTypeNames() {
072        return factoriesByDocType.keySet();
073    }
074
075    public Set<String> getFactoryByFacetNames() {
076        return factoriesByFacets.keySet();
077    }
078
079    public ThumbnailFactory getDefaultFactory() {
080        return defaultFactory;
081    }
082
083    @Override
084    public Blob getThumbnail(DocumentModel doc, CoreSession session) {
085        ThumbnailFactory factory = getThumbnailFactory(doc, session);
086        return factory.getThumbnail(doc, session);
087    }
088
089    @Override
090    public Blob computeThumbnail(DocumentModel doc, CoreSession session) {
091        ThumbnailFactory factory = getThumbnailFactory(doc, session);
092        return factory.computeThumbnail(doc, session);
093    }
094
095    public ThumbnailFactory getThumbnailFactory(DocumentModel doc, CoreSession session) {
096        if (factoriesByDocType.containsKey(doc.getType())) {
097            ThumbnailFactory factory = factoriesByDocType.get(doc.getType());
098            return factory;
099        }
100        for (Map.Entry<String, ThumbnailFactory> entry : factoriesByFacets.entrySet()) {
101            if (doc.hasFacet(entry.getKey())) {
102                return entry.getValue();
103            }
104        }
105        if (defaultFactory == null) {
106            throw new NuxeoException("Please contribute a default thumbnail factory");
107        }
108        return defaultFactory;
109    }
110}