Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233

  • added StateLog to log state transitions of hive jobs
  • added permissions to hive experiments (in data access layer, no UI for that yet)
  • extended unit tests
File size: 11.0 KB
RevLine 
[4598]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
[5266]22using System;
[4598]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;
[4649]32      return new DT.Job {
33        Id = source.JobId,
34        CoresNeeded = source.CoresNeeded,
[5511]35        ExecutionTime = string.IsNullOrEmpty(source.ExecutionTime) ? TimeSpan.Zero : TimeSpan.Parse(source.ExecutionTime),
[4649]36        MemoryNeeded = source.MemoryNeeded,
37        ParentJobId = source.ParentJobId,
38        Priority = source.Priority,
[5405]39        PluginsNeededIds = source.RequiredPlugins.Select(x => x.PluginId).ToList(),
[5511]40        LastHeartbeat = source.LastHeartbeat,
41        State = source.State,
42        StateLog = source.StateLogs.Select(x => Convert.ToDto(x)).OrderBy(x => x.DateTime).ToList()
[4649]43      };
[4598]44    }
45    public static Job ToEntity(DT.Job source) {
46      if (source == null) return null;
[4649]47      var entity = new Job(); ToEntity(source, entity);
48      return entity;
[4598]49    }
50    public static void ToEntity(DT.Job source, Job target) {
51      if ((source != null) && (target != null)) {
[4649]52        target.JobId = source.Id;
53        target.CoresNeeded = source.CoresNeeded;
[5511]54        target.ExecutionTime = source.ExecutionTime.ToString();
[4649]55        target.MemoryNeeded = source.MemoryNeeded;
56        target.ParentJobId = source.ParentJobId;
57        target.Priority = source.Priority;
[5405]58        target.LastHeartbeat = source.LastHeartbeat;
[5511]59        target.State = source.State;
60        if (target.StateLogs == null) target.StateLogs = new EntitySet<StateLog>();
61        target.StateLogs.AddRange(source.StateLog.Select(x => Convert.ToEntity(x)).OrderBy(x => x.DateTime));
[5405]62        // RequiredPlugins are added by Dao
[4598]63      }
64    }
65    #endregion
66
67    #region JobData
68    public static DT.JobData ToDto(JobData source) {
69      if (source == null) return null;
[5106]70      return new DT.JobData { JobId = source.JobId, Data = source.Data.ToArray(), LastUpdate = source.LastUpdate };
[4598]71    }
72    public static JobData ToEntity(DT.JobData source) {
73      if (source == null) return null;
[4649]74      var entity = new JobData(); ToEntity(source, entity);
75      return entity;
[4598]76    }
77    public static void ToEntity(DT.JobData source, JobData target) {
78      if ((source != null) && (target != null)) {
[5106]79        target.JobId = source.JobId; target.Data = new Binary(source.Data); target.LastUpdate = source.LastUpdate;
[4598]80      }
81    }
82    #endregion
[4649]83
[5511]84    #region StateLog
85    public static DT.StateLog ToDto(StateLog source) {
86      if (source == null) return null;
87      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 };
88    }
89    public static StateLog ToEntity(DT.StateLog source) {
90      if (source == null) return null;
91      var entity = new StateLog(); ToEntity(source, entity);
92      return entity;
93    }
94    public static void ToEntity(DT.StateLog source, StateLog target) {
95      if ((source != null) && (target != null)) {
96        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;
97      }
98    }
99    #endregion
100
[4598]101    #region HiveExperiment
102    public static DT.HiveExperiment ToDto(HiveExperiment source) {
103      if (source == null) return null;
[5511]104      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 };
[4598]105    }
106    public static HiveExperiment ToEntity(DT.HiveExperiment source) {
107      if (source == null) return null;
[4649]108      var entity = new HiveExperiment(); ToEntity(source, entity);
109      return entity;
[4598]110    }
111    public static void ToEntity(DT.HiveExperiment source, HiveExperiment target) {
112      if ((source != null) && (target != null)) {
[5511]113        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;
[4598]114      }
115    }
116    #endregion
117
[5511]118    #region HiveExperimentPermission
119    public static DT.HiveExperimentPermission ToDto(HiveExperimentPermission source) {
120      if (source == null) return null;
121      return new DT.HiveExperimentPermission { HiveExperimentId = source.HiveExperimentId, GrantedUserId = source.GrantedUserId, GrantedByUserId = source.GrantedByUserId, Permission = source.Permission };
122    }
123    public static HiveExperimentPermission ToEntity(DT.HiveExperimentPermission source) {
124      if (source == null) return null;
125      var entity = new HiveExperimentPermission(); ToEntity(source, entity);
126      return entity;
127    }
128    public static void ToEntity(DT.HiveExperimentPermission source, HiveExperimentPermission target) {
129      if ((source != null) && (target != null)) {
130        target.HiveExperimentId = source.HiveExperimentId; target.GrantedUserId = source.GrantedUserId; target.GrantedByUserId = source.GrantedByUserId; target.Permission = source.Permission;
131      }
132    }
133    #endregion
134
[4905]135    #region Plugin
136    public static DT.Plugin ToDto(Plugin source) {
137      if (source == null) return null;
[5402]138      return new DT.Plugin { Id = source.PluginId, Name = source.Name, Version = new Version(source.Version), UserId = source.UserId, IsLocal = source.IsLocal, DateCreated = source.DateCreated };
[4905]139    }
140    public static Plugin ToEntity(DT.Plugin source) {
141      if (source == null) return null;
142      var entity = new Plugin(); ToEntity(source, entity);
143      return entity;
144    }
145    public static void ToEntity(DT.Plugin source, Plugin target) {
146      if ((source != null) && (target != null)) {
[5402]147        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;
[4905]148      }
149    }
150    #endregion
151
152    #region PluginData
153    public static DT.PluginData ToDto(PluginData source) {
154      if (source == null) return null;
[5402]155      return new DT.PluginData { Id = source.PluginDataId, PluginId = source.PluginId, Data = source.Data.ToArray(), FileName = source.FileName };
[4905]156    }
157    public static PluginData ToEntity(DT.PluginData source) {
158      if (source == null) return null;
159      var entity = new PluginData(); ToEntity(source, entity);
160      return entity;
161    }
162    public static void ToEntity(DT.PluginData source, PluginData target) {
163      if ((source != null) && (target != null)) {
[5402]164        target.PluginDataId = source.Id; target.PluginId = source.PluginId; target.Data = new Binary(source.Data); target.FileName = source.FileName;
[4905]165      }
166    }
167    #endregion
168
[4598]169    #region Slave
170    public static DT.Slave ToDto(Slave source) {
171      if (source == null) return null;
[5511]172      return new DT.Slave {
173        Id = source.ResourceId,
174        ParentResourceId = source.ParentResourceId,
[5405]175        Cores = source.Cores,
[5511]176        CpuSpeed = source.CpuSpeed,
177        FreeCores = source.FreeCores,
178        FreeMemory = source.FreeMemory,
179        IsAllowedToCalculate = source.IsAllowedToCalculate,
180        Memory = source.Memory,
181        Name = source.Name,
182        SlaveState = source.SlaveState,
183        CpuArchitecture = source.CpuArchitecture,
[5405]184        OperatingSystem = source.OperatingSystem,
185        LastHeartbeat = source.LastHeartbeat
186      };
[4598]187    }
188    public static Slave ToEntity(DT.Slave source) {
189      if (source == null) return null;
[4649]190      var entity = new Slave(); ToEntity(source, entity);
191      return entity;
[4598]192    }
193    public static void ToEntity(DT.Slave source, Slave target) {
194      if ((source != null) && (target != null)) {
[5405]195        target.ResourceId = source.Id;
196        target.ParentResourceId = source.ParentResourceId;
[5511]197        target.Cores = source.Cores;
[5405]198        target.CpuSpeed = source.CpuSpeed;
199        target.FreeCores = source.FreeCores;
[5511]200        target.FreeMemory = source.FreeMemory;
[5405]201        target.IsAllowedToCalculate = source.IsAllowedToCalculate;
202        target.Memory = source.Memory;
[5511]203        target.Name = source.Name;
204        target.SlaveState = source.SlaveState;
205        target.CpuArchitecture = source.CpuArchitecture;
[5405]206        target.OperatingSystem = source.OperatingSystem;
207        target.LastHeartbeat = source.LastHeartbeat;
[4598]208      }
209    }
210    #endregion
211
212    #region SlaveGroup
213    public static DT.SlaveGroup ToDto(SlaveGroup source) {
214      if (source == null) return null;
[5106]215      return new DT.SlaveGroup { Id = source.ResourceId, Name = source.Name, ParentResourceId = source.ParentResourceId };
[4598]216    }
217    public static SlaveGroup ToEntity(DT.SlaveGroup source) {
218      if (source == null) return null;
[4649]219      var entity = new SlaveGroup(); ToEntity(source, entity);
220      return entity;
[4598]221    }
222    public static void ToEntity(DT.SlaveGroup source, SlaveGroup target) {
223      if ((source != null) && (target != null)) {
[5106]224        target.ResourceId = source.Id; target.Name = source.Name; target.ParentResourceId = source.ParentResourceId;
[4598]225      }
226    }
227    #endregion
228
229    #region Resource
230    public static DT.Resource ToDto(Resource source) {
231      if (source == null) return null;
[5106]232      return new DT.Resource { Id = source.ResourceId, Name = source.Name, ParentResourceId = source.ParentResourceId };
[4598]233    }
234    public static Resource ToEntity(DT.Resource source) {
235      if (source == null) return null;
[4649]236      var entity = new Resource(); ToEntity(source, entity);
237      return entity;
[4598]238    }
239    public static void ToEntity(DT.Resource source, Resource target) {
240      if ((source != null) && (target != null)) {
[5106]241        target.ResourceId = source.Id; target.Name = source.Name; target.ParentResourceId = source.ParentResourceId;
[4598]242      }
243    }
244    #endregion
245  }
246}
Note: See TracBrowser for help on using the repository browser.