Index: branches/OaaS/HeuristicLab.Services.Optimization.Controller/HL/HiveScenarioManager.cs
===================================================================
--- branches/OaaS/HeuristicLab.Services.Optimization.Controller/HL/HiveScenarioManager.cs	(revision 9062)
+++ branches/OaaS/HeuristicLab.Services.Optimization.Controller/HL/HiveScenarioManager.cs	(revision 9166)
@@ -20,4 +20,5 @@
 using HeuristicLab.Services.Optimization.ControllerService.Azure;
 using System.Data;
+using HeuristicLab.Services.Optimization.ControllerService.General;
 
 namespace HeuristicLab.Services.Optimization.ControllerService {
@@ -43,5 +44,5 @@
     private static object lockable;
     private Dictionary<string, IScenarioMapper> mappers = new Dictionary<string, IScenarioMapper>();
-    private IDataAccessLayer dal = new AzureDataAccessLayer();
+    private IDataAccessLayer dal = DataAccessLayerProvider.GetLayer();
 
     public static readonly string SCENARIO_TABLE = "Scenario";
@@ -51,15 +52,23 @@
     }
 
-    public void DispatchScenario(Model.User user, Model.OptimizationScenario scenario) {
-      Experiment experiment = new Experiment();
-      var problem = new TravelingSalesmanProblem();
-      var algo = new GeneticAlgorithm();
-      algo.Problem = problem;      
+    public void DispatchScenario(Model.User user, Model.OptimizationScenario scenario, JobExecutionDetails details) {
+      // Experiment experiment = new Experiment();
+      // var problem = new TravelingSalesmanProblem();
+      // var algo = new GeneticAlgorithm();
+      // algo.Problem = problem;      
       
       IScenarioMapper mapper = GetMapper(scenario);
-      mapper.MapScenario(scenario, algo);  
-
-      //experiment.Optimizers.Add(algo);
-      SendExperimentToHive(user, algo);
+      IAlgorithm algo;
+      mapper.MapScenario(scenario, out algo);
+      if (details.Repititions > 1) {
+        BatchRun br = new BatchRun();
+        //br.Name = details.JobTitle;
+        br.Optimizer = algo;        
+        br.Repetitions = details.Repititions;
+        SendExperimentToHive(user, br, details);
+      }
+      else {       
+        SendExperimentToHive(user, algo, details);
+      }
     }
 
@@ -73,5 +82,5 @@
     }
 
