Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored DAL (now using GUIDs as IDs instead of longs) (#527)

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