Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Hive.Server.ADODataAccess/ClientAdapter.cs @ 1093

Last change on this file since 1093 was 845, checked in by svonolfe, 16 years ago

Added user adapter (#372).

File size: 4.2 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.Server.Core.InternalInterfaces.DataAccess;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28
29namespace HeuristicLab.Hive.Server.ADODataAccess {
30  class ClientAdapter: IClientAdapter {
31    private dsHiveServerTableAdapters.ClientTableAdapter adapter =
32        new dsHiveServerTableAdapters.ClientTableAdapter();
33
34    private ResourceAdapter resAdapter =
35      new ResourceAdapter();
36   
37    #region IClientAdapter Members
38    private ClientInfo Convert(dsHiveServer.ClientRow row) {
39      if(row != null) {
40        ClientInfo client = new ClientInfo();
41       
42        /*Parent - resource*/
43        Resource resource =
44          resAdapter.GetResourceById(row.ResourceId);
45        client.ResourceId = resource.ResourceId;
46        client.Name = resource.Name;
47
48        /*ClientInfo*/
49        client.ClientId = row.GUID;
50        client.CpuSpeedPerCore = row.CPUSpeed;
51        client.Memory = row.Memory;
52        client.Login = row.Login;
53        if (row.Status != null)
54          client.State = (State)Enum.Parse(typeof(State), row.Status, true);
55        client.NrOfCores = row.NumberOfCores;
56
57        //todo: config adapter (client.config)
58
59        return client;
60      }
61      else
62        return null;
63    }
64
65    private dsHiveServer.ClientRow Convert(ClientInfo client,
66      dsHiveServer.ClientRow row) {
67      if (client != null && row != null) {     
68        row.ResourceId = client.ResourceId;
69        row.GUID = client.ClientId;
70        row.CPUSpeed = client.CpuSpeedPerCore;
71        row.Memory = client.Memory;
72        row.Login = client.Login;
73        row.Status = client.State.ToString();
74        row.NumberOfCores = client.NrOfCores;
75
76        //todo: config adapter
77        /*if (client.Config != null)
78          row.ClientConfigId = client.Config.ClientConfigId;
79         else
80          row.ClientConfigId = null;*/
81      }
82
83      return row;
84    }
85
86    public void UpdateClient(ClientInfo client) {
87      if (client != null) {
88        resAdapter.UpdateResource(client);
89
90        dsHiveServer.ClientDataTable data =
91          adapter.GetDataById(client.ClientId);
92
93        dsHiveServer.ClientRow row;
94        if (data.Count == 0) {
95          row = data.NewClientRow();
96          row.ResourceId = client.ResourceId;
97          data.AddClientRow(row);
98        } else {
99          row = data[0];
100        }
101
102        Convert(client, row);
103
104        adapter.Update(data);
105      }
106    }
107
108    public ClientInfo GetClientById(Guid clientId) {
109      dsHiveServer.ClientDataTable data =
110          adapter.GetDataById(clientId);
111      if (data.Count == 1) {
112        dsHiveServer.ClientRow row =
113          data[0];
114        return Convert(row);
115      } else {
116        return null;
117      }
118    }
119
120    public ICollection<ClientInfo> GetAllClients() {
121      ICollection<ClientInfo> allClients =
122        new List<ClientInfo>();
123
124      dsHiveServer.ClientDataTable data =
125          adapter.GetData();
126
127      foreach (dsHiveServer.ClientRow row in data) {
128        allClients.Add(Convert(row));
129      }
130
131      return allClients;
132    }
133
134    public bool DeleteClient(ClientInfo client) {
135      //referential integrity will delete the client object
136      return resAdapter.DeleteResource(client);
137    }
138
139    #endregion
140  }
141}
Note: See TracBrowser for help on using the repository browser.