Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated Job adapter (#372)

File size: 12.2 KB
RevLine 
[971]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;
[1377]26using HeuristicLab.Hive.Server.DataAccess;
[971]27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using System.Linq.Expressions;
[1377]29using HeuristicLab.DataAccess.ADOHelper;
[1468]30using HeuristicLab.Hive.Server.ADODataAccess.dsHiveServerTableAdapters;
31using System.Data.Common;
32using System.Data.SqlClient;
[1580]33using HeuristicLab.Hive.Server.ADODataAccess.TableAdapterWrapper;
[971]34
35namespace HeuristicLab.Hive.Server.ADODataAccess {
[995]36  class JobAdapter :
[1468]37    DataAdapterBase<dsHiveServerTableAdapters.JobTableAdapter,
[995]38                      Job,
[1468]39                      dsHiveServer.JobRow>,
[995]40    IJobAdapter {
41    #region Fields
[1580]42    private ManyToManyRelationHelper<
43      dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
[1947]44      dsHiveServer.RequiredPluginsRow> pluginsManyToManyRelationHelper = null;
[1580]45
46    private ManyToManyRelationHelper<
47      dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
[1947]48      dsHiveServer.RequiredPluginsRow> PluginsManyToManyRelationHelper {
[1580]49      get {
[1947]50        if (pluginsManyToManyRelationHelper == null) {
51          pluginsManyToManyRelationHelper =
[1580]52            new ManyToManyRelationHelper<dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
[1758]53              dsHiveServer.RequiredPluginsRow>(new RequiredPluginsAdapterWrapper(), 1);
[1580]54        }
55
[1947]56        pluginsManyToManyRelationHelper.Session = Session as Session;
[1580]57
[1947]58        return pluginsManyToManyRelationHelper;
[1580]59      }
60    }
61
[1947]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
[971]82    private IClientAdapter clientAdapter = null;
83
84    private IClientAdapter ClientAdapter {
85      get {
86        if (clientAdapter == null)
[1468]87          clientAdapter =
88            this.Session.GetDataAdapter<ClientInfo, IClientAdapter>();
[971]89
90        return clientAdapter;
91      }
92    }
93
[1005]94    private IJobResultsAdapter resultsAdapter = null;
95
96    private IJobResultsAdapter ResultsAdapter {
97      get {
98        if (resultsAdapter == null) {
[1468]99          resultsAdapter =
100            this.Session.GetDataAdapter<JobResult, IJobResultsAdapter>();
[1005]101        }
102
103        return resultsAdapter;
104      }
105    }
[1580]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    }
[1947]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    }
[995]132    #endregion
[971]133
[1468]134    public JobAdapter(): base(new JobAdapterWrapper()) {
135    }
136
[995]137    #region Overrides
[1131]138    protected override Job ConvertRow(dsHiveServer.JobRow row,
[971]139      Job job) {
140      if (row != null && job != null) {
[995]141        job.Id = row.JobId;
[971]142
143        if (!row.IsParentJobIdNull())
[995]144          job.ParentJob = GetById(row.ParentJobId);
[971]145        else
[975]146          job.ParentJob = null;
[971]147
148        if (!row.IsResourceIdNull())
[995]149          job.Client = ClientAdapter.GetById(row.ResourceId);
[971]150        else
151          job.Client = null;
[1161]152
[1449]153        if (!row.IsUserIdNull())
[1377]154          job.UserId = Guid.Empty;
[1161]155        else
[1377]156          job.UserId = Guid.Empty;
[971]157       
[1092]158        if (!row.IsJobStateNull())
159          job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
[971]160        else
[995]161          job.State = State.nullState;
[971]162
[1092]163        if (!row.IsPercentageNull())
164          job.Percentage = row.Percentage;
165        else
166          job.Percentage = 0.0;
167
[1120]168        if (!row.IsSerializedJobNull())
169          job.SerializedJob = row.SerializedJob;
170        else
171          job.SerializedJob = null;
172
[1169]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
[1175]183        if (!row.IsPriorityNull())
184          job.Priority = row.Priority;
185        else
186          job.Priority = default(int);
[1169]187
[1505]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
[1947]198        if (!row.IsProjectIdNull())
199          job.Project = ProjectAdapter.GetById(
200            row.ProjectId);
201        else
202          job.Project = null;
203
[1580]204        ICollection<Guid> requiredPlugins =
[1947]205          PluginsManyToManyRelationHelper.GetRelationships(job.Id);
[1580]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
[1947]215        job.AssignedResourceIds =
216          new List<Guid>(
217            AssignedManyToManyRelationHelper.GetRelationships(job.Id));
218
[971]219        return job;
220      } else
221        return null;
222    }
223
[1131]224    protected override dsHiveServer.JobRow ConvertObj(Job job,
[971]225      dsHiveServer.JobRow row) {
226      if (job != null && row != null) {
[1449]227        row.JobId = job.Id;
228       
[971]229        if (job.Client != null) {
[1952]230          if (row.IsResourceIdNull()) {
[1021]231            row.ResourceId = job.Client.Id;
232          }
[971]233        } else
234          row.SetResourceIdNull();
235
[975]236        if (job.ParentJob != null) {
[1952]237          if (row.IsParentJobIdNull()) {
[1021]238            row.ParentJobId = job.ParentJob.Id;
239          }
[971]240        } else
241          row.SetParentJobIdNull();
242
[1377]243        if (job.UserId != Guid.Empty) {
[1449]244          if (row.IsUserIdNull() ||
245           row.UserId != Guid.Empty) {
246            row.UserId = Guid.Empty;
[1161]247          }
248        } else
[1449]249          row.SetUserIdNull();
[1161]250
[995]251        if (job.State != State.nullState)
[1092]252          row.JobState = job.State.ToString();
[995]253        else
[1092]254          row.SetJobStateNull();
255
256        row.Percentage = job.Percentage;
[1120]257
258        row.SerializedJob = job.SerializedJob;
[1169]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;
[1505]271
272        row.CoresNeeded = job.CoresNeeded;
273
274        row.MemoryNeeded = job.MemoryNeeded;
[1947]275
276        if (job.Project != null)
277          row.ProjectId = job.Project.Id;
278        else
279          row.SetProjectIdNull();
[971]280      }
281
282      return row;
283    }
[995]284    #endregion
285
286    #region IJobAdapter Members
[971]287    public ICollection<Job> GetAllSubjobs(Job job) {
288      if (job != null) {
[995]289        return
290          base.FindMultiple(
291            delegate() {
[1449]292              return Adapter.GetDataByParentJob(job.Id);
[995]293            });
[971]294      }
295
[995]296      return null;
[971]297    }
298
[998]299    public ICollection<Job> GetJobsByState(State state) {
300      return
301         base.FindMultiple(
302           delegate() {
[1131]303             return Adapter.GetDataByState(state.ToString());
[998]304           });
305    }
306
[971]307    public ICollection<Job> GetJobsOf(ClientInfo client) {
308      if (client != null) {
[995]309        return
310          base.FindMultiple(
311            delegate() {
[1131]312              return Adapter.GetDataByClient(client.Id);
[995]313            });
[971]314      }
315
[995]316      return null;
[971]317    }
318
[1166]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
[1468]332    public ICollection<Job> GetJobsOf(Guid userId) {     
[1377]333      return
[995]334          base.FindMultiple(
335            delegate() {
[1449]336              return Adapter.GetDataByUser(userId);
[995]337            });
[971]338    }
[1005]339
[1508]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
[1944]348    public ICollection<Job> GetJobsByProject(Guid projectId) {
[1947]349      return
350         base.FindMultiple(
351           delegate() {
352             return Adapter.GetDataByProjectId(projectId);
353           });
[1944]354    }
355
[1580]356    protected override void doUpdate(Job obj) {
[1955]357      if (obj != null) {
358        ProjectAdapter.Update(obj.Project);
359        ClientAdapter.Update(obj.Client);
360        Update(obj.ParentJob);
[1952]361
[1955]362        base.doUpdate(obj);
[1580]363
[1955]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);
[1592]377        }
378
[1955]379        PluginsManyToManyRelationHelper.UpdateRelationships(
380          obj.Id, relationships);
381
382        AssignedManyToManyRelationHelper.UpdateRelationships(
383          obj.Id, obj.AssignedResourceIds);
[1580]384      }
385    }
386
[1468]387    protected override bool doDelete(Job job) {
[1005]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
[1580]401          //delete all relationships
[1947]402          PluginsManyToManyRelationHelper.UpdateRelationships(job.Id,
[1759]403            new List<Guid>());
[1580]404
[1592]405          //delete orphaned pluginInfos
406          ICollection<HivePluginInfo> orphanedPluginInfos =
407             PluginInfoAdapter.GetOrphanedPluginInfos();
408          foreach(HivePluginInfo orphanedPlugin in orphanedPluginInfos) {
409            PluginInfoAdapter.Delete(orphanedPlugin);
410          }
411
[1468]412          return base.doDelete(job);
[1005]413        }
414      }
415
416      return false;
417    }
[1944]418
[971]419    #endregion
420  }
421}
Note: See TracBrowser for help on using the repository browser.