using HeuristicLab.Clients.Hive.WebJobManager.Services; using HeuristicLab.Clients.Hive.WebJobManager.Services.Imports; using HeuristicLab.Clients.OKB.Administration; using Microsoft.AspNetCore.SignalR; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace HeuristicLab.Clients.Hive.WebJobManager.Hubs { public class OkbManagerHub : Hub { private WebLoginService weblog; private Guid userId; private OkbAdministrationWebClient adminclient; /// /// Loads up the services for the client (Instance is only created upon a call from a client) /// private void loader() { weblog = WebLoginService.Instance; string uid = Context.QueryString["userid"]; if (uid == null || uid == "" || Guid.Parse(uid) == Guid.Empty) { userId = Guid.Empty; } else { userId = Guid.Parse(uid); adminclient = weblog.getOkbAdminClient(userId); } } public void Save(string json, string name) { loader(); JObject obj = JObject.Parse(json); NamedOKBItem tostore = null; Type objtype = null; int id = ((int)obj.GetValue("Id")); switch (name) { case "Algorithms": objtype = typeof(Algorithm); if (id != -1) tostore = adminclient.Algorithms.Find(x => x.Id == id); else tostore = new Algorithm(); break; case "Platforms": objtype = typeof(Platform); if (id != -1) tostore = adminclient.Platforms.Find(x => x.Id == id); else tostore = new Platform(); break; case "Algorithm classes": objtype = typeof(AlgorithmClass); if (id != -1) tostore = adminclient.AlgorithmClasses.Find(x => x.Id == id); else tostore = new AlgorithmClass(); break; case "Problem classes": objtype = typeof(ProblemClass); if (id != -1) tostore = adminclient.ProblemClasses.Find(x => x.Id == id); else tostore = new ProblemClass(); break; case "Problems": objtype = typeof(Problem); if (id != -1) tostore = adminclient.Problems.Find(x => x.Id == id); else tostore = new Problem(); break; } tostore.Name = obj.GetValue("Name").ToString(); tostore.Description = obj.GetValue("Description").ToString(); if (objtype == typeof(Algorithm)) { ((Algorithm)tostore).PlatformId = ((int)obj.GetValue("PlatformId")); ((Algorithm)tostore).AlgorithmClassId = ((int)obj.GetValue("AlgorithmClassId")); } else if (objtype == typeof(Problem)) { ((Problem)tostore).PlatformId = ((int)obj.GetValue("PlatformId")); ((Problem)tostore).ProblemClassId = ((int)obj.GetValue("ProblemClassId")); } adminclient.Store(tostore); Clients.Caller.saveComplete("Succesfully saved"); refresh(name); } public void Delete(string id, string name) { loader(); IOKBItem todel = null; int todelid = int.Parse(id); switch (name) { case "Algorithms": todel = adminclient.Algorithms.Find(x => x.Id == todelid); break; case "Platforms": todel = adminclient.Platforms.Find(x => x.Id == todelid); break; case "Algorithm classes": todel = adminclient.AlgorithmClasses.Find(x => x.Id == todelid); break; case "Problem classes": todel = adminclient.ProblemClasses.Find(x => x.Id == todelid); break; case "Problems": todel = adminclient.Problems.Find(x => x.Id == todelid); break; } adminclient.Delete(todel); Clients.Caller.deleteComplete("Succesfully deleted"); refresh(name); } private void refresh(string name) { adminclient.Refresh(); switch (name) { case "Algorithms": Clients.Caller.refreshData("algos", JsonConvert.SerializeObject(adminclient.Algorithms)); break; case "Platforms": Clients.Caller.refreshData("platforms", JsonConvert.SerializeObject(adminclient.Platforms)); break; case "Algorithm classes": Clients.Caller.refreshData("algoclass", JsonConvert.SerializeObject(adminclient.AlgorithmClasses)); break; case "Problem classes": Clients.Caller.refreshData("probclass", JsonConvert.SerializeObject(adminclient.ProblemClasses)); break; case "Problems": Clients.Caller.refreshData("problems", JsonConvert.SerializeObject(adminclient.Problems)); break; } } } }