Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1807 was 1759, checked in by svonolfe, 15 years ago

Small refactoring of the manyToManyRelationHelper 2 (#527)

File size: 10.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,
44      dsHiveServer.RequiredPluginsRow> manyToManyRelationHelper = null;
45
46    private ManyToManyRelationHelper<
47      dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
48      dsHiveServer.RequiredPluginsRow> ManyToManyRelationHelper {
49      get {
50        if (manyToManyRelationHelper == null) {
51          manyToManyRelationHelper =
52            new ManyToManyRelationHelper<dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
[1758]53              dsHiveServer.RequiredPluginsRow>(new RequiredPluginsAdapterWrapper(), 1);
[1580]54        }
55
56        manyToManyRelationHelper.Session = Session as Session;
57
58        return manyToManyRelationHelper;
59      }
60    }
61
[971]62    private IClientAdapter clientAdapter = null;
63
64    private IClientAdapter ClientAdapter {
65      get {
66        if (clientAdapter == null)
[1468]67          clientAdapter =
68            this.Session.GetDataAdapter<ClientInfo, IClientAdapter>();
[971]69
70        return clientAdapter;
71      }
72    }
73
[1005]74    private IJobResultsAdapter resultsAdapter = null;
75
76    private IJobResultsAdapter ResultsAdapter {
77      get {
78        if (resultsAdapter == null) {
[1468]79          resultsAdapter =
80            this.Session.GetDataAdapter<JobResult, IJobResultsAdapter>();
[1005]81        }
82
83        return resultsAdapter;
84      }
85    }
[1580]86
87    private IPluginInfoAdapter pluginInfoAdapter = null;
88
89    private IPluginInfoAdapter PluginInfoAdapter {
90      get {
91        if (pluginInfoAdapter == null) {
92          pluginInfoAdapter =
93            this.Session.GetDataAdapter<HivePluginInfo, IPluginInfoAdapter>();
94        }
95
96        return pluginInfoAdapter;
97      }
98    }
[995]99    #endregion
[971]100
[1468]101    public JobAdapter(): base(new JobAdapterWrapper()) {
102    }
103
[995]104    #region Overrides
[1131]105    protected override Job ConvertRow(dsHiveServer.JobRow row,
[971]106      Job job) {
107      if (row != null && job != null) {
[995]108        job.Id = row.JobId;
[971]109
110        if (!row.IsParentJobIdNull())
[995]111          job.ParentJob = GetById(row.ParentJobId);
[971]112        else
[975]113          job.ParentJob = null;
[971]114
115        if (!row.IsResourceIdNull())
[995]116          job.Client = ClientAdapter.GetById(row.ResourceId);
[971]117        else
118          job.Client = null;
[1161]119
[1449]120        if (!row.IsUserIdNull())
[1377]121          job.UserId = Guid.Empty;
[1161]122        else
[1377]123          job.UserId = Guid.Empty;
[971]124       
[1092]125        if (!row.IsJobStateNull())
126          job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
[971]127        else
[995]128          job.State = State.nullState;
[971]129
[1092]130        if (!row.IsPercentageNull())
131          job.Percentage = row.Percentage;
132        else
133          job.Percentage = 0.0;
134
[1120]135        if (!row.IsSerializedJobNull())
136          job.SerializedJob = row.SerializedJob;
137        else
138          job.SerializedJob = null;
139
[1169]140        if (!row.IsDateCreatedNull())
141          job.DateCreated = row.DateCreated;
142        else
143          job.DateCreated = DateTime.MinValue;
144
145        if (!row.IsDateCalculatedNull())
146          job.DateCalculated = row.DateCalculated;
147        else
148          job.DateCalculated = DateTime.MinValue;
149
[1175]150        if (!row.IsPriorityNull())
151          job.Priority = row.Priority;
152        else
153          job.Priority = default(int);
[1169]154
[1505]155        if (!row.IsCoresNeededNull())
156          job.CoresNeeded = row.CoresNeeded;
157        else
158          job.CoresNeeded = default(int);
159
160        if (!row.IsMemoryNeededNull())
161          job.MemoryNeeded = row.MemoryNeeded;
162        else
163          job.MemoryNeeded = default(int);
164
[1580]165        ICollection<Guid> requiredPlugins =
[1758]166          ManyToManyRelationHelper.GetRelationships(job.Id);
[1580]167       
168        job.PluginsNeeded.Clear();
169        foreach (Guid requiredPlugin in requiredPlugins) {
170          HivePluginInfo pluginInfo =
171            PluginInfoAdapter.GetById(requiredPlugin);
172
173          job.PluginsNeeded.Add(pluginInfo);
174        }
175
[971]176        return job;
177      } else
178        return null;
179    }
180
[1131]181    protected override dsHiveServer.JobRow ConvertObj(Job job,
[971]182      dsHiveServer.JobRow row) {
183      if (job != null && row != null) {
[1449]184        row.JobId = job.Id;
185       
[971]186        if (job.Client != null) {
[1021]187          if (row.IsResourceIdNull() ||
188            row.ResourceId != job.Client.Id) {
189            ClientAdapter.Update(job.Client);
190            row.ResourceId = job.Client.Id;
191          }
[971]192        } else
193          row.SetResourceIdNull();
194
[975]195        if (job.ParentJob != null) {
[1021]196          if (row.IsParentJobIdNull() ||
197            row.ParentJobId != job.ParentJob.Id) {
198            Update(job.ParentJob);
199            row.ParentJobId = job.ParentJob.Id;
200          }
[971]201        } else
202          row.SetParentJobIdNull();
203
[1377]204        if (job.UserId != Guid.Empty) {
[1449]205          if (row.IsUserIdNull() ||
206           row.UserId != Guid.Empty) {
207            row.UserId = Guid.Empty;
[1161]208          }
209        } else
[1449]210          row.SetUserIdNull();
[1161]211
[995]212        if (job.State != State.nullState)
[1092]213          row.JobState = job.State.ToString();
[995]214        else
[1092]215          row.SetJobStateNull();
216
217        row.Percentage = job.Percentage;
[1120]218
219        row.SerializedJob = job.SerializedJob;
[1169]220
221        if (job.DateCreated != DateTime.MinValue)
222          row.DateCreated = job.DateCreated;
223        else
224          row.SetDateCreatedNull();
225
226        if (job.DateCalculated != DateTime.MinValue)
227          row.DateCalculated = job.DateCalculated;
228        else
229          row.SetDateCalculatedNull();
230
231        row.Priority = job.Priority;
[1505]232
233        row.CoresNeeded = job.CoresNeeded;
234
235        row.MemoryNeeded = job.MemoryNeeded;
[971]236      }
237
238      return row;
239    }
[995]240    #endregion
241
242    #region IJobAdapter Members
[971]243    public ICollection<Job> GetAllSubjobs(Job job) {
244      if (job != null) {
[995]245        return
246          base.FindMultiple(
247            delegate() {
[1449]248              return Adapter.GetDataByParentJob(job.Id);
[995]249            });
[971]250      }
251
[995]252      return null;
[971]253    }
254
[998]255    public ICollection<Job> GetJobsByState(State state) {
256      return
257         base.FindMultiple(
258           delegate() {
[1131]259             return Adapter.GetDataByState(state.ToString());
[998]260           });
261    }
262
[971]263    public ICollection<Job> GetJobsOf(ClientInfo client) {
264      if (client != null) {
[995]265        return
266          base.FindMultiple(
267            delegate() {
[1131]268              return Adapter.GetDataByClient(client.Id);
[995]269            });
[971]270      }
271
[995]272      return null;
[971]273    }
274
[1166]275    public ICollection<Job> GetActiveJobsOf(ClientInfo client) {
276
277      if (client != null) {
278        return
279          base.FindMultiple(
280            delegate() {
281              return Adapter.GetDataByCalculatingClient(client.Id);
282            });
283      }
284
285      return null;
286    }
287
[1468]288    public ICollection<Job> GetJobsOf(Guid userId) {     
[1377]289      return
[995]290          base.FindMultiple(
291            delegate() {
[1449]292              return Adapter.GetDataByUser(userId);
[995]293            });
[971]294    }
[1005]295
[1508]296    public ICollection<Job> FindJobs(State state, int cores, int memory) {
297      return
298         base.FindMultiple(
299           delegate() {
300             return Adapter.GetDataByStateCoresMemory(state.ToString(), cores, memory);
301           });
302    }
303
[1580]304    protected override void doUpdate(Job obj) {
305      base.doUpdate(obj);
306
307      //update relationships
308      List<Guid> relationships =
309        new List<Guid>();
310      foreach (HivePluginInfo pluginInfo in obj.PluginsNeeded) {
[1592]311        //first check if pluginInfo already exists in the db
312        HivePluginInfo found = PluginInfoAdapter.GetByNameVersionBuilddate(
313          pluginInfo.Name, pluginInfo.Version, pluginInfo.BuildDate);
314        if (found != null) {
315          pluginInfo.Id = found.Id;
316        }
317
[1580]318        PluginInfoAdapter.Update(pluginInfo);
319        relationships.Add(pluginInfo.Id);
320      }
321
322      ManyToManyRelationHelper.UpdateRelationships(
[1759]323        obj.Id, relationships);
[1580]324    }
325
[1468]326    protected override bool doDelete(Job job) {
[1005]327      if (job != null) {
328        dsHiveServer.JobRow row =
329          GetRowById(job.Id);
330
331        if (row != null) {
332          //Referential integrity with job results
333          ICollection<JobResult> results =
334            ResultsAdapter.GetResultsOf(job);
335
336          foreach (JobResult result in results) {
337            ResultsAdapter.Delete(result);
338          }
339
[1580]340          //delete all relationships
341          ManyToManyRelationHelper.UpdateRelationships(job.Id,
[1759]342            new List<Guid>());
[1580]343
[1592]344          //delete orphaned pluginInfos
345          ICollection<HivePluginInfo> orphanedPluginInfos =
346             PluginInfoAdapter.GetOrphanedPluginInfos();
347          foreach(HivePluginInfo orphanedPlugin in orphanedPluginInfos) {
348            PluginInfoAdapter.Delete(orphanedPlugin);
349          }
350
[1468]351          return base.doDelete(job);
[1005]352        }
353      }
354
355      return false;
356    }
[971]357    #endregion
358  }
359}
Note: See TracBrowser for help on using the repository browser.