Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233

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