package stroom;

import org.springframework.stereotype.Component;
import stroom.importexport.server.ImportExportService;
import stroom.test.StroomCoreServerTestFileUtil;
import stroom.util.shared.Version;

import javax.inject.Inject;
import java.io.File;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

/**
 * This class should be used when integration tests require stroom content. The downloadStroomContent gradle
 * task that is part of the setupSampleData task should be used when you need stroom content for manual testing
 * inside stroom
 */
@Component
public class ContentImportService {

   public static final String CONTENT_PACK_IMPORT_DIR = "transientContentPacks";

   private static final Version CORE_XML_SCHEMAS_VERSION = Version.of(1,0);
   private static final Version EVENT_LOGGING_XML_SCHEMA_VERSION = Version.of(1,0);
   private static final Version VISUALISATIONS_VERSION = Version.of(3,0,4);

   private ImportExportService importExportService;

   @Inject
   public ContentImportService(final ImportExportService importExportService) {
      this.importExportService = importExportService;
   }

   public void importXmlSchemas() {
     importContentPacks(Arrays.asList(
             ContentPack.of("core-xml-schemas", CORE_XML_SCHEMAS_VERSION),
             ContentPack.of("event-logging-xml-schema", EVENT_LOGGING_XML_SCHEMA_VERSION)
     ));
   }

   public void importVisualisations() {

      Path contentPackDirPath = getContentPackDirPath();

      Path packPath = VisualisationsDownloader.downloadVisualisations(VISUALISATIONS_VERSION, contentPackDirPath);
      importExportService.performImportWithoutConfirmation(packPath);
   }

   public void importContentPacks(final List<ContentPack> packs) {

      Path contentPackDirPath = getContentPackDirPath();

      packs.forEach(pack -> {
         Path packPath = ContentPackDownloader.downloadContentPack(
                 pack.getName(), pack.getVersion(), contentPackDirPath);
         importExportService.performImportWithoutConfirmation(packPath);
      });
   }

   private Path getContentPackDirPath() {
      return new File(StroomCoreServerTestFileUtil.getTestResourcesDir(), CONTENT_PACK_IMPORT_DIR).toPath();
   }

   public static class ContentPack {
      private final String name;
      private final Version version;

      public static ContentPack of(final String name, final Version version) {
         return new ContentPack(name, version);
      }

      public ContentPack(final String name, final Version version) {
         this.name = name;
         this.version = version;
      }

      public String getName() {
         return name;
      }

      public Version getVersion() {
         return version;
      }
   }

}

@@ -248,16 +248,72 @@
 o.File;%0A
+import java.io.IOException;%0Aimport java.nio.file.Files;%0A
 import j
@@ -2172,30 +2172,45 @@
 h() %7B%0A      
-return
+Path contentPackDir =
  new File(St
@@ -2294,16 +2294,312 @@
 Path();%0A
+      try %7B%0A         Files.createDirectories(contentPackDir);%0A      %7D catch (IOException e) %7B%0A         throw new RuntimeException(String.format(%22Error creating directory %25s for content packs%22,%0A                 contentPackDir.toAbsolutePath().toString()), e);%0A      %7D%0A      return contentPackDir;%0A
    %7D%0A%0A  

Add creation of contentPackDir