Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/Convert.cs @ 5779

Last change on this file since 5779 was 5779, checked in by cneumuel, 13 years ago

#1233

  • implemented pause, stop for single jobs
  • introduced Command property for jobs (to distinguish between state and command (abort vs. aborted))
  • improved behaviour of ItemTreeView (double click opens new window, selected item stays marked)
  • fixed bugs in StateLogGanttChartListView and HiveJobView
  • fixed cloning of client-side dtos
File size: 12.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Data.Linq;
24using System.Linq;
25using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
26
27namespace HeuristicLab.Services.Hive.DataAccess {
28  public static class Convert {
29    #region Job
30    public static DT.Job ToDto(Job source) {
31      if (source == null) return null;
32      return new DT.Job {
33        Id = source.JobId,
34        CoresNeeded = source.CoresNeeded,
35        ExecutionTime = string.IsNullOrEmpty(source.ExecutionTime) ? TimeSpan.Zero : TimeSpan.Parse(source.ExecutionTime),
36        MemoryNeeded = source.MemoryNeeded,
37        ParentJobId = source.ParentJobId,
38        Priority = source.Priority,
39        PluginsNeededIds = source.RequiredPlugins.Select(x => x.PluginId).ToList(),
40        LastHeartbeat = source.LastHeartbeat,
41        State = source.State,
42        StateLog = source.StateLogs.Select(x => Convert.ToDto(x)).OrderBy(x => x.DateTime).ToList(),
43        IsParentJob = source.IsParentJob,
44        FinishWhenChildJobsFinished = source.FinishWhenChildJobsFinished,
45        Command = source.Command
46      };
47    }
48    public static Job ToEntity(DT.Job source) {
49      if (source == null) return null;
50      var entity = new Job(); ToEntity(source, entity);
51      return entity;
52    }
53    public static void ToEntity(DT.Job source, Job target) {
54      if ((source != null) && (target != null)) {
55        target.JobId = source.Id;
56        target.CoresNeeded = source.CoresNeeded;
57        target.ExecutionTime = source.ExecutionTime.ToString();
58        target.MemoryNeeded = source.MemoryNeeded;
59        target.ParentJobId = source.ParentJobId;
60        target.Priority = source.Priority;
61        target.LastHeartbeat = source.LastHeartbeat;
62        target.State = source.State;
63        if (target.StateLogs == null) target.StateLogs = new EntitySet<StateLog>();
64        foreach (DT.StateLog sl in source.StateLog.Where(x => x.Id == Guid.Empty)) {
65          target.StateLogs.Add(Convert.ToEntity(sl));
66        }
67        //target.StateLogs.AddRange(source.StateLog.Select(x => Convert.ToEntity(x)).OrderBy(x => x.DateTime));
68        target.IsParentJob = source.IsParentJob;
69        target.FinishWhenChildJobsFinished = source.FinishWhenChildJobsFinished;
70        target.Command = source.Command;
71        // RequiredPlugins are added by Dao
72      }
73    }
74    #endregion
75
76    #region JobData
77    public static DT.JobData ToDto(JobData source) {
78      if (source == null) return null;
79      return new DT.JobData { JobId = source.JobId, Data = source.Data.ToArray(), LastUpdate = source.LastUpdate };
80    }
81    public static JobData ToEntity(DT.JobData source) {
82      if (source == null) return null;
83      var entity = new JobData(); ToEntity(source, entity);
84      return entity;
85    }
86    public static void ToEntity(DT.JobData source, JobData target) {
87      if ((source != null) && (target != null)) {
88        target.JobId = source.JobId; target.Data = new Binary(source.Data); target.LastUpdate = source.LastUpdate;
89      }
90    }
91    #endregion
92
93    #region StateLog
94    public static DT.StateLog ToDto(StateLog source) {
95      if (source == null) return null;
96      return new DT.StateLog { Id = source.StateLogId, DateTime = source.DateTime, Exception = source.Exception, JobId = source.JobId, SlaveId = source.SlaveId, State = source.State, UserId = source.UserId };
97    }
98    public static StateLog ToEntity(DT.StateLog source) {
99      if (source == null) return null;
100      var entity = new StateLog(); ToEntity(source, entity);
101      return entity;
102    }
103    public static void ToEntity(DT.StateLog source, StateLog target) {
104      if ((source != null) && (target != null)) {
105        target.StateLogId = source.Id; target.DateTime = source.DateTime; target.Exception = source.Exception; target.JobId = source.JobId; target.SlaveId = source.SlaveId; target.State = source.State; target.UserId = source.UserId;
106      }
107    }
108    #endregion
109
110    #region Appointment
111    public static DT.Appointment ToDto(UptimeCalendar source) {
112      if (source == null) return null;
113      return new DT.Appointment { Id = source.UptimeCalendarId, AllDayEvent = source.AllDayEvent, EndDate = source.EndDate, Recurring = source.Recurring, RecurringId = source.RecurringId, ResourceId = source.ResourceId, StartDate = source.StartDate };
114    }
115    public static UptimeCalendar ToEntity(DT.Appointment source) {
116      if (source == null) return null;
117      var entity = new UptimeCalendar(); ToEntity(source, entity);
118      return entity;
119    }
120    public static void ToEntity(DT.Appointment source, UptimeCalendar target) {
121      if ((source != null) && (target != null)) {
122        target.UptimeCalendarId = 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;
123      }
124    }
125    #endregion
126
127    #region HiveExperiment
128    public static DT.HiveExperiment ToDto(HiveExperiment source) {
129      if (source == null) return null;
130      return new DT.HiveExperiment { Id = source.HiveExperimentId, Description = source.Description, Name = source.Name, RootJobId = source.RootJobId, OwnerUserId = source.OwnerUserId, DateCreated = source.DateCreated, ResourceNames = source.ResourceIds, LastAccessed = source.LastAccessed };
131    }
132    public static HiveExperiment ToEntity(DT.HiveExperiment source) {
133      if (source == null) return null;
134      var entity = new HiveExperiment(); ToEntity(source, entity);
135      return entity;
136    }
137    public static void ToEntity(DT.HiveExperiment source, HiveExperiment target) {
138      if ((source != null) && (target != null)) {
139        target.HiveExperimentId = source.Id; target.Description = source.Description; target.Name = source.Name; target.RootJobId = source.RootJobId; target.OwnerUserId = source.OwnerUserId; target.DateCreated = source.DateCreated; target.ResourceIds = source.ResourceNames; target.LastAccessed = source.LastAccessed;
140      }
141    }
142    #endregion
143
144    #region HiveExperimentPermission
145    public static DT.HiveExperimentPermission ToDto(HiveExperimentPermission source) {
146      if (source == null) return null;
147      return new DT.HiveExperimentPermission { HiveExperimentId = source.HiveExperimentId, GrantedUserId = source.GrantedUserId, GrantedByUserId = source.GrantedByUserId, Permission = source.Permission };
148    }
149    public static HiveExperimentPermission ToEntity(DT.HiveExperimentPermission source) {
150      if (source == null) return null;
151      var entity = new HiveExperimentPermission(); ToEntity(source, entity);
152      return entity;
153    }
154    public static void ToEntity(DT.HiveExperimentPermission source, HiveExperimentPermission target) {
155      if ((source != null) && (target != null)) {
156        target.HiveExperimentId = source.HiveExperimentId; target.GrantedUserId = source.GrantedUserId; target.GrantedByUserId = source.GrantedByUserId; target.Permission = source.Permission;
157      }
158    }
159    #endregion
160
161    #region Plugin
162    public static DT.Plugin ToDto(Plugin source) {
163      if (source == null) return null;
164      return new DT.Plugin { Id = source.PluginId, Name = source.Name, Version = new Version(source.Version), UserId = source.UserId, IsLocal = source.IsLocal, DateCreated = source.DateCreated };
165    }
166    public static Plugin ToEntity(DT.Plugin source) {
167      if (source == null) return null;
168      var entity = new Plugin(); ToEntity(source, entity);
169      return entity;
170    }
171    public static void ToEntity(DT.Plugin source, Plugin target) {
172      if ((source != null) && (target != null)) {
173        target.PluginId = source.Id; target.Name = source.Name; target.Version = source.Version.ToString(); target.UserId = source.UserId; target.IsLocal = source.IsLocal; target.DateCreated = source.DateCreated;
174      }
175    }
176    #endregion
177
178    #region PluginData
179    public static DT.PluginData ToDto(PluginData source) {
180      if (source == null) return null;
181      return new DT.PluginData { Id = source.PluginDataId, PluginId = source.PluginId, Data = source.Data.ToArray(), FileName = source.FileName };
182    }
183    public static PluginData ToEntity(DT.PluginData source) {
184      if (source == null) return null;
185      var entity = new PluginData(); ToEntity(source, entity);
186      return entity;
187    }
188    public static void ToEntity(DT.PluginData source, PluginData target) {
189      if ((source != null) && (target != null)) {
190        target.PluginDataId = source.Id; target.PluginId = source.PluginId; target.Data = new Binary(source.Data); target.FileName = source.FileName;
191      }
192    }
193    #endregion
194
195    #region Slave
196    public static DT.Slave ToDto(Slave source) {
197      if (source == null) return null;
198      return new DT.Slave {
199        Id = source.ResourceId,
200        ParentResourceId = source.ParentResourceId,
201        Cores = source.Cores,
202        CpuSpeed = source.CpuSpeed,
203        FreeCores = source.FreeCores,
204        FreeMemory = source.FreeMemory,
205        IsAllowedToCalculate = source.IsAllowedToCalculate,
206        Memory = source.Memory,
207        Name = source.Name,
208        SlaveState = source.SlaveState,
209        CpuArchitecture = source.CpuArchitecture,
210        OperatingSystem = source.OperatingSystem,
211        LastHeartbeat = source.LastHeartbeat
212      };
213    }
214    public static Slave ToEntity(DT.Slave source) {
215      if (source == null) return null;
216      var entity = new Slave(); ToEntity(source, entity);
217      return entity;
218    }
219    public static void ToEntity(DT.Slave source, Slave target) {
220      if ((source != null) && (target != null)) {
221        target.ResourceId = source.Id;
222        target.ParentResourceId = source.ParentResourceId;
223        target.Cores = source.Cores;
224        target.CpuSpeed = source.CpuSpeed;
225        target.FreeCores = source.FreeCores;
226        target.FreeMemory = source.FreeMemory;
227        target.IsAllowedToCalculate = source.IsAllowedToCalculate;
228        target.Memory = source.Memory;
229        target.Name = source.Name;
230        target.SlaveState = source.SlaveState;
231        target.CpuArchitecture = source.CpuArchitecture;
232        target.OperatingSystem = source.OperatingSystem;
233        target.LastHeartbeat = source.LastHeartbeat;
234      }
235    }
236    #endregion
237
238    #region SlaveGroup
239    public static DT.SlaveGroup ToDto(SlaveGroup source) {
240      if (source == null) return null;
241      return new DT.SlaveGroup { Id = source.ResourceId, Name = source.Name, ParentResourceId = source.ParentResourceId };
242    }
243    public static SlaveGroup ToEntity(DT.SlaveGroup source) {
244      if (source == null) return null;
245      var entity = new SlaveGroup(); ToEntity(source, entity);
246      return entity;
247    }
248    public static void ToEntity(DT.SlaveGroup source, SlaveGroup target) {
249      if ((source != null) && (target != null)) {
250        target.ResourceId = source.Id; target.Name = source.Name; target.ParentResourceId = source.ParentResourceId;
251      }
252    }
253    #endregion
254
255    #region Resource
256    public static DT.Resource ToDto(Resource source) {
257      if (source == null) return null;
258      return new DT.Resource { Id = source.ResourceId, Name = source.Name, ParentResourceId = source.ParentResourceId };
259    }
260    public static Resource ToEntity(DT.Resource source) {
261      if (source == null) return null;
262      var entity = new Resource(); ToEntity(source, entity);
263      return entity;
264    }
265    public static void ToEntity(DT.Resource source, Resource target) {
266      if ((source != null) && (target != null)) {
267        target.ResourceId = source.Id; target.Name = source.Name; target.ParentResourceId = source.ParentResourceId;
268      }
269    }
270    #endregion
271  }
272}
Note: See TracBrowser for help on using the repository browser.