Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveProjectManagement/HeuristicLab.Services.Hive/3.3/Converter.cs @ 15411

Last change on this file since 15411 was 15411, checked in by jkarder, 7 years ago

#2839: worked on database model

File size: 20.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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      };
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.ProjectId = source.ProjectId;
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 Project
336    public static DT.Project ToDto(this DA.Project source) {
337      if (source == null) return null;
338      return new DT.Project {
339        Id = source.ProjectId,
340        ParentProjectId = source.ParentProjectId,
341        DateCreated = source.DateCreated,
342        Name = source.Name,
343        Description = source.Description,
344        OwnerUserId = source.OwnerUserId,
345        StartDate = source.StartDate,
346        EndDate = source.EndDate
347      };
348    }
349    public static DA.Project ToEntity(this DT.Project source) {
350      if (source == null) return null;
351      var result = new DA.Project();
352      source.CopyToEntity(result);
353      return result;
354    }
355    public static void CopyToEntity(this DT.Project source, DA.Project target) {
356      if ((source == null) || (target == null)) return;
357      target.ProjectId = source.Id;
358      target.ParentProjectId = source.ParentProjectId;
359      target.DateCreated = source.DateCreated;
360      target.Name = source.Name;
361      target.Description = source.Description;
362      target.OwnerUserId = source.OwnerUserId;
363      target.StartDate = source.StartDate;
364      target.EndDate = source.EndDate;
365    }
366    #endregion
367
368    #region ProjectPermission
369    public static DT.ProjectPermission ToDto(this DA.ProjectPermission source) {
370      if (source == null) return null;
371      return new DT.ProjectPermission {
372        ProjectId = source.ProjectId,
373        GrantedUserId = source.GrantedUserId,
374        GrantedByUserId = source.GrantedByUserId
375      };
376    }
377    public static DA.ProjectPermission ToEntity(this DT.ProjectPermission source) {
378      if (source == null) return null;
379      var result = new DA.ProjectPermission();
380      source.CopyToEntity(result);
381      return result;
382    }
383    public static void CopyToEntity(this DT.ProjectPermission source, DA.ProjectPermission target) {
384      if ((source == null) || (target == null)) return;
385      target.ProjectId = source.ProjectId;
386      target.GrantedUserId = source.GrantedUserId;
387      target.GrantedByUserId = source.GrantedByUserId;
388    }
389    #endregion
390
391    #region AssignedProjectResource
392    public static DT.AssignedProjectResource ToDto(this DA.AssignedProjectResource source) {
393      if (source == null) return null;
394      return new DT.AssignedProjectResource {
395        ProjectId = source.ProjectId,
396        ResourceId = source.ResourceId
397      };
398    }
399    public static DA.AssignedProjectResource ToEntity(this DT.AssignedProjectResource source) {
400      if (source == null) return null;
401      var result = new DA.AssignedProjectResource();
402      source.CopyToEntity(result);
403      return result;
404    }
405    public static void CopyToEntity(this DT.AssignedProjectResource source, DA.AssignedProjectResource target) {
406      if ((source == null) || (target == null)) return;
407      target.ProjectId = source.ProjectId;
408      target.ResourceId = source.ResourceId;
409    }
410    #endregion
411
412    #region SlaveGroup
413    public static DT.SlaveGroup ToDto(this DA.SlaveGroup source) {
414      if (source == null) return null;
415      return new DT.SlaveGroup {
416        Id = source.ResourceId,
417        Name = source.Name,
418        ParentResourceId = source.ParentResourceId,
419        HbInterval = source.HbInterval,
420        OwnerUserId = source.OwnerUserId
421      };
422    }
423
424    public static DA.SlaveGroup ToEntity(this DT.SlaveGroup source) {
425      if (source == null) return null;
426      var result = new DA.SlaveGroup();
427      source.CopyToEntity(result);
428      return result;
429    }
430
431    public static void CopyToEntity(this DT.SlaveGroup source, DA.SlaveGroup target) {
432      if ((source == null) || (target == null)) return;
433      target.ResourceId = source.Id;
434      target.Name = source.Name;
435      target.ParentResourceId = source.ParentResourceId;
436      target.HbInterval = source.HbInterval;
437      target.OwnerUserId = source.OwnerUserId;
438    }
439    #endregion
440
441    #region Downtimes
442    public static DT.Downtime ToDto(this DA.Downtime source) {
443      if (source == null) return null;
444      return new DT.Downtime {
445        Id = source.DowntimeId,
446        AllDayEvent = source.AllDayEvent,
447        EndDate = source.EndDate,
448        Recurring = source.Recurring,
449        RecurringId = source.RecurringId,
450        ResourceId = source.ResourceId,
451        StartDate = source.StartDate,
452        DowntimeType = source.DowntimeType
453      };
454    }
455    public static DA.Downtime ToEntity(this DT.Downtime source) {
456      if (source == null) return null;
457      var result = new DA.Downtime();
458      source.CopyToEntity(result);
459      return result;
460    }
461    public static void CopyToEntity(this DT.Downtime source, DA.Downtime target) {
462      if ((source == null) || (target == null)) return;
463      target.DowntimeId = source.Id;
464      target.AllDayEvent = source.AllDayEvent;
465      target.EndDate = source.EndDate;
466      target.Recurring = source.Recurring;
467      target.RecurringId = source.RecurringId;
468      target.ResourceId = source.ResourceId;
469      target.StartDate = source.StartDate;
470      target.DowntimeType = source.DowntimeType;
471    }
472    #endregion
473
474
475    #region Command
476    public static DT.Command? ToDto(this DA.Command? source) {
477      if (source.HasValue) {
478        switch (source) {
479          case DA.Command.Abort: return DT.Command.Abort;
480          case DA.Command.Pause: return DT.Command.Pause;
481          case DA.Command.Stop: return DT.Command.Stop;
482          default: return DT.Command.Pause;
483        }
484      }
485      return null;
486    }
487
488    public static DA.Command? ToEntity(this DT.Command? source) {
489      if (source.HasValue) {
490        switch (source) {
491          case DT.Command.Abort: return DA.Command.Abort;
492          case DT.Command.Pause: return DA.Command.Pause;
493          case DT.Command.Stop: return DA.Command.Stop;
494          default: return DA.Command.Pause;
495        }
496      }
497      return null;
498    }
499    #endregion
500
501    #region Permission
502    public static DT.Permission ToDto(this DA.Permission source) {
503      switch (source) {
504        case DA.Permission.Full: return DT.Permission.Full;
505        case DA.Permission.NotAllowed: return DT.Permission.NotAllowed;
506        case DA.Permission.Read: return DT.Permission.Read;
507        default: return DT.Permission.NotAllowed;
508      }
509    }
510
511    public static DA.Permission ToEntity(this DT.Permission source) {
512      switch (source) {
513        case DT.Permission.Full: return DA.Permission.Full;
514        case DT.Permission.NotAllowed: return DA.Permission.NotAllowed;
515        case DT.Permission.Read: return DA.Permission.Read;
516        default: return DA.Permission.NotAllowed;
517      }
518    }
519    #endregion
520
521    #region CpuArchiteture
522    public static DT.CpuArchitecture ToDto(this DA.CpuArchitecture source) {
523      switch (source) {
524        case DA.CpuArchitecture.x64: return DT.CpuArchitecture.x64;
525        case DA.CpuArchitecture.x86: return DT.CpuArchitecture.x86;
526        default: return DT.CpuArchitecture.x86;
527      }
528    }
529
530    public static DA.CpuArchitecture ToEntity(this DT.CpuArchitecture source) {
531      switch (source) {
532        case DT.CpuArchitecture.x64: return DA.CpuArchitecture.x64;
533        case DT.CpuArchitecture.x86: return DA.CpuArchitecture.x86;
534        default: return DA.CpuArchitecture.x86;
535      }
536    }
537    #endregion
538
539    #region SlaveState
540    public static DT.SlaveState ToDto(this DA.SlaveState source) {
541      switch (source) {
542        case DA.SlaveState.Calculating: return DT.SlaveState.Calculating;
543        case DA.SlaveState.Idle: return DT.SlaveState.Idle;
544        case DA.SlaveState.Offline: return DT.SlaveState.Offline;
545        default: return DT.SlaveState.Offline;
546      }
547    }
548
549    public static DA.SlaveState ToEntity(this DT.SlaveState source) {
550      switch (source) {
551        case DT.SlaveState.Calculating: return DA.SlaveState.Calculating;
552        case DT.SlaveState.Idle: return DA.SlaveState.Idle;
553        case DT.SlaveState.Offline: return DA.SlaveState.Offline;
554        default: return DA.SlaveState.Offline;
555      }
556    }
557    #endregion
558
559    #region UserPriority
560    public static DT.UserPriority ToDto(this DA.UserPriority source) {
561      if (source == null) return null;
562      return new DT.UserPriority() {
563        Id = source.UserId,
564        DateEnqueued = source.DateEnqueued
565      };
566    }
567    public static DA.UserPriority ToEntity(this DT.UserPriority source) {
568      if (source == null) return null;
569      var result = new DA.UserPriority();
570      source.CopyToEntity(result);
571      return result;
572    }
573    public static void CopyToEntity(this DT.UserPriority source, DA.UserPriority target) {
574      if ((source == null) || (target == null)) return;
575      target.UserId = source.Id;
576      target.DateEnqueued = source.DateEnqueued;
577    }
578    #endregion
579
580  }
581}
Note: See TracBrowser for help on using the repository browser.