Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.Hive/3.3/Convert.cs @ 9120

Last change on this file since 9120 was 8957, checked in by ascheibe, 12 years ago

#1986 merged SlaveShutdown branch back into trunk

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