#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
using HeuristicLab.Common;
using HeuristicLab.Optimization;
using Microsoft.AspNetCore.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
namespace HeuristicLab.Clients.Hive.WebJobManager.Services
{
///
/// Used for opening a file and uploading this to Hive.
/// Saves all relevant Job information needed for uploading.
/// Outsourced to selectedJob keep the current job loaded
///
public class FileOpeningService
{
public Guid UserId { get; set; }
private WebLoginService weblog;
public RefreshableJob Job { get; set; }
public FileOpeningViewModel vm { get; set; }
public IHostingEnvironment env { get; set; }
public List previousids {
get; set; }
public List previousLogs { get; set; }
public FileOpeningService(Guid u)
{
weblog = WebLoginService.Instance;
UserId = u;
}
///
/// Clears loaded jobs and start new one
///
public void NewModel()
{
vm = new FileOpeningViewModel(weblog.getCurrentUser(UserId));
Job = new RefreshableJob();
Job.Job.Name = "init";
}
///
/// Attaches tasks from viewmodel to job
///
public void setTasks()
{
Job.HiveTasks.Add(vm.SelectedTask);
}
///
/// Refresh all info from a job from the server
///
public void refreshJob()
{
weblog.getClientWeb(UserId).LoadJob(Job);
}
///
/// Adds loaded job from vm onto server
///
/// Job being uploaded
public RefreshableJob AddCurrentModelToHive()
{
if (vm != null)
{
var clientWeb = weblog.getClientWeb(UserId);
clientWeb.CurrentEnv = (Microsoft.AspNetCore.Hosting.IHostingEnvironment)env;
clientWeb.Refresh();
clientWeb.StartJob((ex) => { throw ex; }, Job, CancellationToken.None);
return Job;
}
return null;
}
public void SaveToFile(string file, string dname)
{
if(vm != null)
{
var uploads = Path.Combine(env.WebRootPath, "uploads", weblog.getServiceLocator(UserId).getHiveServiceClient().ClientCredentials.UserName.UserName,
dname);
Directory.CreateDirectory(uploads);
var cont = Job.HiveTasks.First().ItemTask.Item;
if (!file.EndsWith(".hl"))
file += ".hl";
var fileloc = uploads + "\\" + file;
if(cont is Experiment)
ContentManager.Save((Experiment)cont, fileloc, false);
else if(cont is BatchRun)
ContentManager.Save((BatchRun)cont, fileloc, false);
else if(cont is Algorithm)
{
IAlgorithm t = (IAlgorithm)Job.HiveTasks.First().ItemTask.Item;
ContentManager.Save((IStorableContent)t, fileloc, false);
}
}
}
}
}