Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7862 was 7857, checked in by jkarder, 12 years ago

#1854:

  • integrated marker attribute (IsDisposable) into the Hive components
  • adjusted database scheme
  • extended administration user interface
File size: 21.2 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 };
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;
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 Slave
203    public static DT.Slave ToDto(DB.Slave source) {
204      if (source == null) return null;
205      return new DT.Slave {
206        Id = source.ResourceId,
207        ParentResourceId = source.ParentResourceId,
208        Cores = source.Cores,
209        CpuSpeed = source.CpuSpeed,
210        FreeCores = source.FreeCores,
211        FreeMemory = source.FreeMemory,
212        IsAllowedToCalculate = source.IsAllowedToCalculate,
213        Memory = source.Memory,
214        Name = source.Name,
215        SlaveState = Convert.ToDto(source.SlaveState),
216        CpuArchitecture = Convert.ToDto(source.CpuArchitecture),
217        OperatingSystem = source.OperatingSystem,
218        LastHeartbeat = source.LastHeartbeat,
219        CpuUtilization = source.CpuUtilization,
220        HbInterval = source.HbInterval,
221        IsDisposable = source.IsDisposable
222      };
223    }
224    public static DB.Slave ToEntity(DT.Slave source) {
225      if (source == null) return null;
226      var entity = new DB.Slave(); ToEntity(source, entity);
227      return entity;
228    }
229    public static void ToEntity(DT.Slave source, DB.Slave target) {
230      if ((source != null) && (target != null)) {
231        target.ResourceId = source.Id;
232        target.ParentResourceId = source.ParentResourceId;
233        target.Cores = source.Cores;
234        target.CpuSpeed = source.CpuSpeed;
235        target.FreeCores = source.FreeCores;
236        target.FreeMemory = source.FreeMemory;
237        target.IsAllowedToCalculate = source.IsAllowedToCalculate;
238        target.Memory = source.Memory;
239        target.Name = source.Name;
240        target.SlaveState = Convert.ToEntity(source.SlaveState);
241        target.CpuArchitecture = Convert.ToEntity(source.CpuArchitecture);
242        target.OperatingSystem = source.OperatingSystem;
243        target.LastHeartbeat = source.LastHeartbeat;
244        target.CpuUtilization = source.CpuUtilization;
245        target.HbInterval = source.HbInterval;
246        target.IsDisposable = source.IsDisposable;
247      }
248    }
249    #endregion
250
251    #region SlaveGroup
252    public static DT.SlaveGroup ToDto(DB.SlaveGroup source) {
253      if (source == null) return null;
254      return new DT.SlaveGroup { Id = source.ResourceId, Name = source.Name, ParentResourceId = source.ParentResourceId, HbInterval = source.HbInterval };
255    }
256    public static DB.SlaveGroup ToEntity(DT.SlaveGroup source) {
257      if (source == null) return null;
258      var entity = new DB.SlaveGroup(); ToEntity(source, entity);
259      return entity;
260    }
261    public static void ToEntity(DT.SlaveGroup source, DB.SlaveGroup target) {
262      if ((source != null) && (target != null)) {
263        target.ResourceId = source.Id; target.Name = source.Name; target.ParentResourceId = source.ParentResourceId; target.HbInterval = source.HbInterval;
264      }
265    }
266    #endregion
267
268    #region Resource
269    public static DT.Resource ToDto(DB.Resource source) {
270      if (source == null) return null;
271      return new DT.Resource { Id = source.ResourceId, Name = source.Name, ParentResourceId = source.ParentResourceId, HbInterval = source.HbInterval };
272    }
273    public static DB.Resource ToEntity(DT.Resource source) {
274      if (source == null) return null;
275      var entity = new DB.Resource(); ToEntity(source, entity);
276      return entity;
277    }
278    public static void ToEntity(DT.Resource source, DB.Resource target) {
279      if ((source != null) && (target != null)) {
280        target.ResourceId = source.Id; target.Name = source.Name; target.ParentResourceId = source.ParentResourceId; target.HbInterval = source.HbInterval;
281      }
282    }
283    #endregion
284
285    #region Statistics
286    public static DT.Statistics ToDto(DB.Statistics source) {
287      if (source == null) return null;
288      return new DT.Statistics {
289        Id = source.StatisticsId,
290        TimeStamp = source.Timestamp,
291        SlaveStatistics = source.SlaveStatistics.Select(x => Convert.ToDto(x)).ToArray(),
292        UserStatistics = source.UserStatistics.Select(x => Convert.ToDto(x)).ToArray()
293      };
294    }
295    public static DB.Statistics ToEntity(DT.Statistics source) {
296      if (source == null) return null;
297      var entity = new DB.Statistics(); ToEntity(source, entity);
298      return entity;
299    }
300    public static void ToEntity(DT.Statistics source, DB.Statistics target) {
301      if ((source != null) && (target != null)) {
302        target.StatisticsId = source.Id;
303        target.Timestamp = source.TimeStamp;
304
305      }
306    }
307    #endregion
308
309    #region SlaveStatistics
310    public static DT.SlaveStatistics ToDto(DB.SlaveStatistics source) {
311      if (source == null) return null;
312      return new DT.SlaveStatistics {
313        Id = source.StatisticsId,
314        SlaveId = source.SlaveId,
315        Cores = source.Cores,
316        CpuUtilization = source.CpuUtilization,
317        FreeCores = source.FreeCores,
318        FreeMemory = source.FreeMemory,
319        Memory = source.Memory
320      };
321    }
322    public static DB.SlaveStatistics ToEntity(DT.SlaveStatistics source) {
323      if (source == null) return null;
324      var entity = new DB.SlaveStatistics(); ToEntity(source, entity);
325      return entity;
326    }
327    public static void ToEntity(DT.SlaveStatistics source, DB.SlaveStatistics target) {
328      if ((source != null) && (target != null)) {
329        target.StatisticsId = source.Id;
330        target.SlaveId = source.SlaveId;
331        target.Cores = source.Cores;
332        target.CpuUtilization = source.CpuUtilization;
333        target.FreeCores = source.FreeCores;
334        target.FreeMemory = source.FreeMemory;
335        target.Memory = source.Memory;
336      }
337    }
338    #endregion
339
340    #region UserStatistics
341    public static DT.UserStatistics ToDto(DB.UserStatistics source) {
342      if (source == null) return null;
343      return new DT.UserStatistics {
344        Id = source.StatisticsId,
345        UserId = source.UserId,
346        UsedCores = source.UsedCores,
347        ExecutionTime = TimeSpan.FromMilliseconds(source.ExecutionTimeMs),
348        ExecutionTimeFinishedJobs = TimeSpan.FromMilliseconds(source.ExecutionTimeMsFinishedJobs),
349        StartToEndTime = TimeSpan.FromMilliseconds(source.StartToEndTimeMs)
350      };
351    }
352    public static DB.UserStatistics ToEntity(DT.UserStatistics source) {
353      if (source == null) return null;
354      var entity = new DB.UserStatistics(); ToEntity(source, entity);
355      return entity;
356    }
357    public static void ToEntity(DT.UserStatistics source, DB.UserStatistics target) {
358      if ((source != null) && (target != null)) {
359        target.StatisticsId = source.Id;
360        target.UserId = source.UserId;
361        target.UsedCores = source.UsedCores;
362        target.ExecutionTimeMs = source.ExecutionTime.TotalMilliseconds;
363        target.ExecutionTimeMsFinishedJobs = source.ExecutionTimeFinishedJobs.TotalMilliseconds;
364        target.StartToEndTimeMs = source.StartToEndTime.TotalMilliseconds;
365      }
366    }
367    #endregion
368
369    #region TaskData
370    public static DT.TaskState ToDto(DB.TaskState source) {
371      if (source == DB.TaskState.Aborted) {
372        return TaskState.Aborted;
373      } else if (source == DB.TaskState.Calculating) {
374        return TaskState.Calculating;
375      } else if (source == DB.TaskState.Failed) {
376        return TaskState.Failed;
377      } else if (source == DB.TaskState.Finished) {
378        return TaskState.Finished;
379      } else if (source == DB.TaskState.Offline) {
380        return TaskState.Offline;
381      } else if (source == DB.TaskState.Paused) {
382        return TaskState.Paused;
383      } else if (source == DB.TaskState.Transferring) {
384        return TaskState.Transferring;
385      } else if (source == DB.TaskState.Waiting) {
386        return TaskState.Waiting;
387      } else
388        return TaskState.Failed;
389    }
390
391    public static DB.TaskState ToEntity(DT.TaskState source) {
392      if (source == DT.TaskState.Aborted) {
393        return DB.TaskState.Aborted;
394      } else if (source == DT.TaskState.Calculating) {
395        return DB.TaskState.Calculating;
396      } else if (source == DT.TaskState.Failed) {
397        return DB.TaskState.Failed;
398      } else if (source == DT.TaskState.Finished) {
399        return DB.TaskState.Finished;
400      } else if (source == DT.TaskState.Offline) {
401        return DB.TaskState.Offline;
402      } else if (source == DT.TaskState.Paused) {
403        return DB.TaskState.Paused;
404      } else if (source == DT.TaskState.Transferring) {
405        return DB.TaskState.Transferring;
406      } else if (source == DT.TaskState.Waiting) {
407        return DB.TaskState.Waiting;
408      } else
409        return DB.TaskState.Failed;
410    }
411    #endregion
412
413    #region Permission
414    public static DT.Permission ToDto(DB.Permission source) {
415      if (source == DB.Permission.Full) {
416        return Permission.Full;
417      } else if (source == DB.Permission.NotAllowed) {
418        return Permission.NotAllowed;
419      } else if (source == DB.Permission.Read) {
420        return Permission.Read;
421      } else
422        return Permission.NotAllowed;
423    }
424
425    public static DB.Permission ToEntity(DT.Permission source) {
426      if (source == DT.Permission.Full) {
427        return DB.Permission.Full;
428      } else if (source == DT.Permission.NotAllowed) {
429        return DB.Permission.NotAllowed;
430      } else if (source == DT.Permission.Read) {
431        return DB.Permission.Read;
432      } else
433        return DB.Permission.NotAllowed;
434    }
435    #endregion
436
437    #region Command
438    public static DT.Command? ToDto(DB.Command? source) {
439      if (source.HasValue) {
440        if (source.Value == DB.Command.Abort) {
441          return Command.Abort;
442        } else if (source.Value == DB.Command.Pause) {
443          return Command.Pause;
444        } else if (source.Value == DB.Command.Stop) {
445          return Command.Stop;
446        } else
447          return Command.Pause;
448      }
449      return null;
450    }
451
452    public static DB.Command? ToEntity(DT.Command? source) {
453      if (source.HasValue) {
454        if (source == DT.Command.Abort) {
455          return DB.Command.Abort;
456        } else if (source == DT.Command.Pause) {
457          return DB.Command.Pause;
458        } else if (source == DT.Command.Stop) {
459          return DB.Command.Stop;
460        } else
461          return DB.Command.Pause;
462      } else
463        return null;
464    }
465    #endregion
466
467    #region CpuArchiteture
468    public static DT.CpuArchitecture ToDto(DB.CpuArchitecture source) {
469      if (source == DB.CpuArchitecture.x64) {
470        return CpuArchitecture.x64;
471      } else if (source == DB.CpuArchitecture.x86) {
472        return CpuArchitecture.x86;
473      } else
474        return CpuArchitecture.x86;
475    }
476
477    public static DB.CpuArchitecture ToEntity(DT.CpuArchitecture source) {
478      if (source == DT.CpuArchitecture.x64) {
479        return DB.CpuArchitecture.x64;
480      } else if (source == DT.CpuArchitecture.x86) {
481        return DB.CpuArchitecture.x86;
482      } else
483        return DB.CpuArchitecture.x86;
484    }
485    #endregion
486
487    #region SlaveState
488    public static DT.SlaveState ToDto(DB.SlaveState source) {
489      if (source == DB.SlaveState.Calculating) {
490        return SlaveState.Calculating;
491      } else if (source == DB.SlaveState.Idle) {
492        return SlaveState.Idle;
493      } else if (source == DB.SlaveState.Offline) {
494        return SlaveState.Offline;
495      } else
496        return SlaveState.Offline;
497    }
498
499    public static DB.SlaveState ToEntity(DT.SlaveState source) {
500      if (source == DT.SlaveState.Calculating) {
501        return DB.SlaveState.Calculating;
502      } else if (source == DT.SlaveState.Idle) {
503        return DB.SlaveState.Idle;
504      } else if (source == DT.SlaveState.Offline) {
505        return DB.SlaveState.Offline;
506      } else
507        return DB.SlaveState.Offline;
508    }
509    #endregion
510  }
511}
Note: See TracBrowser for help on using the repository browser.