Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/ClientAdapter.cs @ 954

Last change on this file since 954 was 948, checked in by svonolfe, 16 years ago

Refactored DAL (#372)

File size: 5.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;
28using System.Linq.Expressions;
29using System.Runtime.CompilerServices;
30
31namespace HeuristicLab.Hive.Server.ADODataAccess {
32  class ClientAdapter: DataAdapterBase, IClientAdapter {
33    private dsHiveServerTableAdapters.ClientTableAdapter adapter =
34        new dsHiveServerTableAdapters.ClientTableAdapter();
35
36    private dsHiveServer.ClientDataTable data =
37      new dsHiveServer.ClientDataTable();
38
39    private IResourceAdapter resAdapter = null;
40
41    private IResourceAdapter ResAdapter {
42      get {
43        if (resAdapter == null)
44          resAdapter = ServiceLocator.GetResourceAdapter();
45
46        return resAdapter;
47      }
48    }
49
50    public ClientAdapter() {
51      adapter.Fill(data);
52    }
53
54    protected override void Update() {
55      this.adapter.Update(this.data);
56    }
57   
58    private ClientInfo Convert(dsHiveServer.ClientRow row,
59      ClientInfo client) {
60      if(row != null && client != null) {     
61        /*Parent - resource*/
62        client.ResourceId = row.ResourceId;
63        ResAdapter.GetResourceById(client);
64
65        /*ClientInfo*/
66        client.ClientId = row.GUID;
67       
68        if (!row.IsCPUSpeedNull())
69          client.CpuSpeedPerCore = row.CPUSpeed;
70        else
71          client.CpuSpeedPerCore = 0;
72
73        if (!row.IsMemoryNull())
74          client.Memory = row.Memory;
75        else
76          client.Memory = 0;
77
78        if (!row.IsLoginNull())
79          client.Login = row.Login;
80        else
81          client.Login = DateTime.MinValue;
82
83        if (!row.IsStatusNull())
84          client.State = (State)Enum.Parse(typeof(State), row.Status, true);
85        else
86          client.State = State.idle;
87
88        if (!row.IsNumberOfCoresNull())
89          client.NrOfCores = row.NumberOfCores;
90        else
91          client.NrOfCores = 0;
92
93        //todo: config adapter (client.config)
94
95        return client;
96      }
97      else
98        return null;
99    }
100
101    private dsHiveServer.ClientRow Convert(ClientInfo client,
102      dsHiveServer.ClientRow row) {
103      if (client != null && row != null) {     
104        row.GUID = client.ClientId;
105        row.CPUSpeed = client.CpuSpeedPerCore;
106        row.Memory = client.Memory;
107        row.Login = client.Login;
108        row.Status = client.State.ToString();
109        row.NumberOfCores = client.NrOfCores;
110
111        //todo: config adapter
112        /*if (client.Config != null)
113          row.ClientConfigId = client.Config.ClientConfigId;
114         else
115          row.ClientConfigId = null;*/
116      }
117
118      return row;
119    }
120
121    #region IClientAdapter Members
122    [MethodImpl(MethodImplOptions.Synchronized)]
123    public void UpdateClient(ClientInfo client) {
124      if (client != null) {
125        ResAdapter.UpdateResource(client);
126
127        dsHiveServer.ClientRow row =
128          data.FindByResourceId(client.ResourceId);
129
130        if (row == null) {
131          row = data.NewClientRow();
132          row.ResourceId = client.ResourceId;
133          data.AddClientRow(row);
134        }
135
136        Convert(client, row);
137      }
138    }
139
140    public ClientInfo GetClientById(Guid clientId) {
141      ClientInfo client = new ClientInfo();
142
143      dsHiveServer.ClientRow row = null;
144      IEnumerable<dsHiveServer.ClientRow> clients =
145            from c in
146              data.AsEnumerable<dsHiveServer.ClientRow>()
147            where !c.IsGUIDNull() && c.GUID == clientId
148            select c;
149      if (clients.Count<dsHiveServer.ClientRow>() == 1)
150        row = clients.First<dsHiveServer.ClientRow>();
151
152      if (row != null) {
153        Convert(row, client);
154
155        return client;
156      } else {
157        return null;
158      }
159    }
160
161    public ICollection<ClientInfo> GetAllClients() {
162      ICollection<ClientInfo> allClients =
163        new List<ClientInfo>();
164
165      foreach (dsHiveServer.ClientRow row in data) {
166        ClientInfo client = new ClientInfo();
167        Convert(row, client);
168        allClients.Add(client);
169      }
170
171      return allClients;
172    }
173
174    [MethodImpl(MethodImplOptions.Synchronized)]
175    public bool DeleteClient(ClientInfo client) {
176      if (client != null) {
177        dsHiveServer.ClientRow row = null;
178        IEnumerable<dsHiveServer.ClientRow> clients =
179              from c in
180                data.AsEnumerable<dsHiveServer.ClientRow>()
181              where !c.IsGUIDNull() && c.GUID == client.ClientId
182              select c;
183        if (clients.Count<dsHiveServer.ClientRow>() == 1)
184          row = clients.First<dsHiveServer.ClientRow>();
185
186        if (row != null) {
187          data.RemoveClientRow(row);
188
189          return ResAdapter.DeleteResource(client);
190        }
191      }
192
193      return false;
194    }
195
196    #endregion
197  }
198}
Note: See TracBrowser for help on using the repository browser.