001/*
002 * (C) Copyright 2012 Nuxeo SA (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 *     Antoine Taillefer <ataillefer@nuxeo.com>
016 */
017package org.nuxeo.drive.operations.test;
018
019import org.nuxeo.ecm.automation.core.Constants;
020import org.nuxeo.ecm.automation.core.annotations.Context;
021import org.nuxeo.ecm.automation.core.annotations.Operation;
022import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
023import org.nuxeo.ecm.automation.core.annotations.Param;
024import org.nuxeo.ecm.core.api.Blob;
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.impl.blob.StringBlob;
028import org.nuxeo.ecm.platform.filemanager.api.FileManager;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * Create batch of test documents in a single automation query
033 *
034 * @author Olivier Grisel
035 */
036@Operation(id = NuxeoDriveCreateTestDocuments.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Create test documents")
037public class NuxeoDriveCreateTestDocuments {
038
039    public static final String ID = "NuxeoDrive.CreateTestDocuments";
040
041    @Context
042    protected CoreSession session;
043
044    @Param(name = "namePattern", required = false)
045    protected String namePattern = "file_%03d.txt";
046
047    @Param(name = "contentPattern", required = false)
048    protected String contentPattern = "Content for file_%03d.txt";
049
050    @Param(name = "number", required = false)
051    protected Integer number = 10;
052
053    // delay in ms between two consecutive document creations to
054    // artificially space the events in the logs (e.g. simulating long
055    // operations such as S3BinaryManager blob uploads).
056    @Param(name = "delay", required = false)
057    protected long delay = 1000L;
058
059    @OperationMethod
060    public Blob run(DocumentModel parent) throws Exception {
061        NuxeoDriveIntegrationTestsHelper.checkOperationAllowed();
062        FileManager fileManager = Framework.getLocalService(FileManager.class);
063        for (int i = 0; i < number; i++) {
064            String name = String.format(namePattern, i);
065            Blob content = new StringBlob(String.format(contentPattern, i));
066            content.setFilename(name);
067            fileManager.createDocumentFromBlob(session, content, parent.getPathAsString(), true, name);
068            if (delay > 0) {
069                Thread.sleep(delay);
070            }
071        }
072        return new StringBlob(number.toString(), "text/plain");
073    }
074}