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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.platform.picture.rendition;
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.platform.mimetype.interfaces.MimetypeEntry;
029import org.nuxeo.ecm.platform.mimetype.interfaces.MimetypeRegistry;
030import org.nuxeo.ecm.platform.picture.api.ImagingService;
031import org.nuxeo.ecm.platform.picture.api.PictureConversion;
032import org.nuxeo.ecm.platform.picture.api.PictureView;
033import org.nuxeo.ecm.platform.picture.api.adapters.MultiviewPicture;
034import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition;
035import org.nuxeo.ecm.platform.rendition.service.RenditionDefinitionProvider;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * Provides rendition definitions based on the existing picture views.
040 *
041 * @since 7.2
042 */
043public class PictureRenditionDefinitionProvider implements RenditionDefinitionProvider {
044
045    public static final String PICTURE_RENDITION_KIND = "nuxeo:picture:conversion";
046
047    @Override
048    public List<RenditionDefinition> getRenditionDefinitions(DocumentModel doc) {
049        MultiviewPicture multiviewPicture = doc.getAdapter(MultiviewPicture.class);
050        if (multiviewPicture == null) {
051            return Collections.emptyList();
052        }
053
054        List<RenditionDefinition> renditionDefinitions = new ArrayList<>();
055        ImagingService imagingService = Framework.getService(ImagingService.class);
056        MimetypeRegistry mimetypeRegistry = Framework.getService(MimetypeRegistry.class);
057        for (PictureView pictureView : multiviewPicture.getViews()) {
058            PictureConversion pictureConversion = imagingService.getPictureConversion(pictureView.getTitle());
059            if (pictureConversion != null && pictureConversion.isRendition()) {
060                Blob blob = pictureView.getBlob();
061                if (blob != null) {
062                    RenditionDefinition renditionDefinition = new RenditionDefinition();
063                    renditionDefinition.setEnabled(true);
064                    renditionDefinition.setName(pictureView.getTitle());
065                    renditionDefinition.setKind(PICTURE_RENDITION_KIND);
066                    renditionDefinition.setProvider(new PictureRenditionProvider());
067                    renditionDefinition.setVisible(pictureConversion.isRenditionVisible());
068                    renditionDefinition.setLabel(pictureView.getTitle());
069                    MimetypeEntry mimeType = mimetypeRegistry.getMimetypeEntryByMimeType(blob.getMimeType());
070                    renditionDefinition.setIcon("/icons/" + mimeType.getIconPath());
071                    renditionDefinitions.add(renditionDefinition);
072                }
073            }
074        }
075        return renditionDefinitions;
076    }
077
078}