Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5593 was 5526, checked in by cneumuel, 14 years ago

#1233

  • fixed handling of StateLog in DataLayer
  • extended unit tests
  • changed style of service calls to OKB-like style (using delegates)
  • added possibility that parent jobs can be finished immediately when child jobs are finished
File size: 11.4 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      };
46    }
47    public static Job ToEntity(DT.Job source) {
48      if (source == null) return null;
49      var entity = new Job(); ToEntity(source, entity);
50      return entity;
51    }
52    public static void ToEntity(DT.Job source, Job target) {
53      if ((source != null) && (target != null)) {
54        target.JobId = source.Id;
55        target.CoresNeeded = source.CoresNeeded;
56        target.ExecutionTime = source.ExecutionTime.ToString();
57        target.MemoryNeeded = source.MemoryNeeded;
58        target.ParentJobId = source.ParentJobId;
59        target.Priority = source.Priority;
60        target.LastHeartbeat = source.LastHeartbeat;
61        target.State = source.State;
62        if (target.StateLogs == null) target.StateLogs = new EntitySet<StateLog>();
63        foreach (DT.StateLog sl in source.StateLog.Where(x => x.Id == Guid.Empty)) {
64          target.StateLogs.Add(Convert.ToEntity(sl));
65        }
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        // RequiredPlugins are added by Dao
71      }
72    }
73    #endregion
74
75    #region JobData
76    public static DT.JobData ToDto(JobData source) {
77      if (source == null) return null;
78      return new DT.JobData { JobId = source.JobId, Data = source.Data.ToArray(), LastUpdate = source.LastUpdate };
79    }
80    public static JobData ToEntity(DT.JobData source) {
81      if (source == null) return null;
82      var entity = new JobData(); ToEntity(source, entity);
83      return entity;
84    }
85    public static void ToEntity(DT.JobData source, JobData target) {
86      if ((source != null) && (target != null)) {
87        target.JobId = source.JobId; target.Data = new Binary(source.Data); target.LastUpdate = source.LastUpdate;
88      }
89    }
90    #endregion
91
92    #region StateLog
93    public static DT.StateLog ToDto(StateLog source) {
94      if (source == null) return null;
95      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 };
96    }
97    public static StateLog ToEntity(DT.StateLog source) {
98      if (source == null) return null;
99      var entity = new StateLog(); ToEntity(source, entity);
100      return entity;
101    }
102    public static void ToEntity(DT.StateLog source, StateLog target) {
103      if ((source != null) && (target != null)) {
104        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;
105      }
106    }
107    #endregion
108
109    #region HiveExperiment
110    public static DT.HiveExperiment ToDto(HiveExperiment source) {
111      if (source == null) return null;
112      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 };
113    }
114    public static HiveExperiment ToEntity(DT.HiveExperiment source) {
115      if (source == null) return null;
116      var entity = new HiveExperiment(); ToEntity(source, entity);
117      return entity;
118    }
119    public static void ToEntity(DT.HiveExperiment source, HiveExperiment target) {
120      if ((source != null) && (target != null)) {
121        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;
122      }
123    }
124    #endregion
125
126    #region HiveExperimentPermission
127    public static DT.HiveExperimentPermission ToDto(HiveExperimentPermission source) {
128      if (source == null) return null;
129      return new DT.HiveExperimentPermission { HiveExperimentId = source.HiveExperimentId, GrantedUserId = source.GrantedUserId, GrantedByUserId = source.GrantedByUserId, Permission = source.Permission };
130    }
131    public static HiveExperimentPermission ToEntity(DT.HiveExperimentPermission source) {
132      if (source == null) return null;
133      var entity = new HiveExperimentPermission(); ToEntity(source, entity);
134      return entity;
135    }
136    public static void ToEntity(DT.HiveExperimentPermission source, HiveExperimentPermission target) {
137      if ((source != null) && (target != null)) {
138        target.HiveExperimentId = source.HiveExperimentId; target.GrantedUserId = source.GrantedUserId; target.GrantedByUserId = source.GrantedByUserId; target.Permission = source.Permission;
139      }
140    }
141    #endregion
142
143    #region Plugin
144    public static DT.Plugin ToDto(Plugin source) {
145      if (source == null) return null;
146      return new DT.Plugin { Id = source.PluginId, Name = source.Name, Version = new Version(source.Version), UserId = source.UserId, IsLocal = source.IsLocal, DateCreated = source.DateCreated };
147    }
148    public static Plugin ToEntity(DT.Plugin source) {
149      if (source == null) return null;
150      var entity = new Plugin(); ToEntity(source, entity);
151      return entity;
152    }
153    public static void ToEntity(DT.Plugin source, Plugin target) {
154      if ((source != null) && (target != null)) {
155        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;
156      }
157    }
158    #endregion
159
160    #region PluginData
161    public static DT.PluginData ToDto(PluginData source) {
162      if (source == null) return null;
163      return new DT.PluginData { Id = source.PluginDataId, PluginId = source.PluginId, Data = source.Data.ToArray(), FileName = source.FileName };
164    }
165    public static PluginData ToEntity(DT.PluginData source) {
166      if (source == null) return null;
167      var entity = new PluginData(); ToEntity(source, entity);
168      return entity;
169    }
170    public static void ToEntity(DT.PluginData source, PluginData target) {
171      if ((source != null) && (target != null)) {
172        target.PluginDataId = source.Id; target.PluginId = source.PluginId; target.Data = new Binary(source.Data); target.FileName = source.FileName;
173      }
174    }
175    #endregion
176
177    #region Slave
178    public static DT.Slave ToDto(Slave source) {
179      if (source == null) return null;
180      return new DT.Slave {
181        Id = source.ResourceId,
182        ParentResourceId = source.ParentResourceId,
183        Cores = source.Cores,
184        CpuSpeed = source.CpuSpeed,
185        FreeCores = source.FreeCores,
186        FreeMemory = source.FreeMemory,
187        IsAllowedToCalculate = source.IsAllowedToCalculate,
188        Memory = source.Memory,
189        Name = source.Name,
190        SlaveState = source.SlaveState,
191        CpuArchitecture = source.CpuArchitecture,
192        OperatingSystem = source.OperatingSystem,
193        LastHeartbeat = source.LastHeartbeat
194      };
195    }
196    public static Slave ToEntity(DT.Slave source) {
197      if (source == null) return null;
198      var entity = new Slave(); ToEntity(source, entity);
199      return entity;
200    }
201    public static void ToEntity(DT.Slave source, Slave target) {
202      if ((source != null) && (target != null)) {
203        target.ResourceId = source.Id;
204        target.ParentResourceId = source.ParentResourceId;
205        target.Cores = source.Cores;
206        target.CpuSpeed = source.CpuSpeed;
207        target.FreeCores = source.FreeCores;
208        target.FreeMemory = source.FreeMemory;
209        target.IsAllowedToCalculate = source.IsAllowedToCalculate;
210        target.Memory = source.Memory;
211        target.Name = source.Name;
212        target.SlaveState = source.SlaveState;
213        target.CpuArchitecture = source.CpuArchitecture;
214        target.OperatingSystem = source.OperatingSystem;
215        target.LastHeartbeat = source.LastHeartbeat;
216      }
217    }
218    #endregion
219
220    #region SlaveGroup
221    public static DT.SlaveGroup ToDto(SlaveGroup source) {
222      if (source == null) return null;
223      return new DT.SlaveGroup { Id = source.ResourceId, Name = source.Name, ParentResourceId = source.ParentResourceId };
224    }
225    public static SlaveGroup ToEntity(DT.SlaveGroup source) {
226      if (source == null) return null;
227      var entity = new SlaveGroup(); ToEntity(source, entity);
228      return entity;
229    }
230    public static void ToEntity(DT.SlaveGroup source, SlaveGroup target) {
231      if ((source != null) && (target != null)) {
232        target.ResourceId = source.Id; target.Name = source.Name; target.ParentResourceId = source.ParentResourceId;
233      }
234    }
235    #endregion
236
237    #region Resource
238    public static DT.Resource ToDto(Resource source) {
239      if (source == null) return null;
240      return new DT.Resource { Id = source.ResourceId, Name = source.Name, ParentResourceId = source.ParentResourceId };
241    }
242    public static Resource ToEntity(DT.Resource source) {
243      if (source == null) return null;
244      var entity = new Resource(); ToEntity(source, entity);
245      return entity;
246    }
247    public static void ToEntity(DT.Resource source, Resource target) {
248      if ((source != null) && (target != null)) {
249        target.ResourceId = source.Id; target.Name = source.Name; target.ParentResourceId = source.ParentResourceId;
250      }
251    }
252    #endregion
253  }
254}
Note: See TracBrowser for help on using the repository browser.