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