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
RevLine 
[907]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;
[800]23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Hive.Contracts.Interfaces;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
[902]28using HeuristicLab.Hive.Contracts;
[1377]29using HeuristicLab.Hive.Server.DataAccess;
[1468]30using HeuristicLab.DataAccess.Interfaces;
[800]31
32namespace HeuristicLab.Hive.Server.Core {
33  class ClientManager: IClientManager {
[820]34
[1468]35    ISessionFactory factory;
[820]36    List<ClientGroup> clientGroups;
37
38    public ClientManager() {
[1468]39      factory = ServiceLocator.GetSessionFactory();
40     
[820]41      clientGroups = new List<ClientGroup>();
42    }
43
[800]44    #region IClientManager Members
45
[1121]46    /// <summary>
47    /// Returns all clients stored in the database
48    /// </summary>
49    /// <returns></returns>
[902]50    public ResponseList<ClientInfo> GetAllClients() {
[1468]51      ISession session = factory.GetSessionForCurrentThread();
[929]52
[1468]53      try {
54        IClientAdapter clientAdapter =
55          session.GetDataAdapter<ClientInfo, IClientAdapter>();
[929]56
[1468]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      }
[800]69    }
70
[1121]71    /// <summary>
72    /// returns all client groups stored in the database
73    /// </summary>
74    /// <returns></returns>
[902]75    public ResponseList<ClientGroup> GetAllClientGroups() {
[1468]76      ISession session = factory.GetSessionForCurrentThread();
[934]77
[1468]78      try {
79        IClientGroupAdapter clientGroupAdapter =
80          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
[1633]81        IClientAdapter clientAdapter =
82          session.GetDataAdapter<ClientInfo, IClientAdapter>();
[1468]83        ResponseList<ClientGroup> response = new ResponseList<ClientGroup>();
[934]84
[1633]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        }
[1824]93        emptyClientGroup.Id = Guid.Empty;
[1633]94        allClientGroups.Add(emptyClientGroup);
95
96        response.List = allClientGroups;
[1468]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      }
[800]106    }
107
[902]108    public ResponseList<UpTimeStatistics> GetAllUpTimeStatistics() {
[907]109      ResponseList<UpTimeStatistics> response = new ResponseList<UpTimeStatistics>();
110      response.Success = true;
111      return response;
[800]112    }
113
[1121]114    /// <summary>
115    /// Add a client group into the database
116    /// </summary>
117    /// <param name="clientGroup"></param>
118    /// <returns></returns>
[1825]119    public ResponseObject<ClientGroup> AddClientGroup(ClientGroup clientGroup) {
[1468]120      ISession session = factory.GetSessionForCurrentThread();
[937]121
[1468]122      try {
123        IClientGroupAdapter clientGroupAdapter =
124          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
125
[1825]126        ResponseObject<ClientGroup> response = new ResponseObject<ClientGroup>();
[1468]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);
[1825]133          response.Obj = clientGroup;
[1468]134          response.Success = true;
135          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_ADDED;
136        }
137
[942]138        return response;
139      }
[1468]140      finally {
141        if (session != null)
142          session.EndSession();
143      }
[934]144    }
145
[1121]146    /// <summary>
147    ///  Add a resource to a group
148    /// </summary>
149    /// <param name="clientGroupId"></param>
150    /// <param name="resource"></param>
151    /// <returns></returns>
[1449]152    public Response AddResourceToGroup(Guid clientGroupId, Resource resource) {
[1468]153      ISession session = factory.GetSessionForCurrentThread();
[942]154
[1468]155      try {
156        IClientGroupAdapter clientGroupAdapter =
157          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
[942]158
[1468]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);
[1826]168        clientGroupAdapter.Update(clientGroup);
[1468]169
170        response.Success = true;
171        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_ADDED_TO_GROUP;
172
[942]173        return response;
174      }
[1468]175      finally {
176        if (session != null)
177          session.EndSession();
178      }
[934]179    }
180
[1121]181    /// <summary>
182    /// Remove a resource from a group
183    /// </summary>
184    /// <param name="clientGroupId"></param>
185    /// <param name="resourceId"></param>
186    /// <returns></returns>
[1449]187    public Response DeleteResourceFromGroup(Guid clientGroupId, Guid resourceId) {
[1468]188      ISession session = factory.GetSessionForCurrentThread();
[942]189
[1468]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);
[2003]205            clientGroupAdapter.Update(clientGroup);
[1468]206            response.Success = true;
207            response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_REMOVED;
208            return response;
209          }
210        }
[942]211        response.Success = false;
[1468]212        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_NOT_FOUND;
213
[942]214        return response;
215      }
[1468]216      finally {
217        if (session != null)
218          session.EndSession();
[942]219      }
[934]220    }
[1757]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
[1824]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
[800]275    #endregion
276  }
277}
Note: See TracBrowser for help on using the repository browser.