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