001/*
002 * (C) Copyright 2006-2013 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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
018 *     Antoine Taillefer <ataillefer@nuxeo.com>
019 *
020 */
021package org.nuxeo.ecm.core.api.thumbnail;
022
023import java.util.HashMap;
024import java.util.Map;
025import java.util.Set;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.core.api.Blob;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.NuxeoException;
033import org.nuxeo.ecm.core.api.blobholder.BlobHolderAdapterComponent;
034import org.nuxeo.runtime.model.ComponentInstance;
035import org.nuxeo.runtime.model.DefaultComponent;
036
037/**
038 * Thumbnail service providing 3 kind of factories: by facet, by doctype, and thumbnail default one
039 *
040 * @since 5.7
041 */
042public class ThumbnailServiceImpl extends DefaultComponent implements ThumbnailService {
043
044    private static final Log log = LogFactory.getLog(BlobHolderAdapterComponent.class);
045
046    public static final String THUMBNAILFACTORY_EP = "thumbnailFactory";
047
048    protected ThumbnailFactory defaultFactory;
049
050    protected final Map<String, ThumbnailFactory> factoriesByDocType = new HashMap<>();
051
052    protected final Map<String, ThumbnailFactory> factoriesByFacets = new HashMap<>();
053
054    @Override
055    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
056        if (THUMBNAILFACTORY_EP.equals(extensionPoint)) {
057            ThumbnailFactoryDescriptor desc = (ThumbnailFactoryDescriptor) contribution;
058            String docType = desc.getDocType();
059            if (docType != null) {
060                factoriesByDocType.put(docType, desc.getFactory());
061            }
062            String facet = desc.getFacet();
063            if (facet != null) {
064                factoriesByFacets.put(facet, desc.getFactory());
065            }
066            if (docType == null && facet == null) {
067                defaultFactory = desc.getFactory();
068            }
069        } else {
070            log.error("Unknown extension point " + extensionPoint);
071        }
072    }
073
074    @Override
075    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
076    }
077
078    public Set<String> getFactoryByDocTypeNames() {
079        return factoriesByDocType.keySet();
080    }
081
082    public Set<String> getFactoryByFacetNames() {
083        return factoriesByFacets.keySet();
084    }
085
086    public ThumbnailFactory getDefaultFactory() {
087        return defaultFactory;
088    }
089
090    @Override
091    public Blob getThumbnail(DocumentModel doc, CoreSession session) {
092        ThumbnailFactory factory = getThumbnailFactory(doc, session);
093        return factory.getThumbnail(doc, session);
094    }
095
096    @Override
097    public Blob computeThumbnail(DocumentModel doc, CoreSession session) {
098        ThumbnailFactory factory = getThumbnailFactory(doc, session);
099        return factory.computeThumbnail(doc, session);
100    }
101
102    public ThumbnailFactory getThumbnailFactory(DocumentModel doc, CoreSession session) {
103        if (factoriesByDocType.containsKey(doc.getType())) {
104            ThumbnailFactory factory = factoriesByDocType.get(doc.getType());
105            return factory;
106        }
107        for (Map.Entry<String, ThumbnailFactory> entry : factoriesByFacets.entrySet()) {
108            if (doc.hasFacet(entry.getKey())) {
109                return entry.getValue();
110            }
111        }
112        if (defaultFactory == null) {
113            throw new NuxeoException("Please contribute a default thumbnail factory");
114        }
115        return defaultFactory;
116    }
117}