Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added transaction management (#527)

File size: 8.1 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        return job;
161      } else
162        return null;
163    }
164
165    protected override dsHiveServer.JobRow ConvertObj(Job job,
166      dsHiveServer.JobRow row) {
167      if (job != null && row != null) {
168        row.JobId = job.Id;
169       
170        if (job.Client != null) {
171          if (row.IsResourceIdNull() ||
172            row.ResourceId != job.Client.Id) {
173            ClientAdapter.Update(job.Client);
174            row.ResourceId = job.Client.Id;
175          }
176        } else
177          row.SetResourceIdNull();
178
179        if (job.ParentJob != null) {
180          if (row.IsParentJobIdNull() ||
181            row.ParentJobId != job.ParentJob.Id) {
182            Update(job.ParentJob);
183            row.ParentJobId = job.ParentJob.Id;
184          }
185        } else
186          row.SetParentJobIdNull();
187
188        if (job.UserId != Guid.Empty) {
189          if (row.IsUserIdNull() ||
190           row.UserId != Guid.Empty) {
191            row.UserId = Guid.Empty;
192          }
193        } else
194          row.SetUserIdNull();
195
196        if (job.State != State.nullState)
197          row.JobState = job.State.ToString();
198        else
199          row.SetJobStateNull();
200
201        row.Percentage = job.Percentage;
202
203        row.SerializedJob = job.SerializedJob;
204
205        if (job.DateCreated != DateTime.MinValue)
206          row.DateCreated = job.DateCreated;
207        else
208          row.SetDateCreatedNull();
209
210        if (job.DateCalculated != DateTime.MinValue)
211          row.DateCalculated = job.DateCalculated;
212        else
213          row.SetDateCalculatedNull();
214
215        row.Priority = job.Priority;
216      }
217
218      return row;
219    }
220    #endregion
221
222    #region IJobAdapter Members
223    public ICollection<Job> GetAllSubjobs(Job job) {
224      if (job != null) {
225        return
226          base.FindMultiple(
227            delegate() {
228              return Adapter.GetDataByParentJob(job.Id);
229            });
230      }
231
232      return null;
233    }
234
235    public ICollection<Job> GetJobsByState(State state) {
236      return
237         base.FindMultiple(
238           delegate() {
239             return Adapter.GetDataByState(state.ToString());
240           });
241    }
242
243    public ICollection<Job> GetJobsOf(ClientInfo client) {
244      if (client != null) {
245        return
246          base.FindMultiple(
247            delegate() {
248              return Adapter.GetDataByClient(client.Id);
249            });
250      }
251
252      return null;
253    }
254
255    public ICollection<Job> GetActiveJobsOf(ClientInfo client) {
256
257      if (client != null) {
258        return
259          base.FindMultiple(
260            delegate() {
261              return Adapter.GetDataByCalculatingClient(client.Id);
262            });
263      }
264
265      return null;
266    }
267
268    public ICollection<Job> GetJobsOf(Guid userId) {     
269      return
270          base.FindMultiple(
271            delegate() {
272              return Adapter.GetDataByUser(userId);
273            });
274    }
275
276    protected override bool doDelete(Job job) {
277      if (job != null) {
278        dsHiveServer.JobRow row =
279          GetRowById(job.Id);
280
281        if (row != null) {
282          //Referential integrity with job results
283          ICollection<JobResult> results =
284            ResultsAdapter.GetResultsOf(job);
285
286          foreach (JobResult result in results) {
287            ResultsAdapter.Delete(result);
288          }
289
290          return base.doDelete(job);
291        }
292      }
293
294      return false;
295    }
296    #endregion
297  }
298}
Note: See TracBrowser for help on using the repository browser.