Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added a state to jobs and a message to job results in the db. (#372)

File size: 8.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    dsHiveServer.JobDataTable data =
40        new dsHiveServer.JobDataTable();
41
42    private IClientAdapter clientAdapter = null;
43
44    private IClientAdapter ClientAdapter {
45      get {
46        if (clientAdapter == null)
47          clientAdapter = ServiceLocator.GetClientAdapter();
48
49        return clientAdapter;
50      }
51    }
52
53    private IUserAdapter userAdapter = null;
54
55    private IUserAdapter UserAdapter {
56      get {
57        if (userAdapter == null) {
58          userAdapter = ServiceLocator.GetUserAdapter();
59        }
60
61        return userAdapter;
62      }
63    }
64
65    private IJobResultsAdapter resultsAdapter = null;
66
67    private IJobResultsAdapter ResultsAdapter {
68      get {
69        if (resultsAdapter == null) {
70          resultsAdapter = ServiceLocator.GetJobResultsAdapter();
71        }
72
73        return resultsAdapter;
74      }
75    }
76    #endregion
77
78    #region Overrides
79    protected override Job Convert(dsHiveServer.JobRow row,
80      Job job) {
81      if (row != null && job != null) {
82        job.Id = row.JobId;
83
84        if (!row.IsParentJobIdNull())
85          job.ParentJob = GetById(row.ParentJobId);
86        else
87          job.ParentJob = null;
88
89        if (!row.IsResourceIdNull())
90          job.Client = ClientAdapter.GetById(row.ResourceId);
91        else
92          job.Client = null;
93       
94        if (!row.IsJobStateNull())
95          job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
96        else
97          job.State = State.nullState;
98
99        if (!row.IsPercentageNull())
100          job.Percentage = row.Percentage;
101        else
102          job.Percentage = 0.0;
103
104        return job;
105      } else
106        return null;
107    }
108
109    protected override dsHiveServer.JobRow Convert(Job job,
110      dsHiveServer.JobRow row) {
111      if (job != null && row != null) {
112        if (job.Client != null) {
113          if (row.IsResourceIdNull() ||
114            row.ResourceId != job.Client.Id) {
115            ClientAdapter.Update(job.Client);
116            row.ResourceId = job.Client.Id;
117          }
118        } else
119          row.SetResourceIdNull();
120
121        if (job.ParentJob != null) {
122          if (row.IsParentJobIdNull() ||
123            row.ParentJobId != job.ParentJob.Id) {
124            Update(job.ParentJob);
125            row.ParentJobId = job.ParentJob.Id;
126          }
127        } else
128          row.SetParentJobIdNull();
129
130        if (job.State != State.nullState)
131          row.JobState = job.State.ToString();
132        else
133          row.SetJobStateNull();
134
135        row.Percentage = job.Percentage;
136      }
137
138      return row;
139    }
140
141    protected override void UpdateRow(dsHiveServer.JobRow row) {
142      adapter.Update(row);
143    }
144
145    protected override dsHiveServer.JobRow
146      InsertNewRow(Job job) {     
147      dsHiveServer.JobRow row = data.NewJobRow();
148      data.AddJobRow(row);
149
150      return row;
151    }
152
153    protected override dsHiveServer.JobRow
154      InsertNewRowInCache(Job job) {
155      dsHiveServer.JobRow row = cache.NewJobRow();
156      cache.AddJobRow(row);
157
158      return row;
159    }
160
161    protected override void FillCache() {
162      adapter.FillByActive(cache);
163    }
164
165    public override void SyncWithDb() {
166      this.adapter.Update(this.cache);
167    }
168
169    protected override bool PutInCache(Job job) {
170      return job != null
171        && (job.State == State.calculating
172            || job.State == State.idle);
173    }
174
175    protected override IEnumerable<dsHiveServer.JobRow>
176      FindById(long id) {
177      return adapter.GetDataById(id);
178    }
179
180    protected override dsHiveServer.JobRow
181      FindCachedById(long id) {
182      return cache.FindByJobId(id);
183    }
184
185    protected override IEnumerable<dsHiveServer.JobRow>
186      FindAll() {
187      return FindMultipleRows(
188        new Selector(adapter.GetData),
189        new Selector(cache.AsEnumerable<dsHiveServer.JobRow>));
190    }
191
192    #endregion
193
194    #region IJobAdapter Members
195    public ICollection<Job> GetAllSubjobs(Job job) {
196      if (job != null) {
197        return
198          base.FindMultiple(
199            delegate() {
200              return adapter.GetDataBySubjobs(job.Id);
201            },
202            delegate() {
203              return from j in
204                   cache.AsEnumerable<dsHiveServer.JobRow>()
205                 where  !j.IsParentJobIdNull() &&
206                        j.ParentJobId == job.Id
207                 select j;
208            });
209      }
210
211      return null;
212    }
213
214    public ICollection<Job> GetJobsByState(State state) {
215      return
216         base.FindMultiple(
217           delegate() {
218             return adapter.GetDataByState(state.ToString());
219           },
220           delegate() {
221             return from job in
222                      cache.AsEnumerable<dsHiveServer.JobRow>()
223                    where !job.IsJobStateNull() &&
224                           job.JobState == state.ToString()
225                    select job;
226           });
227    }
228
229    public ICollection<Job> GetJobsOf(ClientInfo client) {
230      if (client != null) {
231        return
232          base.FindMultiple(
233            delegate() {
234              return adapter.GetDataByClient(client.Id);
235            },
236            delegate() {
237              return from job in
238                 cache.AsEnumerable<dsHiveServer.JobRow>()
239               where !job.IsResourceIdNull() &&
240                      job.ResourceId == client.Id
241               select job;
242            });
243      }
244
245      return null;
246    }
247
248    public ICollection<Job> GetJobsOf(User user) {
249      if (user != null) {
250        return
251          base.FindMultiple(
252            delegate() {
253              return adapter.GetDataByUser(user.Id);
254            },
255            delegate() {
256              return from job in
257                cache.AsEnumerable<dsHiveServer.JobRow>()
258              where !job.IsPermissionOwnerIdNull() &&
259                job.PermissionOwnerId == user.Id
260              select job;
261            });
262      }
263
264      return null;
265    }
266
267    [MethodImpl(MethodImplOptions.Synchronized)]
268    public override bool Delete(Job job) {
269      if (job != null) {
270        dsHiveServer.JobRow row =
271          GetRowById(job.Id);
272
273        if (row != null) {
274          //Referential integrity with job results
275          ICollection<JobResult> results =
276            ResultsAdapter.GetResultsOf(job);
277
278          foreach (JobResult result in results) {
279            ResultsAdapter.Delete(result);
280          }
281
282          return base.Delete(job);
283        }
284      }
285
286      return false;
287    }
288    #endregion
289  }
290}
Note: See TracBrowser for help on using the repository browser.