Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated Job adapter (#372)

File size: 12.2 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;
33using HeuristicLab.Hive.Server.ADODataAccess.TableAdapterWrapper;
34
35namespace HeuristicLab.Hive.Server.ADODataAccess {
36  class JobAdapter :
37    DataAdapterBase<dsHiveServerTableAdapters.JobTableAdapter,
38                      Job,
39                      dsHiveServer.JobRow>,
40    IJobAdapter {
41    #region Fields
42    private ManyToManyRelationHelper<
43      dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
44      dsHiveServer.RequiredPluginsRow> pluginsManyToManyRelationHelper = null;
45
46    private ManyToManyRelationHelper<
47      dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
48      dsHiveServer.RequiredPluginsRow> PluginsManyToManyRelationHelper {
49      get {
50        if (pluginsManyToManyRelationHelper == null) {
51          pluginsManyToManyRelationHelper =
52            new ManyToManyRelationHelper<dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
53              dsHiveServer.RequiredPluginsRow>(new RequiredPluginsAdapterWrapper(), 1);
54        }
55
56        pluginsManyToManyRelationHelper.Session = Session as Session;
57
58        return pluginsManyToManyRelationHelper;
59      }
60    }
61
62    private ManyToManyRelationHelper<
63      dsHiveServerTableAdapters.AssignedResourcesTableAdapter,
64      dsHiveServer.AssignedResourcesRow> assignedManyToManyRelationHelper = null;
65
66    private ManyToManyRelationHelper<
67      dsHiveServerTableAdapters.AssignedResourcesTableAdapter,
68      dsHiveServer.AssignedResourcesRow> AssignedManyToManyRelationHelper {
69      get {
70        if (assignedManyToManyRelationHelper == null) {
71          assignedManyToManyRelationHelper =
72            new ManyToManyRelationHelper<dsHiveServerTableAdapters.AssignedResourcesTableAdapter,
73              dsHiveServer.AssignedResourcesRow>(new AssignedResourcesAdapterWrapper(), 0);
74        }
75
76        assignedManyToManyRelationHelper.Session = Session as Session;
77
78        return assignedManyToManyRelationHelper;
79      }
80    }
81
82    private IClientAdapter clientAdapter = null;
83
84    private IClientAdapter ClientAdapter {
85      get {
86        if (clientAdapter == null)
87          clientAdapter =
88            this.Session.GetDataAdapter<ClientInfo, IClientAdapter>();
89
90        return clientAdapter;
91      }
92    }
93
94    private IJobResultsAdapter resultsAdapter = null;
95
96    private IJobResultsAdapter ResultsAdapter {
97      get {
98        if (resultsAdapter == null) {
99          resultsAdapter =
100            this.Session.GetDataAdapter<JobResult, IJobResultsAdapter>();
101        }
102
103        return resultsAdapter;
104      }
105    }
106
107    private IPluginInfoAdapter pluginInfoAdapter = null;
108
109    private IPluginInfoAdapter PluginInfoAdapter {
110      get {
111        if (pluginInfoAdapter == null) {
112          pluginInfoAdapter =
113            this.Session.GetDataAdapter<HivePluginInfo, IPluginInfoAdapter>();
114        }
115
116        return pluginInfoAdapter;
117      }
118    }
119
120    private IProjectAdapter projectAdapter = null;
121
122    private IProjectAdapter ProjectAdapter {
123      get {
124        if (projectAdapter == null) {
125          projectAdapter =
126            this.Session.GetDataAdapter<Project, IProjectAdapter>();
127        }
128
129        return projectAdapter;
130      }
131    }
132    #endregion
133
134    public JobAdapter(): base(new JobAdapterWrapper()) {
135    }
136
137    #region Overrides
138    protected override Job ConvertRow(dsHiveServer.JobRow row,
139      Job job) {
140      if (row != null && job != null) {
141        job.Id = row.JobId;
142
143        if (!row.IsParentJobIdNull())
144          job.ParentJob = GetById(row.ParentJobId);
145        else
146          job.ParentJob = null;
147
148        if (!row.IsResourceIdNull())
149          job.Client = ClientAdapter.GetById(row.ResourceId);
150        else
151          job.Client = null;
152
153        if (!row.IsUserIdNull())
154          job.UserId = Guid.Empty;
155        else
156          job.UserId = Guid.Empty;
157       
158        if (!row.IsJobStateNull())
159          job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
160        else
161          job.State = State.nullState;
162
163        if (!row.IsPercentageNull())
164          job.Percentage = row.Percentage;
165        else
166          job.Percentage = 0.0;
167
168        if (!row.IsSerializedJobNull())
169          job.SerializedJob = row.SerializedJob;
170        else
171          job.SerializedJob = null;
172
173        if (!row.IsDateCreatedNull())
174          job.DateCreated = row.DateCreated;
175        else
176          job.DateCreated = DateTime.MinValue;
177
178        if (!row.IsDateCalculatedNull())
179          job.DateCalculated = row.DateCalculated;
180        else
181          job.DateCalculated = DateTime.MinValue;
182
183        if (!row.IsPriorityNull())
184          job.Priority = row.Priority;
185        else
186          job.Priority = default(int);
187
188        if (!row.IsCoresNeededNull())
189          job.CoresNeeded = row.CoresNeeded;
190        else
191          job.CoresNeeded = default(int);
192
193        if (!row.IsMemoryNeededNull())
194          job.MemoryNeeded = row.MemoryNeeded;
195        else
196          job.MemoryNeeded = default(int);
197
198        if (!row.IsProjectIdNull())
199          job.Project = ProjectAdapter.GetById(
200            row.ProjectId);
201        else
202          job.Project = null;
203
204        ICollection<Guid> requiredPlugins =
205          PluginsManyToManyRelationHelper.GetRelationships(job.Id);
206       
207        job.PluginsNeeded.Clear();
208        foreach (Guid requiredPlugin in requiredPlugins) {
209          HivePluginInfo pluginInfo =
210            PluginInfoAdapter.GetById(requiredPlugin);
211
212          job.PluginsNeeded.Add(pluginInfo);
213        }
214
215        job.AssignedResourceIds =
216          new List<Guid>(
217            AssignedManyToManyRelationHelper.GetRelationships(job.Id));
218
219        return job;
220      } else
221        return null;
222    }
223
224    protected override dsHiveServer.JobRow ConvertObj(Job job,
225      dsHiveServer.JobRow row) {
226      if (job != null && row != null) {
227        row.JobId = job.Id;
228       
229        if (job.Client != null) {
230          if (row.IsResourceIdNull()) {
231            row.ResourceId = job.Client.Id;
232          }
233        } else
234          row.SetResourceIdNull();
235
236        if (job.ParentJob != null) {
237          if (row.IsParentJobIdNull()) {
238            row.ParentJobId = job.ParentJob.Id;
239          }
240        } else
241          row.SetParentJobIdNull();
242
243        if (job.UserId != Guid.Empty) {
244          if (row.IsUserIdNull() ||
245           row.UserId != Guid.Empty) {
246            row.UserId = Guid.Empty;
247          }
248        } else
249          row.SetUserIdNull();
250
251        if (job.State != State.nullState)
252          row.JobState = job.State.ToString();
253        else
254          row.SetJobStateNull();
255
256        row.Percentage = job.Percentage;
257
258        row.SerializedJob = job.SerializedJob;
259
260        if (job.DateCreated != DateTime.MinValue)
261          row.DateCreated = job.DateCreated;
262        else
263          row.SetDateCreatedNull();
264
265        if (job.DateCalculated != DateTime.MinValue)
266          row.DateCalculated = job.DateCalculated;
267        else
268          row.SetDateCalculatedNull();
269
270        row.Priority = job.Priority;
271
272        row.CoresNeeded = job.CoresNeeded;
273
274        row.MemoryNeeded = job.MemoryNeeded;
275
276        if (job.Project != null)
277          row.ProjectId = job.Project.Id;
278        else
279          row.SetProjectIdNull();
280      }
281
282      return row;
283    }
284    #endregion
285
286    #region IJobAdapter Members
287    public ICollection<Job> GetAllSubjobs(Job job) {
288      if (job != null) {
289        return
290          base.FindMultiple(
291            delegate() {
292              return Adapter.GetDataByParentJob(job.Id);
293            });
294      }
295
296      return null;
297    }
298
299    public ICollection<Job> GetJobsByState(State state) {
300      return
301         base.FindMultiple(
302           delegate() {
303             return Adapter.GetDataByState(state.ToString());
304           });
305    }
306
307    public ICollection<Job> GetJobsOf(ClientInfo client) {
308      if (client != null) {
309        return
310          base.FindMultiple(
311            delegate() {
312              return Adapter.GetDataByClient(client.Id);
313            });
314      }
315
316      return null;
317    }
318
319    public ICollection<Job> GetActiveJobsOf(ClientInfo client) {
320
321      if (client != null) {
322        return
323          base.FindMultiple(
324            delegate() {
325              return Adapter.GetDataByCalculatingClient(client.Id);
326            });
327      }
328
329      return null;
330    }
331
332    public ICollection<Job> GetJobsOf(Guid userId) {     
333      return
334          base.FindMultiple(
335            delegate() {
336              return Adapter.GetDataByUser(userId);
337            });
338    }
339
340    public ICollection<Job> FindJobs(State state, int cores, int memory) {
341      return
342         base.FindMultiple(
343           delegate() {
344             return Adapter.GetDataByStateCoresMemory(state.ToString(), cores, memory);
345           });
346    }
347
348    public ICollection<Job> GetJobsByProject(Guid projectId) {
349      return
350         base.FindMultiple(
351           delegate() {
352             return Adapter.GetDataByProjectId(projectId);
353           });
354    }
355
356    protected override void doUpdate(Job obj) {
357      if (obj != null) {
358        ProjectAdapter.Update(obj.Project);
359        ClientAdapter.Update(obj.Client);
360        Update(obj.ParentJob);
361
362        base.doUpdate(obj);
363
364        //update relationships
365        List<Guid> relationships =
366          new List<Guid>();
367        foreach (HivePluginInfo pluginInfo in obj.PluginsNeeded) {
368          //first check if pluginInfo already exists in the db
369          HivePluginInfo found = PluginInfoAdapter.GetByNameVersionBuilddate(
370            pluginInfo.Name, pluginInfo.Version, pluginInfo.BuildDate);
371          if (found != null) {
372            pluginInfo.Id = found.Id;
373          }
374
375          PluginInfoAdapter.Update(pluginInfo);
376          relationships.Add(pluginInfo.Id);
377        }
378
379        PluginsManyToManyRelationHelper.UpdateRelationships(
380          obj.Id, relationships);
381
382        AssignedManyToManyRelationHelper.UpdateRelationships(
383          obj.Id, obj.AssignedResourceIds);
384      }
385    }
386
387    protected override bool doDelete(Job job) {
388      if (job != null) {
389        dsHiveServer.JobRow row =
390          GetRowById(job.Id);
391
392        if (row != null) {
393          //Referential integrity with job results
394          ICollection<JobResult> results =
395            ResultsAdapter.GetResultsOf(job);
396
397          foreach (JobResult result in results) {
398            ResultsAdapter.Delete(result);
399          }
400
401          //delete all relationships
402          PluginsManyToManyRelationHelper.UpdateRelationships(job.Id,
403            new List<Guid>());
404
405          //delete orphaned pluginInfos
406          ICollection<HivePluginInfo> orphanedPluginInfos =
407             PluginInfoAdapter.GetOrphanedPluginInfos();
408          foreach(HivePluginInfo orphanedPlugin in orphanedPluginInfos) {
409            PluginInfoAdapter.Delete(orphanedPlugin);
410          }
411
412          return base.doDelete(job);
413        }
414      }
415
416      return false;
417    }
418
419    #endregion
420  }
421}
Note: See TracBrowser for help on using the repository browser.