Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1463 was 1449, checked in by svonolfe, 16 years ago

Refactored DAL (now using GUIDs as IDs instead of longs) (#527)

File size: 5.6 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;
30
31namespace HeuristicLab.Hive.Server.Core {
32  class ClientManager: IClientManager {
33
34    IClientAdapter clientAdapter;
35    IClientGroupAdapter clientGroupAdapter;
36
37    List<ClientGroup> clientGroups;
38
39    public ClientManager() {
40      clientAdapter = ServiceLocator.GetClientAdapter();
41      clientGroupAdapter = ServiceLocator.GetClientGroupAdapter();
42
43      clientGroups = new List<ClientGroup>();
44
45      ClientGroup cg = new ClientGroup { Id = Guid.NewGuid(), Name = "SuperGroup" };
46      cg.Resources = new List<Resource>();
47
48      clientGroups.Add(cg);
49    }
50
51    #region IClientManager Members
52
53    /// <summary>
54    /// Returns all clients stored in the database
55    /// </summary>
56    /// <returns></returns>
57    public ResponseList<ClientInfo> GetAllClients() {
58      ResponseList<ClientInfo> response = new ResponseList<ClientInfo>();
59
60      response.List = new List<ClientInfo>(clientAdapter.GetAll());
61      response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_GET_ALL_CLIENTS;
62      response.Success = true;
63
64      return response;
65    }
66
67    /// <summary>
68    /// returns all client groups stored in the database
69    /// </summary>
70    /// <returns></returns>
71    public ResponseList<ClientGroup> GetAllClientGroups() {
72      ResponseList<ClientGroup> response = new ResponseList<ClientGroup>();
73
74      response.List = new List<ClientGroup>(clientGroupAdapter.GetAll());
75      response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_GET_ALL_CLIENTGROUPS;
76      response.Success = true;
77
78      return response;
79    }
80
81    public ResponseList<UpTimeStatistics> GetAllUpTimeStatistics() {
82      ResponseList<UpTimeStatistics> response = new ResponseList<UpTimeStatistics>();
83      response.Success = true;
84      return response;
85    }
86
87    /// <summary>
88    /// Add a client group into the database
89    /// </summary>
90    /// <param name="clientGroup"></param>
91    /// <returns></returns>
92    public Response AddClientGroup(ClientGroup clientGroup) {
93      Response response = new Response();
94
95      if (clientGroup.Id != Guid.Empty) {
96        response.Success = false;
97        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_ID_MUST_NOT_BE_SET;
98        return response;
99      }
100      clientGroupAdapter.Update(clientGroup);
101      response.Success = true;
102      response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_ADDED;
103
104      return response;
105    }
106
107    /// <summary>
108    ///  Add a resource to a group
109    /// </summary>
110    /// <param name="clientGroupId"></param>
111    /// <param name="resource"></param>
112    /// <returns></returns>
113    public Response AddResourceToGroup(Guid clientGroupId, Resource resource) {
114      Response response = new Response();
115
116      if (resource.Id != Guid.Empty) {
117        response.Success = false;
118        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_ID_MUST_NOT_BE_SET;
119        return response;
120      }
121
122      ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId);
123      if (clientGroup == null) {
124        response.Success = false;
125        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST;
126        return response;
127      }
128      clientGroup.Resources.Add(resource);
129
130      response.Success = true;
131      response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_ADDED_TO_GROUP;
132
133      return response;
134    }
135
136    /// <summary>
137    /// Remove a resource from a group
138    /// </summary>
139    /// <param name="clientGroupId"></param>
140    /// <param name="resourceId"></param>
141    /// <returns></returns>
142    public Response DeleteResourceFromGroup(Guid clientGroupId, Guid resourceId) {
143      Response response = new Response();
144
145      ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId);
146      if (clientGroup == null) {
147        response.Success = false;
148        response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST;
149        return response;
150      }
151      foreach (Resource resource in clientGroup.Resources) {
152        if (resource.Id == resourceId) {
153          clientGroup.Resources.Remove(resource);
154          response.Success = true;
155          response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_REMOVED;
156          return response;
157        }
158      }
159      response.Success = false;
160      response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_NOT_FOUND;
161
162      return response;
163    }
164    #endregion
165  }
166}
Note: See TracBrowser for help on using the repository browser.