Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added transaction management (#527)

File size: 6.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.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        //todo: config adapter (client.config)
160
161        return client;
162      }
163      else
164        return null;
165    }
166
167    protected override dsHiveServer.ClientRow ConvertObj(ClientInfo client,
168      dsHiveServer.ClientRow row) {
169      if (client != null && row != null) {
170        row.ResourceId = client.Id;
171        row.CPUSpeed = client.CpuSpeedPerCore;
172        row.Memory = client.Memory;
173        row.Login = client.Login;
174        if (client.State != State.nullState)
175          row.Status = client.State.ToString();
176        else
177          row.SetStatusNull();
178        row.NumberOfCores = client.NrOfCores;
179
180        //todo: config adapter
181        /*if (client.Config != null)
182          row.ClientConfigId = client.Config.ClientConfigId;
183         else
184          row.ClientConfigId = null;*/
185      }
186
187      return row;
188    }
189
190    #endregion
191
192    #region IClientAdapter Members
193    protected override void doUpdate(ClientInfo client) {
194      if (client != null) {
195        ResAdapter.Update(client);
196
197        base.doUpdate(client);
198      }
199    }
200
201    public ClientInfo GetByName(string name) {
202      ClientInfo client = new ClientInfo();
203      Resource res =
204        ResAdapter.GetByName(name);
205
206      return GetById(res.Id);
207    }
208
209    protected override bool doDelete(ClientInfo client) {
210      bool success = false;
211      Guid locked = Guid.Empty;
212     
213      if (client != null) {
214        if (client.Id != Guid.Empty) {
215          LockRow(client.Id);
216          locked = client.Id;
217        }
218
219        dsHiveServer.ClientRow row =
220          GetRowById(client.Id);
221
222        if (row != null) {
223          //Referential integrity with jobs - they are cached
224          ICollection<Job> jobs =
225            JobAdapter.GetJobsOf(client);
226          foreach (Job job in jobs) {
227            JobAdapter.Delete(job);
228          }
229
230          success = base.doDelete(client) &&
231            ResAdapter.Delete(client);
232        }
233      }
234
235      if (locked != Guid.Empty) {
236        UnlockRow(locked);
237      }
238
239      return success;
240    }
241
242    #endregion
243  }
244}
Note: See TracBrowser for help on using the repository browser.