001/* 002 * (C) Copyright 2006-2007 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 - initial API and implementation 018 * 019 */ 020package org.nuxeo.ecm.automation.core.operations.services; 021 022import java.io.IOException; 023 024import org.nuxeo.ecm.automation.AutomationService; 025import org.nuxeo.ecm.automation.OperationContext; 026import org.nuxeo.ecm.automation.OperationException; 027import org.nuxeo.ecm.automation.core.Constants; 028import org.nuxeo.ecm.automation.core.annotations.Context; 029import org.nuxeo.ecm.automation.core.annotations.Operation; 030import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 031import org.nuxeo.ecm.automation.core.annotations.Param; 032import org.nuxeo.ecm.automation.core.util.BlobList; 033import org.nuxeo.ecm.core.api.Blob; 034import org.nuxeo.ecm.core.api.CoreSession; 035import org.nuxeo.ecm.core.api.DocumentModel; 036import org.nuxeo.ecm.core.api.DocumentModelList; 037import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl; 038import org.nuxeo.ecm.platform.filemanager.api.FileManager; 039 040/** 041 * Use {@link FileManager} to create documents from blobs 042 * 043 * @author Tiry (tdelprat@nuxeo.com) 044 */ 045@Operation(id = FileManagerImport.ID, category = Constants.CAT_SERVICES, label = "Create Document from file", description = "Create Document(s) from Blob(s) using the FileManagerService.") 046public class FileManagerImport { 047 048 public static final String ID = "FileManager.Import"; 049 050 @Context 051 protected CoreSession session; 052 053 @Context 054 protected FileManager fileManager; 055 056 @Context 057 protected AutomationService as; 058 059 @Context 060 protected OperationContext context; 061 062 @Param(name = "overwite", required = false) 063 protected Boolean overwite = false; 064 065 @Param(name = "noMimeTypeCheck", required = false) 066 protected Boolean noMimeTypeCheck = false; 067 068 protected DocumentModel getCurrentDocument() throws OperationException { 069 String cdRef = (String) context.get("currentDocument"); 070 return as.getAdaptedValue(context, cdRef, DocumentModel.class); 071 } 072 073 @OperationMethod 074 public DocumentModel run(Blob blob) throws OperationException, IOException { 075 DocumentModel currentDocument = getCurrentDocument(); 076 return fileManager.createDocumentFromBlob(session, blob, currentDocument.getPathAsString(), overwite, 077 blob.getFilename(), noMimeTypeCheck); 078 } 079 080 @OperationMethod 081 public DocumentModelList run(BlobList blobs) throws OperationException, IOException { 082 DocumentModelList result = new DocumentModelListImpl(); 083 for (Blob blob : blobs) { 084 result.add(run(blob)); 085 } 086 return result; 087 } 088 089}