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 * bstefanescu 011 */ 012package org.nuxeo.ecm.automation.core.operations.blob; 013 014import org.nuxeo.ecm.automation.OperationContext; 015import org.nuxeo.ecm.automation.core.Constants; 016import org.nuxeo.ecm.automation.core.annotations.Context; 017import org.nuxeo.ecm.automation.core.annotations.Operation; 018import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 019import org.nuxeo.ecm.automation.core.annotations.Param; 020import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector; 021import org.nuxeo.ecm.core.api.Blob; 022import org.nuxeo.ecm.core.api.CoreSession; 023import org.nuxeo.ecm.core.api.DocumentModel; 024import org.nuxeo.ecm.core.api.model.Property; 025 026/** 027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 028 */ 029@Operation(id = SetBlobFileName.ID, category = Constants.CAT_BLOB, label = "Set File Name", description = "Modify the filename of a file stored in the input document. The file is found in the input document given its xpath specified through the 'xpath' parameter. Return back the input document.", aliases = { "Blob.SetFilename" }) 030public class SetBlobFileName { 031 032 public static final String ID = "Document.SetBlobName"; 033 034 @Context 035 protected OperationContext ctx; 036 037 @Context 038 protected CoreSession session; 039 040 @Param(name = "name") 041 protected String name; 042 043 @Param(name = "xpath", required = false, values = "file:content") 044 protected String xpath = "file:content"; 045 046 @Param(name = "save", required = false, values = "true") 047 protected boolean save = true; 048 049 @OperationMethod(collector = DocumentModelCollector.class) 050 public DocumentModel run(DocumentModel doc) throws java.lang.Exception { 051 Property p = doc.getProperty(xpath); 052 Object o = p.getValue(); 053 if (o instanceof Blob) { 054 Blob blob = (Blob) o; 055 blob.setFilename(name); 056 p.setValue(blob); 057 } 058 if (save) { 059 doc = session.saveDocument(doc); 060 } 061 return doc; 062 } 063 064}