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