001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 */ 018package org.nuxeo.ecm.automation.core.operations.services; 019 020import java.io.IOException; 021 022import org.nuxeo.ecm.automation.AutomationService; 023import org.nuxeo.ecm.automation.OperationContext; 024import org.nuxeo.ecm.automation.OperationException; 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.automation.core.util.BlobList; 031import org.nuxeo.ecm.core.api.Blob; 032import org.nuxeo.ecm.core.api.CoreSession; 033import org.nuxeo.ecm.core.api.DocumentModel; 034import org.nuxeo.ecm.core.api.DocumentModelList; 035import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl; 036import org.nuxeo.ecm.platform.filemanager.api.FileManager; 037 038/** 039 * Use {@link FileManager} to create documents from blobs 040 * 041 * @author Tiry (tdelprat@nuxeo.com) 042 */ 043@Operation(id = FileManagerImport.ID, category = Constants.CAT_SERVICES, label = "Create Document from file", description = "Create Document(s) from Blob(s) using the FileManagerService.") 044public class FileManagerImport { 045 046 public static final String ID = "FileManager.Import"; 047 048 @Context 049 protected CoreSession session; 050 051 @Context 052 protected FileManager fileManager; 053 054 @Context 055 protected AutomationService as; 056 057 @Context 058 protected OperationContext context; 059 060 @Param(name = "overwite", required = false) 061 protected Boolean overwite = false; 062 063 protected DocumentModel getCurrentDocument() throws OperationException { 064 String cdRef = (String) context.get("currentDocument"); 065 return as.getAdaptedValue(context, cdRef, DocumentModel.class); 066 } 067 068 @OperationMethod 069 public DocumentModel run(Blob blob) throws OperationException, IOException { 070 DocumentModel currentDocument = getCurrentDocument(); 071 return fileManager.createDocumentFromBlob(session, blob, currentDocument.getPathAsString(), overwite, 072 blob.getFilename()); 073 } 074 075 @OperationMethod 076 public DocumentModelList run(BlobList blobs) throws OperationException, IOException { 077 DocumentModelList result = new DocumentModelListImpl(); 078 for (Blob blob : blobs) { 079 result.add(run(blob)); 080 } 081 return result; 082 } 083 084}