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