Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated jobAdapter and clientAdapter (#372)

File size: 7.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;
31using HeuristicLab.Hive.Server.ADODataAccess.dsHiveServerTableAdapters;
32using System.Data.Common;
33using System.Data.SqlClient;
34
35namespace HeuristicLab.Hive.Server.ADODataAccess {
36  class ClientAdapterWrapper :
37    DataAdapterWrapperBase<
38        dsHiveServerTableAdapters.ClientTableAdapter,
39    ClientInfo,
40    dsHiveServer.ClientRow> {
41    public override void UpdateRow(dsHiveServer.ClientRow row) {
42      TransactionalAdapter.Update(row);
43    }
44
45    public override dsHiveServer.ClientRow
46      InsertNewRow(ClientInfo client) {
47      dsHiveServer.ClientDataTable data =
48        new dsHiveServer.ClientDataTable();
49
50      dsHiveServer.ClientRow row = data.NewClientRow();
51      row.ResourceId = client.Id;
52      data.AddClientRow(row);
53
54      return row;
55    }
56
57    public override IEnumerable<dsHiveServer.ClientRow>
58      FindById(Guid id) {
59      return TransactionalAdapter.GetDataById(id);
60    }
61
62    public override IEnumerable<dsHiveServer.ClientRow>
63      FindAll() {
64      return TransactionalAdapter.GetData();
65    }
66
67    protected override void SetConnection(DbConnection connection) {
68      adapter.Connection = connection as SqlConnection;
69    }
70
71    protected override void SetTransaction(DbTransaction transaction) {
72      adapter.Transaction = transaction as SqlTransaction;
73    }
74  }
75
76  class ClientAdapter:
77    DataAdapterBase<
78      dsHiveServerTableAdapters.ClientTableAdapter,
79      ClientInfo,
80      dsHiveServer.ClientRow>,
81    IClientAdapter {
82    #region Fields
83    private IResourceAdapter resAdapter = null;
84
85    private IResourceAdapter ResAdapter {
86      get {
87        if (resAdapter == null)
88          resAdapter =
89            this.Session.GetDataAdapter<Resource, IResourceAdapter>();
90
91        return resAdapter;
92      }
93    }
94
95    private IClientGroupAdapter clientGroupAdapter = null;
96
97    private IClientGroupAdapter ClientGroupAdapter {
98      get {
99        if (clientGroupAdapter == null) {
100          clientGroupAdapter =
101            this.Session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
102        }
103
104        return clientGroupAdapter;
105      }
106    }
107
108    private IJobAdapter jobAdapter = null;
109
110    private IJobAdapter JobAdapter {
111      get {
112        if (jobAdapter == null) {
113          this.Session.GetDataAdapter<Job, IJobAdapter>();
114        }
115
116        return jobAdapter;
117      }
118    }
119    #endregion
120
121    public ClientAdapter():
122      base(new ClientAdapterWrapper()) {
123    }
124
125    #region Overrides
126    protected override ClientInfo ConvertRow(dsHiveServer.ClientRow row,
127      ClientInfo client) {
128      if(row != null && client != null) {     
129        /*Parent - resource*/
130        client.Id = row.ResourceId;
131        ResAdapter.GetById(client);
132
133        /*ClientInfo*/       
134        if (!row.IsCPUSpeedNull())
135          client.CpuSpeedPerCore = row.CPUSpeed;
136        else
137          client.CpuSpeedPerCore = 0;
138
139        if (!row.IsMemoryNull())
140          client.Memory = row.Memory;
141        else
142          client.Memory = 0;
143
144        if (!row.IsLoginNull())
145          client.Login = row.Login;
146        else
147          client.Login = DateTime.MinValue;
148
149        if (!row.IsStatusNull())
150          client.State = (State)Enum.Parse(typeof(State), row.Status, true);
151        else
152          client.State = State.nullState;
153
154        if (!row.IsNumberOfCoresNull())
155          client.NrOfCores = row.NumberOfCores;
156        else
157          client.NrOfCores = 0;
158
159        if (!row.IsNumberOfFreeCoresNull())
160          client.NrOfFreeCores = row.NumberOfFreeCores;
161        else
162          client.NrOfFreeCores = 0;
163
164        if (!row.IsFreeMemoryNull())
165          client.FreeMemory = row.FreeMemory;
166        else
167          client.FreeMemory = 0;
168
169        //todo: config adapter (client.config)
170
171        return client;
172      }
173      else
174        return null;
175    }
176
177    protected override dsHiveServer.ClientRow ConvertObj(ClientInfo client,
178      dsHiveServer.ClientRow row) {
179      if (client != null && row != null) {
180        row.ResourceId = client.Id;
181        row.CPUSpeed = client.CpuSpeedPerCore;
182        row.Memory = client.Memory;
183        row.Login = client.Login;
184        if (client.State != State.nullState)
185          row.Status = client.State.ToString();
186        else
187          row.SetStatusNull();
188        row.NumberOfCores = client.NrOfCores;
189        row.NumberOfFreeCores = client.NrOfFreeCores;
190        row.FreeMemory = client.FreeMemory;
191
192        //todo: config adapter
193        /*if (client.Config != null)
194          row.ClientConfigId = client.Config.ClientConfigId;
195         else
196          row.ClientConfigId = null;*/
197      }
198
199      return row;
200    }
201
202    #endregion
203
204    #region IClientAdapter Members
205    protected override void doUpdate(ClientInfo client) {
206      if (client != null) {
207        ResAdapter.Update(client);
208
209        base.doUpdate(client);
210      }
211    }
212
213    public ClientInfo GetByName(string name) {
214      ClientInfo client = new ClientInfo();
215      Resource res =
216        ResAdapter.GetByName(name);
217
218      return GetById(res.Id);
219    }
220
221    protected override bool doDelete(ClientInfo client) {
222      bool success = false;
223      Guid locked = Guid.Empty;
224     
225      if (client != null) {
226        if (client.Id != Guid.Empty) {
227          LockRow(client.Id);
228          locked = client.Id;
229        }
230
231        dsHiveServer.ClientRow row =
232          GetRowById(client.Id);
233
234        if (row != null) {
235          //Referential integrity with jobs - they are cached
236          ICollection<Job> jobs =
237            JobAdapter.GetJobsOf(client);
238          foreach (Job job in jobs) {
239            JobAdapter.Delete(job);
240          }
241
242          success = base.doDelete(client) &&
243            ResAdapter.Delete(client);
244        }
245      }
246
247      if (locked != Guid.Empty) {
248        UnlockRow(locked);
249      }
250
251      return success;
252    }
253
254    #endregion
255  }
256}
Note: See TracBrowser for help on using the repository browser.