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.platform.userworkspace.operations; 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.util.BlobList; 032import org.nuxeo.ecm.core.api.Blob; 033import org.nuxeo.ecm.core.api.CoreSession; 034import org.nuxeo.ecm.core.api.DocumentModel; 035import org.nuxeo.ecm.core.api.DocumentModelList; 036import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl; 037import org.nuxeo.ecm.platform.filemanager.api.FileImporterContext; 038import org.nuxeo.ecm.platform.filemanager.api.FileManager; 039import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService; 040 041/** 042 * Uses {@link FileManager} to import files inside the User's personal workspace. 043 * 044 * @author Tiry ([email protected]) 045 */ 046@Operation(id = UserWorkspaceCreateFromBlob.ID, category = Constants.CAT_SERVICES, label = "Create Document from file in User's workspace", description = "Create Document(s) in the user's workspace from Blob(s) using the FileManagerService.") 047public class UserWorkspaceCreateFromBlob { 048 049 public static final String ID = "UserWorkspace.CreateDocumentFromBlob"; 050 051 @Context 052 protected CoreSession session; 053 054 @Context 055 protected FileManager fileManager; 056 057 @Context 058 protected OperationContext context; 059 060 @Context 061 protected UserWorkspaceService userWorkspace; 062 063 @Context 064 protected AutomationService as; 065 066 protected DocumentModel getCurrentDocument() throws OperationException { 067 String cdRef = (String) context.get("currentDocument"); 068 return as.getAdaptedValue(context, cdRef, DocumentModel.class); 069 } 070 071 @OperationMethod 072 public DocumentModel run(Blob blob) throws OperationException, IOException { 073 DocumentModel userws = userWorkspace.getCurrentUserPersonalWorkspace(session, getCurrentDocument()); 074 FileImporterContext fileImporterContext = FileImporterContext.builder(session, blob, userws.getPathAsString()) 075 .build(); 076 return fileManager.createOrUpdateDocument(fileImporterContext); 077 } 078 079 @OperationMethod 080 public DocumentModelList run(BlobList blobs) throws OperationException, IOException { 081 DocumentModelList result = new DocumentModelListImpl(); 082 for (Blob blob : blobs) { 083 result.add(run(blob)); 084 } 085 return result; 086 } 087 088}