Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.Core/3.2/ClientManager.cs @ 2036

Last change on this file since 2036 was 2003, checked in by msteinbi, 15 years ago

update when removing resource from group was missing (#466)

File size: 9.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Hive.Contracts.Interfaces;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using HeuristicLab.Hive.Contracts;
29using HeuristicLab.Hive.Server.DataAccess;
30using HeuristicLab.DataAccess.Interfaces;
31
32namespace HeuristicLab.Hive.Server.Core {
33  class ClientManager: IClientManager {
34
35    ISessionFactory factory;
36    List<ClientGroup> clientGroups;
37
38    public ClientManager() {
39      factory = ServiceLocator.GetSessionFactory();
40     
41      clientGroups = new List<ClientGroup>();
42    }
43
44    #region IClientManager Members
45
46    /// <summary>
47    /// Returns all clients stored in the database
48    /// </summary>
49    /// <returns></returns>
50    public ResponseList<ClientInfo> GetAllClients() {
51      ISession session = factory.GetSessionForCurrentThread();
52
53      try {
54        IClientAdapter clientAdapter =
55          session.GetDataAdapter<ClientInfo, IClientAdapter>();
56
57        ResponseList<ClientInfo> response = new ResponseList<ClientInfo>();
58
59        response.List = new List<ClientInfo>(clientAdapter.GetAll());
60        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_GET_ALL_CLIENTS;
61        response.Success = true;
62
63        return response;
64      }
65      finally {
66        if (session != null)
67          session.EndSession();
68      }
69    }
70
71    /// <summary>
72    /// returns all client groups stored in the database
73    /// </summary>
74    /// <returns></returns>
75    public ResponseList<ClientGroup> GetAllClientGroups() {
76      ISession session = factory.GetSessionForCurrentThread();
77
78      try {
79        IClientGroupAdapter clientGroupAdapter =
80          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
81        IClientAdapter clientAdapter =
82          session.GetDataAdapter<ClientInfo, IClientAdapter>();
83        ResponseList<ClientGroup> response = new ResponseList<ClientGroup>();
84
85        List<ClientGroup> allClientGroups = new List<ClientGroup>(clientGroupAdapter.GetAll());
86        ClientGroup emptyClientGroup = new ClientGroup();
87        ICollection<ClientInfo> groupLessClients = clientAdapter.GetGrouplessClients();
88        if (groupLessClients != null) {
89          foreach (ClientInfo currClient in groupLessClients) {
90            emptyClientGroup.Resources.Add(currClient);
91          }
92        }
93        emptyClientGroup.Id = Guid.Empty;
94        allClientGroups.Add(emptyClientGroup);
95
96        response.List = allClientGroups;
97        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_GET_ALL_CLIENTGROUPS;
98        response.Success = true;
99
100        return response;
101      }
102      finally {
103        if (session != null)
104          session.EndSession();
105      }
106    }
107
108    public ResponseList<UpTimeStatistics> GetAllUpTimeStatistics() {
109      ResponseList<UpTimeStatistics> response = new ResponseList<UpTimeStatistics>();
110      response.Success = true;
111      return response;
112    }
113
114    /// <summary>
115    /// Add a client group into the database
116    /// </summary>
117    /// <param name="clientGroup"></param>
118    /// <returns></returns>
119    public ResponseObject<ClientGroup> AddClientGroup(ClientGroup clientGroup) {
120      ISession session = factory.GetSessionForCurrentThread();
121
122      try {
123        IClientGroupAdapter clientGroupAdapter =
124          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
125
126        ResponseObject<ClientGroup> response = new ResponseObject<ClientGroup>();
127
128        if (clientGroup.Id != Guid.Empty) {
129          response.Success = false;
130          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_ID_MUST_NOT_BE_SET;
131        } else {
132          clientGroupAdapter.Update(clientGroup);
133          response.Obj = clientGroup;
134          response.Success = true;
135          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_ADDED;
136        }
137
138        return response;
139      }
140      finally {
141        if (session != null)
142          session.EndSession();
143      }
144    }
145
146    /// <summary>
147    ///  Add a resource to a group
148    /// </summary>
149    /// <param name="clientGroupId"></param>
150    /// <param name="resource"></param>
151    /// <returns></returns>
152    public Response AddResourceToGroup(Guid clientGroupId, Resource resource) {
153      ISession session = factory.GetSessionForCurrentThread();
154
155      try {
156        IClientGroupAdapter clientGroupAdapter =
157          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
158
159        Response response = new Response();
160
161        ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId);
162        if (clientGroup == null) {
163          response.Success = false;
164          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST;
165          return response;
166        }
167        clientGroup.Resources.Add(resource);
168        clientGroupAdapter.Update(clientGroup);
169
170        response.Success = true;
171        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_ADDED_TO_GROUP;
172
173        return response;
174      }
175      finally {
176        if (session != null)
177          session.EndSession();
178      }
179    }
180
181    /// <summary>
182    /// Remove a resource from a group
183    /// </summary>
184    /// <param name="clientGroupId"></param>
185    /// <param name="resourceId"></param>
186    /// <returns></returns>
187    public Response DeleteResourceFromGroup(Guid clientGroupId, Guid resourceId) {
188      ISession session = factory.GetSessionForCurrentThread();
189
190      try {
191        IClientGroupAdapter clientGroupAdapter =
192          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
193
194        Response response = new Response();
195
196        ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId);
197        if (clientGroup == null) {
198          response.Success = false;
199          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST;
200          return response;
201        }
202        foreach (Resource resource in clientGroup.Resources) {
203          if (resource.Id == resourceId) {
204            clientGroup.Resources.Remove(resource);
205            clientGroupAdapter.Update(clientGroup);
206            response.Success = true;
207            response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_REMOVED;
208            return response;
209          }
210        }
211        response.Success = false;
212        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_NOT_FOUND;
213
214        return response;
215      }
216      finally {
217        if (session != null)
218          session.EndSession();
219      }
220    }
221
222    public ResponseObject<List<ClientGroup>> GetAllGroupsOfResource(Guid resourceId) {
223      ISession session = factory.GetSessionForCurrentThread();
224
225      try {
226        IClientGroupAdapter clientGroupAdapter =
227          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
228        IClientAdapter clientAdapter =
229          session.GetDataAdapter<ClientInfo, IClientAdapter>();
230
231        ResponseObject<List<ClientGroup>> response = new ResponseObject<List<ClientGroup>>();
232
233        ClientInfo client = clientAdapter.GetById(resourceId);
234        List<ClientGroup> groupsOfClient = new List<ClientGroup>(clientGroupAdapter.MemberOf(client));
235        response.Obj = groupsOfClient;
236        response.Success = true;
237        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_GET_GROUPS_OF_CLIENT;
238
239        return response;
240      }
241      finally {
242        if (session != null)
243          session.EndSession();
244      }
245    }
246
247    public Response DeleteClientGroup(Guid clientGroupId) {
248      ISession session = factory.GetSessionForCurrentThread();
249
250      try {
251        IClientGroupAdapter clientGroupAdapter =
252          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
253
254        Response response = new Response();
255
256        ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId);
257        if (clientGroup == null) {
258          response.Success = false;
259          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST;
260          return response;
261        }
262
263        clientGroupAdapter.Delete(clientGroup);
264
265        response.Success = true;
266        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DELETED;
267        return response;
268
269      } finally {
270        if (session != null)
271          session.EndSession();
272      }
273    }
274
275    #endregion
276  }
277}
Note: See TracBrowser for help on using the repository browser.