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.FileManager; 038import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService; 039 040/** 041 * Uses {@link FileManager} to import files inside the User's personal workspace. 042 * 043 * @author Tiry (tdelprat@nuxeo.com) 044 */ 045@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.") 046public class UserWorkspaceCreateFromBlob { 047 048 public static final String ID = "UserWorkspace.CreateDocumentFromBlob"; 049 050 @Context 051 protected CoreSession session; 052 053 @Context 054 protected FileManager fileManager; 055 056 @Context 057 protected OperationContext context; 058 059 @Context 060 protected UserWorkspaceService userWorkspace; 061 062 @Context 063 protected AutomationService as; 064 065 protected DocumentModel getCurrentDocument() throws OperationException { 066 String cdRef = (String) context.get("currentDocument"); 067 return as.getAdaptedValue(context, cdRef, DocumentModel.class); 068 } 069 070 @OperationMethod 071 public DocumentModel run(Blob blob) throws OperationException, IOException { 072 DocumentModel userws = userWorkspace.getCurrentUserPersonalWorkspace(session, getCurrentDocument()); 073 DocumentModel doc = fileManager.createDocumentFromBlob(session, blob, userws.getPathAsString(), false, 074 blob.getFilename()); 075 return doc; 076 } 077 078 @OperationMethod 079 public DocumentModelList run(BlobList blobs) throws OperationException, IOException { 080 DocumentModelList result = new DocumentModelListImpl(); 081 for (Blob blob : blobs) { 082 result.add(run(blob)); 083 } 084 return result; 085 } 086 087}