Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Services.Hive/3.3/Converter.cs @ 16124

Last change on this file since 16124 was 16117, checked in by jkarder, 6 years ago

#2839: merged [15377-16116/branches/2839_HiveProjectManagement] into trunk

File size: 22.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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        ProjectId = source.ProjectId,
124        State = source.State.ToDto()
125      };
126    }
127
128    public static DA.Job ToEntity(this DT.Job source) {
129      if (source == null) return null;
130      var result = new DA.Job();
131      source.CopyToEntity(result);
132      return result;
133    }
134
135    public static void CopyToEntity(this DT.Job source, DA.Job target) {
136      if ((source == null) || (target == null)) return;
137      target.JobId = source.Id;
138      target.Description = source.Description;
139      target.Name = source.Name;
140      target.OwnerUserId = source.OwnerUserId;
141      target.DateCreated = source.DateCreated;
142      target.ProjectId = source.ProjectId;
143      target.State = source.State.ToEntity();
144    }
145    #endregion
146
147    #region AssignedJobResource
148    public static DT.AssignedJobResource ToDto(this DA.AssignedJobResource source) {
149      if (source == null) return null;
150      return new DT.AssignedJobResource {
151        JobId = source.JobId,
152        ResourceId = source.ResourceId
153      };
154    }
155    public static DA.AssignedJobResource ToEntity(this DT.AssignedJobResource source) {
156      if (source == null) return null;
157      var result = new DA.AssignedJobResource();
158      source.CopyToEntity(result);
159      return result;
160    }
161    public static void CopyToEntity(this DT.AssignedJobResource source, DA.AssignedJobResource target) {
162      if ((source == null) || (target == null)) return;
163      target.JobId = source.JobId;
164      target.ResourceId = source.ResourceId;
165    }
166    #endregion
167
168    #region JobPermission
169    public static DT.JobPermission ToDto(this DA.JobPermission source) {
170      if (source == null) return null;
171      return new DT.JobPermission {
172        JobId = source.JobId,
173        GrantedUserId = source.GrantedUserId,
174        GrantedByUserId = source.GrantedByUserId,
175        Permission = source.Permission.ToDto()
176      };
177    }
178
179    public static DA.JobPermission ToEntity(this DT.JobPermission source) {
180      if (source == null) return null;
181      var result = new DA.JobPermission();
182      source.CopyToEntity(result);
183      return result;
184    }
185
186    public static void CopyToEntity(this DT.JobPermission source, DA.JobPermission target) {
187      if ((source == null) || (target == null)) return;
188      target.JobId = source.JobId;
189      target.GrantedUserId = source.GrantedUserId;
190      target.GrantedByUserId = source.GrantedByUserId;
191      target.Permission = source.Permission.ToEntity();
192    }
193    #endregion
194
195    #region Slave
196    public static DT.Slave ToDto(this DA.Slave source) {
197      if (source == null) return null;
198      return new DT.Slave {
199        Id = source.ResourceId,
200        ParentResourceId = source.ParentResourceId,
201        Cores = source.Cores,
202        CpuSpeed = source.CpuSpeed,
203        FreeCores = source.FreeCores,
204        FreeMemory = source.FreeMemory,
205        IsAllowedToCalculate = source.IsAllowedToCalculate,
206        Memory = source.Memory,
207        Name = source.Name,
208        SlaveState = source.SlaveState.ToDto(),
209        CpuArchitecture = source.CpuArchitecture.ToDto(),
210        OperatingSystem = source.OperatingSystem,
211        LastHeartbeat = source.LastHeartbeat,
212        CpuUtilization = source.CpuUtilization,
213        HbInterval = source.HbInterval,
214        IsDisposable = source.IsDisposable,
215        OwnerUserId = source.OwnerUserId
216      };
217    }
218    public static DA.Slave ToEntity(this DT.Slave source) {
219      if (source == null) return null;
220      var result = new DA.Slave();
221      source.CopyToEntity(result);
222      return result;
223    }
224    public static void CopyToEntity(this DT.Slave source, DA.Slave target) {
225      if ((source == null) || (target == null)) return;
226      target.ResourceId = source.Id;
227      target.ParentResourceId = source.ParentResourceId;
228      target.Cores = source.Cores;
229      target.CpuSpeed = source.CpuSpeed;
230      target.FreeCores = source.FreeCores;
231      target.FreeMemory = source.FreeMemory;
232      target.IsAllowedToCalculate = source.IsAllowedToCalculate;
233      target.Memory = source.Memory;
234      target.Name = source.Name;
235      target.SlaveState = source.SlaveState.ToEntity();
236      target.CpuArchitecture = source.CpuArchitecture.ToEntity();
237      target.OperatingSystem = source.OperatingSystem;
238      target.LastHeartbeat = source.LastHeartbeat;
239      target.CpuUtilization = source.CpuUtilization;
240      target.HbInterval = source.HbInterval;
241      target.IsDisposable = source.IsDisposable;
242      target.OwnerUserId = source.OwnerUserId;
243    }
244    #endregion
245
246    #region TaskState
247    public static DT.TaskState ToDto(this DA.TaskState source) {
248      switch (source) {
249        case DA.TaskState.Aborted: return DT.TaskState.Aborted;
250        case DA.TaskState.Calculating: return DT.TaskState.Calculating;
251        case DA.TaskState.Failed: return DT.TaskState.Failed;
252        case DA.TaskState.Finished: return DT.TaskState.Finished;
253        case DA.TaskState.Offline: return DT.TaskState.Offline;
254        case DA.TaskState.Paused: return DT.TaskState.Paused;
255        case DA.TaskState.Transferring: return DT.TaskState.Transferring;
256        case DA.TaskState.Waiting: return DT.TaskState.Waiting;
257        default: return DT.TaskState.Failed;
258      }
259    }
260
261    public static DA.TaskState ToEntity(this DT.TaskState source) {
262      switch (source) {
263        case DT.TaskState.Aborted: return DA.TaskState.Aborted;
264        case DT.TaskState.Calculating: return DA.TaskState.Calculating;
265        case DT.TaskState.Failed: return DA.TaskState.Failed;
266        case DT.TaskState.Finished: return DA.TaskState.Finished;
267        case DT.TaskState.Offline: return DA.TaskState.Offline;
268        case DT.TaskState.Paused: return DA.TaskState.Paused;
269        case DT.TaskState.Transferring: return DA.TaskState.Transferring;
270        case DT.TaskState.Waiting: return DA.TaskState.Waiting;
271        default: return DA.TaskState.Failed;
272      }
273    }
274    #endregion
275
276    #region JobState
277    public static DT.JobState ToDto(this DA.JobState source) {
278      switch (source) {
279        case DA.JobState.Online: return DT.JobState.Online;
280        case DA.JobState.StatisticsPending: return DT.JobState.StatisticsPending;
281        case DA.JobState.DeletionPending: return DT.JobState.DeletionPending;
282        default: return DT.JobState.Online;
283      }
284    }
285
286    public static DA.JobState ToEntity(this DT.JobState source) {
287      switch (source) {
288        case DT.JobState.Online: return DA.JobState.Online;
289        case DT.JobState.StatisticsPending: return DA.JobState.StatisticsPending;
290        case DT.JobState.DeletionPending: return DA.JobState.DeletionPending;
291        default: return DA.JobState.Online;
292      }
293    }
294    #endregion
295
296    #region StateLogs
297    public static DT.StateLog ToDto(this DA.StateLog source) {
298      return new DT.StateLog {
299        Id = source.StateLogId,
300        State = source.State.ToDto(),
301        DateTime = source.DateTime,
302        TaskId = source.TaskId,
303        UserId = source.UserId,
304        SlaveId = source.SlaveId,
305        Exception = source.Exception
306      };
307    }
308
309    public static DA.StateLog ToEntity(this DT.StateLog source) {
310      return new DA.StateLog {
311        StateLogId = source.Id,
312        State = source.State.ToEntity(),
313        DateTime = source.DateTime,
314        TaskId = source.TaskId,
315        UserId = source.UserId,
316        SlaveId = source.SlaveId,
317        Exception = source.Exception
318      };
319    }
320    #endregion
321
322    #region Plugin
323    public static DT.Plugin ToDto(this DA.Plugin source) {
324      if (source == null) return null;
325      return new DT.Plugin {
326        Id = source.PluginId,
327        Name = source.Name,
328        Version = new Version(source.Version),
329        UserId = source.UserId,
330        DateCreated = source.DateCreated,
331        Hash = source.Hash
332      };
333    }
334    public static DA.Plugin ToEntity(this DT.Plugin source) {
335      if (source == null) return null;
336      var result = new DA.Plugin();
337      source.CopyToEntity(result);
338      return result;
339    }
340    public static void CopyToEntity(this DT.Plugin source, DA.Plugin target) {
341      if ((source == null) || (target == null)) return;
342      target.PluginId = source.Id;
343      target.Name = source.Name;
344      target.Version = source.Version.ToString();
345      target.UserId = source.UserId;
346      target.DateCreated = source.DateCreated;
347      target.Hash = source.Hash;
348    }
349    #endregion
350
351    #region PluginData
352    public static DT.PluginData ToDto(this DA.PluginData source) {
353      if (source == null) return null;
354      return new DT.PluginData {
355        Id = source.PluginDataId,
356        PluginId = source.PluginId,
357        Data = source.Data.ToArray(),
358        FileName = source.FileName
359      };
360    }
361
362    public static DA.PluginData ToEntity(this DT.PluginData source) {
363      if (source == null) return null;
364      var result = new DA.PluginData();
365      source.CopyToEntity(result);
366      return result;
367    }
368
369    public static void CopyToEntity(this DT.PluginData source, DA.PluginData target) {
370      if ((source == null) || (target == null)) return;
371      target.PluginDataId = source.Id;
372      target.PluginId = source.PluginId;
373      target.Data = source.Data;
374      target.FileName = source.FileName;
375    }
376    #endregion
377
378    #region Project
379    public static DT.Project ToDto(this DA.Project source) {
380      if (source == null) return null;
381      return new DT.Project {
382        Id = source.ProjectId,
383        ParentProjectId = source.ParentProjectId,
384        DateCreated = source.DateCreated,
385        Name = source.Name,
386        Description = source.Description,
387        OwnerUserId = source.OwnerUserId,
388        StartDate = source.StartDate,
389        EndDate = source.EndDate
390      };
391    }
392    public static DA.Project ToEntity(this DT.Project source) {
393      if (source == null) return null;
394      var result = new DA.Project();
395      source.CopyToEntity(result);
396      return result;
397    }
398    public static void CopyToEntity(this DT.Project source, DA.Project target) {
399      if ((source == null) || (target == null)) return;
400      target.ProjectId = source.Id;
401      target.ParentProjectId = source.ParentProjectId;
402      target.DateCreated = source.DateCreated;
403      target.Name = source.Name;
404      target.Description = source.Description;
405      target.OwnerUserId = source.OwnerUserId;
406      target.StartDate = source.StartDate;
407      target.EndDate = source.EndDate;
408    }
409    #endregion
410
411    #region ProjectPermission
412    public static DT.ProjectPermission ToDto(this DA.ProjectPermission source) {
413      if (source == null) return null;
414      return new DT.ProjectPermission {
415        ProjectId = source.ProjectId,
416        GrantedUserId = source.GrantedUserId,
417        GrantedByUserId = source.GrantedByUserId
418      };
419    }
420    public static DA.ProjectPermission ToEntity(this DT.ProjectPermission source) {
421      if (source == null) return null;
422      var result = new DA.ProjectPermission();
423      source.CopyToEntity(result);
424      return result;
425    }
426    public static void CopyToEntity(this DT.ProjectPermission source, DA.ProjectPermission target) {
427      if ((source == null) || (target == null)) return;
428      target.ProjectId = source.ProjectId;
429      target.GrantedUserId = source.GrantedUserId;
430      target.GrantedByUserId = source.GrantedByUserId;
431    }
432    #endregion
433
434    #region AssignedProjectResource
435    public static DT.AssignedProjectResource ToDto(this DA.AssignedProjectResource source) {
436      if (source == null) return null;
437      return new DT.AssignedProjectResource {
438        ProjectId = source.ProjectId,
439        ResourceId = source.ResourceId
440      };
441    }
442    public static DA.AssignedProjectResource ToEntity(this DT.AssignedProjectResource source) {
443      if (source == null) return null;
444      var result = new DA.AssignedProjectResource();
445      source.CopyToEntity(result);
446      return result;
447    }
448    public static void CopyToEntity(this DT.AssignedProjectResource source, DA.AssignedProjectResource target) {
449      if ((source == null) || (target == null)) return;
450      target.ProjectId = source.ProjectId;
451      target.ResourceId = source.ResourceId;
452    }
453    #endregion
454
455    #region SlaveGroup
456    public static DT.SlaveGroup ToDto(this DA.SlaveGroup source) {
457      if (source == null) return null;
458      return new DT.SlaveGroup {
459        Id = source.ResourceId,
460        Name = source.Name,
461        ParentResourceId = source.ParentResourceId,
462        HbInterval = source.HbInterval,
463        OwnerUserId = source.OwnerUserId
464      };
465    }
466
467    public static DA.SlaveGroup ToEntity(this DT.SlaveGroup source) {
468      if (source == null) return null;
469      var result = new DA.SlaveGroup();
470      source.CopyToEntity(result);
471      return result;
472    }
473
474    public static void CopyToEntity(this DT.SlaveGroup source, DA.SlaveGroup target) {
475      if ((source == null) || (target == null)) return;
476      target.ResourceId = source.Id;
477      target.Name = source.Name;
478      target.ParentResourceId = source.ParentResourceId;
479      target.HbInterval = source.HbInterval;
480      target.OwnerUserId = source.OwnerUserId;
481    }
482    #endregion
483
484    #region Downtimes
485    public static DT.Downtime ToDto(this DA.Downtime source) {
486      if (source == null) return null;
487      return new DT.Downtime {
488        Id = source.DowntimeId,
489        AllDayEvent = source.AllDayEvent,
490        EndDate = source.EndDate,
491        Recurring = source.Recurring,
492        RecurringId = source.RecurringId,
493        ResourceId = source.ResourceId,
494        StartDate = source.StartDate,
495        DowntimeType = source.DowntimeType
496      };
497    }
498    public static DA.Downtime ToEntity(this DT.Downtime source) {
499      if (source == null) return null;
500      var result = new DA.Downtime();
501      source.CopyToEntity(result);
502      return result;
503    }
504    public static void CopyToEntity(this DT.Downtime source, DA.Downtime target) {
505      if ((source == null) || (target == null)) return;
506      target.DowntimeId = source.Id;
507      target.AllDayEvent = source.AllDayEvent;
508      target.EndDate = source.EndDate;
509      target.Recurring = source.Recurring;
510      target.RecurringId = source.RecurringId;
511      target.ResourceId = source.ResourceId;
512      target.StartDate = source.StartDate;
513      target.DowntimeType = source.DowntimeType;
514    }
515    #endregion
516
517
518    #region Command
519    public static DT.Command? ToDto(this DA.Command? source) {
520      if (source.HasValue) {
521        switch (source) {
522          case DA.Command.Abort: return DT.Command.Abort;
523          case DA.Command.Pause: return DT.Command.Pause;
524          case DA.Command.Stop: return DT.Command.Stop;
525          default: return DT.Command.Pause;
526        }
527      }
528      return null;
529    }
530
531    public static DA.Command? ToEntity(this DT.Command? source) {
532      if (source.HasValue) {
533        switch (source) {
534          case DT.Command.Abort: return DA.Command.Abort;
535          case DT.Command.Pause: return DA.Command.Pause;
536          case DT.Command.Stop: return DA.Command.Stop;
537          default: return DA.Command.Pause;
538        }
539      }
540      return null;
541    }
542    #endregion
543
544    #region Permission
545    public static DT.Permission ToDto(this DA.Permission source) {
546      switch (source) {
547        case DA.Permission.Full: return DT.Permission.Full;
548        case DA.Permission.NotAllowed: return DT.Permission.NotAllowed;
549        case DA.Permission.Read: return DT.Permission.Read;
550        default: return DT.Permission.NotAllowed;
551      }
552    }
553
554    public static DA.Permission ToEntity(this DT.Permission source) {
555      switch (source) {
556        case DT.Permission.Full: return DA.Permission.Full;
557        case DT.Permission.NotAllowed: return DA.Permission.NotAllowed;
558        case DT.Permission.Read: return DA.Permission.Read;
559        default: return DA.Permission.NotAllowed;
560      }
561    }
562    #endregion
563
564    #region CpuArchiteture
565    public static DT.CpuArchitecture ToDto(this DA.CpuArchitecture source) {
566      switch (source) {
567        case DA.CpuArchitecture.x64: return DT.CpuArchitecture.x64;
568        case DA.CpuArchitecture.x86: return DT.CpuArchitecture.x86;
569        default: return DT.CpuArchitecture.x86;
570      }
571    }
572
573    public static DA.CpuArchitecture ToEntity(this DT.CpuArchitecture source) {
574      switch (source) {
575        case DT.CpuArchitecture.x64: return DA.CpuArchitecture.x64;
576        case DT.CpuArchitecture.x86: return DA.CpuArchitecture.x86;
577        default: return DA.CpuArchitecture.x86;
578      }
579    }
580    #endregion
581
582    #region SlaveState
583    public static DT.SlaveState ToDto(this DA.SlaveState source) {
584      switch (source) {
585        case DA.SlaveState.Calculating: return DT.SlaveState.Calculating;
586        case DA.SlaveState.Idle: return DT.SlaveState.Idle;
587        case DA.SlaveState.Offline: return DT.SlaveState.Offline;
588        default: return DT.SlaveState.Offline;
589      }
590    }
591
592    public static DA.SlaveState ToEntity(this DT.SlaveState source) {
593      switch (source) {
594        case DT.SlaveState.Calculating: return DA.SlaveState.Calculating;
595        case DT.SlaveState.Idle: return DA.SlaveState.Idle;
596        case DT.SlaveState.Offline: return DA.SlaveState.Offline;
597        default: return DA.SlaveState.Offline;
598      }
599    }
600    #endregion
601
602    #region UserPriority
603    public static DT.UserPriority ToDto(this DA.UserPriority source) {
604      if (source == null) return null;
605      return new DT.UserPriority() {
606        Id = source.UserId,
607        DateEnqueued = source.DateEnqueued
608      };
609    }
610    public static DA.UserPriority ToEntity(this DT.UserPriority source) {
611      if (source == null) return null;
612      var result = new DA.UserPriority();
613      source.CopyToEntity(result);
614      return result;
615    }
616    public static void CopyToEntity(this DT.UserPriority source, DA.UserPriority target) {
617      if ((source == null) || (target == null)) return;
618      target.UserId = source.Id;
619      target.DateEnqueued = source.DateEnqueued;
620    }
621    #endregion
622
623  }
624}
Note: See TracBrowser for help on using the repository browser.