Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RefactorPluginInfrastructure-2522/HeuristicLab.Services.Hive/3.3/Converter.cs @ 13797

Last change on this file since 13797 was 12959, checked in by ascheibe, 9 years ago

#2355 added backwards compatibility to task in the service

File size: 18.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Linq;
24using DA = HeuristicLab.Services.Hive.DataAccess;
25using DT = HeuristicLab.Services.Hive.DataTransfer;
26
27namespace HeuristicLab.Services.Hive {
28  public static class Converter {
29
30    #region Task
31    public static DT.Task ToDto(this DA.Task source) {
32      if (source == null) return null;
33      return new DT.Task {
34        Id = source.TaskId,
35        State = source.State.ToDto(),
36        ExecutionTime = TimeSpan.FromMilliseconds(source.ExecutionTimeMs),
37        LastHeartbeat = source.LastHeartbeat,
38        ParentTaskId = source.ParentTaskId,
39        Priority = source.Priority,
40        CoresNeeded = source.CoresNeeded,
41        MemoryNeeded = source.MemoryNeeded,
42        IsParentTask = source.IsParentTask,
43        FinishWhenChildJobsFinished = source.FinishWhenChildJobsFinished,
44        Command = source.Command.ToDto(),
45        JobId = source.JobId,
46        PluginsNeededIds = source.RequiredPlugins.Select(x => x.PluginId).ToList(),
47        StateLog = source.StateLogs.Select(x => x.ToDto()).OrderBy(x => x.DateTime).ToList(),
48        LastTaskDataUpdate = source.JobData == null ? DateTime.MinValue : source.JobData.LastUpdate,
49        IsPrivileged = true
50      };
51    }
52
53    public static DA.Task ToEntity(this DT.Task source) {
54      if (source == null) return null;
55      var result = new DA.Task();
56      source.CopyToEntity(result);
57      return result;
58    }
59
60    public static void CopyToEntity(this DT.Task source, DA.Task target) {
61      if ((source == null) || (target == null)) return;
62      target.TaskId = source.Id;
63      target.State = source.State.ToEntity();
64      target.ExecutionTimeMs = source.ExecutionTime.TotalMilliseconds;
65      target.LastHeartbeat = source.LastHeartbeat;
66      target.ParentTaskId = source.ParentTaskId;
67      target.Priority = source.Priority;
68      target.CoresNeeded = source.CoresNeeded;
69      target.MemoryNeeded = source.MemoryNeeded;
70      target.IsParentTask = source.IsParentTask;
71      target.FinishWhenChildJobsFinished = source.FinishWhenChildJobsFinished;
72      target.Command = source.Command.ToEntity();
73      target.JobId = source.JobId;
74      var ids = target.RequiredPlugins.Select(x => x.PluginId);
75      target.RequiredPlugins.AddRange(source.PluginsNeededIds
76        .Where(x => !ids.Contains(x))
77        .Select(x => new DA.RequiredPlugin {
78          PluginId = x
79        })
80      );
81      target.StateLogs.AddRange(source.StateLog
82        .Where(x => x.Id == Guid.Empty)
83        .Select(x => x.ToEntity())
84      );
85      // result.JobData missing
86      // result.AssignedResources missing
87    }
88    #endregion
89
90    #region TaskData
91    public static DT.TaskData ToDto(this DA.TaskData source) {
92      if (source == null) return null;
93      return new DT.TaskData {
94        TaskId = source.TaskId,
95        Data = source.Data,
96        LastUpdate = source.LastUpdate
97      };
98    }
99
100    public static DA.TaskData ToEntity(this DT.TaskData source) {
101      if (source == null) return null;
102      var result = new DA.TaskData();
103      source.CopyToEntity(result);
104      return result;
105    }
106
107    public static void CopyToEntity(this DT.TaskData source, DA.TaskData target) {
108      if ((source == null) || (target == null)) return;
109      target.TaskId = source.TaskId;
110      target.Data = source.Data;
111      target.LastUpdate = source.LastUpdate;
112    }
113    #endregion
114
115    #region Job
116    public static DT.Job ToDto(this DA.Job source) {
117      return new DT.Job {
118        Id = source.JobId,
119        Description = source.Description,
120        Name = source.Name,
121        OwnerUserId = source.OwnerUserId,
122        DateCreated = source.DateCreated,
123        ResourceNames = source.ResourceIds
124      };
125    }
126
127    public static DA.Job ToEntity(this DT.Job source) {
128      if (source == null) return null;
129      var result = new DA.Job();
130      source.CopyToEntity(result);
131      return result;
132    }
133
134    public static void CopyToEntity(this DT.Job source, DA.Job target) {
135      if ((source == null) || (target == null)) return;
136      target.JobId = source.Id;
137      target.Description = source.Description;
138      target.Name = source.Name;
139      target.OwnerUserId = source.OwnerUserId;
140      target.DateCreated = source.DateCreated;
141      target.ResourceIds = source.ResourceNames;
142    }
143    #endregion
144
145    #region JobPermission
146    public static DT.JobPermission ToDto(this DA.JobPermission source) {
147      if (source == null) return null;
148      return new DT.JobPermission {
149        JobId = source.JobId,
150        GrantedUserId = source.GrantedUserId,
151        GrantedByUserId = source.GrantedByUserId,
152        Permission = source.Permission.ToDto()
153      };
154    }
155
156    public static DA.JobPermission ToEntity(this DT.JobPermission source) {
157      if (source == null) return null;
158      var result = new DA.JobPermission();
159      source.CopyToEntity(result);
160      return result;
161    }
162
163    public static void CopyToEntity(this DT.JobPermission source, DA.JobPermission target) {
164      if ((source == null) || (target == null)) return;
165      target.JobId = source.JobId;
166      target.GrantedUserId = source.GrantedUserId;
167      target.GrantedByUserId = source.GrantedByUserId;
168      target.Permission = source.Permission.ToEntity();
169    }
170    #endregion
171
172    #region Slave
173    public static DT.Slave ToDto(this DA.Slave source) {
174      if (source == null) return null;
175      return new DT.Slave {
176        Id = source.ResourceId,
177        ParentResourceId = source.ParentResourceId,
178        Cores = source.Cores,
179        CpuSpeed = source.CpuSpeed,
180        FreeCores = source.FreeCores,
181        FreeMemory = source.FreeMemory,
182        IsAllowedToCalculate = source.IsAllowedToCalculate,
183        Memory = source.Memory,
184        Name = source.Name,
185        SlaveState = source.SlaveState.ToDto(),
186        CpuArchitecture = source.CpuArchitecture.ToDto(),
187        OperatingSystem = source.OperatingSystem,
188        LastHeartbeat = source.LastHeartbeat,
189        CpuUtilization = source.CpuUtilization,
190        HbInterval = source.HbInterval,
191        IsDisposable = source.IsDisposable,
192        OwnerUserId = source.OwnerUserId
193      };
194    }
195    public static DA.Slave ToEntity(this DT.Slave source) {
196      if (source == null) return null;
197      var result = new DA.Slave();
198      source.CopyToEntity(result);
199      return result;
200    }
201    public static void CopyToEntity(this DT.Slave source, DA.Slave target) {
202      if ((source == null) || (target == null)) return;
203      target.ResourceId = source.Id;
204      target.ParentResourceId = source.ParentResourceId;
205      target.Cores = source.Cores;
206      target.CpuSpeed = source.CpuSpeed;
207      target.FreeCores = source.FreeCores;
208      target.FreeMemory = source.FreeMemory;
209      target.IsAllowedToCalculate = source.IsAllowedToCalculate;
210      target.Memory = source.Memory;
211      target.Name = source.Name;
212      target.SlaveState = source.SlaveState.ToEntity();
213      target.CpuArchitecture = source.CpuArchitecture.ToEntity();
214      target.OperatingSystem = source.OperatingSystem;
215      target.LastHeartbeat = source.LastHeartbeat;
216      target.CpuUtilization = source.CpuUtilization;
217      target.HbInterval = source.HbInterval;
218      target.IsDisposable = source.IsDisposable;
219      target.OwnerUserId = source.OwnerUserId;
220    }
221    #endregion
222
223    #region State
224    public static DT.TaskState ToDto(this DA.TaskState source) {
225      switch (source) {
226        case DA.TaskState.Aborted: return DT.TaskState.Aborted;
227        case DA.TaskState.Calculating: return DT.TaskState.Calculating;
228        case DA.TaskState.Failed: return DT.TaskState.Failed;
229        case DA.TaskState.Finished: return DT.TaskState.Finished;
230        case DA.TaskState.Offline: return DT.TaskState.Offline;
231        case DA.TaskState.Paused: return DT.TaskState.Paused;
232        case DA.TaskState.Transferring: return DT.TaskState.Transferring;
233        case DA.TaskState.Waiting: return DT.TaskState.Waiting;
234        default: return DT.TaskState.Failed;
235      }
236    }
237
238    public static DA.TaskState ToEntity(this DT.TaskState source) {
239      switch (source) {
240        case DT.TaskState.Aborted: return DA.TaskState.Aborted;
241        case DT.TaskState.Calculating: return DA.TaskState.Calculating;
242        case DT.TaskState.Failed: return DA.TaskState.Failed;
243        case DT.TaskState.Finished: return DA.TaskState.Finished;
244        case DT.TaskState.Offline: return DA.TaskState.Offline;
245        case DT.TaskState.Paused: return DA.TaskState.Paused;
246        case DT.TaskState.Transferring: return DA.TaskState.Transferring;
247        case DT.TaskState.Waiting: return DA.TaskState.Waiting;
248        default: return DA.TaskState.Failed;
249      }
250    }
251    #endregion
252
253    #region StateLogs
254    public static DT.StateLog ToDto(this DA.StateLog source) {
255      return new DT.StateLog {
256        Id = source.StateLogId,
257        State = source.State.ToDto(),
258        DateTime = source.DateTime,
259        TaskId = source.TaskId,
260        UserId = source.UserId,
261        SlaveId = source.SlaveId,
262        Exception = source.Exception
263      };
264    }
265
266    public static DA.StateLog ToEntity(this DT.StateLog source) {
267      return new DA.StateLog {
268        StateLogId = source.Id,
269        State = source.State.ToEntity(),
270        DateTime = source.DateTime,
271        TaskId = source.TaskId,
272        UserId = source.UserId,
273        SlaveId = source.SlaveId,
274        Exception = source.Exception
275      };
276    }
277    #endregion
278
279    #region Plugin
280    public static DT.Plugin ToDto(this DA.Plugin source) {
281      if (source == null) return null;
282      return new DT.Plugin {
283        Id = source.PluginId,
284        Name = source.Name,
285        Version = new Version(source.Version),
286        UserId = source.UserId,
287        DateCreated = source.DateCreated,
288        Hash = source.Hash
289      };
290    }
291    public static DA.Plugin ToEntity(this DT.Plugin source) {
292      if (source == null) return null;
293      var result = new DA.Plugin();
294      source.CopyToEntity(result);
295      return result;
296    }
297    public static void CopyToEntity(this DT.Plugin source, DA.Plugin target) {
298      if ((source == null) || (target == null)) return;
299      target.PluginId = source.Id;
300      target.Name = source.Name;
301      target.Version = source.Version.ToString();
302      target.UserId = source.UserId;
303      target.DateCreated = source.DateCreated;
304      target.Hash = source.Hash;
305    }
306    #endregion
307
308    #region PluginData
309    public static DT.PluginData ToDto(this DA.PluginData source) {
310      if (source == null) return null;
311      return new DT.PluginData {
312        Id = source.PluginDataId,
313        PluginId = source.PluginId,
314        Data = source.Data.ToArray(),
315        FileName = source.FileName
316      };
317    }
318
319    public static DA.PluginData ToEntity(this DT.PluginData source) {
320      if (source == null) return null;
321      var result = new DA.PluginData();
322      source.CopyToEntity(result);
323      return result;
324    }
325
326    public static void CopyToEntity(this DT.PluginData source, DA.PluginData target) {
327      if ((source == null) || (target == null)) return;
328      target.PluginDataId = source.Id;
329      target.PluginId = source.PluginId;
330      target.Data = source.Data;
331      target.FileName = source.FileName;
332    }
333    #endregion
334
335    #region ResourcePermission
336    public static DT.ResourcePermission ToDto(this DA.ResourcePermission source) {
337      if (source == null) return null;
338      return new DT.ResourcePermission {
339        ResourceId = source.ResourceId,
340        GrantedUserId = source.GrantedUserId,
341        GrantedByUserId = source.GrantedByUserId
342      };
343    }
344    public static DA.ResourcePermission ToEntity(this DT.ResourcePermission source) {
345      if (source == null) return null;
346      var result = new DA.ResourcePermission();
347      source.CopyToEntity(result);
348      return result;
349    }
350    public static void CopyToEntity(this DT.ResourcePermission source, DA.ResourcePermission target) {
351      if ((source == null) || (target == null)) return;
352      target.ResourceId = source.ResourceId;
353      target.GrantedUserId = source.GrantedUserId;
354      target.GrantedByUserId = source.GrantedByUserId;
355    }
356    #endregion
357
358    #region SlaveGroup
359    public static DT.SlaveGroup ToDto(this DA.SlaveGroup source) {
360      if (source == null) return null;
361      return new DT.SlaveGroup {
362        Id = source.ResourceId,
363        Name = source.Name,
364        ParentResourceId = source.ParentResourceId,
365        HbInterval = source.HbInterval,
366        OwnerUserId = source.OwnerUserId
367      };
368    }
369
370    public static DA.SlaveGroup ToEntity(this DT.SlaveGroup source) {
371      if (source == null) return null;
372      var result = new DA.SlaveGroup();
373      source.CopyToEntity(result);
374      return result;
375    }
376
377    public static void CopyToEntity(this DT.SlaveGroup source, DA.SlaveGroup target) {
378      if ((source == null) || (target == null)) return;
379      target.ResourceId = source.Id;
380      target.Name = source.Name;
381      target.ParentResourceId = source.ParentResourceId;
382      target.HbInterval = source.HbInterval;
383      target.OwnerUserId = source.OwnerUserId;
384    }
385    #endregion
386
387    #region Downtimes
388    public static DT.Downtime ToDto(this DA.Downtime source) {
389      if (source == null) return null;
390      return new DT.Downtime {
391        Id = source.DowntimeId,
392        AllDayEvent = source.AllDayEvent,
393        EndDate = source.EndDate,
394        Recurring = source.Recurring,
395        RecurringId = source.RecurringId,
396        ResourceId = source.ResourceId,
397        StartDate = source.StartDate,
398        DowntimeType = source.DowntimeType
399      };
400    }
401    public static DA.Downtime ToEntity(this DT.Downtime source) {
402      if (source == null) return null;
403      var result = new DA.Downtime();
404      source.CopyToEntity(result);
405      return result;
406    }
407    public static void CopyToEntity(this DT.Downtime source, DA.Downtime target) {
408      if ((source == null) || (target == null)) return;
409      target.DowntimeId = source.Id;
410      target.AllDayEvent = source.AllDayEvent;
411      target.EndDate = source.EndDate;
412      target.Recurring = source.Recurring;
413      target.RecurringId = source.RecurringId;
414      target.ResourceId = source.ResourceId;
415      target.StartDate = source.StartDate;
416      target.DowntimeType = source.DowntimeType;
417    }
418    #endregion
419
420
421    #region Command
422    public static DT.Command? ToDto(this DA.Command? source) {
423      if (source.HasValue) {
424        switch (source) {
425          case DA.Command.Abort: return DT.Command.Abort;
426          case DA.Command.Pause: return DT.Command.Pause;
427          case DA.Command.Stop: return DT.Command.Stop;
428          default: return DT.Command.Pause;
429        }
430      }
431      return null;
432    }
433
434    public static DA.Command? ToEntity(this DT.Command? source) {
435      if (source.HasValue) {
436        switch (source) {
437          case DT.Command.Abort: return DA.Command.Abort;
438          case DT.Command.Pause: return DA.Command.Pause;
439          case DT.Command.Stop: return DA.Command.Stop;
440          default: return DA.Command.Pause;
441        }
442      }
443      return null;
444    }
445    #endregion
446
447    #region Permission
448    public static DT.Permission ToDto(this DA.Permission source) {
449      switch (source) {
450        case DA.Permission.Full: return DT.Permission.Full;
451        case DA.Permission.NotAllowed: return DT.Permission.NotAllowed;
452        case DA.Permission.Read: return DT.Permission.Read;
453        default: return DT.Permission.NotAllowed;
454      }
455    }
456
457    public static DA.Permission ToEntity(this DT.Permission source) {
458      switch (source) {
459        case DT.Permission.Full: return DA.Permission.Full;
460        case DT.Permission.NotAllowed: return DA.Permission.NotAllowed;
461        case DT.Permission.Read: return DA.Permission.Read;
462        default: return DA.Permission.NotAllowed;
463      }
464    }
465    #endregion
466
467    #region CpuArchiteture
468    public static DT.CpuArchitecture ToDto(this DA.CpuArchitecture source) {
469      switch (source) {
470        case DA.CpuArchitecture.x64: return DT.CpuArchitecture.x64;
471        case DA.CpuArchitecture.x86: return DT.CpuArchitecture.x86;
472        default: return DT.CpuArchitecture.x86;
473      }
474    }
475
476    public static DA.CpuArchitecture ToEntity(this DT.CpuArchitecture source) {
477      switch (source) {
478        case DT.CpuArchitecture.x64: return DA.CpuArchitecture.x64;
479        case DT.CpuArchitecture.x86: return DA.CpuArchitecture.x86;
480        default: return DA.CpuArchitecture.x86;
481      }
482    }
483    #endregion
484
485    #region SlaveState
486    public static DT.SlaveState ToDto(this DA.SlaveState source) {
487      switch (source) {
488        case DA.SlaveState.Calculating: return DT.SlaveState.Calculating;
489        case DA.SlaveState.Idle: return DT.SlaveState.Idle;
490        case DA.SlaveState.Offline: return DT.SlaveState.Offline;
491        default: return DT.SlaveState.Offline;
492      }
493    }
494
495    public static DA.SlaveState ToEntity(this DT.SlaveState source) {
496      switch (source) {
497        case DT.SlaveState.Calculating: return DA.SlaveState.Calculating;
498        case DT.SlaveState.Idle: return DA.SlaveState.Idle;
499        case DT.SlaveState.Offline: return DA.SlaveState.Offline;
500        default: return DA.SlaveState.Offline;
501      }
502    }
503    #endregion
504
505    #region UserPriority
506    public static DT.UserPriority ToDto(this DA.UserPriority source) {
507      if (source == null) return null;
508      return new DT.UserPriority() {
509        Id = source.UserId,
510        DateEnqueued = source.DateEnqueued
511      };
512    }
513    public static DA.UserPriority ToEntity(this DT.UserPriority source) {
514      if (source == null) return null;
515      var result = new DA.UserPriority();
516      source.CopyToEntity(result);
517      return result;
518    }
519    public static void CopyToEntity(this DT.UserPriority source, DA.UserPriority target) {
520      if ((source == null) || (target == null)) return;
521      target.UserId = source.Id;
522      target.DateEnqueued = source.DateEnqueued;
523    }
524    #endregion
525
526  }
527}
Note: See TracBrowser for help on using the repository browser.