Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added assigned resources to job adapter (#372)

File size: 12.3 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            ClientAdapter.Update(job.Client);
233            row.ResourceId = job.Client.Id;
234          }
235        } else
236          row.SetResourceIdNull();
237
238        if (job.ParentJob != null) {
239          if (row.IsParentJobIdNull() ||
240            row.ParentJobId != job.ParentJob.Id) {
241            Update(job.ParentJob);
242            row.ParentJobId = job.ParentJob.Id;
243          }
244        } else
245          row.SetParentJobIdNull();
246
247        if (job.UserId != Guid.Empty) {
248          if (row.IsUserIdNull() ||
249           row.UserId != Guid.Empty) {
250            row.UserId = Guid.Empty;
251          }
252        } else
253          row.SetUserIdNull();
254
255        if (job.State != State.nullState)
256          row.JobState = job.State.ToString();
257        else
258          row.SetJobStateNull();
259
260        row.Percentage = job.Percentage;
261
262        row.SerializedJob = job.SerializedJob;
263
264        if (job.DateCreated != DateTime.MinValue)
265          row.DateCreated = job.DateCreated;
266        else
267          row.SetDateCreatedNull();
268
269        if (job.DateCalculated != DateTime.MinValue)
270          row.DateCalculated = job.DateCalculated;
271        else
272          row.SetDateCalculatedNull();
273
274        row.Priority = job.Priority;
275
276        row.CoresNeeded = job.CoresNeeded;
277
278        row.MemoryNeeded = job.MemoryNeeded;
279
280        if (job.Project != null)
281          row.ProjectId = job.Project.Id;
282        else
283          row.SetProjectIdNull();
284      }
285
286      return row;
287    }
288    #endregion
289
290    #region IJobAdapter Members
291    public ICollection<Job> GetAllSubjobs(Job job) {
292      if (job != null) {
293        return
294          base.FindMultiple(
295            delegate() {
296              return Adapter.GetDataByParentJob(job.Id);
297            });
298      }
299
300      return null;
301    }
302
303    public ICollection<Job> GetJobsByState(State state) {
304      return
305         base.FindMultiple(
306           delegate() {
307             return Adapter.GetDataByState(state.ToString());
308           });
309    }
310
311    public ICollection<Job> GetJobsOf(ClientInfo client) {
312      if (client != null) {
313        return
314          base.FindMultiple(
315            delegate() {
316              return Adapter.GetDataByClient(client.Id);
317            });
318      }
319
320      return null;
321    }
322
323    public ICollection<Job> GetActiveJobsOf(ClientInfo client) {
324
325      if (client != null) {
326        return
327          base.FindMultiple(
328            delegate() {
329              return Adapter.GetDataByCalculatingClient(client.Id);
330            });
331      }
332
333      return null;
334    }
335
336    public ICollection<Job> GetJobsOf(Guid userId) {     
337      return
338          base.FindMultiple(
339            delegate() {
340              return Adapter.GetDataByUser(userId);
341            });
342    }
343
344    public ICollection<Job> FindJobs(State state, int cores, int memory) {
345      return
346         base.FindMultiple(
347           delegate() {
348             return Adapter.GetDataByStateCoresMemory(state.ToString(), cores, memory);
349           });
350    }
351
352    public ICollection<Job> GetJobsByProject(Guid projectId) {
353      return
354         base.FindMultiple(
355           delegate() {
356             return Adapter.GetDataByProjectId(projectId);
357           });
358    }
359
360    protected override void doUpdate(Job obj) {
361      ProjectAdapter.Update(obj.Project);
362     
363      base.doUpdate(obj);
364
365      //update relationships
366      List<Guid> relationships =
367        new List<Guid>();
368      foreach (HivePluginInfo pluginInfo in obj.PluginsNeeded) {
369        //first check if pluginInfo already exists in the db
370        HivePluginInfo found = PluginInfoAdapter.GetByNameVersionBuilddate(
371          pluginInfo.Name, pluginInfo.Version, pluginInfo.BuildDate);
372        if (found != null) {
373          pluginInfo.Id = found.Id;
374        }
375
376        PluginInfoAdapter.Update(pluginInfo);
377        relationships.Add(pluginInfo.Id);
378      }
379
380      PluginsManyToManyRelationHelper.UpdateRelationships(
381        obj.Id, relationships);
382
383      AssignedManyToManyRelationHelper.UpdateRelationships(
384        obj.Id, obj.AssignedResourceIds);
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.