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