using HeuristicLab.Clients.Hive.WebJobManager.Services; using HeuristicLab.Clients.Hive.WebJobManager.Services.Imports; using HeuristicLab.Clients.Hive.WebJobManager.ViewModels; using HeuristicLab.Common; using HeuristicLab.Optimization; using HeuristicLab.Persistence.Default.Xml; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers { public class OkbManagementController : Controller { private OkbManagementVM vm; private WebLoginService weblog; private Guid userId; private bool init() { var u = HttpContext.Session.GetString("UserId"); weblog = WebLoginService.Instance; if (u == null || u == "" || Guid.Parse(u) == Guid.Empty) { return false; } else { userId = Guid.Parse(u); vm = new OkbManagementVM(weblog.getCurrentUser(userId), weblog.getOkbAdminClient(userId)); if (vm.client != null) { vm.client.Refresh(); return true; } } return false; } public IActionResult Index() { if (init()) { ViewBag.SessionId = HttpContext.Session.GetString("UserId"); ViewBag.Title = "OKB Management"; return View("Index", vm); } else { return RedirectToAction("Index", "Query"); } } [HttpPost] public async Task Uploader(ICollection fileupl, string name, int id) { if (init()) { try { byte[] data = null; foreach (var file in fileupl) { if (file.Length > 0) { using (var fileStream = file.OpenReadStream()) using (var ms = new MemoryStream()) { fileStream.CopyTo(ms); data = ms.ToArray(); // act on the Base64 data } } } var deser = PersistenceUtil.Deserialize(data, CompressionType.Zip); OKB.Administration.IOKBItem obj = null; if (name == "Algorithms" && deser is IAlgorithm) { vm.client.UpdateAlgorithmData((long)id, PersistenceUtil.Serialize(deser)); 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; obj = vm.client.Algorithms.Find(x => x.Id == id); ((OKB.Administration.Algorithm)obj).DataTypeName = deser.GetType().Name; ((OKB.Administration.Algorithm)obj).DataTypeTypeName = deser.GetType().AssemblyQualifiedName; } else if (name == "Problems" && deser is IProblem) { vm.client.UpdateProblemData((long)id, PersistenceUtil.Serialize(deser)); 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; obj = vm.client.Problems.Find(x => x.Id == id); ((OKB.Administration.Problem)obj).DataTypeName = deser.GetType().Name; ((OKB.Administration.Problem)obj).DataTypeTypeName = deser.GetType().AssemblyQualifiedName; } else { throw new Exception(); } vm.client.Store(obj); ViewBag.Title = "Upload complete - OKB Management"; ViewBag.Success = true; } catch (Exception e) { vm.message = "Upload failed, please try again."; ViewBag.Success = false; ViewBag.Title = "Upload failed - OKB Management"; } ViewBag.SessionId = HttpContext.Session.GetString("UserId"); vm.client.Refresh(); return View("Index", vm); } else { return RedirectToAction("Index", "Query"); } } } }