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 *
019 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
020 */
021
022package org.nuxeo.ecm.webapp.thumb;
023
024import java.io.Serializable;
025
026import org.jboss.seam.ScopeType;
027import org.jboss.seam.annotations.In;
028import org.jboss.seam.annotations.Name;
029import org.jboss.seam.annotations.Scope;
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentLocation;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.thumbnail.ThumbnailAdapter;
035import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
036import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils;
037import org.nuxeo.ecm.platform.url.api.DocumentView;
038import org.nuxeo.ecm.platform.util.RepositoryLocation;
039
040/**
041 * Thumbnail Manager seam bean
042 *
043 * @since 5.7
044 */
045@Name("thumbnailManager")
046@Scope(ScopeType.CONVERSATION)
047public class ThumbnailManagerBean implements Serializable {
048
049    private static final long serialVersionUID = 1L;
050
051    @In(create = true, required = false)
052    protected transient CoreSession documentManager;
053
054    @In(required = true, create = true)
055    protected transient NavigationContext navigationContext;
056
057    /**
058     * @since 5.7
059     */
060    public void downloadThumbnail(DocumentView docView) {
061        if (docView != null) {
062            DocumentLocation docLoc = docView.getDocumentLocation();
063            if (documentManager == null) {
064                RepositoryLocation loc = new RepositoryLocation(docLoc.getServerName());
065                navigationContext.setCurrentServerLocation(loc);
066                documentManager = navigationContext.getOrCreateDocumentManager();
067            }
068            DocumentModel doc = documentManager.getDocument(docLoc.getDocRef());
069            if (doc != null) {
070                ThumbnailAdapter thumbnailDoc = doc.getAdapter(ThumbnailAdapter.class);
071                Blob thumbnail = thumbnailDoc.getThumbnail(documentManager);
072                if (thumbnail == null) {
073                    return;
074                }
075                ComponentUtils.download(doc, null, thumbnail, thumbnail.getFilename(), "thumbnail");
076            }
077        }
078    }
079}