Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.Hive.Server.ADODataAccess/ClientAdapter.cs @ 1044

Last change on this file since 1044 was 899, checked in by svonolfe, 15 years ago

Added user adapter to the service locator (#372)

File size: 4.7 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      ClientInfo client) {
40      if(row != null && client != null) {     
41        /*Parent - resource*/
42        client.ResourceId = row.ResourceId;
43        resAdapter.FillResource(client);
44
45        /*ClientInfo*/
46        client.ClientId = row.GUID;
47       
48        if (!row.IsCPUSpeedNull())
49          client.CpuSpeedPerCore = row.CPUSpeed;
50        else
51          client.CpuSpeedPerCore = 0;
52
53        if (!row.IsMemoryNull())
54          client.Memory = row.Memory;
55        else
56          client.Memory = 0;
57
58        if (!row.IsLoginNull())
59          client.Login = row.Login;
60        else
61          client.Login = DateTime.MinValue;
62
63        if (!row.IsStatusNull())
64          client.State = (State)Enum.Parse(typeof(State), row.Status, true);
65        else
66          client.State = State.idle;
67
68        if (!row.IsNumberOfCoresNull())
69          client.NrOfCores = row.NumberOfCores;
70        else
71          client.NrOfCores = 0;
72
73        //todo: config adapter (client.config)
74
75        return client;
76      }
77      else
78        return null;
79    }
80
81    private dsHiveServer.ClientRow Convert(ClientInfo client,
82      dsHiveServer.ClientRow row) {
83      if (client != null && row != null) {     
84        row.ResourceId = client.ResourceId;
85        row.GUID = client.ClientId;
86        row.CPUSpeed = client.CpuSpeedPerCore;
87        row.Memory = client.Memory;
88        row.Login = client.Login;
89        row.Status = client.State.ToString();
90        row.NumberOfCores = client.NrOfCores;
91
92        //todo: config adapter
93        /*if (client.Config != null)
94          row.ClientConfigId = client.Config.ClientConfigId;
95         else
96          row.ClientConfigId = null;*/
97      }
98
99      return row;
100    }
101
102    public void UpdateClient(ClientInfo client) {
103      if (client != null) {
104        resAdapter.UpdateResource(client);
105
106        dsHiveServer.ClientDataTable data =
107          adapter.GetDataById(client.ClientId);
108
109        dsHiveServer.ClientRow row;
110        if (data.Count == 0) {
111          row = data.NewClientRow();
112          row.ResourceId = client.ResourceId;
113          data.AddClientRow(row);
114        } else {
115          row = data[0];
116        }
117
118        Convert(client, row);
119
120        adapter.Update(data);
121      }
122    }
123
124    public ClientInfo GetClientById(Guid clientId) {
125      ClientInfo client = new ClientInfo();
126     
127      dsHiveServer.ClientDataTable data =
128          adapter.GetDataById(clientId);
129      if (data.Count == 1) {
130        dsHiveServer.ClientRow row =
131          data[0];
132        Convert(row, client);
133
134        return client;
135      } else {
136        return null;
137      }
138    }
139
140    public ICollection<ClientInfo> GetAllClients() {
141      ICollection<ClientInfo> allClients =
142        new List<ClientInfo>();
143
144      dsHiveServer.ClientDataTable data =
145          adapter.GetData();
146
147      foreach (dsHiveServer.ClientRow row in data) {
148        ClientInfo client = new ClientInfo();
149        Convert(row, client);
150        allClients.Add(client);
151      }
152
153      return allClients;
154    }
155
156    public bool DeleteClient(ClientInfo client) {
157      //referential integrity will delete the client object
158      return resAdapter.DeleteResource(client);
159    }
160
161    #endregion
162  }
163}
Note: See TracBrowser for help on using the repository browser.