Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1741 was 1633, checked in by msteinbi, 16 years ago

added empty group for all clients that aren't in a group (#599)

File size: 7.5 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        allClientGroups.Add(emptyClientGroup);
94
95        response.List = allClientGroups;
96        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_GET_ALL_CLIENTGROUPS;
97        response.Success = true;
98
99        return response;
100      }
101      finally {
102        if (session != null)
103          session.EndSession();
104      }
105    }
106
107    public ResponseList<UpTimeStatistics> GetAllUpTimeStatistics() {
108      ResponseList<UpTimeStatistics> response = new ResponseList<UpTimeStatistics>();
109      response.Success = true;
110      return response;
111    }
112
113    /// <summary>
114    /// Add a client group into the database
115    /// </summary>
116    /// <param name="clientGroup"></param>
117    /// <returns></returns>
118    public Response AddClientGroup(ClientGroup clientGroup) {
119      ISession session = factory.GetSessionForCurrentThread();
120
121      try {
122        IClientGroupAdapter clientGroupAdapter =
123          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
124
125        Response response = new Response();
126
127        if (clientGroup.Id != Guid.Empty) {
128          response.Success = false;
129          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_ID_MUST_NOT_BE_SET;
130        } else {
131          clientGroupAdapter.Update(clientGroup);
132          response.Success = true;
133          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_ADDED;
134        }
135
136        return response;
137      }
138      finally {
139        if (session != null)
140          session.EndSession();
141      }
142    }
143
144    /// <summary>
145    ///  Add a resource to a group
146    /// </summary>
147    /// <param name="clientGroupId"></param>
148    /// <param name="resource"></param>
149    /// <returns></returns>
150    public Response AddResourceToGroup(Guid clientGroupId, Resource resource) {
151      ISession session = factory.GetSessionForCurrentThread();
152
153      try {
154        IClientGroupAdapter clientGroupAdapter =
155          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
156
157        Response response = new Response();
158
159        if (resource.Id != Guid.Empty) {
160          response.Success = false;
161          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_ID_MUST_NOT_BE_SET;
162          return response;
163        }
164
165        ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId);
166        if (clientGroup == null) {
167          response.Success = false;
168          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST;
169          return response;
170        }
171        clientGroup.Resources.Add(resource);
172
173        response.Success = true;
174        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_ADDED_TO_GROUP;
175
176        return response;
177      }
178      finally {
179        if (session != null)
180          session.EndSession();
181      }
182    }
183
184    /// <summary>
185    /// Remove a resource from a group
186    /// </summary>
187    /// <param name="clientGroupId"></param>
188    /// <param name="resourceId"></param>
189    /// <returns></returns>
190    public Response DeleteResourceFromGroup(Guid clientGroupId, Guid resourceId) {
191      ISession session = factory.GetSessionForCurrentThread();
192
193      try {
194        IClientGroupAdapter clientGroupAdapter =
195          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
196
197        Response response = new Response();
198
199        ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId);
200        if (clientGroup == null) {
201          response.Success = false;
202          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST;
203          return response;
204        }
205        foreach (Resource resource in clientGroup.Resources) {
206          if (resource.Id == resourceId) {
207            clientGroup.Resources.Remove(resource);
208            response.Success = true;
209            response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_REMOVED;
210            return response;
211          }
212        }
213        response.Success = false;
214        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_NOT_FOUND;
215
216        return response;
217      }
218      finally {
219        if (session != null)
220          session.EndSession();
221      }
222    }
223    #endregion
224  }
225}
Note: See TracBrowser for help on using the repository browser.