Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2930 was 2904, checked in by kgrading, 14 years ago

added functionality (#830)

File size: 9.8 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;
31using HeuristicLab.Hive.Server.LINQDataAccess;
32using ClientGroup=HeuristicLab.Hive.Contracts.BusinessObjects.ClientGroup;
33using Resource=HeuristicLab.Hive.Contracts.BusinessObjects.Resource;
34
35namespace HeuristicLab.Hive.Server.Core {
36  class ClientManager: IClientManager {
37
38    ClientDao clientDao = new ClientDao();
39
40    ISessionFactory factory;
41    List<ClientGroup> clientGroups;
42
43    public ClientManager() {
44      factory = ServiceLocator.GetSessionFactory();
45     
46      clientGroups = new List<ClientGroup>();
47    }
48
49    #region IClientManager Members
50
51    /// <summary>
52    /// Returns all clients stored in the database
53    /// </summary>
54    /// <returns></returns>
55    public ResponseList<ClientInfo> GetAllClients() {
56      ISession session = factory.GetSessionForCurrentThread();
57
58      try {
59        IClientAdapter clientAdapter =
60          session.GetDataAdapter<ClientInfo, IClientAdapter>();
61
62        ResponseList<ClientInfo> response = new ResponseList<ClientInfo>();
63
64        response.List = new List<ClientInfo>(clientAdapter.GetAll());
65        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_GET_ALL_CLIENTS;
66        response.Success = true;
67
68        return response;
69      }
70      finally {
71        if (session != null)
72          session.EndSession();
73      }
74    }
75
76    /// <summary>
77    /// returns all client groups stored in the database
78    /// </summary>
79    /// <returns></returns>
80    public ResponseList<ClientGroup> GetAllClientGroups() {
81      ISession session = factory.GetSessionForCurrentThread();
82
83      try {
84        IClientGroupAdapter clientGroupAdapter =
85          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
86        IClientAdapter clientAdapter =
87          session.GetDataAdapter<ClientInfo, IClientAdapter>();
88        ResponseList<ClientGroup> response = new ResponseList<ClientGroup>();
89
90        List<ClientGroup> allClientGroups = new List<ClientGroup>(clientGroupAdapter.GetAll());
91        ClientGroup emptyClientGroup = new ClientGroup();
92        ICollection<ClientInfo> groupLessClients = clientAdapter.GetGrouplessClients();
93        if (groupLessClients != null) {
94          foreach (ClientInfo currClient in groupLessClients) {
95            emptyClientGroup.Resources.Add(currClient);
96          }
97        }
98        emptyClientGroup.Id = Guid.Empty;
99        allClientGroups.Add(emptyClientGroup);
100
101        response.List = allClientGroups;
102        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_GET_ALL_CLIENTGROUPS;
103        response.Success = true;
104
105        return response;
106      }
107      finally {
108        if (session != null)
109          session.EndSession();
110      }
111    }
112
113    public ResponseList<UpTimeStatistics> GetAllUpTimeStatistics() {
114      ResponseList<UpTimeStatistics> response = new ResponseList<UpTimeStatistics>();
115      response.Success = true;
116      return response;
117    }
118
119    /// <summary>
120    /// Add a client group into the database
121    /// </summary>
122    /// <param name="clientGroup"></param>
123    /// <returns></returns>
124    public ResponseObject<ClientGroup> AddClientGroup(ClientGroup clientGroup) {
125      ISession session = factory.GetSessionForCurrentThread();
126
127      try {
128        IClientGroupAdapter clientGroupAdapter =
129          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
130
131        ResponseObject<ClientGroup> response = new ResponseObject<ClientGroup>();
132
133        if (clientGroup.Id != Guid.Empty) {
134          response.Success = false;
135          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_ID_MUST_NOT_BE_SET;
136        } else {
137          clientGroupAdapter.Update(clientGroup);
138          response.Obj = clientGroup;
139          response.Success = true;
140          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_ADDED;
141        }
142
143        return response;
144      }
145      finally {
146        if (session != null)
147          session.EndSession();
148      }
149    }
150
151    /// <summary>
152    ///  Add a resource to a group
153    /// </summary>
154    /// <param name="clientGroupId"></param>
155    /// <param name="resource"></param>
156    /// <returns></returns>
157    public Response AddResourceToGroup(Guid clientGroupId, Resource resource) {
158      ISession session = factory.GetSessionForCurrentThread();
159
160      try {
161        IClientGroupAdapter clientGroupAdapter =
162          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
163
164        Response response = new Response();
165
166        ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId);
167        if (clientGroup == null) {
168          response.Success = false;
169          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST;
170          return response;
171        }
172        clientGroup.Resources.Add(resource);
173        clientGroupAdapter.Update(clientGroup);
174
175        response.Success = true;
176        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_ADDED_TO_GROUP;
177
178        return response;
179      }
180      finally {
181        if (session != null)
182          session.EndSession();
183      }
184    }
185
186    /// <summary>
187    /// Remove a resource from a group
188    /// </summary>
189    /// <param name="clientGroupId"></param>
190    /// <param name="resourceId"></param>
191    /// <returns></returns>
192    public Response DeleteResourceFromGroup(Guid clientGroupId, Guid resourceId) {
193      ISession session = factory.GetSessionForCurrentThread();
194
195      try {
196        IClientGroupAdapter clientGroupAdapter =
197          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
198
199        Response response = new Response();
200
201        ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId);
202        if (clientGroup == null) {
203          response.Success = false;
204          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST;
205          return response;
206        }
207        foreach (Resource resource in clientGroup.Resources) {
208          if (resource.Id == resourceId) {
209            clientGroup.Resources.Remove(resource);
210            clientGroupAdapter.Update(clientGroup);
211            response.Success = true;
212            response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_REMOVED;
213            return response;
214          }
215        }
216        response.Success = false;
217        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_NOT_FOUND;
218
219        return response;
220      }
221      finally {
222        if (session != null)
223          session.EndSession();
224      }
225    }
226
227    public ResponseObject<List<ClientGroup>> GetAllGroupsOfResource(Guid resourceId) {
228      ISession session = factory.GetSessionForCurrentThread();
229
230      try {
231        IClientGroupAdapter clientGroupAdapter =
232          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
233        IClientAdapter clientAdapter =
234          session.GetDataAdapter<ClientInfo, IClientAdapter>();
235
236        ResponseObject<List<ClientGroup>> response = new ResponseObject<List<ClientGroup>>();
237
238        ClientInfo client = clientAdapter.GetById(resourceId);
239        if (client != null) {
240          List<ClientGroup> groupsOfClient = new List<ClientGroup>(clientGroupAdapter.MemberOf(client));
241          response.Obj = groupsOfClient;
242          response.Success = true;
243          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_GET_GROUPS_OF_CLIENT;
244        } else {
245          response.Obj = new List<ClientGroup>();
246          response.Success = false;
247          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_NOT_FOUND;
248        }
249
250        return response;
251      }
252      finally {
253        if (session != null)
254          session.EndSession();
255      }
256    }
257
258    public Response DeleteClientGroup(Guid clientGroupId) {
259      ISession session = factory.GetSessionForCurrentThread();
260
261      try {
262        IClientGroupAdapter clientGroupAdapter =
263          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
264
265        Response response = new Response();
266
267        ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId);
268        if (clientGroup == null) {
269          response.Success = false;
270          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST;
271          return response;
272        }
273
274        clientGroupAdapter.Delete(clientGroup);
275
276        response.Success = true;
277        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DELETED;
278        return response;
279
280      } finally {
281        if (session != null)
282          session.EndSession();
283      }
284    }
285
286    #endregion
287  }
288}
Note: See TracBrowser for help on using the repository browser.