Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1390 was 1377, checked in by svonolfe, 16 years ago

Created Heuristiclab DB Core (refactoring) #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.IsPermissionOwnerIdNull())
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        if (job.Client != null) {
127          if (row.IsResourceIdNull() ||
128            row.ResourceId != job.Client.Id) {
129            ClientAdapter.Update(job.Client);
130            row.ResourceId = job.Client.Id;
131          }
132        } else
133          row.SetResourceIdNull();
134
135        if (job.ParentJob != null) {
136          if (row.IsParentJobIdNull() ||
137            row.ParentJobId != job.ParentJob.Id) {
138            Update(job.ParentJob);
139            row.ParentJobId = job.ParentJob.Id;
140          }
141        } else
142          row.SetParentJobIdNull();
143
144        if (job.UserId != Guid.Empty) {
145          if (row.IsPermissionOwnerIdNull() ||
146           row.PermissionOwnerId != 0) {
147            row.PermissionOwnerId = 0;
148          }
149        } else
150          row.SetPermissionOwnerIdNull();
151
152        if (job.State != State.nullState)
153          row.JobState = job.State.ToString();
154        else
155          row.SetJobStateNull();
156
157        row.Percentage = job.Percentage;
158
159        row.SerializedJob = job.SerializedJob;
160
161        if (job.DateCreated != DateTime.MinValue)
162          row.DateCreated = job.DateCreated;
163        else
164          row.SetDateCreatedNull();
165
166        if (job.DateCalculated != DateTime.MinValue)
167          row.DateCalculated = job.DateCalculated;
168        else
169          row.SetDateCalculatedNull();
170
171        row.Priority = job.Priority;
172      }
173
174      return row;
175    }
176
177    protected override void UpdateRow(dsHiveServer.JobRow row) {
178      Adapter.Update(row);
179    }
180
181    protected override dsHiveServer.JobRow
182      InsertNewRow(Job job) {
183      dsHiveServer.JobDataTable data =
184        new dsHiveServer.JobDataTable();
185
186      dsHiveServer.JobRow row = data.NewJobRow();
187      data.AddJobRow(row);
188
189      return row;
190    }
191
192    protected override dsHiveServer.JobRow
193      InsertNewRowInCache(Job job) {
194      dsHiveServer.JobRow row = cache.NewJobRow();
195      cache.AddJobRow(row);
196
197      return row;
198    }
199
200    protected override void FillCache() {
201      Adapter.FillByActive(cache);
202    }
203
204    protected override void SynchronizeWithDb() {
205      this.Adapter.Update(cache);
206    }
207
208    protected override bool PutInCache(Job job) {
209      return job != null
210        && (job.State == State.calculating
211            || job.State == State.idle);
212    }
213
214    protected override IEnumerable<dsHiveServer.JobRow>
215      FindById(long id) {
216      return Adapter.GetDataById(id);
217    }
218
219    protected override dsHiveServer.JobRow
220      FindCachedById(long id) {
221      return cache.FindByJobId(id);
222    }
223
224    protected override IEnumerable<dsHiveServer.JobRow>
225      FindAll() {
226      return FindMultipleRows(
227        new Selector(Adapter.GetData),
228        new Selector(cache.AsEnumerable<dsHiveServer.JobRow>));
229    }
230
231    #endregion
232
233    #region IJobAdapter Members
234    public ICollection<Job> GetAllSubjobs(Job job) {
235      if (job != null) {
236        return
237          base.FindMultiple(
238            delegate() {
239              return Adapter.GetDataBySubjobs(job.Id);
240            },
241            delegate() {
242              return from j in
243                   cache.AsEnumerable<dsHiveServer.JobRow>()
244                 where  !j.IsParentJobIdNull() &&
245                        j.ParentJobId == job.Id
246                 select j;
247            });
248      }
249
250      return null;
251    }
252
253    public ICollection<Job> GetJobsByState(State state) {
254      return
255         base.FindMultiple(
256           delegate() {
257             return Adapter.GetDataByState(state.ToString());
258           },
259           delegate() {
260             return from job in
261                      cache.AsEnumerable<dsHiveServer.JobRow>()
262                    where !job.IsJobStateNull() &&
263                           job.JobState == state.ToString()
264                    select job;
265           });
266    }
267
268    public ICollection<Job> GetJobsOf(ClientInfo client) {
269      if (client != null) {
270        return
271          base.FindMultiple(
272            delegate() {
273              return Adapter.GetDataByClient(client.Id);
274            },
275            delegate() {
276              return from job in
277                 cache.AsEnumerable<dsHiveServer.JobRow>()
278               where !job.IsResourceIdNull() &&
279                      job.ResourceId == client.Id
280               select job;
281            });
282      }
283
284      return null;
285    }
286
287    public ICollection<Job> GetActiveJobsOf(ClientInfo client) {
288
289      if (client != null) {
290        return
291          base.FindMultiple(
292            delegate() {
293              return Adapter.GetDataByCalculatingClient(client.Id);
294            },
295            delegate() {
296              return from job in
297                       cache.AsEnumerable<dsHiveServer.JobRow>()
298                     where !job.IsResourceIdNull() &&
299                            job.ResourceId == client.Id &&
300                           !job.IsJobStateNull() &&
301                            job.JobState == "calculating"
302                     select job;
303            });
304      }
305
306      return null;
307    }
308
309    public ICollection<Job> GetJobsOf(Guid userId) {
310      throw new NotImplementedException(); 
311     
312      return
313          base.FindMultiple(
314            delegate() {
315              return Adapter.GetDataByUser(0);
316            },
317            delegate() {
318              return from job in
319                cache.AsEnumerable<dsHiveServer.JobRow>()
320              where !job.IsPermissionOwnerIdNull() &&
321                job.PermissionOwnerId == 0
322              select job;
323            });
324
325      return null;
326    }
327
328    public override bool Delete(Job job) {
329      if (job != null) {
330        dsHiveServer.JobRow row =
331          GetRowById(job.Id);
332
333        if (row != null) {
334          //Referential integrity with job results
335          ICollection<JobResult> results =
336            ResultsAdapter.GetResultsOf(job);
337
338          foreach (JobResult result in results) {
339            ResultsAdapter.Delete(result);
340          }
341
342          return base.Delete(job);
343        }
344      }
345
346      return false;
347    }
348    #endregion
349  }
350}
Note: See TracBrowser for help on using the repository browser.