Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented JobResultsAdapter (#372)

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