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

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

#2355:

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