package com.hp.octane.plugins.jenkins.tests.gherkin;

import hudson.FilePath;
import hudson.model.AbstractBuild;
import org.apache.logging.log4j.LogManager;

import java.io.File;

/**
 * Created by franksha on 22/03/2016.
 */
public class GherkinEventsService {

  private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(GherkinEventsService.class);

  public static void copyGherkinTestResultsToBuildDir(AbstractBuild build) {
    FilePath workspacePath = build.getWorkspace();

    try {
      FilePath[] gherkinResults = workspacePath.list(GherkinTestResultsCollector.GHERKIN_NGA_RESULTS_XML_);
      String buildDirPath = build.getRootDir().getAbsolutePath();
      for (int i=0; i<gherkinResults.length; i++) {
        String fileName = buildDirPath + File.separator + GherkinTestResultsCollector.GHERKIN_NGA_RESULTS + i + ".xml";
        FilePath gherkinResPathInBuildDir = new FilePath(new File(fileName));
        gherkinResults[i].copyTo(gherkinResPathInBuildDir);
      }
    } catch (Exception e) {
      logger.error("Error getting the Gherkin results file.", e);
    }
  }
}

@@ -171,16 +171,95 @@
 io.File;
+%0Aimport java.io.IOException;%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -595,24 +595,29 @@
 %7B%0A      
+List%3C
 FilePath
 %5B%5D gherk
@@ -612,10 +612,9 @@
 Path
-%5B%5D
+%3E
  ghe
@@ -631,26 +631,16 @@
 s = 
-workspacePath.list
+findFile
 (Ghe
@@ -688,16 +688,31 @@
 LTS_XML_
+, workspacePath
 );%0A     
@@ -813,14 +813,14 @@
 lts.
-length
+size()
 ; i+
@@ -1048,11 +1048,15 @@
 ults
-%5Bi%5D
+.get(i)
 .cop
@@ -1188,16 +1188,16 @@
 .%22, e);%0A
-
     %7D%0A  
@@ -1197,11 +1197,505 @@
    %7D%0A  %7D
+%0A%0A  private static List%3CFilePath%3E findFile(String fileName, FilePath directoy) throws IOException, InterruptedException %7B%0A    List%3CFilePath%3E results = new ArrayList%3C%3E();%0A    FilePath%5B%5D files = directoy.list(fileName);%0A    for(int i=0; i%3Cfiles.length; i++) %7B%0A      results.add(files%5Bi%5D);%0A    %7D%0A%0A    List%3CFilePath%3E subDirectories = directoy.listDirectories();%0A    for(FilePath subDirectory : subDirectories) %7B%0A      results.addAll(findFile(fileName, subDirectory));%0A    %7D%0A%0A    return results;%0A  %7D
 %0A%7D%0A

fix the way we find the gherkin test results files in the workspace