-    private IScenarioMapper CompileMapper(string scenarioMapper) {
+    public static IScenarioMapper CompileMapper(string scenarioMapper) {
        // http://stackoverflow.com/questions/3188882/compile-and-run-dynamic-code-without-generating-exe
       using (var csCodeProvider = new Microsoft.CSharp.CSharpCodeProvider()) {
@@ -105,13 +114,14 @@
 
     private IScenarioMapper GetMapper(Model.OptimizationScenario scenario) {
-      if (!mappers.ContainsKey(scenario.Name)) {
+      var id = scenario.Id;
+      if (!mappers.ContainsKey(id)) {
         lock (lockable) {
-          if (mappers.ContainsKey(scenario.Name))
-            return mappers[scenario.Name];
+          if (mappers.ContainsKey(id))
+            return mappers[id];
           
-          var mapperString = GetMapperFromBlobStore(scenario.Name);
+          var mapperString = GetMapperFromBlobStore(id);
           var mapper = CompileMapper(mapperString);
 
-          mappers[scenario.Name] = mapper;
+          mappers[id] = mapper;
           return mapper;
         } // lock        
@@ -120,19 +130,26 @@
      }  
 
-    private void MapExperiment(Model.OptimizationScenario scenario, IAlgorithm algorithm) {      
+    private void MapExperiment(Model.OptimizationScenario scenario, out IAlgorithm algorithm) {      
       IScenarioMapper mapper = GetMapper(scenario);
-      mapper.MapScenario(scenario, algorithm);     
-    }
-
-    private void SendExperimentToHive(Model.User user, IAlgorithm exp) {
+      mapper.MapScenario(scenario, out algorithm);     
+    }
+
+    private void ConfigureHive(Model.User user) {
+      HiveServiceLocator.Instance.Username = user.Username;
+      HiveServiceLocator.Instance.Password = user.Password;
+      HiveServiceLocator.Instance.EndpointConfigurationName = Configuration.HiveEndpointName;      
+    }
+
+    private void SendExperimentToHive(Model.User user, IOptimizer exp, JobExecutionDetails details) {      
       var job = new RefreshableJob();
       job.IsAllowedPrivileged = true;      
-      job.Job.Name = "Web Scheduled Traveling Salesman Job";
-      job.Job.ResourceNames = "TESTGROUP";
-      job.RefreshAutomatically = false;
+      job.Job.Name = details.JobTitle;
+      job.Job.ResourceNames = details.Group;      
+      job.RefreshAutomatically = false;     
+      if (exp.ExecutionState != ExecutionState.Prepared) {
+        exp.Prepare();
+      }      
       job.HiveTasks.Add(new OptimizerHiveTask(exp));
-      HiveServiceLocator.Instance.Username = user.Username;
-      HiveServiceLocator.Instance.Password = user.Password;
-      HiveServiceLocator.Instance.EndpointConfigurationName = "WSHttpBinding_IHiveService";
+      ConfigureHive(user);
       
       HiveClient.StartJob((ex) => { 
@@ -145,10 +162,8 @@
 
     public IList<Model.Job> GetJobs(User user) {
-      HiveServiceLocator.Instance.Username = user.Username;
-      HiveServiceLocator.Instance.Password = user.Password;
-      HiveServiceLocator.Instance.EndpointConfigurationName = "WSHttpBinding_IHiveService";
+      ConfigureHive(user);
       var jobsLoaded = HiveServiceLocator.Instance.CallHiveService<IEnumerable<HeuristicLab.Clients.Hive.Job>>(s => s.GetJobs());
       IList<Model.Job> jobs = new List<Model.Job>();
-      
+
       foreach (var job in jobsLoaded) {
         jobs.Add(ConvertJob(user, job));
@@ -163,28 +178,8 @@
       if (job.CalculatingCount > 0)
         state = JobState.Calculating;
-      else if (job.JobCount == job.FinishedCount)
+      else if (job.JobCount > 0 && job.JobCount == job.FinishedCount)
         state = JobState.Finished;
       else
         state = JobState.Waiting;
-      /*var jobTasks = HiveServiceLocator.Instance.CallHiveService<IEnumerable<HeuristicLab.Clients.Hive.LightweightTask>>(s => s.GetLightweightJobTasks(job.Id));
-      
-      foreach (var task in jobTasks) {        
-        switch (task.State) {
-          case TaskState.Aborted:
-            state = JobState.Aborted;
-            break;
-          case TaskState.Failed:
-            state = JobState.Failed;
-            break;
-        }
-      }
-      if (!state.HasValue) {
-        if (job.CalculatingCount > 0)
-          state = JobState.Calculating;
-        else if (waitingJobs > 0)
-          state = JobState.Waiting;
-        else
-          state = JobState.Finished;        
-      }*/
       return new Model.Job() { Id = job.Id.ToString(), Name = job.Name, Resource = job.ResourceNames, State = state.Value, DateCreated = job.DateCreated };
     }
@@ -192,7 +187,5 @@
 
     public Model.Job GetJob(User user, string id) {
-      HiveServiceLocator.Instance.Username = user.Username;
-      HiveServiceLocator.Instance.Password = user.Password;
-      HiveServiceLocator.Instance.EndpointConfigurationName = "WSHttpBinding_IHiveService";
+      ConfigureHive(user);
       var guid = Guid.Parse(id);
       return ConvertJob(user, HiveServiceLocator.Instance.CallHiveService<HeuristicLab.Clients.Hive.Job>(s => s.GetJob(guid)));
@@ -201,7 +194,5 @@
 
     public void DeleteJob(User user, string id) {
-      HiveServiceLocator.Instance.Username = user.Username;
-      HiveServiceLocator.Instance.Password = user.Password;
-      HiveServiceLocator.Instance.EndpointConfigurationName = "WSHttpBinding_IHiveService";
+      ConfigureHive(user);
       var guid = Guid.Parse(id);
       HiveServiceLocator.Instance.CallHiveService(s => s.DeleteJob(guid));      
@@ -209,7 +200,5 @@
 
     public IList<Model.Run> GetJobResults(User user, string id) {
-      HiveServiceLocator.Instance.Username = user.Username;
-      HiveServiceLocator.Instance.Password = user.Password;
-      HiveServiceLocator.Instance.EndpointConfigurationName = "WSHttpBinding_IHiveService";
+      ConfigureHive(user);
       var guid = Guid.Parse(id);
       var jobTasks = HiveServiceLocator.Instance.CallHiveService<IEnumerable<HeuristicLab.Clients.Hive.LightweightTask>>(s => s.GetLightweightJobTasks(guid));
