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