1 | using HeuristicLab.Clients.Hive.WebJobManager.Services;
|
---|
2 | using HeuristicLab.Clients.Hive.WebJobManager.Services.Imports;
|
---|
3 | using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Optimization;
|
---|
6 | using HeuristicLab.Persistence.Default.Xml;
|
---|
7 | using Microsoft.AspNetCore.Http;
|
---|
8 | using Microsoft.AspNetCore.Mvc;
|
---|
9 | using System;
|
---|
10 | using System.Collections.Generic;
|
---|
11 | using System.IO;
|
---|
12 | using System.Threading.Tasks;
|
---|
13 |
|
---|
14 | namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
|
---|
15 | {
|
---|
16 | public class OkbManagementController : Controller
|
---|
17 | {
|
---|
18 | private OkbManagementVM vm;
|
---|
19 | private WebLoginService weblog;
|
---|
20 | private Guid userId;
|
---|
21 |
|
---|
22 | private bool init()
|
---|
23 | {
|
---|
24 | var u = HttpContext.Session.GetString("UserId");
|
---|
25 | weblog = WebLoginService.Instance;
|
---|
26 | if (u == null || u == "" || Guid.Parse(u) == Guid.Empty)
|
---|
27 | {
|
---|
28 | return false;
|
---|
29 | }
|
---|
30 | else
|
---|
31 | {
|
---|
32 | userId = Guid.Parse(u);
|
---|
33 | vm = new OkbManagementVM(weblog.getCurrentUser(userId), weblog.getOkbAdminClient(userId));
|
---|
34 | if (vm.client != null)
|
---|
35 | {
|
---|
36 | vm.client.Refresh();
|
---|
37 | return true;
|
---|
38 | }
|
---|
39 | }
|
---|
40 | return false;
|
---|
41 | }
|
---|
42 |
|
---|
43 | public IActionResult Index()
|
---|
44 | {
|
---|
45 |
|
---|
46 | if (init())
|
---|
47 | {
|
---|
48 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
49 | ViewBag.Title = "OKB Management";
|
---|
50 | return View("Index", vm);
|
---|
51 | }
|
---|
52 | else
|
---|
53 | {
|
---|
54 | return RedirectToAction("Index", "Query");
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | [HttpPost]
|
---|
59 | public async Task<IActionResult> Uploader(ICollection<IFormFile> fileupl, string name, int id)
|
---|
60 | {
|
---|
61 | if (init())
|
---|
62 | {
|
---|
63 | try
|
---|
64 | {
|
---|
65 | byte[] data = null;
|
---|
66 | foreach (var file in fileupl)
|
---|
67 | {
|
---|
68 | if (file.Length > 0)
|
---|
69 | {
|
---|
70 | using (var fileStream = file.OpenReadStream())
|
---|
71 | using (var ms = new MemoryStream())
|
---|
72 | {
|
---|
73 | fileStream.CopyTo(ms);
|
---|
74 | data = ms.ToArray();
|
---|
75 | // act on the Base64 data
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 | var deser = PersistenceUtil.Deserialize<IOptimizer>(data, CompressionType.Zip);
|
---|
80 | OKB.Administration.IOKBItem obj = null;
|
---|
81 | if (name == "Algorithms" && deser is IAlgorithm)
|
---|
82 | {
|
---|
83 | vm.client.UpdateAlgorithmData((long)id, PersistenceUtil.Serialize(deser));
|
---|
84 | vm.message = "Upload has been completed, the file is now set as the new algorithm for " + vm.client.Algorithms.Find(x => x.Id == id).Name;
|
---|
85 | obj = vm.client.Algorithms.Find(x => x.Id == id);
|
---|
86 | ((OKB.Administration.Algorithm)obj).DataTypeName = deser.GetType().Name;
|
---|
87 | ((OKB.Administration.Algorithm)obj).DataTypeTypeName = deser.GetType().AssemblyQualifiedName;
|
---|
88 | }
|
---|
89 | else if (name == "Problems" && deser is IProblem)
|
---|
90 | {
|
---|
91 | vm.client.UpdateProblemData((long)id, PersistenceUtil.Serialize(deser));
|
---|
92 | vm.message = "Upload has been completed, the file is now set as the new problem for " + vm.client.Problems.Find(x => x.Id == id).Name;
|
---|
93 | obj = vm.client.Problems.Find(x => x.Id == id);
|
---|
94 | ((OKB.Administration.Problem)obj).DataTypeName = deser.GetType().Name;
|
---|
95 | ((OKB.Administration.Problem)obj).DataTypeTypeName = deser.GetType().AssemblyQualifiedName;
|
---|
96 | }
|
---|
97 | else
|
---|
98 | {
|
---|
99 | throw new Exception();
|
---|
100 | }
|
---|
101 |
|
---|
102 | vm.client.Store(obj);
|
---|
103 | ViewBag.Title = "Upload complete - OKB Management";
|
---|
104 | ViewBag.Success = true;
|
---|
105 |
|
---|
106 | }
|
---|
107 | catch (Exception e)
|
---|
108 | {
|
---|
109 |
|
---|
110 | vm.message = "Upload failed, please try again.";
|
---|
111 | ViewBag.Success = false;
|
---|
112 | ViewBag.Title = "Upload failed - OKB Management";
|
---|
113 |
|
---|
114 | }
|
---|
115 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
116 | vm.client.Refresh();
|
---|
117 | return View("Index", vm);
|
---|
118 | }
|
---|
119 | else
|
---|
120 | {
|
---|
121 | return RedirectToAction("Index", "Query");
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|