/*
 *
 *  * (C) Copyright ${year} 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
 *
 */

package com.nuxeo.perforce.importer;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

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.core.api.CoreSession;
import org.nuxeo.ecm.core.api.NuxeoException;
import org.nuxeo.ecm.core.api.NuxeoPrincipal;
import org.nuxeo.ecm.core.work.api.WorkManager;
import org.nuxeo.runtime.api.Framework;

import com.nuxeo.perforce.VCSEventsPerforce;
import com.nuxeo.perforce.VCSEventsProvider;
import com.nuxeo.perforce.VCSEventsService;
import com.nuxeo.perforce.blob.PerforceBlobProvider;
import com.perforce.p4java.exception.AccessException;
import com.perforce.p4java.exception.ConnectionException;

/**
 *
 */
@Operation(id = PerforceInitializer.ID, category = Constants.CAT_SERVICES, label = "Perforce Depot Initializer", description = "Describe here what your operation does.")
public class PerforceInitializer {

    public static final String ID = "Perforce.Initializer";

    private static final int BATCH = 20;

    @Context
    protected CoreSession session;

    @OperationMethod
    public void run() throws ConnectionException, AccessException {
        if (!((NuxeoPrincipal) session.getPrincipal()).isAdministrator()) {
            throw new NuxeoException("You must be an Administrator.");
        }

        VCSEventsService service = Framework.getService(VCSEventsService.class);
        VCSEventsProvider provider = service.getEventsProvider(VCSEventsPerforce.NAME);

        PerforceBlobProvider blobProvider = (PerforceBlobProvider) provider.getBlobProvider();
        List<String> files = blobProvider.listAllDepotFilesPath()
                                         .stream()
                                         .filter(provider::handleFilename)
                                         .collect(Collectors.toList());
        IntStream.range(0, (files.size() + BATCH - 1) / BATCH)
                 .mapToObj(i -> files.subList(i * BATCH, Math.min(files.size(), (i + 1) * BATCH)))
                 .forEach(f -> {
                     VCSInitializerWorker work = new VCSInitializerWorker(provider.getName());
                     work.addRemoteFile(f);

                     Framework.getService(WorkManager.class).schedule(work, true);
                 });
    }
}

@@ -812,43 +812,8 @@
 ors;
-%0Aimport java.util.stream.IntStream;
 %0A%0Aim
@@ -1266,16 +1266,56 @@
 ework;%0A%0A
+import com.google.common.collect.Lists;%0A
 import c
@@ -2487,16 +2487,93 @@
 ider();%0A
+        // Get all Depot file and filter on only Perforce accepted mime-type%0A
         
@@ -2828,24 +2828,25 @@
 ist());%0A
+%0A
         
 IntStrea
@@ -2841,179 +2841,91 @@
     
-IntStream.range(0, (files.size() + BATCH - 1) / BATCH)%0A                 .mapToObj(i -%3E files.subList(i * BATCH, Math.min(files.size(), (i + 1) * BATCH)))%0A                 
+// Batch the result list to 20 files per work%0A        Lists.partition(files, BATCH)
 .for
@@ -2936,25 +2936,16 @@
 (f -%3E %7B%0A
-         
         
@@ -3034,25 +3034,16 @@
         
-  
-       
 work.add
@@ -3058,25 +3058,16 @@
 le(f);%0A%0A
-         
         
@@ -3132,25 +3132,16 @@
  true);%0A
-         
         

Simplify list partition