Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added job results data adapter interface for the Hive DAL (#372)

File size: 6.7 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    #endregion
65
66    #region Overrides
67    protected override Job Convert(dsHiveServer.JobRow row,
68      Job job) {
69      if (row != null && job != null) {
70        job.Id = row.JobId;
71
72        if (!row.IsParentJobIdNull())
73          job.ParentJob = GetById(row.ParentJobId);
74        else
75          job.ParentJob = null;
76
77        if (!row.IsResourceIdNull())
78          job.Client = ClientAdapter.GetById(row.ResourceId);
79        else
80          job.Client = null;
81       
82        if (!row.IsStatusNull())
83          job.State = (State)Enum.Parse(job.State.GetType(), row.Status);
84        else
85          job.State = State.nullState;
86
87        return job;
88      } else
89        return null;
90    }
91
92    protected override dsHiveServer.JobRow Convert(Job job,
93      dsHiveServer.JobRow row) {
94      if (job != null && row != null) {
95        if (job.Client != null) {
96          ClientAdapter.Update(job.Client);
97          row.ResourceId = job.Client.Id;
98        } else
99          row.SetResourceIdNull();
100
101        if (job.ParentJob != null) {
102          Update(job.ParentJob);
103          row.ParentJobId = job.ParentJob.Id;
104        } else
105          row.SetParentJobIdNull();
106
107        if (job.State != State.nullState)
108          row.Status = job.State.ToString();
109        else
110          row.SetStatusNull();
111      }
112
113      return row;
114    }
115
116    protected override void UpdateRow(dsHiveServer.JobRow row) {
117      adapter.Update(row);
118    }
119
120    protected override dsHiveServer.JobRow
121      InsertNewRow(Job job) {     
122      dsHiveServer.JobRow row = data.NewJobRow();
123      data.AddJobRow(row);
124
125      return row;
126    }
127
128    protected override dsHiveServer.JobRow
129      InsertNewRowInCache(Job job) {
130      dsHiveServer.JobRow row = cache.NewJobRow();
131      cache.AddJobRow(row);
132
133      return row;
134    }
135
136    protected override void FillCache() {
137      adapter.FillByActive(cache);
138    }
139
140    public override void SyncWithDb() {
141      this.adapter.Update(this.cache);
142    }
143
144    protected override bool PutInCache(Job job) {
145      return job != null
146        && job.State != State.offline
147        && job.State != State.nullState;
148    }
149
150    protected override IEnumerable<dsHiveServer.JobRow>
151      FindById(long id) {
152      return adapter.GetDataById(id);
153    }
154
155    protected override dsHiveServer.JobRow
156      FindCachedById(long id) {
157      return cache.FindByJobId(id);
158    }
159
160    protected override IEnumerable<dsHiveServer.JobRow>
161      FindAll() {
162      return FindMultipleRows(
163        new Selector(adapter.GetData),
164        new Selector(cache.AsEnumerable<dsHiveServer.JobRow>));
165    }
166
167    #endregion
168
169    #region IJobAdapter Members
170    public ICollection<Job> GetAllSubjobs(Job job) {
171      if (job != null) {
172        return
173          base.FindMultiple(
174            delegate() {
175              return adapter.GetDataBySubjobs(job.Id);
176            },
177            delegate() {
178              return from j in
179                   cache.AsEnumerable<dsHiveServer.JobRow>()
180                 where  !j.IsParentJobIdNull() &&
181                        j.ParentJobId == job.Id
182                 select j;
183            });
184      }
185
186      return null;
187    }
188
189    public ICollection<Job> GetJobsByState(State state) {
190      return
191         base.FindMultiple(
192           delegate() {
193             return adapter.GetDataByState(state.ToString());
194           },
195           delegate() {
196             return from job in
197                      cache.AsEnumerable<dsHiveServer.JobRow>()
198                    where !job.IsStatusNull() &&
199                           job.Status == state.ToString()
200                    select job;
201           });
202    }
203
204    public ICollection<Job> GetJobsOf(ClientInfo client) {
205      if (client != null) {
206        return
207          base.FindMultiple(
208            delegate() {
209              return adapter.GetDataByClient(client.Id);
210            },
211            delegate() {
212              return from job in
213                 cache.AsEnumerable<dsHiveServer.JobRow>()
214               where !job.IsResourceIdNull() &&
215                      job.ResourceId == client.Id
216               select job;
217            });
218      }
219
220      return null;
221    }
222
223    public ICollection<Job> GetJobsOf(User user) {
224      if (user != null) {
225        return
226          base.FindMultiple(
227            delegate() {
228              return adapter.GetDataByUser(user.Id);
229            },
230            delegate() {
231              return from job in
232                cache.AsEnumerable<dsHiveServer.JobRow>()
233              where !job.IsPermissionOwnerIdNull() &&
234                job.PermissionOwnerId == user.Id
235              select job;
236            });
237      }
238
239      return null;
240    }
241    #endregion
242  }
243}
Note: See TracBrowser for help on using the repository browser.