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, 16 years ago

Added job attributes to DB (#372)

File size: 10.0 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;
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 {
[995]32  class JobAdapter :
33    CachedDataAdapter<dsHiveServerTableAdapters.JobTableAdapter,
34                      Job,
35                      dsHiveServer.JobRow,
36                      dsHiveServer.JobDataTable>,
37    IJobAdapter {
38    #region Fields
[971]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    }
[1005]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    }
[995]73    #endregion
[971]74
[995]75    #region Overrides
[1131]76    protected override Job ConvertRow(dsHiveServer.JobRow row,
[971]77      Job job) {
78      if (row != null && job != null) {
[995]79        job.Id = row.JobId;
[971]80
81        if (!row.IsParentJobIdNull())
[995]82          job.ParentJob = GetById(row.ParentJobId);
[971]83        else
[975]84          job.ParentJob = null;
[971]85
86        if (!row.IsResourceIdNull())
[995]87          job.Client = ClientAdapter.GetById(row.ResourceId);
[971]88        else
89          job.Client = null;
[1161]90
91        if (!row.IsPermissionOwnerIdNull())
92          job.User = UserAdapter.GetById(row.PermissionOwnerId);
93        else
94          job.User = null;
[971]95       
[1092]96        if (!row.IsJobStateNull())
97          job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
[971]98        else
[995]99          job.State = State.nullState;
[971]100
[1092]101        if (!row.IsPercentageNull())
102          job.Percentage = row.Percentage;
103        else
104          job.Percentage = 0.0;
105
[1120]106        if (!row.IsSerializedJobNull())
107          job.SerializedJob = row.SerializedJob;
108        else
109          job.SerializedJob = null;
110
[1169]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
[971]123        return job;
124      } else
125        return null;
126    }
127
[1131]128    protected override dsHiveServer.JobRow ConvertObj(Job job,
[971]129      dsHiveServer.JobRow row) {
130      if (job != null && row != null) {
131        if (job.Client != null) {
[1021]132          if (row.IsResourceIdNull() ||
133            row.ResourceId != job.Client.Id) {
134            ClientAdapter.Update(job.Client);
135            row.ResourceId = job.Client.Id;
136          }
[971]137        } else
138          row.SetResourceIdNull();
139
[975]140        if (job.ParentJob != null) {
[1021]141          if (row.IsParentJobIdNull() ||
142            row.ParentJobId != job.ParentJob.Id) {
143            Update(job.ParentJob);
144            row.ParentJobId = job.ParentJob.Id;
145          }
[971]146        } else
147          row.SetParentJobIdNull();
148
[1161]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
[995]158        if (job.State != State.nullState)
[1092]159          row.JobState = job.State.ToString();
[995]160        else
[1092]161          row.SetJobStateNull();
162
163        row.Percentage = job.Percentage;
[1120]164
165        row.SerializedJob = job.SerializedJob;
[1169]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;
[971]178      }
179
180      return row;
181    }
182
[995]183    protected override void UpdateRow(dsHiveServer.JobRow row) {
[1131]184      Adapter.Update(row);
[995]185    }
[971]186
[995]187    protected override dsHiveServer.JobRow
[1166]188      InsertNewRow(Job job) {
189      dsHiveServer.JobDataTable data =
190        new dsHiveServer.JobDataTable();
191
[995]192      dsHiveServer.JobRow row = data.NewJobRow();
193      data.AddJobRow(row);
[971]194
[995]195      return row;
[971]196    }
197
[995]198    protected override dsHiveServer.JobRow
199      InsertNewRowInCache(Job job) {
[1134]200      dsHiveServer.JobRow row = cache.NewJobRow();
[995]201      cache.AddJobRow(row);
[971]202
[995]203      return row;
204    }
[971]205
[995]206    protected override void FillCache() {
[1131]207      Adapter.FillByActive(cache);
[995]208    }
[971]209
[1149]210    protected override void SynchronizeWithDb() {
[1131]211      this.Adapter.Update(cache);
[971]212    }
213
[995]214    protected override bool PutInCache(Job job) {
215      return job != null
[1029]216        && (job.State == State.calculating
217            || job.State == State.idle);
[995]218    }
[971]219
[995]220    protected override IEnumerable<dsHiveServer.JobRow>
221      FindById(long id) {
[1131]222      return Adapter.GetDataById(id);
[995]223    }
[971]224
[995]225    protected override dsHiveServer.JobRow
226      FindCachedById(long id) {
227      return cache.FindByJobId(id);
[971]228    }
229
[995]230    protected override IEnumerable<dsHiveServer.JobRow>
231      FindAll() {
232      return FindMultipleRows(
[1131]233        new Selector(Adapter.GetData),
[995]234        new Selector(cache.AsEnumerable<dsHiveServer.JobRow>));
235    }
236
237    #endregion
238
239    #region IJobAdapter Members
[971]240    public ICollection<Job> GetAllSubjobs(Job job) {
241      if (job != null) {
[995]242        return
243          base.FindMultiple(
244            delegate() {
[1131]245              return Adapter.GetDataBySubjobs(job.Id);
[995]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            });
[971]254      }
255
[995]256      return null;
[971]257    }
258
[998]259    public ICollection<Job> GetJobsByState(State state) {
260      return
261         base.FindMultiple(
262           delegate() {
[1131]263             return Adapter.GetDataByState(state.ToString());
[998]264           },
265           delegate() {
266             return from job in
267                      cache.AsEnumerable<dsHiveServer.JobRow>()
[1092]268                    where !job.IsJobStateNull() &&
269                           job.JobState == state.ToString()
[998]270                    select job;
271           });
272    }
273
[971]274    public ICollection<Job> GetJobsOf(ClientInfo client) {
275      if (client != null) {
[995]276        return
277          base.FindMultiple(
278            delegate() {
[1131]279              return Adapter.GetDataByClient(client.Id);
[995]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            });
[971]288      }
289
[995]290      return null;
[971]291    }
292
[1166]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
[971]315    public ICollection<Job> GetJobsOf(User user) {
316      if (user != null) {
[995]317        return
318          base.FindMultiple(
319            delegate() {
[1131]320              return Adapter.GetDataByUser(user.Id);
[995]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            });
[971]329      }
330
[995]331      return null;
[971]332    }
[1005]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    }
[971]355    #endregion
356  }
357}
Note: See TracBrowser for help on using the repository browser.