Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1096 was 1096, checked in by msteinbi, 15 years ago

Implementing Lifecycle Management (#453)

File size: 7.4 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:
33    CachedDataAdapter<
34      dsHiveServerTableAdapters.ClientTableAdapter,
35      ClientInfo,
36      dsHiveServer.ClientRow,
37      dsHiveServer.ClientDataTable>,
38    IClientAdapter {
39    #region Fields
40    dsHiveServer.ClientDataTable data =
41        new dsHiveServer.ClientDataTable();
42
43    private IResourceAdapter resAdapter = null;
44
45    private IResourceAdapter ResAdapter {
46      get {
47        if (resAdapter == null)
48          resAdapter = ServiceLocator.GetResourceAdapter();
49
50        return resAdapter;
51      }
52    }
53
54    private IClientGroupAdapter clientGroupAdapter = null;
55
56    private IClientGroupAdapter ClientGroupAdapter {
57      get {
58        if (clientGroupAdapter == null) {
59          clientGroupAdapter = ServiceLocator.GetClientGroupAdapter();
60        }
61
62        return clientGroupAdapter;
63      }
64    }
65
66    private IJobAdapter jobAdapter = null;
67
68    private IJobAdapter JobAdapter {
69      get {
70        if (jobAdapter == null) {
71          jobAdapter = ServiceLocator.GetJobAdapter();
72        }
73
74        return jobAdapter;
75      }
76    }
77    #endregion
78
79    public ClientAdapter() {
80      parentAdapters.Add(this.ResAdapter as ICachedDataAdapter);
81    }
82
83    #region Overrides
84    protected override ClientInfo Convert(dsHiveServer.ClientRow row,
85      ClientInfo client) {
86      if(row != null && client != null) {     
87        /*Parent - resource*/
88        client.Id = row.ResourceId;
89        ResAdapter.GetById(client);
90
91        /*ClientInfo*/
92        if (!row.IsGUIDNull())
93          client.ClientId = row.GUID;
94        else
95          client.ClientId = Guid.Empty;
96       
97        if (!row.IsCPUSpeedNull())
98          client.CpuSpeedPerCore = row.CPUSpeed;
99        else
100          client.CpuSpeedPerCore = 0;
101
102        if (!row.IsMemoryNull())
103          client.Memory = row.Memory;
104        else
105          client.Memory = 0;
106
107        if (!row.IsLoginNull())
108          client.Login = row.Login;
109        else
110          client.Login = DateTime.MinValue;
111
112        if (!row.IsStatusNull())
113          client.State = (State)Enum.Parse(typeof(State), row.Status, true);
114        else
115          client.State = State.nullState;
116
117        if (!row.IsNumberOfCoresNull())
118          client.NrOfCores = row.NumberOfCores;
119        else
120          client.NrOfCores = 0;
121
122        //todo: config adapter (client.config)
123
124        return client;
125      }
126      else
127        return null;
128    }
129
130    protected override dsHiveServer.ClientRow Convert(ClientInfo client,
131      dsHiveServer.ClientRow row) {
132      if (client != null && row != null) {     
133        row.GUID = client.ClientId;
134        row.CPUSpeed = client.CpuSpeedPerCore;
135        row.Memory = client.Memory;
136        row.Login = client.Login;
137        if (client.State != State.nullState)
138          row.Status = client.State.ToString();
139        else
140          row.SetStatusNull();
141        row.NumberOfCores = client.NrOfCores;
142
143        //todo: config adapter
144        /*if (client.Config != null)
145          row.ClientConfigId = client.Config.ClientConfigId;
146         else
147          row.ClientConfigId = null;*/
148      }
149
150      return row;
151    }
152
153    protected override void UpdateRow(dsHiveServer.ClientRow row) {
154      adapter.Update(row);
155    }
156
157    protected override dsHiveServer.ClientRow
158      InsertNewRow(ClientInfo client) {
159      dsHiveServer.ClientRow row = data.NewClientRow();
160      row.ResourceId = client.Id;
161      data.AddClientRow(row);
162
163      return row;
164    }
165
166    protected override dsHiveServer.ClientRow
167      InsertNewRowInCache(ClientInfo client) {
168      dsHiveServer.ClientRow row = cache.NewClientRow();
169      row.ResourceId = client.Id;
170      cache.AddClientRow(row);
171
172      return row;
173    }
174
175    protected override void FillCache() {
176      cache = adapter.GetDataByActive();
177    }
178
179    public override void SyncWithDb() {
180      adapter.Update(cache);
181    }
182
183    protected override bool PutInCache(ClientInfo obj) {
184      return (obj.State != State.offline && obj.State != State.nullState);
185    }
186
187    protected override IEnumerable<dsHiveServer.ClientRow>
188      FindById(long id) {
189      return adapter.GetDataByResourceId(id);
190    }
191
192    protected override dsHiveServer.ClientRow
193      FindCachedById(long id) {
194      return cache.FindByResourceId(id);
195    }
196
197    protected override IEnumerable<dsHiveServer.ClientRow>
198      FindAll() {
199      return FindMultipleRows(
200        new Selector(adapter.GetData),
201        new Selector(cache.AsEnumerable<dsHiveServer.ClientRow>));
202    }
203
204    #endregion
205
206    #region IClientAdapter Members
207    [MethodImpl(MethodImplOptions.Synchronized)]
208    public override void Update(ClientInfo client) {
209      if (client != null) {
210        if (client.Id == default(long)) {
211          ClientInfo found = GetById(client.ClientId);
212          if (found != null)
213            client.Id = found.Id;
214        }
215
216        ResAdapter.Update(client);
217
218        base.Update(client);
219      }
220    }
221
222    public ClientInfo GetById(Guid clientId) {
223      return base.FindSingle(
224        delegate() {
225          return adapter.GetDataById(clientId);
226        },
227        delegate() {
228          return from c in
229                     cache.AsEnumerable<dsHiveServer.ClientRow>()
230                   where !c.IsGUIDNull() &&
231                          c.GUID == clientId
232                   select c;
233        });
234    }
235
236    public ClientInfo GetByName(string name) {
237      ClientInfo client = new ClientInfo();
238      Resource res =
239        ResAdapter.GetByName(name);
240
241      return GetById(res.Id);
242    }
243
244    [MethodImpl(MethodImplOptions.Synchronized)]
245    public override bool Delete(ClientInfo client) {     
246      if (client != null) {
247        dsHiveServer.ClientRow row =
248          GetRowById(client.Id);
249
250        if (row != null) {
251          //Referential integrity with jobs - they are cached
252          ICollection<Job> jobs =
253            JobAdapter.GetJobsOf(client);
254          foreach (Job job in jobs) {
255            JobAdapter.Delete(job);
256          }
257
258          return base.Delete(client) &&
259            ResAdapter.Delete(client);
260        }
261      }
262
263      return false;
264    }
265
266    #endregion
267  }
268}
Note: See TracBrowser for help on using the repository browser.