Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/JobAdapter.cs @ 1508

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

Updated jobAdapter (#372)

File size: 8.8 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.ADOHelper;
30using HeuristicLab.Hive.Server.ADODataAccess.dsHiveServerTableAdapters;
31using System.Data.Common;
32using System.Data.SqlClient;
33
34namespace HeuristicLab.Hive.Server.ADODataAccess {
35  class JobAdapterWrapper :
36    DataAdapterWrapperBase<dsHiveServerTableAdapters.JobTableAdapter,
37                      Job,
38                      dsHiveServer.JobRow> {   
39    public override void UpdateRow(dsHiveServer.JobRow row) {
40      TransactionalAdapter.Update(row);
41    }
42
43    public override dsHiveServer.JobRow
44      InsertNewRow(Job job) {
45      dsHiveServer.JobDataTable data =
46        new dsHiveServer.JobDataTable();
47
48      dsHiveServer.JobRow row = data.NewJobRow();
49      row.JobId = job.Id;
50      data.AddJobRow(row);
51
52      return row;
53    }
54
55    public override IEnumerable<dsHiveServer.JobRow>
56      FindById(Guid id) {
57      return TransactionalAdapter.GetDataById(id);
58    }
59
60    public override IEnumerable<dsHiveServer.JobRow>
61      FindAll() {
62      return TransactionalAdapter.GetData();
63    }
64
65    protected override void SetConnection(DbConnection connection) {
66      adapter.Connection = connection as SqlConnection;
67    }
68
69    protected override void SetTransaction(DbTransaction transaction) {
70      adapter.Transaction = transaction as SqlTransaction;
71    }
72  }
73 
74  class JobAdapter :
75    DataAdapterBase<dsHiveServerTableAdapters.JobTableAdapter,
76                      Job,
77                      dsHiveServer.JobRow>,
78    IJobAdapter {
79    #region Fields
80    private IClientAdapter clientAdapter = null;
81
82    private IClientAdapter ClientAdapter {
83      get {
84        if (clientAdapter == null)
85          clientAdapter =
86            this.Session.GetDataAdapter<ClientInfo, IClientAdapter>();
87
88        return clientAdapter;
89      }
90    }
91
92    private IJobResultsAdapter resultsAdapter = null;
93
94    private IJobResultsAdapter ResultsAdapter {
95      get {
96        if (resultsAdapter == null) {
97          resultsAdapter =
98            this.Session.GetDataAdapter<JobResult, IJobResultsAdapter>();
99        }
100
101        return resultsAdapter;
102      }
103    }
104    #endregion
105
106    public JobAdapter(): base(new JobAdapterWrapper()) {
107    }
108
109    #region Overrides
110    protected override Job ConvertRow(dsHiveServer.JobRow row,
111      Job job) {
112      if (row != null && job != null) {
113        job.Id = row.JobId;
114
115        if (!row.IsParentJobIdNull())
116          job.ParentJob = GetById(row.ParentJobId);
117        else
118          job.ParentJob = null;
119
120        if (!row.IsResourceIdNull())
121          job.Client = ClientAdapter.GetById(row.ResourceId);
122        else
123          job.Client = null;
124
125        if (!row.IsUserIdNull())
126          job.UserId = Guid.Empty;
127        else
128          job.UserId = Guid.Empty;
129       
130        if (!row.IsJobStateNull())
131          job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
132        else
133          job.State = State.nullState;
134
135        if (!row.IsPercentageNull())
136          job.Percentage = row.Percentage;
137        else
138          job.Percentage = 0.0;
139
140        if (!row.IsSerializedJobNull())
141          job.SerializedJob = row.SerializedJob;
142        else
143          job.SerializedJob = null;
144
145        if (!row.IsDateCreatedNull())
146          job.DateCreated = row.DateCreated;
147        else
148          job.DateCreated = DateTime.MinValue;
149
150        if (!row.IsDateCalculatedNull())
151          job.DateCalculated = row.DateCalculated;
152        else
153          job.DateCalculated = DateTime.MinValue;
154
155        if (!row.IsPriorityNull())
156          job.Priority = row.Priority;
157        else
158          job.Priority = default(int);
159
160        if (!row.IsCoresNeededNull())
161          job.CoresNeeded = row.CoresNeeded;
162        else
163          job.CoresNeeded = default(int);
164
165        if (!row.IsMemoryNeededNull())
166          job.MemoryNeeded = row.MemoryNeeded;
167        else
168          job.MemoryNeeded = default(int);
169
170        return job;
171      } else
172        return null;
173    }
174
175    protected override dsHiveServer.JobRow ConvertObj(Job job,
176      dsHiveServer.JobRow row) {
177      if (job != null && row != null) {
178        row.JobId = job.Id;
179       
180        if (job.Client != null) {
181          if (row.IsResourceIdNull() ||
182            row.ResourceId != job.Client.Id) {
183            ClientAdapter.Update(job.Client);
184            row.ResourceId = job.Client.Id;
185          }
186        } else
187          row.SetResourceIdNull();
188
189        if (job.ParentJob != null) {
190          if (row.IsParentJobIdNull() ||
191            row.ParentJobId != job.ParentJob.Id) {
192            Update(job.ParentJob);
193            row.ParentJobId = job.ParentJob.Id;
194          }
195        } else
196          row.SetParentJobIdNull();
197
198        if (job.UserId != Guid.Empty) {
199          if (row.IsUserIdNull() ||
200           row.UserId != Guid.Empty) {
201            row.UserId = Guid.Empty;
202          }
203        } else
204          row.SetUserIdNull();
205
206        if (job.State != State.nullState)
207          row.JobState = job.State.ToString();
208        else
209          row.SetJobStateNull();
210
211        row.Percentage = job.Percentage;
212
213        row.SerializedJob = job.SerializedJob;
214
215        if (job.DateCreated != DateTime.MinValue)
216          row.DateCreated = job.DateCreated;
217        else
218          row.SetDateCreatedNull();
219
220        if (job.DateCalculated != DateTime.MinValue)
221          row.DateCalculated = job.DateCalculated;
222        else
223          row.SetDateCalculatedNull();
224
225        row.Priority = job.Priority;
226
227        row.CoresNeeded = job.CoresNeeded;
228
229        row.MemoryNeeded = job.MemoryNeeded;
230      }
231
232      return row;
233    }
234    #endregion
235
236    #region IJobAdapter Members
237    public ICollection<Job> GetAllSubjobs(Job job) {
238      if (job != null) {
239        return
240          base.FindMultiple(
241            delegate() {
242              return Adapter.GetDataByParentJob(job.Id);
243            });
244      }
245
246      return null;
247    }
248
249    public ICollection<Job> GetJobsByState(State state) {
250      return
251         base.FindMultiple(
252           delegate() {
253             return Adapter.GetDataByState(state.ToString());
254           });
255    }
256
257    public ICollection<Job> GetJobsOf(ClientInfo client) {
258      if (client != null) {
259        return
260          base.FindMultiple(
261            delegate() {
262              return Adapter.GetDataByClient(client.Id);
263            });
264      }
265
266      return null;
267    }
268
269    public ICollection<Job> GetActiveJobsOf(ClientInfo client) {
270
271      if (client != null) {
272        return
273          base.FindMultiple(
274            delegate() {
275              return Adapter.GetDataByCalculatingClient(client.Id);
276            });
277      }
278
279      return null;
280    }
281
282    public ICollection<Job> GetJobsOf(Guid userId) {     
283      return
284          base.FindMultiple(
285            delegate() {
286              return Adapter.GetDataByUser(userId);
287            });
288    }
289
290    public ICollection<Job> FindJobs(State state, int cores, int memory) {
291      return
292         base.FindMultiple(
293           delegate() {
294             return Adapter.GetDataByStateCoresMemory(state.ToString(), cores, memory);
295           });
296    }
297
298    protected override bool doDelete(Job job) {
299      if (job != null) {
300        dsHiveServer.JobRow row =
301          GetRowById(job.Id);
302
303        if (row != null) {
304          //Referential integrity with job results
305          ICollection<JobResult> results =
306            ResultsAdapter.GetResultsOf(job);
307
308          foreach (JobResult result in results) {
309            ResultsAdapter.Delete(result);
310          }
311
312          return base.doDelete(job);
313        }
314      }
315
316      return false;
317    }
318    #endregion
319  }
320}
Note: See TracBrowser for help on using the repository browser.