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
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) {
[1021]230          if (row.IsResourceIdNull() ||
231            row.ResourceId != job.Client.Id) {
232            ClientAdapter.Update(job.Client);
233            row.ResourceId = job.Client.Id;
234          }
[971]235        } else
236          row.SetResourceIdNull();
237
[975]238        if (job.ParentJob != null) {
[1021]239          if (row.IsParentJobIdNull() ||
240            row.ParentJobId != job.ParentJob.Id) {
241            Update(job.ParentJob);
242            row.ParentJobId = job.ParentJob.Id;
243          }
[971]244        } else
245          row.SetParentJobIdNull();
246
[1377]247        if (job.UserId != Guid.Empty) {
[1449]248          if (row.IsUserIdNull() ||
249           row.UserId != Guid.Empty) {
250            row.UserId = Guid.Empty;
[1161]251          }
252        } else
[1449]253          row.SetUserIdNull();
[1161]254
[995]255        if (job.State != State.nullState)
[1092]256          row.JobState = job.State.ToString();
[995]257        else
[1092]258          row.SetJobStateNull();
259
260        row.Percentage = job.Percentage;
[1120]261
262        row.SerializedJob = job.SerializedJob;
[1169]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;
[1505]275
276        row.CoresNeeded = job.CoresNeeded;
277
278        row.MemoryNeeded = job.MemoryNeeded;
[1947]279
280        if (job.Project != null)
281          row.ProjectId = job.Project.Id;
282        else
283          row.SetProjectIdNull();
[971]284      }
285
286      return row;
287    }
[995]288    #endregion
289
290    #region IJobAdapter Members
[971]291    public ICollection<Job> GetAllSubjobs(Job job) {
292      if (job != null) {
[995]293        return
294          base.FindMultiple(
295            delegate() {
[1449]296              return Adapter.GetDataByParentJob(job.Id);
[995]297            });
[971]298      }
299
[995]300      return null;
[971]301    }
302
[998]303    public ICollection<Job> GetJobsByState(State state) {
304      return
305         base.FindMultiple(
306           delegate() {
[1131]307             return Adapter.GetDataByState(state.ToString());
[998]308           });
309    }
310
[971]311    public ICollection<Job> GetJobsOf(ClientInfo client) {
312      if (client != null) {
[995]313        return
314          base.FindMultiple(
315            delegate() {
[1131]316              return Adapter.GetDataByClient(client.Id);
[995]317            });
[971]318      }
319
[995]320      return null;
[971]321    }
322
[1166]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
[1468]336    public ICollection<Job> GetJobsOf(Guid userId) {     
[1377]337      return
[995]338          base.FindMultiple(
339            delegate() {
[1449]340              return Adapter.GetDataByUser(userId);
[995]341            });
[971]342    }
[1005]343
[1508]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
[1944]352    public ICollection<Job> GetJobsByProject(Guid projectId) {
[1947]353      return
354         base.FindMultiple(
355           delegate() {
356             return Adapter.GetDataByProjectId(projectId);
357           });
[1944]358    }
359
[1580]360    protected override void doUpdate(Job obj) {
[1947]361      ProjectAdapter.Update(obj.Project);
362     
[1580]363      base.doUpdate(obj);
364
365      //update relationships
366      List<Guid> relationships =
367        new List<Guid>();
368      foreach (HivePluginInfo pluginInfo in obj.PluginsNeeded) {
[1592]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
[1580]376        PluginInfoAdapter.Update(pluginInfo);
377        relationships.Add(pluginInfo.Id);
378      }
379
[1947]380      PluginsManyToManyRelationHelper.UpdateRelationships(
[1759]381        obj.Id, relationships);
[1947]382
383      AssignedManyToManyRelationHelper.UpdateRelationships(
384        obj.Id, obj.AssignedResourceIds);
[1580]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.