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