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.adapters.MultiviewPicture; 037 038/** 039 * Simple Operation to convert the size of a picture blob 040 * 041 * @author Tiry (tdelprat@nuxeo.com) 042 */ 043@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" }) 044public class PictureResize { 045 046 private static final String HEIGHT = "height"; 047 048 private static final String WIDTH = "width"; 049 050 private static final String PICTURE_RESIZE_CONVERTER = "pictureResize"; 051 052 public static final String ID = "Picture.Resize"; 053 054 @Param(name = "maxWidth", required = true) 055 protected int maxWidth = 0; 056 057 @Param(name = "maxHeight", required = true) 058 protected int maxHeigh = 0; 059 060 @Context 061 protected ConversionService service; 062 063 @OperationMethod 064 public Blob run(DocumentModel doc) { 065 066 Blob pictureBlob = null; 067 MultiviewPicture mvp = doc.getAdapter(MultiviewPicture.class); 068 if (mvp != null) { 069 pictureBlob = mvp.getView(mvp.getOrigin()).getBlob(); 070 } else { 071 BlobHolder bh = doc.getAdapter(BlobHolder.class); 072 if (bh != null) { 073 pictureBlob = bh.getBlob(); 074 } 075 } 076 077 if (pictureBlob == null) { 078 Blob blob = Blobs.createBlob("Unable to find a picture in the Document"); 079 blob.setFilename(doc.getName() + ".null"); 080 return blob; 081 } else { 082 return run(pictureBlob); 083 } 084 } 085 086 @OperationMethod 087 public Blob run(Blob blob) { 088 089 SimpleBlobHolder bh = new SimpleBlobHolder(blob); 090 Map<String, Serializable> parameters = new HashMap<String, Serializable>(); 091 092 parameters.put(WIDTH, maxWidth); 093 parameters.put(HEIGHT, maxHeigh); 094 095 if (!service.isConverterAvailable(PICTURE_RESIZE_CONVERTER).isAvailable()) { 096 return blob; 097 } 098 099 BlobHolder result = service.convert(PICTURE_RESIZE_CONVERTER, bh, parameters); 100 101 if (result != null) { 102 return result.getBlob(); 103 } else { 104 return Blobs.createBlob("Converter did not return any result"); 105 } 106 } 107 108}