Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15628 was 15628, checked in by jzenisek, 6 years ago

#2839

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