Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2582 Resource calendar and client group creation, control implemented

File size: 5.4 KB
Line 
1using HeuristicLab.Clients.Access;
2using HeuristicLab.Clients.Access.Administration;
3using HeuristicLab.Clients.Hive.WebJobManager.Services;
4using Microsoft.AspNet.Hosting;
5using Microsoft.AspNet.Http;
6using Microsoft.AspNet.Mvc;
7using System;
8using System.Collections.Generic;
9using System.Linq;
10using System.ServiceModel.Security;
11using System.Threading;
12using System.Threading.Tasks;
13
14namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
15{
16    public class ResourceController : Controller
17    {
18        private WebLoginService weblog;
19        private HiveServiceLocatorWeb serviceLocator;
20        private AccessAdministrationClient accessClient;
21        private HiveAdminClientWeb adminClient;
22        private Guid userId;
23
24        private IHostingEnvironment _environment;
25
26        public ResourceController(IHostingEnvironment env)
27        {
28            weblog = WebLoginService.Instance;
29            _environment = env;
30        }
31        private bool init()
32        {
33            var u = HttpContext.Session.GetString("UserId");
34            if (u == null || u == "" || Guid.Parse(u) == Guid.Empty)
35            {
36                return false;
37            }
38            else
39            {
40                userId = Guid.Parse(u);
41                serviceLocator = weblog.getServiceLocator(userId);
42                adminClient = weblog.getAdminClient(userId);
43                accessClient = weblog.getAccessAdminClient(userId);
44                return serviceLocator.CheckLogin();
45
46            }
47        }
48        public IActionResult Index()
49        {
50           
51            if (init())
52            {
53                ViewBag.SessionId = HttpContext.Session.GetString("UserId");
54                ViewBag.Title = "Resources";
55                return View("Index");
56            }
57            else
58            {
59                return RedirectToAction("Index", "Home");
60            }
61        }
62        [HttpPost]
63        public IActionResult changeParent(string inpid, string inpidpar)
64        {
65            if (init())
66            {
67                var gid = Guid.Parse(inpid);
68               
69
70                adminClient.Refresh();
71                var tochange = adminClient.Resources.ToList().Find(x => x.Id == gid);
72               
73                if (inpidpar != null && inpidpar != "") {
74                    Guid tempid = Guid.Parse(inpidpar);
75                    while (tempid != null)
76                    {
77                        if (tempid != gid)
78                        {
79                            tempid = (Guid)adminClient.Resources.ToList().Find(x => x.Id == tempid).ParentResourceId;
80                        }
81                        else
82                            break;
83                    }
84                    if(tempid == null)
85                        tochange.ParentResourceId = Guid.Parse(inpidpar);
86                }
87                else
88                    tochange.ParentResourceId = null;
89                adminClient.Store(tochange, CancellationToken.None);
90
91                return RedirectToAction("Index", "Resource");
92            }
93            else
94            {
95                return RedirectToAction("Index", "Home");
96            }
97        }
98        [HttpPost]
99        public IActionResult deleteClientGroup(string inpid)
100        {
101            if (init())
102            {
103                var gid = Guid.Parse(inpid);
104
105                adminClient.Refresh();
106                var group = adminClient.Resources.ToList().Find(x => x.Id == gid);
107                var list = adminClient.Resources.ToList().FindAll(x => x.ParentResourceId == gid);
108                foreach(var cl in list)
109                {
110                    cl.ParentResourceId = group.ParentResourceId;
111                    adminClient.Store(cl, CancellationToken.None);
112                }
113                adminClient.Delete(group);
114
115                return RedirectToAction("Index", "Resource");
116            }
117            else
118            {
119                return RedirectToAction("Index", "Home");
120            }
121        }
122
123        [HttpPost]
124        public IActionResult newClientGroup(string inpname, int inpheart, string inpparent, string[] clientstoadd)
125        {
126            if (init()) {
127                adminClient.Refresh();
128
129                SlaveGroup sg = new SlaveGroup();
130                sg.Id = Guid.Empty;
131                sg.Name = inpname;
132                sg.HbInterval = inpheart;
133                if (inpparent != "")
134                    sg.ParentResourceId = Guid.Parse(inpparent);
135                sg.OwnerUserId = serviceLocator.getHiveServiceClient().GetUserIdByUsername(serviceLocator.getHiveServiceClient().ClientCredentials.UserName.UserName);
136                adminClient.Store(sg, CancellationToken.None);
137                adminClient.Refresh();
138                var id = adminClient.Resources.ToList().Find(x => x.Name == inpname).Id;
139                foreach(var s in clientstoadd)
140                {
141                    var cid = Guid.Parse(s);
142                    var client = adminClient.Resources.ToList().Find(x => x.Id == cid);
143                    client.ParentResourceId = id;
144                    adminClient.Store(client, CancellationToken.None);
145                }
146
147                return RedirectToAction("Index", "Resource");
148            }
149            else
150            {
151                return RedirectToAction("Index", "Home");
152            }
153        }
154    }
155}
Note: See TracBrowser for help on using the repository browser.