Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Hive.Server.ADODataAccess/3.2/ClientAdapter.cs @ 2273

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

Updated ClientAdapter (#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.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;
34using HeuristicLab.Hive.Server.ADODataAccess.TableAdapterWrapper;
35
36namespace HeuristicLab.Hive.Server.ADODataAccess {
37  class ClientAdapter:
38    DataAdapterBase<
39      dsHiveServerTableAdapters.ClientTableAdapter,
40      ClientInfo,
41      dsHiveServer.ClientRow>,
42    IClientAdapter {
43    #region Fields
44    private IResourceAdapter resAdapter = null;
45
46    private IResourceAdapter ResAdapter {
47      get {
48        if (resAdapter == null)
49          resAdapter =
50            this.Session.GetDataAdapter<Resource, IResourceAdapter>();
51
52        return resAdapter;
53      }
54    }
55
56    private IClientGroupAdapter clientGroupAdapter = null;
57
58    private IClientGroupAdapter ClientGroupAdapter {
59      get {
60        if (clientGroupAdapter == null) {
61          clientGroupAdapter =
62            this.Session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
63        }
64
65        return clientGroupAdapter;
66      }
67    }
68
69    private IJobAdapter jobAdapter = null;
70
71    private IJobAdapter JobAdapter {
72      get {
73        if (jobAdapter == null) {
74          this.Session.GetDataAdapter<Job, IJobAdapter>();
75        }
76
77        return jobAdapter;
78      }
79    }
80    #endregion
81
82    public ClientAdapter():
83      base(new ClientAdapterWrapper()) {
84    }
85
86    #region Overrides
87    protected override ClientInfo ConvertRow(dsHiveServer.ClientRow row,
88      ClientInfo client) {
89      if(row != null && client != null) {     
90        /*Parent - resource*/
91        client.Id = row.ResourceId;
92        ResAdapter.GetById(client);
93
94        /*ClientInfo*/       
95        if (!row.IsCPUSpeedNull())
96          client.CpuSpeedPerCore = row.CPUSpeed;
97        else
98          client.CpuSpeedPerCore = 0;
99
100        if (!row.IsMemoryNull())
101          client.Memory = row.Memory;
102        else
103          client.Memory = 0;
104
105        if (!row.IsLoginNull())
106          client.Login = row.Login;
107        else
108          client.Login = DateTime.MinValue;
109
110        if (!row.IsStatusNull())
111          client.State = (State)Enum.Parse(typeof(State), row.Status, true);
112        else
113          client.State = State.nullState;
114
115        if (!row.IsNumberOfCoresNull())
116          client.NrOfCores = row.NumberOfCores;
117        else
118          client.NrOfCores = 0;
119
120        if (!row.IsNumberOfFreeCoresNull())
121          client.NrOfFreeCores = row.NumberOfFreeCores;
122        else
123          client.NrOfFreeCores = 0;
124
125        if (!row.IsFreeMemoryNull())
126          client.FreeMemory = row.FreeMemory;
127        else
128          client.FreeMemory = 0;
129
130        //todo: config adapter (client.config)
131
132        return client;
133      }
134      else
135        return null;
136    }
137
138    protected override dsHiveServer.ClientRow ConvertObj(ClientInfo client,
139      dsHiveServer.ClientRow row) {
140      if (client != null && row != null) {
141        row.ResourceId = client.Id;
142        row.CPUSpeed = client.CpuSpeedPerCore;
143        row.Memory = client.Memory;
144        row.Login = client.Login;
145        if (client.State != State.nullState)
146          row.Status = client.State.ToString();
147        else
148          row.SetStatusNull();
149        row.NumberOfCores = client.NrOfCores;
150        row.NumberOfFreeCores = client.NrOfFreeCores;
151        row.FreeMemory = client.FreeMemory;
152
153        //todo: config adapter
154        /*if (client.Config != null)
155          row.ClientConfigId = client.Config.ClientConfigId;
156         else
157          row.ClientConfigId = null;*/
158      }
159
160      return row;
161    }
162
163    #endregion
164
165    #region IClientAdapter Members
166    protected override void doUpdate(ClientInfo client) {
167      if (client != null) {
168        ResAdapter.Update(client);
169
170        base.doUpdate(client);
171      }
172    }
173
174    public ClientInfo GetByName(string name) {
175      ClientInfo client = new ClientInfo();
176      Resource res =
177        ResAdapter.GetByName(name);
178
179      return GetById(res.Id);
180    }
181
182    protected override bool doDelete(ClientInfo client) {
183      bool success = false;
184     
185      if (client != null) {
186        dsHiveServer.ClientRow row =
187          GetRowById(client.Id);
188
189        if (row != null) {
190          success = base.doDelete(client) &&
191            ResAdapter.Delete(client);
192        }
193      }
194
195      return success;
196    }
197
198    public ICollection<ClientInfo> GetGrouplessClients() {
199      return
200        base.FindMultiple(
201          delegate() {
202            return Adapter.GetDataByGroupless();
203          });
204    }
205
206    #endregion
207  }
208}
Note: See TracBrowser for help on using the repository browser.