Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Hive.Server.Core/3.2/ClientManager.cs @ 3962

Last change on this file since 3962 was 1826, checked in by msteinbi, 16 years ago

fixed bug with resource check (#599)

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            response.Success = true;
206            response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_REMOVED;
207            return response;
208          }
209        }
210        response.Success = false;
211        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_NOT_FOUND;
212
213        return response;
214      }
215      finally {
216        if (session != null)
217          session.EndSession();
218      }
219    }
220
221    public ResponseObject<List<ClientGroup>> GetAllGroupsOfResource(Guid resourceId) {
222      ISession session = factory.GetSessionForCurrentThread();
223
224      try {
225        IClientGroupAdapter clientGroupAdapter =
226          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
227        IClientAdapter clientAdapter =
228          session.GetDataAdapter<ClientInfo, IClientAdapter>();
229
230        ResponseObject<List<ClientGroup>> response = new ResponseObject<List<ClientGroup>>();
231
232        ClientInfo client = clientAdapter.GetById(resourceId);
233        List<ClientGroup> groupsOfClient = new List<ClientGroup>(clientGroupAdapter.MemberOf(client));
234        response.Obj = groupsOfClient;
235        response.Success = true;
236        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_GET_GROUPS_OF_CLIENT;
237
238        return response;
239      }
240      finally {
241        if (session != null)
242          session.EndSession();
243      }
244    }
245
246    public Response DeleteClientGroup(Guid clientGroupId) {
247      ISession session = factory.GetSessionForCurrentThread();
248
249      try {
250        IClientGroupAdapter clientGroupAdapter =
251          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
252
253        Response response = new Response();
254
255        ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId);
256        if (clientGroup == null) {
257          response.Success = false;
258          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST;
259          return response;
260        }
261
262        clientGroupAdapter.Delete(clientGroup);
263
264        response.Success = true;
265        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DELETED;
266        return response;
267
268      } finally {
269        if (session != null)
270          session.EndSession();
271      }
272    }
273
274    #endregion
275  }
276}
Note: See TracBrowser for help on using the repository browser.