package com.bbn.kbp.events2014.io;

import com.bbn.bue.common.symbols.Symbol;
import com.bbn.kbp.events2014.ArgumentOutput;
import com.bbn.kbp.events2014.ResponseLinking;
import com.bbn.kbp.events2014.SystemOutput2015;
import com.bbn.kbp.events2014.SystemOutputLayout;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

/**
 * Created by jdeyoung on 7/27/15.
 */
final class ImportForeignIDs {

  private static final Logger log = LoggerFactory.getLogger(ImportForeignIDs.class);

  public static void main(String... args) {
    try {
      trueMain(args);
    } catch (final Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  }

  private static void trueMain(final String[] args) throws IOException {
    final File outputStoreBase = new File(args[0]);
    final File outputDirectory = new File(args[1]);
    final ArgumentStore originalArgumentStore =
        AssessmentSpecFormats.openSystemOutputStore(new File(outputStoreBase, "arguments"),
            AssessmentSpecFormats.Format.KBP2015);
    final LinkingStore originalLinkingStore =
        LinkingSpecFormats.openLinkingStore(new File(outputStoreBase, "linking"));

    final SystemOutputStore newOutput =
        SystemOutputLayout.KBP_EA_2015.openOrCreate(outputDirectory);

    for (final Symbol docid : originalArgumentStore.docIDs()) {
      log.info("Converting {}", docid);
      final ImmutableBiMap.Builder<String, String> originalToSystem = ImmutableBiMap.builder();
      final ArgumentOutput originalArguments = AssessmentSpecFormats
          .uncachedReadFromArgumentStoreCachingOldIDS(originalArgumentStore, docid,
              originalToSystem);

      final Optional<ResponseLinking> transformedLinking = LinkingSpecFormats
          .readFromLinkingStoreTransformingIDs(originalLinkingStore, docid,
              originalArguments.responses(),
              Optional.<ImmutableMap<String,String>>of(originalToSystem.build()));
      // create system output
      final SystemOutput2015 newSystemOutput =
          SystemOutput2015.from(originalArguments, transformedLinking.get());
      newOutput.write(newSystemOutput);
    }
    originalArgumentStore.close();
    originalLinkingStore.close();
    newOutput.close();
  }
}

@@ -1012,24 +1012,197 @@
 e(args%5B1%5D);%0A
+    importForeignIDs(outputStoreBase, outputDirectory);%0A  %7D%0A%0A  public static void importForeignIDs(final File source, final File outputDirectory)%0A      throws IOException %7B%0A
     final Ar
@@ -1294,38 +1294,29 @@
 re(new File(
+s
 ou
-tputStoreBas
+rc
 e, %22argument
@@ -1470,30 +1470,21 @@
 ew File(
+s
 ou
-tputStoreBas
+rc
 e, %22link

Allow ImportForeignIDs to be more easily callable from other code