Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1636 was 1592, checked in by svonolfe, 16 years ago

Updated PluginInfoAdapter, added ER model (#372)

File size: 10.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> 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,
53              dsHiveServer.RequiredPluginsRow>(new RequiredPluginsAdapterWrapper());
54        }
55
56        manyToManyRelationHelper.Session = Session as Session;
57
58        return manyToManyRelationHelper;
59      }
60    }
61
62    private IClientAdapter clientAdapter = null;
63
64    private IClientAdapter ClientAdapter {
65      get {
66        if (clientAdapter == null)
67          clientAdapter =
68            this.Session.GetDataAdapter<ClientInfo, IClientAdapter>();
69
70        return clientAdapter;
71      }
72    }
73
74    private IJobResultsAdapter resultsAdapter = null;
75
76    private IJobResultsAdapter ResultsAdapter {
77      get {
78        if (resultsAdapter == null) {
79          resultsAdapter =
80            this.Session.GetDataAdapter<JobResult, IJobResultsAdapter>();
81        }
82
83        return resultsAdapter;
84      }
85    }
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    }
99    #endregion
100
101    public JobAdapter(): base(new JobAdapterWrapper()) {
102    }
103
104    #region Overrides
105    protected override Job ConvertRow(dsHiveServer.JobRow row,
106      Job job) {
107      if (row != null && job != null) {
108        job.Id = row.JobId;
109
110        if (!row.IsParentJobIdNull())
111          job.ParentJob = GetById(row.ParentJobId);
112        else
113          job.ParentJob = null;
114
115        if (!row.IsResourceIdNull())
116          job.Client = ClientAdapter.GetById(row.ResourceId);
117        else
118          job.Client = null;
119
120        if (!row.IsUserIdNull())
121          job.UserId = Guid.Empty;
122        else
123          job.UserId = Guid.Empty;
124       
125        if (!row.IsJobStateNull())
126          job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
127        else
128          job.State = State.nullState;
129
130        if (!row.IsPercentageNull())
131          job.Percentage = row.Percentage;
132        else
133          job.Percentage = 0.0;
134
135        if (!row.IsSerializedJobNull())
136          job.SerializedJob = row.SerializedJob;
137        else
138          job.SerializedJob = null;
139
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
150        if (!row.IsPriorityNull())
151          job.Priority = row.Priority;
152        else
153          job.Priority = default(int);
154
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
165        ICollection<Guid> requiredPlugins =
166          ManyToManyRelationHelper.GetRelationships(job.Id);
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
176        return job;
177      } else
178        return null;
179    }
180
181    protected override dsHiveServer.JobRow ConvertObj(Job job,
182      dsHiveServer.JobRow row) {
183      if (job != null && row != null) {
184        row.JobId = job.Id;
185       
186        if (job.Client != null) {
187          if (row.IsResourceIdNull() ||
188            row.ResourceId != job.Client.Id) {
189            ClientAdapter.Update(job.Client);
190            row.ResourceId = job.Client.Id;
191          }
192        } else
193          row.SetResourceIdNull();
194
195        if (job.ParentJob != null) {
196          if (row.IsParentJobIdNull() ||
197            row.ParentJobId != job.ParentJob.Id) {
198            Update(job.ParentJob);
199            row.ParentJobId = job.ParentJob.Id;
200          }
201        } else
202          row.SetParentJobIdNull();
203
204        if (job.UserId != Guid.Empty) {
205          if (row.IsUserIdNull() ||
206           row.UserId != Guid.Empty) {
207            row.UserId = Guid.Empty;
208          }
209        } else
210          row.SetUserIdNull();
211
212        if (job.State != State.nullState)
213          row.JobState = job.State.ToString();
214        else
215          row.SetJobStateNull();
216
217        row.Percentage = job.Percentage;
218
219        row.SerializedJob = job.SerializedJob;
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;
232
233        row.CoresNeeded = job.CoresNeeded;
234
235        row.MemoryNeeded = job.MemoryNeeded;
236      }
237
238      return row;
239    }
240    #endregion
241
242    #region IJobAdapter Members
243    public ICollection<Job> GetAllSubjobs(Job job) {
244      if (job != null) {
245        return
246          base.FindMultiple(
247            delegate() {
248              return Adapter.GetDataByParentJob(job.Id);
249            });
250      }
251
252      return null;
253    }
254
255    public ICollection<Job> GetJobsByState(State state) {
256      return
257         base.FindMultiple(
258           delegate() {
259             return Adapter.GetDataByState(state.ToString());
260           });
261    }
262
263    public ICollection<Job> GetJobsOf(ClientInfo client) {
264      if (client != null) {
265        return
266          base.FindMultiple(
267            delegate() {
268              return Adapter.GetDataByClient(client.Id);
269            });
270      }
271
272      return null;
273    }
274
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
288    public ICollection<Job> GetJobsOf(Guid userId) {     
289      return
290          base.FindMultiple(
291            delegate() {
292              return Adapter.GetDataByUser(userId);
293            });
294    }
295
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
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) {
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
318        PluginInfoAdapter.Update(pluginInfo);
319        relationships.Add(pluginInfo.Id);
320      }
321
322      ManyToManyRelationHelper.UpdateRelationships(
323        obj.Id, relationships);
324    }
325
326    protected override bool doDelete(Job job) {
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
340          //delete all relationships
341          ManyToManyRelationHelper.UpdateRelationships(job.Id,
342            new List<Guid>());
343
344          //delete orphaned pluginInfos
345          ICollection<HivePluginInfo> orphanedPluginInfos =
346             PluginInfoAdapter.GetOrphanedPluginInfos();
347          foreach(HivePluginInfo orphanedPlugin in orphanedPluginInfos) {
348            PluginInfoAdapter.Delete(orphanedPlugin);
349          }
350
351          return base.doDelete(job);
352        }
353      }
354
355      return false;
356    }
357    #endregion
358  }
359}
Note: See TracBrowser for help on using the repository browser.