#region License Information /* HeuristicLab * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Hive.Contracts.Interfaces; using HeuristicLab.Hive.Contracts.BusinessObjects; using HeuristicLab.Hive.Contracts; using HeuristicLab.Hive.Server.DataAccess; using HeuristicLab.DataAccess.Interfaces; using HeuristicLab.Hive.Server.LINQDataAccess; using ClientGroup=HeuristicLab.Hive.Contracts.BusinessObjects.ClientGroup; using Resource=HeuristicLab.Hive.Contracts.BusinessObjects.Resource; namespace HeuristicLab.Hive.Server.Core { class ClientManager: IClientManager { ClientDao clientDao = new ClientDao(); ISessionFactory factory; List clientGroups; public ClientManager() { factory = ServiceLocator.GetSessionFactory(); clientGroups = new List(); } #region IClientManager Members /// /// Returns all clients stored in the database /// /// public ResponseList GetAllClients() { ISession session = factory.GetSessionForCurrentThread(); try { IClientAdapter clientAdapter = session.GetDataAdapter(); ResponseList response = new ResponseList(); response.List = new List(clientAdapter.GetAll()); response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_GET_ALL_CLIENTS; response.Success = true; return response; } finally { if (session != null) session.EndSession(); } } /// /// returns all client groups stored in the database /// /// public ResponseList GetAllClientGroups() { ISession session = factory.GetSessionForCurrentThread(); try { IClientGroupAdapter clientGroupAdapter = session.GetDataAdapter(); IClientAdapter clientAdapter = session.GetDataAdapter(); ResponseList response = new ResponseList(); List allClientGroups = new List(clientGroupAdapter.GetAll()); ClientGroup emptyClientGroup = new ClientGroup(); ICollection groupLessClients = clientAdapter.GetGrouplessClients(); if (groupLessClients != null) { foreach (ClientInfo currClient in groupLessClients) { emptyClientGroup.Resources.Add(currClient); } } emptyClientGroup.Id = Guid.Empty; allClientGroups.Add(emptyClientGroup); response.List = allClientGroups; response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_GET_ALL_CLIENTGROUPS; response.Success = true; return response; } finally { if (session != null) session.EndSession(); } } public ResponseList GetAllUpTimeStatistics() { ResponseList response = new ResponseList(); response.Success = true; return response; } /// /// Add a client group into the database /// /// /// public ResponseObject AddClientGroup(ClientGroup clientGroup) { ISession session = factory.GetSessionForCurrentThread(); try { IClientGroupAdapter clientGroupAdapter = session.GetDataAdapter(); ResponseObject response = new ResponseObject(); if (clientGroup.Id != Guid.Empty) { response.Success = false; response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_ID_MUST_NOT_BE_SET; } else { clientGroupAdapter.Update(clientGroup); response.Obj = clientGroup; response.Success = true; response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_ADDED; } return response; } finally { if (session != null) session.EndSession(); } } /// /// Add a resource to a group /// /// /// /// public Response AddResourceToGroup(Guid clientGroupId, Resource resource) { ISession session = factory.GetSessionForCurrentThread(); try { IClientGroupAdapter clientGroupAdapter = session.GetDataAdapter(); Response response = new Response(); ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId); if (clientGroup == null) { response.Success = false; response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST; return response; } clientGroup.Resources.Add(resource); clientGroupAdapter.Update(clientGroup); response.Success = true; response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_ADDED_TO_GROUP; return response; } finally { if (session != null) session.EndSession(); } } /// /// Remove a resource from a group /// /// /// /// public Response DeleteResourceFromGroup(Guid clientGroupId, Guid resourceId) { ISession session = factory.GetSessionForCurrentThread(); try { IClientGroupAdapter clientGroupAdapter = session.GetDataAdapter(); Response response = new Response(); ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId); if (clientGroup == null) { response.Success = false; response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST; return response; } foreach (Resource resource in clientGroup.Resources) { if (resource.Id == resourceId) { clientGroup.Resources.Remove(resource); clientGroupAdapter.Update(clientGroup); response.Success = true; response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_REMOVED; return response; } } response.Success = false; response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_NOT_FOUND; return response; } finally { if (session != null) session.EndSession(); } } public ResponseObject> GetAllGroupsOfResource(Guid resourceId) { ISession session = factory.GetSessionForCurrentThread(); try { IClientGroupAdapter clientGroupAdapter = session.GetDataAdapter(); IClientAdapter clientAdapter = session.GetDataAdapter(); ResponseObject> response = new ResponseObject>(); ClientInfo client = clientAdapter.GetById(resourceId); if (client != null) { List groupsOfClient = new List(clientGroupAdapter.MemberOf(client)); response.Obj = groupsOfClient; response.Success = true; response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_GET_GROUPS_OF_CLIENT; } else { response.Obj = new List(); response.Success = false; response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_RESOURCE_NOT_FOUND; } return response; } finally { if (session != null) session.EndSession(); } } public Response DeleteClientGroup(Guid clientGroupId) { ISession session = factory.GetSessionForCurrentThread(); try { IClientGroupAdapter clientGroupAdapter = session.GetDataAdapter(); Response response = new Response(); ClientGroup clientGroup = clientGroupAdapter.GetById(clientGroupId); if (clientGroup == null) { response.Success = false; response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DOESNT_EXIST; return response; } clientGroupAdapter.Delete(clientGroup); response.Success = true; response.StatusMessage = ApplicationConstants.RESPONSE_CLIENT_CLIENTGROUP_DELETED; return response; } finally { if (session != null) session.EndSession(); } } #endregion } }