Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3580 was 3011, checked in by kgrading, 15 years ago

changed the complete DAL to LINQ 2 SQL (with the exception of the job streaming), did a lot of refactoring, Introduced DTOs (that are named DTOs for better understanding), added the spring.NET Interceptor, reintroduced transactions and cleaned up the whole JobResult thing and updated a part of the config merger (#830)

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