001/*
002 * (C) Copyright 2006-2011 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 *     Nuxeo
018 */
019package org.nuxeo.ecm.platform.picture.operation;
020
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.Map;
024
025import org.nuxeo.ecm.automation.core.Constants;
026import org.nuxeo.ecm.automation.core.annotations.Context;
027import org.nuxeo.ecm.automation.core.annotations.Operation;
028import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
029import org.nuxeo.ecm.automation.core.annotations.Param;
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.ecm.core.api.Blobs;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
034import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
035import org.nuxeo.ecm.core.convert.api.ConversionService;
036import org.nuxeo.ecm.platform.picture.api.PictureView;
037import org.nuxeo.ecm.platform.picture.api.adapters.MultiviewPicture;
038
039/**
040 * Simple Operation to convert the size of a picture blob
041 *
042 * @author Tiry (tdelprat@nuxeo.com)
043 */
044@Operation(id = PictureResize.ID, category = Constants.CAT_CONVERSION, label = "Resize a picture", description = "Use conversion service to resize a picture contained in a Document or a Blob", aliases = { "Picture.resize" })
045public class PictureResize {
046
047    private static final String HEIGHT = "height";
048
049    private static final String WIDTH = "width";
050
051    private static final String PICTURE_RESIZE_CONVERTER = "pictureResize";
052
053    public static final String ID = "Picture.Resize";
054
055    @Param(name = "maxWidth", required = true)
056    protected int maxWidth = 0;
057
058    @Param(name = "maxHeight", required = true)
059    protected int maxHeigh = 0;
060
061    @Context
062    protected ConversionService service;
063
064    @OperationMethod
065    public Blob run(DocumentModel doc) {
066
067        Blob pictureBlob = null;
068        MultiviewPicture mvp = doc.getAdapter(MultiviewPicture.class);
069        if (mvp != null) {
070            // picture:origin is not always set.
071            PictureView pv = mvp.getView(mvp.getOrigin());
072            if (pv != null) {
073                pictureBlob = pv.getBlob();
074            }
075        }
076
077        if (pictureBlob == null) {
078            BlobHolder bh = doc.getAdapter(BlobHolder.class);
079            if (bh != null) {
080                pictureBlob = bh.getBlob();
081            }
082        }
083
084        if (pictureBlob == null) {
085            Blob blob = Blobs.createBlob("Unable to find a picture in the Document");
086            blob.setFilename(doc.getName() + ".null");
087            return blob;
088        } else {
089            return run(pictureBlob);
090        }
091    }
092
093    @OperationMethod
094    public Blob run(Blob blob) {
095
096        SimpleBlobHolder bh = new SimpleBlobHolder(blob);
097        Map<String, Serializable> parameters = new HashMap<>();
098
099        parameters.put(WIDTH, maxWidth);
100        parameters.put(HEIGHT, maxHeigh);
101
102        if (!service.isConverterAvailable(PICTURE_RESIZE_CONVERTER).isAvailable()) {
103            return blob;
104        }
105
106        BlobHolder result = service.convert(PICTURE_RESIZE_CONVERTER, bh, parameters);
107
108        if (result != null) {
109            return result.getBlob();
110        } else {
111            return Blobs.createBlob("Converter did not return any result");
112        }
113    }
114
115}