Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5633 was 5633, checked in by ascheibe, 12 years ago

#1233 added Appointment/Schedule ws and dao methods

File size: 12.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 Appointment
110    public static DT.Appointment ToDto(UptimeCalendar source) {
111      if (source == null) return null;
112      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 };
113    }
114    public static UptimeCalendar ToEntity(DT.Appointment source) {
115      if (source == null) return null;
116      var entity = new UptimeCalendar(); ToEntity(source, entity);
117      return entity;
118    }
119    public static void ToEntity(DT.Appointment source, UptimeCalendar target) {
120      if ((source != null) && (target != null)) {
121        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;
122      }
123    }
124    #endregion
125
126    #region HiveExperiment
127    public static DT.HiveExperiment ToDto(HiveExperiment source) {
128      if (source == null) return null;
129      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 };
130    }
131    public static HiveExperiment ToEntity(DT.HiveExperiment source) {
132      if (source == null) return null;
133      var entity = new HiveExperiment(); ToEntity(source, entity);
134      return entity;
135    }
136    public static void ToEntity(DT.HiveExperiment source, HiveExperiment target) {
137      if ((source != null) && (target != null)) {
138        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;
139      }
140    }
141    #endregion
142
143    #region HiveExperimentPermission
144    public static DT.HiveExperimentPermission ToDto(HiveExperimentPermission source) {
145      if (source == null) return null;
146      return new DT.HiveExperimentPermission { HiveExperimentId = source.HiveExperimentId, GrantedUserId = source.GrantedUserId, GrantedByUserId = source.GrantedByUserId, Permission = source.Permission };
147    }
148    public static HiveExperimentPermission ToEntity(DT.HiveExperimentPermission source) {
149      if (source == null) return null;
150      var entity = new HiveExperimentPermission(); ToEntity(source, entity);
151      return entity;
152    }
153    public static void ToEntity(DT.HiveExperimentPermission source, HiveExperimentPermission target) {
154      if ((source != null) && (target != null)) {
155        target.HiveExperimentId = source.HiveExperimentId; target.GrantedUserId = source.GrantedUserId; target.GrantedByUserId = source.GrantedByUserId; target.Permission = source.Permission;
156      }
157    }
158    #endregion
159
160    #region Plugin
161    public static DT.Plugin ToDto(Plugin source) {
162      if (source == null) return null;
163      return new DT.Plugin { Id = source.PluginId, Name = source.Name, Version = new Version(source.Version), UserId = source.UserId, IsLocal = source.IsLocal, DateCreated = source.DateCreated };
164    }
165    public static Plugin ToEntity(DT.Plugin source) {
166      if (source == null) return null;
167      var entity = new Plugin(); ToEntity(source, entity);
168      return entity;
169    }
170    public static void ToEntity(DT.Plugin source, Plugin target) {
171      if ((source != null) && (target != null)) {
172        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;
173      }
174    }
175    #endregion
176
177    #region PluginData
178    public static DT.PluginData ToDto(PluginData source) {
179      if (source == null) return null;
180      return new DT.PluginData { Id = source.PluginDataId, PluginId = source.PluginId, Data = source.Data.ToArray(), FileName = source.FileName };
181    }
182    public static PluginData ToEntity(DT.PluginData source) {
183      if (source == null) return null;
184      var entity = new PluginData(); ToEntity(source, entity);
185      return entity;
186    }
187    public static void ToEntity(DT.PluginData source, PluginData target) {
188      if ((source != null) && (target != null)) {
189        target.PluginDataId = source.Id; target.PluginId = source.PluginId; target.Data = new Binary(source.Data); target.FileName = source.FileName;
190      }
191    }
192    #endregion
193
194    #region Slave
195    public static DT.Slave ToDto(Slave source) {
196      if (source == null) return null;
197      return new DT.Slave {
198        Id = source.ResourceId,
199        ParentResourceId = source.ParentResourceId,
200        Cores = source.Cores,
201        CpuSpeed = source.CpuSpeed,
202        FreeCores = source.FreeCores,
203        FreeMemory = source.FreeMemory,
204        IsAllowedToCalculate = source.IsAllowedToCalculate,
205        Memory = source.Memory,
206        Name = source.Name,
207        SlaveState = source.SlaveState,
208        CpuArchitecture = source.CpuArchitecture,
209        OperatingSystem = source.OperatingSystem,
210        LastHeartbeat = source.LastHeartbeat
211      };
212    }
213    public static Slave ToEntity(DT.Slave source) {
214      if (source == null) return null;
215      var entity = new Slave(); ToEntity(source, entity);
216      return entity;
217    }
218    public static void ToEntity(DT.Slave source, Slave target) {
219      if ((source != null) && (target != null)) {
220        target.ResourceId = source.Id;
221        target.ParentResourceId = source.ParentResourceId;
222        target.Cores = source.Cores;
223        target.CpuSpeed = source.CpuSpeed;
224        target.FreeCores = source.FreeCores;
225        target.FreeMemory = source.FreeMemory;
226        target.IsAllowedToCalculate = source.IsAllowedToCalculate;
227        target.Memory = source.Memory;
228        target.Name = source.Name;
229        target.SlaveState = source.SlaveState;
230        target.CpuArchitecture = source.CpuArchitecture;
231        target.OperatingSystem = source.OperatingSystem;
232        target.LastHeartbeat = source.LastHeartbeat;
233      }
234    }
235    #endregion
236
237    #region SlaveGroup
238    public static DT.SlaveGroup ToDto(SlaveGroup source) {
239      if (source == null) return null;
240      return new DT.SlaveGroup { Id = source.ResourceId, Name = source.Name, ParentResourceId = source.ParentResourceId };
241    }
242    public static SlaveGroup ToEntity(DT.SlaveGroup source) {
243      if (source == null) return null;
244      var entity = new SlaveGroup(); ToEntity(source, entity);
245      return entity;
246    }
247    public static void ToEntity(DT.SlaveGroup source, SlaveGroup 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    #region Resource
255    public static DT.Resource ToDto(Resource source) {
256      if (source == null) return null;
257      return new DT.Resource { Id = source.ResourceId, Name = source.Name, ParentResourceId = source.ParentResourceId };
258    }
259    public static Resource ToEntity(DT.Resource source) {
260      if (source == null) return null;
261      var entity = new Resource(); ToEntity(source, entity);
262      return entity;
263    }
264    public static void ToEntity(DT.Resource source, Resource target) {
265      if ((source != null) && (target != null)) {
266        target.ResourceId = source.Id; target.Name = source.Name; target.ParentResourceId = source.ParentResourceId;
267      }
268    }
269    #endregion
270  }
271}
Note: See TracBrowser for help on using the repository browser.