#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using HeuristicLab.Clients.Hive.WebJobManager.Models;
using HeuristicLab.Clients.Hive.WebJobManager.Services;
using HeuristicLab.Clients.Hive.WebJobManager.Services.Imports;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading;
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 HiveWebUser currentUser;
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
{
try {
userId = Guid.Parse(u);
serviceLocator = weblog.getServiceLocator(userId);
adminClient = weblog.getAdminClient(userId);
accessClient = weblog.getAccessAdminClient(userId);
currentUser = weblog.getCurrentUser(userId);
if (currentUser.hasResourceAdminAccess())
return serviceLocator.CheckLogin();
else
return false;
}
catch (NullReferenceException)
{
return false;
}
}
}
///
/// Loads the main page for the resources
///
///
public IActionResult Index()
{
if (init())
{
ViewBag.SessionId = HttpContext.Session.GetString("UserId");
ViewBag.Title = "Resources";
return View("Index", currentUser);
}
else
{
return RedirectToAction("Index", "Job");
}
}
///
/// 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", "Job");
}
}
///
/// 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", "Job");
}
}
///
/// 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", "Job");
}
}
///
/// 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", "Job");
}
}
}
}