Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added user to job (#372)

File size: 8.6 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 ConvertRow(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.IsPermissionOwnerIdNull())
95          job.User = UserAdapter.GetById(row.PermissionOwnerId);
96        else
97          job.User = null;
98       
99        if (!row.IsJobStateNull())
100          job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
101        else
102          job.State = State.nullState;
103
104        if (!row.IsPercentageNull())
105          job.Percentage = row.Percentage;
106        else
107          job.Percentage = 0.0;
108
109        if (!row.IsSerializedJobNull())
110          job.SerializedJob = row.SerializedJob;
111        else
112          job.SerializedJob = null;
113
114        return job;
115      } else
116        return null;
117    }
118
119    protected override dsHiveServer.JobRow ConvertObj(Job job,
120      dsHiveServer.JobRow row) {
121      if (job != null && row != null) {
122        if (job.Client != null) {
123          if (row.IsResourceIdNull() ||
124            row.ResourceId != job.Client.Id) {
125            ClientAdapter.Update(job.Client);
126            row.ResourceId = job.Client.Id;
127          }
128        } else
129          row.SetResourceIdNull();
130
131        if (job.ParentJob != null) {
132          if (row.IsParentJobIdNull() ||
133            row.ParentJobId != job.ParentJob.Id) {
134            Update(job.ParentJob);
135            row.ParentJobId = job.ParentJob.Id;
136          }
137        } else
138          row.SetParentJobIdNull();
139
140        if (job.User != null) {
141          if (row.IsPermissionOwnerIdNull() ||
142           row.PermissionOwnerId != job.User.Id) {
143            UserAdapter.Update(job.User);
144            row.PermissionOwnerId = job.User.Id;
145          }
146        } else
147          row.SetPermissionOwnerIdNull();
148
149        if (job.State != State.nullState)
150          row.JobState = job.State.ToString();
151        else
152          row.SetJobStateNull();
153
154        row.Percentage = job.Percentage;
155
156        row.SerializedJob = job.SerializedJob;
157      }
158
159      return row;
160    }
161
162    protected override void UpdateRow(dsHiveServer.JobRow row) {
163      Adapter.Update(row);
164    }
165
166    protected override dsHiveServer.JobRow
167      InsertNewRow(Job job) {     
168      dsHiveServer.JobRow row = data.NewJobRow();
169      data.AddJobRow(row);
170
171      return row;
172    }
173
174    protected override dsHiveServer.JobRow
175      InsertNewRowInCache(Job job) {
176      dsHiveServer.JobRow row = cache.NewJobRow();
177      cache.AddJobRow(row);
178
179      return row;
180    }
181
182    protected override void FillCache() {
183      Adapter.FillByActive(cache);
184    }
185
186    protected override void SynchronizeWithDb() {
187      this.Adapter.Update(cache);
188    }
189
190    protected override bool PutInCache(Job job) {
191      return job != null
192        && (job.State == State.calculating
193            || job.State == State.idle);
194    }
195
196    protected override IEnumerable<dsHiveServer.JobRow>
197      FindById(long id) {
198      return Adapter.GetDataById(id);
199    }
200
201    protected override dsHiveServer.JobRow
202      FindCachedById(long id) {
203      return cache.FindByJobId(id);
204    }
205
206    protected override IEnumerable<dsHiveServer.JobRow>
207      FindAll() {
208      return FindMultipleRows(
209        new Selector(Adapter.GetData),
210        new Selector(cache.AsEnumerable<dsHiveServer.JobRow>));
211    }
212
213    #endregion
214
215    #region IJobAdapter Members
216    public ICollection<Job> GetAllSubjobs(Job job) {
217      if (job != null) {
218        return
219          base.FindMultiple(
220            delegate() {
221              return Adapter.GetDataBySubjobs(job.Id);
222            },
223            delegate() {
224              return from j in
225                   cache.AsEnumerable<dsHiveServer.JobRow>()
226                 where  !j.IsParentJobIdNull() &&
227                        j.ParentJobId == job.Id
228                 select j;
229            });
230      }
231
232      return null;
233    }
234
235    public ICollection<Job> GetJobsByState(State state) {
236      return
237         base.FindMultiple(
238           delegate() {
239             return Adapter.GetDataByState(state.ToString());
240           },
241           delegate() {
242             return from job in
243                      cache.AsEnumerable<dsHiveServer.JobRow>()
244                    where !job.IsJobStateNull() &&
245                           job.JobState == state.ToString()
246                    select job;
247           });
248    }
249
250    public ICollection<Job> GetJobsOf(ClientInfo client) {
251      if (client != null) {
252        return
253          base.FindMultiple(
254            delegate() {
255              return Adapter.GetDataByClient(client.Id);
256            },
257            delegate() {
258              return from job in
259                 cache.AsEnumerable<dsHiveServer.JobRow>()
260               where !job.IsResourceIdNull() &&
261                      job.ResourceId == client.Id
262               select job;
263            });
264      }
265
266      return null;
267    }
268
269    public ICollection<Job> GetJobsOf(User user) {
270      if (user != null) {
271        return
272          base.FindMultiple(
273            delegate() {
274              return Adapter.GetDataByUser(user.Id);
275            },
276            delegate() {
277              return from job in
278                cache.AsEnumerable<dsHiveServer.JobRow>()
279              where !job.IsPermissionOwnerIdNull() &&
280                job.PermissionOwnerId == user.Id
281              select job;
282            });
283      }
284
285      return null;
286    }
287
288    [MethodImpl(MethodImplOptions.Synchronized)]
289    public override bool Delete(Job job) {
290      if (job != null) {
291        dsHiveServer.JobRow row =
292          GetRowById(job.Id);
293
294        if (row != null) {
295          //Referential integrity with job results
296          ICollection<JobResult> results =
297            ResultsAdapter.GetResultsOf(job);
298
299          foreach (JobResult result in results) {
300            ResultsAdapter.Delete(result);
301          }
302
303          return base.Delete(job);
304        }
305      }
306
307      return false;
308    }
309    #endregion
310  }
311}
Note: See TracBrowser for help on using the repository browser.