Free cookie consent management tool by TermsFeed Policy Generator

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

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

Created Heuristiclab DB Core (refactoring) #527

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