package com.github;

import org.apache.commons.io.FileUtils;

import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.io.File;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: nirvdrum
 * Date: Aug 24, 2009
 * Time: 6:15:46 PM
 * To change this template use File | Settings | File Templates.
 */
public class DataLoader
{
  public static Map<String, Repository> loadRepositories() throws IOException
  {
    final Map<String, Repository> repositories = new HashMap<String, Repository>();
    final Map<String, String> relationships = new HashMap<String, String>();

    // First, discover all the repositories.
    final File repoFile = FileUtils.toFile(DataLoader.class.getClassLoader().getResource("data/repos.txt"));
    for (final Object o : FileUtils.readLines(repoFile))
    {
      final String line = (String) o;

      final String[] mainParts = line.trim().split(":");
      final String repo_id = mainParts[0];

      final String[] repo_data = mainParts[1].split(",");
      final String created_at = repo_data[1];
      final String parent_id = repo_data.length == 3 ? repo_data[2] : null;

      final String[] fullName = repo_data[0].split("/");
      final String owner = fullName[0];
      final String name = fullName[1];

      repositories.put(repo_id, new Repository(repo_id, owner, name, created_at));

      if (parent_id != null)
      {
        relationships.put(repo_id, parent_id);
      }
    }

    // Now that all the repositories have been loaded, establish any parent-child relationships.
    for (final Map.Entry<String, String> pair : relationships.entrySet())
    {
      repositories.get(pair.getKey()).setParent(repositories.get(pair.getValue()));
    }

    final Map<String, Watcher> watchers = new HashMap<String, Watcher>();
    final File watchingsFile = FileUtils.toFile(DataLoader.class.getClassLoader().getResource("data/data.txt"));
    for (final Object o : FileUtils.readLines(watchingsFile))
    {
      final String line = (String) o;

      final String[] mainParts = line.trim().split(":");
      final String user_id = mainParts[0];
      final String repo_id = mainParts[1];

      final Watcher watcher = watchers.get(user_id) == null ? new Watcher(user_id) : watchers.get(user_id);
      repositories.get(repo_id).associate(watcher);
    }

    return repositories;
  }

  public static DataSet loadWatchings() throws IOException
  {
    final DataSet ret = new DataSet();

    final File watchingsFile = FileUtils.toFile(DataLoader.class.getClassLoader().getResource("data/data.txt"));
    for (final Object o : FileUtils.readLines(watchingsFile))
    {
      final String line = (String) o;

      final String[] mainParts = line.trim().split(":");
      final String user_id = mainParts[0];
      final String repo_id = mainParts[1];

      ret.add(new Watching(user_id, repo_id));
    }

    return ret;
  }

  public static Set<Watcher> loadPredictings() throws IOException
  {
    final Set<Watcher> ret = new HashSet<Watcher>();

    final File predictingsFile = FileUtils.toFile(DataLoader.class.getClassLoader().getResource("data/test.txt"));
    for (final Object o : FileUtils.readLines(predictingsFile))
    {
      final String line = (String) o;

      ret.add(new Watcher(line.trim()));
    }

    return ret;
  }
}

@@ -53,16 +53,54 @@
 leUtils;
+%0Aimport org.apache.commons.io.IOUtils;
 %0A%0Aimport
@@ -725,45 +725,46 @@
    f
-inal File repoFile = FileUtils.toFile
+or (final Object o : IOUtils.readLines
 (Dat
@@ -801,24 +801,32 @@
 .getResource
+AsStream
 (%22data/repos
@@ -836,65 +836,8 @@
 t%22))
-;%0A    for (final Object o : FileUtils.readLines(repoFile)
 )%0A  
@@ -1832,50 +1832,46 @@
    f
-inal File watchingsFile = FileUtils.toFile
+or (final Object o : IOUtils.readLines
 (Dat
@@ -1904,32 +1904,40 @@
 er().getResource
+AsStream
 (%22data/data.txt%22
@@ -1942,70 +1942,8 @@
 t%22))
-;%0A    for (final Object o : FileUtils.readLines(watchingsFile)
 )%0A  
@@ -2438,50 +2438,46 @@
    f
-inal File watchingsFile = FileUtils.toFile
+or (final Object o : IOUtils.readLines
 (Dat
@@ -2514,24 +2514,32 @@
 .getResource
+AsStream
 (%22data/data.
@@ -2548,70 +2548,8 @@
 t%22))
-;%0A    for (final Object o : FileUtils.readLines(watchingsFile)
 )%0A  
@@ -2943,52 +2943,46 @@
    f
-inal File predictingsFile = FileUtils.toFile
+or (final Object o : IOUtils.readLines
 (Dat
@@ -3023,16 +3023,24 @@
 Resource
+AsStream
 (%22data/t
@@ -3053,72 +3053,8 @@
 t%22))
-;%0A    for (final Object o : FileUtils.readLines(predictingsFile)
 )%0A  

Read files from JAR.