Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive/3.3/Convert.cs @ 6723

Last change on this file since 6723 was 6723, checked in by ascheibe, 13 years ago

#1233 Review comments: renamed HiveEperiment to Job

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