Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1434 was 1377, checked in by svonolfe, 16 years ago

Created Heuristiclab DB Core (refactoring) #527

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