[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;
|
---|
| 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 {
|
---|
[995] | 31 | class JobAdapter :
|
---|
| 32 | CachedDataAdapter<dsHiveServerTableAdapters.JobTableAdapter,
|
---|
| 33 | Job,
|
---|
| 34 | dsHiveServer.JobRow,
|
---|
| 35 | dsHiveServer.JobDataTable>,
|
---|
| 36 | IJobAdapter {
|
---|
| 37 | #region Fields
|
---|
[971] | 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 | }
|
---|
[1005] | 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 | }
|
---|
[995] | 72 | #endregion
|
---|
[971] | 73 |
|
---|
[995] | 74 | #region Overrides
|
---|
[1131] | 75 | protected override Job ConvertRow(dsHiveServer.JobRow row,
|
---|
[971] | 76 | Job job) {
|
---|
| 77 | if (row != null && job != null) {
|
---|
[995] | 78 | job.Id = row.JobId;
|
---|
[971] | 79 |
|
---|
| 80 | if (!row.IsParentJobIdNull())
|
---|
[995] | 81 | job.ParentJob = GetById(row.ParentJobId);
|
---|
[971] | 82 | else
|
---|
[975] | 83 | job.ParentJob = null;
|
---|
[971] | 84 |
|
---|
| 85 | if (!row.IsResourceIdNull())
|
---|
[995] | 86 | job.Client = ClientAdapter.GetById(row.ResourceId);
|
---|
[971] | 87 | else
|
---|
| 88 | job.Client = null;
|
---|
[1161] | 89 |
|
---|
| 90 | if (!row.IsPermissionOwnerIdNull())
|
---|
| 91 | job.User = UserAdapter.GetById(row.PermissionOwnerId);
|
---|
| 92 | else
|
---|
| 93 | job.User = null;
|
---|
[971] | 94 |
|
---|
[1092] | 95 | if (!row.IsJobStateNull())
|
---|
| 96 | job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
|
---|
[971] | 97 | else
|
---|
[995] | 98 | job.State = State.nullState;
|
---|
[971] | 99 |
|
---|
[1092] | 100 | if (!row.IsPercentageNull())
|
---|
| 101 | job.Percentage = row.Percentage;
|
---|
| 102 | else
|
---|
| 103 | job.Percentage = 0.0;
|
---|
| 104 |
|
---|
[1120] | 105 | if (!row.IsSerializedJobNull())
|
---|
| 106 | job.SerializedJob = row.SerializedJob;
|
---|
| 107 | else
|
---|
| 108 | job.SerializedJob = null;
|
---|
| 109 |
|
---|
[1169] | 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 |
|
---|
[1175] | 120 | if (!row.IsPriorityNull())
|
---|
| 121 | job.Priority = row.Priority;
|
---|
| 122 | else
|
---|
| 123 | job.Priority = default(int);
|
---|
[1169] | 124 |
|
---|
[971] | 125 | return job;
|
---|
| 126 | } else
|
---|
| 127 | return null;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[1131] | 130 | protected override dsHiveServer.JobRow ConvertObj(Job job,
|
---|
[971] | 131 | dsHiveServer.JobRow row) {
|
---|
| 132 | if (job != null && row != null) {
|
---|
| 133 | if (job.Client != null) {
|
---|
[1021] | 134 | if (row.IsResourceIdNull() ||
|
---|
| 135 | row.ResourceId != job.Client.Id) {
|
---|
| 136 | ClientAdapter.Update(job.Client);
|
---|
| 137 | row.ResourceId = job.Client.Id;
|
---|
| 138 | }
|
---|
[971] | 139 | } else
|
---|
| 140 | row.SetResourceIdNull();
|
---|
| 141 |
|
---|
[975] | 142 | if (job.ParentJob != null) {
|
---|
[1021] | 143 | if (row.IsParentJobIdNull() ||
|
---|
| 144 | row.ParentJobId != job.ParentJob.Id) {
|
---|
| 145 | Update(job.ParentJob);
|
---|
| 146 | row.ParentJobId = job.ParentJob.Id;
|
---|
| 147 | }
|
---|
[971] | 148 | } else
|
---|
| 149 | row.SetParentJobIdNull();
|
---|
| 150 |
|
---|
[1161] | 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 |
|
---|
[995] | 160 | if (job.State != State.nullState)
|
---|
[1092] | 161 | row.JobState = job.State.ToString();
|
---|
[995] | 162 | else
|
---|
[1092] | 163 | row.SetJobStateNull();
|
---|
| 164 |
|
---|
| 165 | row.Percentage = job.Percentage;
|
---|
[1120] | 166 |
|
---|
| 167 | row.SerializedJob = job.SerializedJob;
|
---|
[1169] | 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;
|
---|
[971] | 180 | }
|
---|
| 181 |
|
---|
| 182 | return row;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[995] | 185 | protected override void UpdateRow(dsHiveServer.JobRow row) {
|
---|
[1131] | 186 | Adapter.Update(row);
|
---|
[995] | 187 | }
|
---|
[971] | 188 |
|
---|
[995] | 189 | protected override dsHiveServer.JobRow
|
---|
[1166] | 190 | InsertNewRow(Job job) {
|
---|
| 191 | dsHiveServer.JobDataTable data =
|
---|
| 192 | new dsHiveServer.JobDataTable();
|
---|
| 193 |
|
---|
[995] | 194 | dsHiveServer.JobRow row = data.NewJobRow();
|
---|
| 195 | data.AddJobRow(row);
|
---|
[971] | 196 |
|
---|
[995] | 197 | return row;
|
---|
[971] | 198 | }
|
---|
| 199 |
|
---|
[995] | 200 | protected override dsHiveServer.JobRow
|
---|
| 201 | InsertNewRowInCache(Job job) {
|
---|
[1134] | 202 | dsHiveServer.JobRow row = cache.NewJobRow();
|
---|
[995] | 203 | cache.AddJobRow(row);
|
---|
[971] | 204 |
|
---|
[995] | 205 | return row;
|
---|
| 206 | }
|
---|
[971] | 207 |
|
---|
[995] | 208 | protected override void FillCache() {
|
---|
[1131] | 209 | Adapter.FillByActive(cache);
|
---|
[995] | 210 | }
|
---|
[971] | 211 |
|
---|
[1149] | 212 | protected override void SynchronizeWithDb() {
|
---|
[1131] | 213 | this.Adapter.Update(cache);
|
---|
[971] | 214 | }
|
---|
| 215 |
|
---|
[995] | 216 | protected override bool PutInCache(Job job) {
|
---|
| 217 | return job != null
|
---|
[1029] | 218 | && (job.State == State.calculating
|
---|
| 219 | || job.State == State.idle);
|
---|
[995] | 220 | }
|
---|
[971] | 221 |
|
---|
[995] | 222 | protected override IEnumerable<dsHiveServer.JobRow>
|
---|
| 223 | FindById(long id) {
|
---|
[1131] | 224 | return Adapter.GetDataById(id);
|
---|
[995] | 225 | }
|
---|
[971] | 226 |
|
---|
[995] | 227 | protected override dsHiveServer.JobRow
|
---|
| 228 | FindCachedById(long id) {
|
---|
| 229 | return cache.FindByJobId(id);
|
---|
[971] | 230 | }
|
---|
| 231 |
|
---|
[995] | 232 | protected override IEnumerable<dsHiveServer.JobRow>
|
---|
| 233 | FindAll() {
|
---|
| 234 | return FindMultipleRows(
|
---|
[1131] | 235 | new Selector(Adapter.GetData),
|
---|
[995] | 236 | new Selector(cache.AsEnumerable<dsHiveServer.JobRow>));
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | #endregion
|
---|
| 240 |
|
---|
| 241 | #region IJobAdapter Members
|
---|
[971] | 242 | public ICollection<Job> GetAllSubjobs(Job job) {
|
---|
| 243 | if (job != null) {
|
---|
[995] | 244 | return
|
---|
| 245 | base.FindMultiple(
|
---|
| 246 | delegate() {
|
---|
[1131] | 247 | return Adapter.GetDataBySubjobs(job.Id);
|
---|
[995] | 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 | });
|
---|
[971] | 256 | }
|
---|
| 257 |
|
---|
[995] | 258 | return null;
|
---|
[971] | 259 | }
|
---|
| 260 |
|
---|
[998] | 261 | public ICollection<Job> GetJobsByState(State state) {
|
---|
| 262 | return
|
---|
| 263 | base.FindMultiple(
|
---|
| 264 | delegate() {
|
---|
[1131] | 265 | return Adapter.GetDataByState(state.ToString());
|
---|
[998] | 266 | },
|
---|
| 267 | delegate() {
|
---|
| 268 | return from job in
|
---|
| 269 | cache.AsEnumerable<dsHiveServer.JobRow>()
|
---|
[1092] | 270 | where !job.IsJobStateNull() &&
|
---|
| 271 | job.JobState == state.ToString()
|
---|
[998] | 272 | select job;
|
---|
| 273 | });
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[971] | 276 | public ICollection<Job> GetJobsOf(ClientInfo client) {
|
---|
| 277 | if (client != null) {
|
---|
[995] | 278 | return
|
---|
| 279 | base.FindMultiple(
|
---|
| 280 | delegate() {
|
---|
[1131] | 281 | return Adapter.GetDataByClient(client.Id);
|
---|
[995] | 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 | });
|
---|
[971] | 290 | }
|
---|
| 291 |
|
---|
[995] | 292 | return null;
|
---|
[971] | 293 | }
|
---|
| 294 |
|
---|
[1166] | 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 |
|
---|
[971] | 317 | public ICollection<Job> GetJobsOf(User user) {
|
---|
| 318 | if (user != null) {
|
---|
[995] | 319 | return
|
---|
| 320 | base.FindMultiple(
|
---|
| 321 | delegate() {
|
---|
[1131] | 322 | return Adapter.GetDataByUser(user.Id);
|
---|
[995] | 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 | });
|
---|
[971] | 331 | }
|
---|
| 332 |
|
---|
[995] | 333 | return null;
|
---|
[971] | 334 | }
|
---|
[1005] | 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 | }
|
---|
[971] | 356 | #endregion
|
---|
| 357 | }
|
---|
| 358 | }
|
---|