Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1502 was 1468, checked in by svonolfe, 16 years ago

Added transaction management (#527)

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