/*
* (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* Nuxeo - initial API and implementation
*
*/
package org.nuxeo.ecm.automation.core.operations.services;
import java.io.IOException;
import org.nuxeo.ecm.automation.AutomationService;
import org.nuxeo.ecm.automation.OperationContext;
import org.nuxeo.ecm.automation.OperationException;
import org.nuxeo.ecm.automation.core.Constants;
import org.nuxeo.ecm.automation.core.annotations.Context;
import org.nuxeo.ecm.automation.core.annotations.Operation;
import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
import org.nuxeo.ecm.automation.core.annotations.Param;
import org.nuxeo.ecm.automation.core.util.BlobList;
import org.nuxeo.ecm.core.api.Blob;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentModelList;
import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
import org.nuxeo.ecm.platform.filemanager.api.FileManager;
/**
* Use {@link FileManager} to create documents from blobs
*
* @author Tiry (tdelprat@nuxeo.com)
*/
@Operation(id = FileManagerImport.ID, category = Constants.CAT_SERVICES, label = "Create Document from file", description = "Create Document(s) from Blob(s) using the FileManagerService.")
public class FileManagerImport {
public static final String ID = "FileManager.Import";
@Context
protected CoreSession session;
@Context
protected FileManager fileManager;
@Context
protected AutomationService as;
@Context
protected OperationContext context;
@Param(name = "overwite", required = false)
protected Boolean overwite = false;
@Param(name = "noMimeTypeCheck", required = false)
protected Boolean noMimeTypeCheck = false;
protected DocumentModel getCurrentDocument() throws OperationException {
String cdRef = (String) context.get("currentDocument");
return as.getAdaptedValue(context, cdRef, DocumentModel.class);
}
@OperationMethod
public DocumentModel run(Blob blob) throws OperationException, IOException {
DocumentModel currentDocument = getCurrentDocument();
return fileManager.createDocumentFromBlob(session, blob, currentDocument.getPathAsString(), overwite,
blob.getFilename(), noMimeTypeCheck);
}
@OperationMethod
public DocumentModelList run(BlobList blobs) throws OperationException, IOException {
DocumentModelList result = new DocumentModelListImpl();
for (Blob blob : blobs) {
result.add(run(blob));
}
return result;
}
}