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, 16 years ago

Updated jobAdapter (#372)

File size: 8.8 KB
RevLine 
[971]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;
[1377]26using HeuristicLab.Hive.Server.DataAccess;
[971]27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using System.Linq.Expressions;
[1377]29using HeuristicLab.DataAccess.ADOHelper;
[1468]30using HeuristicLab.Hive.Server.ADODataAccess.dsHiveServerTableAdapters;
31using System.Data.Common;
32using System.Data.SqlClient;
[971]33
34namespace HeuristicLab.Hive.Server.ADODataAccess {
[1468]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 
[995]74  class JobAdapter :
[1468]75    DataAdapterBase<dsHiveServerTableAdapters.JobTableAdapter,
[995]76                      Job,
[1468]77                      dsHiveServer.JobRow>,
[995]78    IJobAdapter {
79    #region Fields
[971]80    private IClientAdapter clientAdapter = null;
81
82    private IClientAdapter ClientAdapter {
83      get {
84        if (clientAdapter == null)
[1468]85          clientAdapter =
86            this.Session.GetDataAdapter<ClientInfo, IClientAdapter>();
[971]87
88        return clientAdapter;
89      }
90    }
91
[1005]92    private IJobResultsAdapter resultsAdapter = null;
93
94    private IJobResultsAdapter ResultsAdapter {
95      get {
96        if (resultsAdapter == null) {
[1468]97          resultsAdapter =
98            this.Session.GetDataAdapter<JobResult, IJobResultsAdapter>();
[1005]99        }
100
101        return resultsAdapter;
102      }
103    }
[995]104    #endregion
[971]105
[1468]106    public JobAdapter(): base(new JobAdapterWrapper()) {
107    }
108
[995]109    #region Overrides
[1131]110    protected override Job ConvertRow(dsHiveServer.JobRow row,
[971]111      Job job) {
112      if (row != null && job != null) {
[995]113        job.Id = row.JobId;
[971]114
115        if (!row.IsParentJobIdNull())
[995]116          job.ParentJob = GetById(row.ParentJobId);
[971]117        else
[975]118          job.ParentJob = null;
[971]119
120        if (!row.IsResourceIdNull())
[995]121          job.Client = ClientAdapter.GetById(row.ResourceId);
[971]122        else
123          job.Client = null;
[1161]124
[1449]125        if (!row.IsUserIdNull())
[1377]126          job.UserId = Guid.Empty;
[1161]127        else
[1377]128          job.UserId = Guid.Empty;
[971]129       
[1092]130        if (!row.IsJobStateNull())
131          job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
[971]132        else
[995]133          job.State = State.nullState;
[971]134
[1092]135        if (!row.IsPercentageNull())
136          job.Percentage = row.Percentage;
137        else
138          job.Percentage = 0.0;
139
[1120]140        if (!row.IsSerializedJobNull())
141          job.SerializedJob = row.SerializedJob;
142        else
143          job.SerializedJob = null;
144
[1169]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
[1175]155        if (!row.IsPriorityNull())
156          job.Priority = row.Priority;
157        else
158          job.Priority = default(int);
[1169]159
[1505]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
[971]170        return job;
171      } else
172        return null;
173    }
174
[1131]175    protected override dsHiveServer.JobRow ConvertObj(Job job,
[971]176      dsHiveServer.JobRow row) {
177      if (job != null && row != null) {
[1449]178        row.JobId = job.Id;
179       
[971]180        if (job.Client != null) {
[1021]181          if (row.IsResourceIdNull() ||
182            row.ResourceId != job.Client.Id) {
183            ClientAdapter.Update(job.Client);
184            row.ResourceId = job.Client.Id;
185          }
[971]186        } else
187          row.SetResourceIdNull();
188
[975]189        if (job.ParentJob != null) {
[1021]190          if (row.IsParentJobIdNull() ||
191            row.ParentJobId != job.ParentJob.Id) {
192            Update(job.ParentJob);
193            row.ParentJobId = job.ParentJob.Id;
194          }
[971]195        } else
196          row.SetParentJobIdNull();
197
[1377]198        if (job.UserId != Guid.Empty) {
[1449]199          if (row.IsUserIdNull() ||
200           row.UserId != Guid.Empty) {
201            row.UserId = Guid.Empty;
[1161]202          }
203        } else
[1449]204          row.SetUserIdNull();
[1161]205
[995]206        if (job.State != State.nullState)
[1092]207          row.JobState = job.State.ToString();
[995]208        else
[1092]209          row.SetJobStateNull();
210
211        row.Percentage = job.Percentage;
[1120]212
213        row.SerializedJob = job.SerializedJob;
[1169]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;
[1505]226
227        row.CoresNeeded = job.CoresNeeded;
228
229        row.MemoryNeeded = job.MemoryNeeded;
[971]230      }
231
232      return row;
233    }
[995]234    #endregion
235
236    #region IJobAdapter Members
[971]237    public ICollection<Job> GetAllSubjobs(Job job) {
238      if (job != null) {
[995]239        return
240          base.FindMultiple(
241            delegate() {
[1449]242              return Adapter.GetDataByParentJob(job.Id);
[995]243            });
[971]244      }
245
[995]246      return null;
[971]247    }
248
[998]249    public ICollection<Job> GetJobsByState(State state) {
250      return
251         base.FindMultiple(
252           delegate() {
[1131]253             return Adapter.GetDataByState(state.ToString());
[998]254           });
255    }
256
[971]257    public ICollection<Job> GetJobsOf(ClientInfo client) {
258      if (client != null) {
[995]259        return
260          base.FindMultiple(
261            delegate() {
[1131]262              return Adapter.GetDataByClient(client.Id);
[995]263            });
[971]264      }
265
[995]266      return null;
[971]267    }
268
[1166]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
[1468]282    public ICollection<Job> GetJobsOf(Guid userId) {     
[1377]283      return
[995]284          base.FindMultiple(
285            delegate() {
[1449]286              return Adapter.GetDataByUser(userId);
[995]287            });
[971]288    }
[1005]289
[1508]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
[1468]298    protected override bool doDelete(Job job) {
[1005]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
[1468]312          return base.doDelete(job);
[1005]313        }
314      }
315
316      return false;
317    }
[971]318    #endregion
319  }
320}
Note: See TracBrowser for help on using the repository browser.