using HeuristicLab.Clients.Access; using HeuristicLab.Clients.Access.Administration; using HeuristicLab.Clients.Hive.WebJobManager.Services; using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel.Security; using System.Threading; using System.Threading.Tasks; namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers { /// /// Controller for the resources. Only handles requests that refresh the page. /// public class ResourceController : Controller { private WebLoginService weblog; private HiveServiceLocatorWeb serviceLocator; private AccessAdministrationClient accessClient; private HiveAdminClientWeb adminClient; private Guid userId; private IHostingEnvironment _environment; public ResourceController(IHostingEnvironment env) { weblog = WebLoginService.Instance; _environment = env; } /// /// Initialize and check the login. Gets all the required services for the user. /// /// private bool init() { var u = HttpContext.Session.GetString("UserId"); if (u == null || u == "" || Guid.Parse(u) == Guid.Empty) { return false; } else { userId = Guid.Parse(u); serviceLocator = weblog.getServiceLocator(userId); adminClient = weblog.getAdminClient(userId); accessClient = weblog.getAccessAdminClient(userId); return serviceLocator.CheckLogin(); } } /// /// Loads the main page for the resources /// /// public IActionResult Index() { if (init()) { ViewBag.SessionId = HttpContext.Session.GetString("UserId"); ViewBag.Title = "Resources"; return View("Index"); } else { return RedirectToAction("Index", "Home"); } } /// /// Changes the parent for a specfic resource /// /// Resource to edit /// New parent resource /// [HttpPost] public IActionResult changeParent(string inpid, string inpidpar) { if (init()) { var gid = Guid.Parse(inpid); adminClient.Refresh(); var tochange = adminClient.Resources.ToList().Find(x => x.Id == gid); //gets current resource if (inpidpar != null && inpidpar != "") { Guid tempid = Guid.Parse(inpidpar); while (tempid != null && tempid != Guid.Empty) {//Check if a loop could occur (some parent of the parent being the parent) if (tempid != gid) {//Not equal so move up a level var t = adminClient.Resources.ToList().Find(x => x.Id == tempid); if (t.ParentResourceId == null) tempid = Guid.Empty; else tempid = (Guid)t.ParentResourceId; } else break; //breaks loop if equal } if(tempid == null || tempid == Guid.Empty)//Broken loop => the tempid is not empty so no change tochange.ParentResourceId = Guid.Parse(inpidpar); } else tochange.ParentResourceId = null; adminClient.Store(tochange, CancellationToken.None); //Save to server return RedirectToAction("Index", "Resource"); } else { return RedirectToAction("Index", "Home"); } } /// /// Adds recources to a resource group /// /// Array containing all the resources ID's from the group /// Group id /// [HttpPost] public IActionResult addResourcesToGroup(string[] addres, string groupid) { if (init()) { Guid gid = Guid.Parse(groupid); adminClient.Refresh(); var exist = adminClient.Resources.ToList().FindAll(x => x.ParentResourceId == gid); //Current existing member of the group foreach(var a in addres) {//loop on each ID Guid tempid = Guid.Parse(a); var elemid = exist.FindIndex(x => x.Id == tempid); if (elemid != -1) {//If element already is a member of the group: scramble temporary ID (for future reference) exist[elemid].Id = Guid.Empty; } else {//If element is not yet in list => change parentresourceid from that element var obj = adminClient.Resources.ToList().Find(x => x.Id == tempid); obj.ParentResourceId = gid; adminClient.Store(obj, CancellationToken.None); } } foreach(var e in exist) {//Loop on existing and check the ID's if(e.Id != Guid.Empty) {//ID != Empty means that element should not be a member of the list anymore e.ParentResourceId = null; //remove ParentResourceID from element adminClient.Store(e, CancellationToken.None); } } return RedirectToAction("Index", "Resource"); } else { return RedirectToAction("Index", "Home"); } } /// /// Delete a client group (relinks the parent from every member to possible grandparent) /// /// Group ID to delete /// [HttpPost] public IActionResult deleteClientGroup(string inpid) { if (init()) { var gid = Guid.Parse(inpid); adminClient.Refresh(); var group = adminClient.Resources.ToList().Find(x => x.Id == gid); //Group to delete var list = adminClient.Resources.ToList().FindAll(x => x.ParentResourceId == gid); //All members from the group foreach(var cl in list) {//Links parent from every child to parent from the group to delete. cl.ParentResourceId = group.ParentResourceId; adminClient.Store(cl, CancellationToken.None); } adminClient.Delete(group); return RedirectToAction("Index", "Resource"); } else { return RedirectToAction("Index", "Home"); } } /// /// Create a new Client Group /// /// Name for the group /// Heartbeat /// Possible parent for the group /// Clients to be added to the group /// [HttpPost] public IActionResult newClientGroup(string inpname, int inpheart, string inpparent, string[] clientstoadd) { if (init()) { adminClient.Refresh(); SlaveGroup sg = new SlaveGroup(); //init sg.Id = Guid.Empty; sg.Name = inpname; sg.HbInterval = inpheart; if (inpparent != null) sg.ParentResourceId = Guid.Parse(inpparent); sg.OwnerUserId = serviceLocator.getHiveServiceClient().GetUserIdByUsername(serviceLocator.getHiveServiceClient().ClientCredentials.UserName.UserName); adminClient.Store(sg, CancellationToken.None); adminClient.Refresh(); //Reloads the resources var id = adminClient.Resources.ToList().Find(x => x.Name == inpname).Id; foreach(var s in clientstoadd) {//Loop so each client can be added to the group var cid = Guid.Parse(s); var client = adminClient.Resources.ToList().Find(x => x.Id == cid); client.ParentResourceId = id; adminClient.Store(client, CancellationToken.None); } return RedirectToAction("Index", "Resource"); } else { return RedirectToAction("Index", "Home"); } } } }