Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Controllers/ResourceController.cs @ 13805

Last change on this file since 13805 was 13805, checked in by jlodewyc, 8 years ago

#2582 Bugfixing, email setup password and code commenting

File size: 9.5 KB
RevLine 
[13782]1using HeuristicLab.Clients.Access;
2using HeuristicLab.Clients.Access.Administration;
[13754]3using HeuristicLab.Clients.Hive.WebJobManager.Services;
[13689]4using Microsoft.AspNet.Hosting;
[13739]5using Microsoft.AspNet.Http;
[13689]6using Microsoft.AspNet.Mvc;
7using System;
8using System.Collections.Generic;
9using System.Linq;
10using System.ServiceModel.Security;
[13782]11using System.Threading;
[13689]12using System.Threading.Tasks;
13
14namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
15{
[13805]16    /// <summary>
17    /// Controller for the resources. Only handles requests that refresh the page.
18    /// </summary>
[13689]19    public class ResourceController : Controller
20    {
[13739]21        private WebLoginService weblog;
22        private HiveServiceLocatorWeb serviceLocator;
[13754]23        private AccessAdministrationClient accessClient;
[13782]24        private HiveAdminClientWeb adminClient;
[13739]25        private Guid userId;
26
[13689]27        private IHostingEnvironment _environment;
28
29        public ResourceController(IHostingEnvironment env)
30        {
[13739]31            weblog = WebLoginService.Instance;
[13740]32            _environment = env;
33        }
[13805]34        /// <summary>
35        /// Initialize and check the login. Gets all the required services for the user. 
36        /// </summary>
37        /// <returns></returns>
[13754]38        private bool init()
[13740]39        {
[13739]40            var u = HttpContext.Session.GetString("UserId");
41            if (u == null || u == "" || Guid.Parse(u) == Guid.Empty)
42            {
[13754]43                return false;
[13739]44            }
[13740]45            else
46            {
[13739]47                userId = Guid.Parse(u);
48                serviceLocator = weblog.getServiceLocator(userId);
[13782]49                adminClient = weblog.getAdminClient(userId);
[13754]50                accessClient = weblog.getAccessAdminClient(userId);
51                return serviceLocator.CheckLogin();
52
[13739]53            }
[13689]54        }
[13805]55        /// <summary>
56        /// Loads the main page for the resources
57        /// </summary>
58        /// <returns></returns>
[13689]59        public IActionResult Index()
60        {
[13754]61           
62            if (init())
[13689]63            {
[13754]64                ViewBag.SessionId = HttpContext.Session.GetString("UserId");
[13689]65                ViewBag.Title = "Resources";
66                return View("Index");
67            }
68            else
69            {
70                return RedirectToAction("Index", "Home");
71            }
72        }
[13805]73        /// <summary>
74        /// Changes the parent for a specfic resource
75        /// </summary>
76        /// <param name="inpid">Resource to edit</param>
77        /// <param name="inpidpar">New parent resource</param>
78        /// <returns></returns>
[13782]79        [HttpPost]
80        public IActionResult changeParent(string inpid, string inpidpar)
81        {
82            if (init())
83            {
84                var gid = Guid.Parse(inpid);
85               
86
87                adminClient.Refresh();
88                var tochange = adminClient.Resources.ToList().Find(x => x.Id == gid);
[13805]89                //gets current resource
[13782]90                if (inpidpar != null && inpidpar != "") {
91                    Guid tempid = Guid.Parse(inpidpar);
[13805]92                    while (tempid != null && tempid != Guid.Empty)
93                    {//Check if a loop could occur (some parent of the parent being the parent)
[13782]94                        if (tempid != gid)
[13805]95                        {//Not equal so move up a level
96                            var t = adminClient.Resources.ToList().Find(x => x.Id == tempid);
97                            if (t.ParentResourceId == null)
98                                tempid = Guid.Empty;
99                            else
100                                tempid = (Guid)t.ParentResourceId;
101
[13782]102                        }
103                        else
[13805]104                            break; //breaks loop if equal
[13782]105                    }
[13805]106                    if(tempid == null || tempid == Guid.Empty)//Broken loop => the tempid is not empty so no change
[13782]107                        tochange.ParentResourceId = Guid.Parse(inpidpar);
108                }
109                else
110                    tochange.ParentResourceId = null;
111                adminClient.Store(tochange, CancellationToken.None);
[13805]112                //Save to server
[13782]113
114                return RedirectToAction("Index", "Resource");
115            }
116            else
117            {
118                return RedirectToAction("Index", "Home");
119            }
120        }
[13805]121        /// <summary>
122        /// Adds recources to a resource group
123        /// </summary>
124        /// <param name="addres">Array containing all the resources ID's from the group</param>
125        /// <param name="groupid">Group id</param>
126        /// <returns></returns>
[13782]127        [HttpPost]
[13805]128        public IActionResult addResourcesToGroup(string[] addres, string groupid)
129        {
130            if (init())
131            {
132                Guid gid = Guid.Parse(groupid);
133                adminClient.Refresh();
134                var exist = adminClient.Resources.ToList().FindAll(x => x.ParentResourceId == gid);
135                //Current existing member of the group
136
137                foreach(var a in addres)
138                {//loop on each ID
139                    Guid tempid = Guid.Parse(a);
140                    var elemid = exist.FindIndex(x => x.Id == tempid);
141                    if (elemid != -1)
142                    {//If element already is a member of the group: scramble temporary ID (for future reference)
143                        exist[elemid].Id = Guid.Empty;
144                    }
145                    else
146                    {//If element is not yet in list => change parentresourceid from that element
147                        var obj = adminClient.Resources.ToList().Find(x => x.Id == tempid);
148                        obj.ParentResourceId = gid;
149                        adminClient.Store(obj, CancellationToken.None);
150                    }
151                }
152                foreach(var e in exist)
153                {//Loop on existing and check the ID's
154                    if(e.Id != Guid.Empty)
155                    {//ID != Empty means that element should not be a member of the list anymore
156                        e.ParentResourceId = null; //remove ParentResourceID from element
157                        adminClient.Store(e, CancellationToken.None);
158                    }
159                }
160
161                return RedirectToAction("Index", "Resource");
162
163            }
164            else
165            {
166                return RedirectToAction("Index", "Home");
167            }
168        }
169        /// <summary>
170        /// Delete a client group (relinks the parent from every member to possible grandparent)
171        /// </summary>
172        /// <param name="inpid">Group ID to delete</param>
173        /// <returns></returns>
174        [HttpPost]
[13782]175        public IActionResult deleteClientGroup(string inpid)
176        {
177            if (init())
178            {
179                var gid = Guid.Parse(inpid);
180
181                adminClient.Refresh();
182                var group = adminClient.Resources.ToList().Find(x => x.Id == gid);
[13805]183                //Group to delete
[13782]184                var list = adminClient.Resources.ToList().FindAll(x => x.ParentResourceId == gid);
[13805]185                //All members from the group
[13782]186                foreach(var cl in list)
[13805]187                {//Links parent from every child to parent from the group to delete.
[13782]188                    cl.ParentResourceId = group.ParentResourceId;
189                    adminClient.Store(cl, CancellationToken.None);
190                }
191                adminClient.Delete(group);
192
193                return RedirectToAction("Index", "Resource");
194            }
195            else
196            {
197                return RedirectToAction("Index", "Home");
198            }
199        }
[13805]200        /// <summary>
201        /// Create a new Client Group
202        /// </summary>
203        /// <param name="inpname">Name for the group</param>
204        /// <param name="inpheart">Heartbeat</param>
205        /// <param name="inpparent">Possible parent for the group</param>
206        /// <param name="clientstoadd">Clients to be added to the group</param>
207        /// <returns></returns>
[13782]208        [HttpPost]
209        public IActionResult newClientGroup(string inpname, int inpheart, string inpparent, string[] clientstoadd)
210        {
211            if (init()) {
212                adminClient.Refresh();
213
214                SlaveGroup sg = new SlaveGroup();
[13805]215                //init
[13782]216                sg.Id = Guid.Empty;
217                sg.Name = inpname;
218                sg.HbInterval = inpheart;
[13805]219                if (inpparent != null)
[13782]220                    sg.ParentResourceId = Guid.Parse(inpparent);
[13805]221
[13782]222                sg.OwnerUserId = serviceLocator.getHiveServiceClient().GetUserIdByUsername(serviceLocator.getHiveServiceClient().ClientCredentials.UserName.UserName);
223                adminClient.Store(sg, CancellationToken.None);
224                adminClient.Refresh();
[13805]225                //Reloads the resources
[13782]226                var id = adminClient.Resources.ToList().Find(x => x.Name == inpname).Id;
227                foreach(var s in clientstoadd)
[13805]228                {//Loop so each client can be added to the group
[13782]229                    var cid = Guid.Parse(s);
230                    var client = adminClient.Resources.ToList().Find(x => x.Id == cid);
231                    client.ParentResourceId = id;
232                    adminClient.Store(client, CancellationToken.None);
233                }
234
235                return RedirectToAction("Index", "Resource");
236            }
237            else
238            {
239                return RedirectToAction("Index", "Home");
240            }
241        }
[13689]242    }
243}
Note: See TracBrowser for help on using the repository browser.