Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2839:

  • added Project and ProjectPermission DAOs and service methods
  • made Project.EndDate nullable
File size: 20.1 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        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 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 SlaveGroup
392    public static DT.SlaveGroup ToDto(this DA.SlaveGroup source) {
393      if (source == null) return null;
394      return new DT.SlaveGroup {
395        Id = source.ResourceId,
396        Name = source.Name,
397        ParentResourceId = source.ParentResourceId,
398        HbInterval = source.HbInterval,
399        OwnerUserId = source.OwnerUserId
400      };
401    }
402
403    public static DA.SlaveGroup ToEntity(this DT.SlaveGroup source) {
404      if (source == null) return null;
405      var result = new DA.SlaveGroup();
406      source.CopyToEntity(result);
407      return result;
408    }
409
410    public static void CopyToEntity(this DT.SlaveGroup source, DA.SlaveGroup target) {
411      if ((source == null) || (target == null)) return;
412      target.ResourceId = source.Id;
413      target.Name = source.Name;
414      target.ParentResourceId = source.ParentResourceId;
415      target.HbInterval = source.HbInterval;
416      target.OwnerUserId = source.OwnerUserId;
417    }
418    #endregion
419
420    #region Downtimes
421    public static DT.Downtime ToDto(this DA.Downtime source) {
422      if (source == null) return null;
423      return new DT.Downtime {
424        Id = source.DowntimeId,
425        AllDayEvent = source.AllDayEvent,
426        EndDate = source.EndDate,
427        Recurring = source.Recurring,
428        RecurringId = source.RecurringId,
429        ResourceId = source.ResourceId,
430        StartDate = source.StartDate,
431        DowntimeType = source.DowntimeType
432      };
433    }
434    public static DA.Downtime ToEntity(this DT.Downtime source) {
435      if (source == null) return null;
436      var result = new DA.Downtime();
437      source.CopyToEntity(result);
438      return result;
439    }
440    public static void CopyToEntity(this DT.Downtime source, DA.Downtime target) {
441      if ((source == null) || (target == null)) return;
442      target.DowntimeId = source.Id;
443      target.AllDayEvent = source.AllDayEvent;
444      target.EndDate = source.EndDate;
445      target.Recurring = source.Recurring;
446      target.RecurringId = source.RecurringId;
447      target.ResourceId = source.ResourceId;
448      target.StartDate = source.StartDate;
449      target.DowntimeType = source.DowntimeType;
450    }
451    #endregion
452
453
454    #region Command
455    public static DT.Command? ToDto(this DA.Command? source) {
456      if (source.HasValue) {
457        switch (source) {
458          case DA.Command.Abort: return DT.Command.Abort;
459          case DA.Command.Pause: return DT.Command.Pause;
460          case DA.Command.Stop: return DT.Command.Stop;
461          default: return DT.Command.Pause;
462        }
463      }
464      return null;
465    }
466
467    public static DA.Command? ToEntity(this DT.Command? source) {
468      if (source.HasValue) {
469        switch (source) {
470          case DT.Command.Abort: return DA.Command.Abort;
471          case DT.Command.Pause: return DA.Command.Pause;
472          case DT.Command.Stop: return DA.Command.Stop;
473          default: return DA.Command.Pause;
474        }
475      }
476      return null;
477    }
478    #endregion
479
480    #region Permission
481    public static DT.Permission ToDto(this DA.Permission source) {
482      switch (source) {
483        case DA.Permission.Full: return DT.Permission.Full;
484        case DA.Permission.NotAllowed: return DT.Permission.NotAllowed;
485        case DA.Permission.Read: return DT.Permission.Read;
486        default: return DT.Permission.NotAllowed;
487      }
488    }
489
490    public static DA.Permission ToEntity(this DT.Permission source) {
491      switch (source) {
492        case DT.Permission.Full: return DA.Permission.Full;
493        case DT.Permission.NotAllowed: return DA.Permission.NotAllowed;
494        case DT.Permission.Read: return DA.Permission.Read;
495        default: return DA.Permission.NotAllowed;
496      }
497    }
498    #endregion
499
500    #region CpuArchiteture
501    public static DT.CpuArchitecture ToDto(this DA.CpuArchitecture source) {
502      switch (source) {
503        case DA.CpuArchitecture.x64: return DT.CpuArchitecture.x64;
504        case DA.CpuArchitecture.x86: return DT.CpuArchitecture.x86;
505        default: return DT.CpuArchitecture.x86;
506      }
507    }
508
509    public static DA.CpuArchitecture ToEntity(this DT.CpuArchitecture source) {
510      switch (source) {
511        case DT.CpuArchitecture.x64: return DA.CpuArchitecture.x64;
512        case DT.CpuArchitecture.x86: return DA.CpuArchitecture.x86;
513        default: return DA.CpuArchitecture.x86;
514      }
515    }
516    #endregion
517
518    #region SlaveState
519    public static DT.SlaveState ToDto(this DA.SlaveState source) {
520      switch (source) {
521        case DA.SlaveState.Calculating: return DT.SlaveState.Calculating;
522        case DA.SlaveState.Idle: return DT.SlaveState.Idle;
523        case DA.SlaveState.Offline: return DT.SlaveState.Offline;
524        default: return DT.SlaveState.Offline;
525      }
526    }
527
528    public static DA.SlaveState ToEntity(this DT.SlaveState source) {
529      switch (source) {
530        case DT.SlaveState.Calculating: return DA.SlaveState.Calculating;
531        case DT.SlaveState.Idle: return DA.SlaveState.Idle;
532        case DT.SlaveState.Offline: return DA.SlaveState.Offline;
533        default: return DA.SlaveState.Offline;
534      }
535    }
536    #endregion
537
538    #region UserPriority
539    public static DT.UserPriority ToDto(this DA.UserPriority source) {
540      if (source == null) return null;
541      return new DT.UserPriority() {
542        Id = source.UserId,
543        DateEnqueued = source.DateEnqueued
544      };
545    }
546    public static DA.UserPriority ToEntity(this DT.UserPriority source) {
547      if (source == null) return null;
548      var result = new DA.UserPriority();
549      source.CopyToEntity(result);
550      return result;
551    }
552    public static void CopyToEntity(this DT.UserPriority source, DA.UserPriority target) {
553      if ((source == null) || (target == null)) return;
554      target.UserId = source.Id;
555      target.DateEnqueued = source.DateEnqueued;
556    }
557    #endregion
558
559  }
560}
Note: See TracBrowser for help on using the repository browser.