[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 |
|
---|
| 22 | using System;
|
---|
[800] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
| 27 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
[902] | 28 | using HeuristicLab.Hive.Contracts;
|
---|
[1377] | 29 | using HeuristicLab.Hive.Server.DataAccess;
|
---|
[1468] | 30 | using HeuristicLab.DataAccess.Interfaces;
|
---|
[2904] | 31 | using HeuristicLab.Hive.Server.LINQDataAccess;
|
---|
| 32 | using ClientGroup=HeuristicLab.Hive.Contracts.BusinessObjects.ClientGroup;
|
---|
| 33 | using Resource=HeuristicLab.Hive.Contracts.BusinessObjects.Resource;
|
---|
[800] | 34 |
|
---|
| 35 | namespace HeuristicLab.Hive.Server.Core {
|
---|
| 36 | class ClientManager: IClientManager {
|
---|
[820] | 37 |
|
---|
[2904] | 38 | ClientDao clientDao = new ClientDao();
|
---|
| 39 |
|
---|
[1468] | 40 | ISessionFactory factory;
|
---|
[820] | 41 | List<ClientGroup> clientGroups;
|
---|
| 42 |
|
---|
| 43 | public ClientManager() {
|
---|
[1468] | 44 | factory = ServiceLocator.GetSessionFactory();
|
---|
| 45 |
|
---|
[820] | 46 | clientGroups = new List<ClientGroup>();
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[800] | 49 | #region IClientManager Members
|
---|
| 50 |
|
---|
[1121] | 51 | /// <summary>
|
---|
| 52 | /// Returns all clients stored in the database
|
---|
| 53 | /// </summary>
|
---|
| 54 | /// <returns></returns>
|
---|
[902] | 55 | public ResponseList<ClientInfo> GetAllClients() {
|
---|
[1468] | 56 | ISession session = factory.GetSessionForCurrentThread();
|
---|
[929] | 57 |
|
---|
[1468] | 58 | try {
|
---|
| 59 | IClientAdapter clientAdapter =
|
---|
| 60 | session.GetDataAdapter<ClientInfo, IClientAdapter>();
|
---|
[929] | 61 |
|
---|
[1468] | 62 | ResponseList<ClientInfo> response = new ResponseList<ClientInfo>();
|
---|
| 63 |
|
---|
| 64 | response.List = new List<ClientInfo>(clientAdapter.GetAll());
|
---|
| 65 | response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_GET_ALL_CLIENTS;
|
---|
| 66 | response.Success = true;
|
---|
| 67 |
|
---|
| 68 | return response;
|
---|
| 69 | }
|
---|
| 70 | finally {
|
---|
| 71 | if (session != null)
|
---|
| 72 | session.EndSession();
|
---|
| 73 | }
|
---|
[800] | 74 | }
|
---|
| 75 |
|
---|
[1121] | 76 | /// <summary>
|
---|
| 77 | /// returns all client groups stored in the database
|
---|
| 78 | /// </summary>
|
---|
| 79 | /// <returns></returns>
|
---|
[902] | 80 | public ResponseList<ClientGroup> GetAllClientGroups() {
|
---|
[1468] | 81 | ISession session = factory.GetSessionForCurrentThread();
|
---|
[934] | 82 |
|
---|
[1468] | 83 | try {
|
---|
| 84 | IClientGroupAdapter clientGroupAdapter =
|
---|
| 85 | session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
|
---|
[1633] | 86 | IClientAdapter clientAdapter =
|
---|
| 87 | session.GetDataAdapter<ClientInfo, IClientAdapter>();
|
---|
[1468] | 88 | ResponseList<ClientGroup> response = new ResponseList<ClientGroup>();
|
---|
[934] | 89 |
|
---|
[1633] | 90 | List<ClientGroup> allClientGroups = new List<ClientGroup>(clientGroupAdapter.GetAll());
|
---|
| 91 | ClientGroup emptyClientGroup = new ClientGroup();
|
---|
| 92 | ICollection<ClientInfo> groupLessClients = clientAdapter.GetGrouplessClients();
|
---|
| 93 | if (groupLessClients != null) {
|
---|
| 94 | foreach (ClientInfo currClient in groupLessClients) {
|
---|
| 95 | emptyClientGroup.Resources.Add(currClient);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
[1824] | 98 | emptyClientGroup.Id = Guid.Empty;
|
---|
[1633] | 99 | allClientGroups.Add(emptyClientGroup);
|
---|
| 100 |
|
---|
| 101 | response.List = allClientGroups;
|
---|
[1468] | 102 | response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_GET_ALL_CLIENTGROUPS;
|
---|
| 103 | response.Success = true;
|
---|
| 104 |
|
---|
| 105 | return response;
|
---|
| 106 | }
|
---|
| 107 | finally {
|
---|
| 108 | if (session != null)
|
---|
| 109 | session.EndSession();
|
---|
| 110 | }
|
---|
[800] | 111 | }
|
---|
| 112 |
|
---|
[902] | 113 | public ResponseList<UpTimeStatistics> GetAllUpTimeStatistics() {
|
---|
[907] | 114 | ResponseList<UpTimeStatistics> response = new ResponseList<UpTimeStatistics>();
|
---|
| 115 | response.Success = true;
|
---|
| 116 | return response;
|
---|
[800] | 117 | }
|
---|
| 118 |
|
---|
[1121] | 119 | /// <summary>
|
---|
| 120 | /// Add a client group into the database
|
---|
| 121 | /// </summary>
|
---|
| 122 | /// <param name="clientGroup"></param>
|
---|
| 123 | /// <returns></returns>
|
---|
[1825] | 124 | public ResponseObject<ClientGroup> AddClientGroup(ClientGroup clientGroup) {
|
---|
[1468] | 125 | ISession session = factory.GetSessionForCurrentThread();
|
---|
[937] | 126 |
|
---|
[1468] | 127 | try {
|
---|
| 128 | IClientGroupAdapter clientGroupAdapter =
|
---|
| 129 | session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
|
---|
| 130 |
|
---|
[1825] | 131 | ResponseObject<ClientGroup> response = new ResponseObject<ClientGroup>();
|
---|
[1468] | 132 |
|
---|
| 133 | if (clientGroup.Id != Guid.Empty) {
|
---|
| 134 | response.Success = false;
|
---|
| 135 | response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_ID_MUST_NOT_BE_SET;
|
---|
| 136 | } else {
|
---|
| 137 | clientGroupAdapter.Update(clientGroup);
|
---|
[1825] | 138 | response.Obj = clientGroup;
|
---|
[1468] | 139 | response.Success = true;
|
---|
| 140 | response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_ADDED;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[942] | 143 | return response;
|
---|
| 144 | }
|
---|
[1468] | 145 | finally {
|
---|
| 146 | if (session != null)
|
---|
| 147 | session.EndSession();
|
---|
| 148 | }
|
---|
[934] | 149 | }
|
---|
| 150 |
|
---|
[1121] | 151 | /// <summary>
|
---|
| 152 | /// Add a resource to a group
|
---|
| 153 | /// </summary>
|
---|
| 154 | /// <param name="clientGroupId"></param>
|
---|
| 155 | /// <param name="resource"></param>
|
---|
| 156 | /// <returns></returns>
|
---|
[1449] | 157 | public Response AddResourceToGroup(Guid clientGroupId, Resource resource) {
|
---|
[1468] | 158 | ISession session = factory.GetSessionForCurrentThread();
|
---|
[942] | 159 |
|
---|
[1468] | 160 | try {
|
---|
| 161 | IClientGroupAdapter clientGroupAdapter =
|
---|
| 162 | session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
|
---|
[942] | 163 |
|
---|
[1468] | 164 | Response response = new Response();
|
---|
| 165 |
|
---|
| 166 | ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId);
|
---|
| 167 | if (clientGroup == null) {
|
---|
| 168 | response.Success = false;
|
---|
| 169 | response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST;
|
---|
| 170 | return response;
|
---|
| 171 | }
|
---|
| 172 | clientGroup.Resources.Add(resource);
|
---|
[1826] | 173 | clientGroupAdapter.Update(clientGroup);
|
---|
[1468] | 174 |
|
---|
| 175 | response.Success = true;
|
---|
| 176 | response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_ADDED_TO_GROUP;
|
---|
| 177 |
|
---|
[942] | 178 | return response;
|
---|
| 179 | }
|
---|
[1468] | 180 | finally {
|
---|
| 181 | if (session != null)
|
---|
| 182 | session.EndSession();
|
---|
| 183 | }
|
---|
[934] | 184 | }
|
---|
| 185 |
|
---|
[1121] | 186 | /// <summary>
|
---|
| 187 | /// Remove a resource from a group
|
---|
| 188 | /// </summary>
|
---|
| 189 | /// <param name="clientGroupId"></param>
|
---|
| 190 | /// <param name="resourceId"></param>
|
---|
| 191 | /// <returns></returns>
|
---|
[1449] | 192 | public Response DeleteResourceFromGroup(Guid clientGroupId, Guid resourceId) {
|
---|
[1468] | 193 | ISession session = factory.GetSessionForCurrentThread();
|
---|
[942] | 194 |
|
---|
[1468] | 195 | try {
|
---|
| 196 | IClientGroupAdapter clientGroupAdapter =
|
---|
| 197 | session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
|
---|
| 198 |
|
---|
| 199 | Response response = new Response();
|
---|
| 200 |
|
---|
| 201 | ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId);
|
---|
| 202 | if (clientGroup == null) {
|
---|
| 203 | response.Success = false;
|
---|
| 204 | response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST;
|
---|
| 205 | return response;
|
---|
| 206 | }
|
---|
| 207 | foreach (Resource resource in clientGroup.Resources) {
|
---|
| 208 | if (resource.Id == resourceId) {
|
---|
| 209 | clientGroup.Resources.Remove(resource);
|
---|
[2003] | 210 | clientGroupAdapter.Update(clientGroup);
|
---|
[1468] | 211 | response.Success = true;
|
---|
| 212 | response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_REMOVED;
|
---|
| 213 | return response;
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
[942] | 216 | response.Success = false;
|
---|
[1468] | 217 | response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_NOT_FOUND;
|
---|
| 218 |
|
---|
[942] | 219 | return response;
|
---|
| 220 | }
|
---|
[1468] | 221 | finally {
|
---|
| 222 | if (session != null)
|
---|
| 223 | session.EndSession();
|
---|
[942] | 224 | }
|
---|
[934] | 225 | }
|
---|
[1757] | 226 |
|
---|
| 227 | public ResponseObject<List<ClientGroup>> GetAllGroupsOfResource(Guid resourceId) {
|
---|
| 228 | ISession session = factory.GetSessionForCurrentThread();
|
---|
| 229 |
|
---|
| 230 | try {
|
---|
| 231 | IClientGroupAdapter clientGroupAdapter =
|
---|
| 232 | session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
|
---|
| 233 | IClientAdapter clientAdapter =
|
---|
| 234 | session.GetDataAdapter<ClientInfo, IClientAdapter>();
|
---|
| 235 |
|
---|
| 236 | ResponseObject<List<ClientGroup>> response = new ResponseObject<List<ClientGroup>>();
|
---|
| 237 |
|
---|
| 238 | ClientInfo client = clientAdapter.GetById(resourceId);
|
---|
[2086] | 239 | if (client != null) {
|
---|
| 240 | List<ClientGroup> groupsOfClient = new List<ClientGroup>(clientGroupAdapter.MemberOf(client));
|
---|
| 241 | response.Obj = groupsOfClient;
|
---|
| 242 | response.Success = true;
|
---|
| 243 | response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_GET_GROUPS_OF_CLIENT;
|
---|
| 244 | } else {
|
---|
| 245 | response.Obj = new List<ClientGroup>();
|
---|
| 246 | response.Success = false;
|
---|
| 247 | response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_NOT_FOUND;
|
---|
| 248 | }
|
---|
[1757] | 249 |
|
---|
| 250 | return response;
|
---|
| 251 | }
|
---|
| 252 | finally {
|
---|
| 253 | if (session != null)
|
---|
| 254 | session.EndSession();
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[1824] | 258 | public Response DeleteClientGroup(Guid clientGroupId) {
|
---|
| 259 | ISession session = factory.GetSessionForCurrentThread();
|
---|
| 260 |
|
---|
| 261 | try {
|
---|
| 262 | IClientGroupAdapter clientGroupAdapter =
|
---|
| 263 | session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
|
---|
| 264 |
|
---|
| 265 | Response response = new Response();
|
---|
| 266 |
|
---|
| 267 | ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId);
|
---|
| 268 | if (clientGroup == null) {
|
---|
| 269 | response.Success = false;
|
---|
| 270 | response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST;
|
---|
| 271 | return response;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | clientGroupAdapter.Delete(clientGroup);
|
---|
| 275 |
|
---|
| 276 | response.Success = true;
|
---|
| 277 | response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DELETED;
|
---|
| 278 | return response;
|
---|
| 279 |
|
---|
| 280 | } finally {
|
---|
| 281 | if (session != null)
|
---|
| 282 | session.EndSession();
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[800] | 286 | #endregion
|
---|
| 287 | }
|
---|
| 288 | }
|
---|