Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added job attributes to DB (#372)

File size: 10.0 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.Core.InternalInterfaces.DataAccess;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using System.Linq.Expressions;
29using System.Runtime.CompilerServices;
30
31namespace HeuristicLab.Hive.Server.ADODataAccess {
32  class JobAdapter :
33    CachedDataAdapter<dsHiveServerTableAdapters.JobTableAdapter,
34                      Job,
35                      dsHiveServer.JobRow,
36                      dsHiveServer.JobDataTable>,
37    IJobAdapter {
38    #region Fields
39    private IClientAdapter clientAdapter = null;
40
41    private IClientAdapter ClientAdapter {
42      get {
43        if (clientAdapter == null)
44          clientAdapter = ServiceLocator.GetClientAdapter();
45
46        return clientAdapter;
47      }
48    }
49
50    private IUserAdapter userAdapter = null;
51
52    private IUserAdapter UserAdapter {
53      get {
54        if (userAdapter == null) {
55          userAdapter = ServiceLocator.GetUserAdapter();
56        }
57
58        return userAdapter;
59      }
60    }
61
62    private IJobResultsAdapter resultsAdapter = null;
63
64    private IJobResultsAdapter ResultsAdapter {
65      get {
66        if (resultsAdapter == null) {
67          resultsAdapter = ServiceLocator.GetJobResultsAdapter();
68        }
69
70        return resultsAdapter;
71      }
72    }
73    #endregion
74
75    #region Overrides
76    protected override Job ConvertRow(dsHiveServer.JobRow row,
77      Job job) {
78      if (row != null && job != null) {
79        job.Id = row.JobId;
80
81        if (!row.IsParentJobIdNull())
82          job.ParentJob = GetById(row.ParentJobId);
83        else
84          job.ParentJob = null;
85
86        if (!row.IsResourceIdNull())
87          job.Client = ClientAdapter.GetById(row.ResourceId);
88        else
89          job.Client = null;
90
91        if (!row.IsPermissionOwnerIdNull())
92          job.User = UserAdapter.GetById(row.PermissionOwnerId);
93        else
94          job.User = null;
95       
96        if (!row.IsJobStateNull())
97          job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
98        else
99          job.State = State.nullState;
100
101        if (!row.IsPercentageNull())
102          job.Percentage = row.Percentage;
103        else
104          job.Percentage = 0.0;
105
106        if (!row.IsSerializedJobNull())
107          job.SerializedJob = row.SerializedJob;
108        else
109          job.SerializedJob = null;
110
111        if (!row.IsDateCreatedNull())
112          job.DateCreated = row.DateCreated;
113        else
114          job.DateCreated = DateTime.MinValue;
115
116        if (!row.IsDateCalculatedNull())
117          job.DateCalculated = row.DateCalculated;
118        else
119          job.DateCalculated = DateTime.MinValue;
120
121        job.Priority = row.Priority;
122
123        return job;
124      } else
125        return null;
126    }
127
128    protected override dsHiveServer.JobRow ConvertObj(Job job,
129      dsHiveServer.JobRow row) {
130      if (job != null && row != null) {
131        if (job.Client != null) {
132          if (row.IsResourceIdNull() ||
133            row.ResourceId != job.Client.Id) {
134            ClientAdapter.Update(job.Client);
135            row.ResourceId = job.Client.Id;
136          }
137        } else
138          row.SetResourceIdNull();
139
140        if (job.ParentJob != null) {
141          if (row.IsParentJobIdNull() ||
142            row.ParentJobId != job.ParentJob.Id) {
143            Update(job.ParentJob);
144            row.ParentJobId = job.ParentJob.Id;
145          }
146        } else
147          row.SetParentJobIdNull();
148
149        if (job.User != null) {
150          if (row.IsPermissionOwnerIdNull() ||
151           row.PermissionOwnerId != job.User.Id) {
152            UserAdapter.Update(job.User);
153            row.PermissionOwnerId = job.User.Id;
154          }
155        } else
156          row.SetPermissionOwnerIdNull();
157
158        if (job.State != State.nullState)
159          row.JobState = job.State.ToString();
160        else
161          row.SetJobStateNull();
162
163        row.Percentage = job.Percentage;
164
165        row.SerializedJob = job.SerializedJob;
166
167        if (job.DateCreated != DateTime.MinValue)
168          row.DateCreated = job.DateCreated;
169        else
170          row.SetDateCreatedNull();
171
172        if (job.DateCalculated != DateTime.MinValue)
173          row.DateCalculated = job.DateCalculated;
174        else
175          row.SetDateCalculatedNull();
176
177        row.Priority = job.Priority;
178      }
179
180      return row;
181    }
182
183    protected override void UpdateRow(dsHiveServer.JobRow row) {
184      Adapter.Update(row);
185    }
186
187    protected override dsHiveServer.JobRow
188      InsertNewRow(Job job) {
189      dsHiveServer.JobDataTable data =
190        new dsHiveServer.JobDataTable();
191
192      dsHiveServer.JobRow row = data.NewJobRow();
193      data.AddJobRow(row);
194
195      return row;
196    }
197
198    protected override dsHiveServer.JobRow
199      InsertNewRowInCache(Job job) {
200      dsHiveServer.JobRow row = cache.NewJobRow();
201      cache.AddJobRow(row);
202
203      return row;
204    }
205
206    protected override void FillCache() {
207      Adapter.FillByActive(cache);
208    }
209
210    protected override void SynchronizeWithDb() {
211      this.Adapter.Update(cache);
212    }
213
214    protected override bool PutInCache(Job job) {
215      return job != null
216        && (job.State == State.calculating
217            || job.State == State.idle);
218    }
219
220    protected override IEnumerable<dsHiveServer.JobRow>
221      FindById(long id) {
222      return Adapter.GetDataById(id);
223    }
224
225    protected override dsHiveServer.JobRow
226      FindCachedById(long id) {
227      return cache.FindByJobId(id);
228    }
229
230    protected override IEnumerable<dsHiveServer.JobRow>
231      FindAll() {
232      return FindMultipleRows(
233        new Selector(Adapter.GetData),
234        new Selector(cache.AsEnumerable<dsHiveServer.JobRow>));
235    }
236
237    #endregion
238
239    #region IJobAdapter Members
240    public ICollection<Job> GetAllSubjobs(Job job) {
241      if (job != null) {
242        return
243          base.FindMultiple(
244            delegate() {
245              return Adapter.GetDataBySubjobs(job.Id);
246            },
247            delegate() {
248              return from j in
249                   cache.AsEnumerable<dsHiveServer.JobRow>()
250                 where  !j.IsParentJobIdNull() &&
251                        j.ParentJobId == job.Id
252                 select j;
253            });
254      }
255
256      return null;
257    }
258
259    public ICollection<Job> GetJobsByState(State state) {
260      return
261         base.FindMultiple(
262           delegate() {
263             return Adapter.GetDataByState(state.ToString());
264           },
265           delegate() {
266             return from job in
267                      cache.AsEnumerable<dsHiveServer.JobRow>()
268                    where !job.IsJobStateNull() &&
269                           job.JobState == state.ToString()
270                    select job;
271           });
272    }
273
274    public ICollection<Job> GetJobsOf(ClientInfo client) {
275      if (client != null) {
276        return
277          base.FindMultiple(
278            delegate() {
279              return Adapter.GetDataByClient(client.Id);
280            },
281            delegate() {
282              return from job in
283                 cache.AsEnumerable<dsHiveServer.JobRow>()
284               where !job.IsResourceIdNull() &&
285                      job.ResourceId == client.Id
286               select job;
287            });
288      }
289
290      return null;
291    }
292
293    public ICollection<Job> GetActiveJobsOf(ClientInfo client) {
294
295      if (client != null) {
296        return
297          base.FindMultiple(
298            delegate() {
299              return Adapter.GetDataByCalculatingClient(client.Id);
300            },
301            delegate() {
302              return from job in
303                       cache.AsEnumerable<dsHiveServer.JobRow>()
304                     where !job.IsResourceIdNull() &&
305                            job.ResourceId == client.Id &&
306                           !job.IsJobStateNull() &&
307                            job.JobState == "calculating"
308                     select job;
309            });
310      }
311
312      return null;
313    }
314
315    public ICollection<Job> GetJobsOf(User user) {
316      if (user != null) {
317        return
318          base.FindMultiple(
319            delegate() {
320              return Adapter.GetDataByUser(user.Id);
321            },
322            delegate() {
323              return from job in
324                cache.AsEnumerable<dsHiveServer.JobRow>()
325              where !job.IsPermissionOwnerIdNull() &&
326                job.PermissionOwnerId == user.Id
327              select job;
328            });
329      }
330
331      return null;
332    }
333
334    [MethodImpl(MethodImplOptions.Synchronized)]
335    public override bool Delete(Job job) {
336      if (job != null) {
337        dsHiveServer.JobRow row =
338          GetRowById(job.Id);
339
340        if (row != null) {
341          //Referential integrity with job results
342          ICollection<JobResult> results =
343            ResultsAdapter.GetResultsOf(job);
344
345          foreach (JobResult result in results) {
346            ResultsAdapter.Delete(result);
347          }
348
349          return base.Delete(job);
350        }
351      }
352
353      return false;
354    }
355    #endregion
356  }
357}
Note: See TracBrowser for help on using the repository browser.