1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 |
|
---|
23 | using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Optimization;
|
---|
26 | using Microsoft.AspNetCore.Hosting;
|
---|
27 | using System;
|
---|
28 | using System.Collections.Generic;
|
---|
29 | using System.IO;
|
---|
30 | using System.Linq;
|
---|
31 | using System.Threading;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Clients.Hive.WebJobManager.Services
|
---|
34 | {
|
---|
35 | /// <summary>
|
---|
36 | /// Used for opening a file and uploading this to Hive.
|
---|
37 | /// Saves all relevant Job information needed for uploading.
|
---|
38 | /// Outsourced to selectedJob keep the current job loaded
|
---|
39 | /// </summary>
|
---|
40 | public class FileOpeningService
|
---|
41 | {
|
---|
42 | public Guid UserId { get; set; }
|
---|
43 |
|
---|
44 | private WebLoginService weblog;
|
---|
45 | public RefreshableJob Job { get; set; }
|
---|
46 | public FileOpeningViewModel vm { get; set; }
|
---|
47 | public IHostingEnvironment env { get; set; }
|
---|
48 | public List<Guid> previousids {
|
---|
49 | get; set; }
|
---|
50 | public List<int> previousLogs { get; set; }
|
---|
51 |
|
---|
52 |
|
---|
53 | public FileOpeningService(Guid u)
|
---|
54 | {
|
---|
55 | weblog = WebLoginService.Instance;
|
---|
56 | UserId = u;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | /// <summary>
|
---|
61 | /// Clears loaded jobs and start new one
|
---|
62 | /// </summary>
|
---|
63 | public void NewModel()
|
---|
64 | {
|
---|
65 | vm = new FileOpeningViewModel(weblog.getCurrentUser(UserId));
|
---|
66 | Job = new RefreshableJob();
|
---|
67 |
|
---|
68 | Job.Job.Name = "init";
|
---|
69 |
|
---|
70 | }
|
---|
71 | /// <summary>
|
---|
72 | /// Attaches tasks from viewmodel to job
|
---|
73 | /// </summary>
|
---|
74 | public void setTasks()
|
---|
75 | {
|
---|
76 |
|
---|
77 | Job.HiveTasks.Add(vm.SelectedTask);
|
---|
78 | }
|
---|
79 | /// <summary>
|
---|
80 | /// Refresh all info from a job from the server
|
---|
81 | /// </summary>
|
---|
82 | public void refreshJob()
|
---|
83 | {
|
---|
84 | weblog.getClientWeb(UserId).LoadJob(Job);
|
---|
85 | }
|
---|
86 | /// <summary>
|
---|
87 | /// Adds loaded job from vm onto server
|
---|
88 | /// </summary>
|
---|
89 | /// <returns>Job being uploaded</returns>
|
---|
90 | public RefreshableJob AddCurrentModelToHive()
|
---|
91 | {
|
---|
92 | if (vm != null)
|
---|
93 | {
|
---|
94 | var clientWeb = weblog.getClientWeb(UserId);
|
---|
95 | clientWeb.CurrentEnv = (Microsoft.AspNetCore.Hosting.IHostingEnvironment)env;
|
---|
96 | clientWeb.Refresh();
|
---|
97 | clientWeb.StartJob((ex) => { throw ex; }, Job, CancellationToken.None);
|
---|
98 | return Job;
|
---|
99 | }
|
---|
100 | return null;
|
---|
101 | }
|
---|
102 | public void SaveToFile(string file, string dname)
|
---|
103 | {
|
---|
104 | if(vm != null)
|
---|
105 | {
|
---|
106 | var uploads = Path.Combine(env.WebRootPath, "uploads", weblog.getServiceLocator(UserId).getHiveServiceClient().ClientCredentials.UserName.UserName,
|
---|
107 | dname);
|
---|
108 | Directory.CreateDirectory(uploads);
|
---|
109 | var cont = Job.HiveTasks.First().ItemTask.Item;
|
---|
110 | if (!file.EndsWith(".hl"))
|
---|
111 | file += ".hl";
|
---|
112 | var fileloc = uploads + "\\" + file;
|
---|
113 | if(cont is Experiment)
|
---|
114 | ContentManager.Save((Experiment)cont, fileloc, false);
|
---|
115 | else if(cont is BatchRun)
|
---|
116 | ContentManager.Save((BatchRun)cont, fileloc, false);
|
---|
117 | else if(cont is Algorithm)
|
---|
118 | {
|
---|
119 | IAlgorithm t = (IAlgorithm)Job.HiveTasks.First().ItemTask.Item;
|
---|
120 | ContentManager.Save((IStorableContent)t, fileloc, false);
|
---|
121 | }
|
---|
122 |
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | }
|
---|
127 | }
|
---|