#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Data.Linq; using System.Linq; using DT = HeuristicLab.Services.Hive.Common.DataTransfer; namespace HeuristicLab.Services.Hive.DataAccess { public static class Convert { #region Job public static DT.Job ToDto(Job source) { if (source == null) return null; return new DT.Job { Id = source.JobId, CoresNeeded = source.CoresNeeded, DateCalculated = source.DateCalculated, DateCreated = source.DateCreated, DateFinished = source.DateFinished, Exception = source.Exception, ExecutionTime = source.ExecutionTime, MemoryNeeded = source.MemoryNeeded, ParentJobId = source.ParentJobId, Priority = source.Priority, SlaveId = source.SlaveId, JobState = source.JobState, UserId = source.UserId, PluginsNeededIds = source.RequiredPlugins.Select(x => x.PluginId).ToList() }; } public static Job ToEntity(DT.Job source) { if (source == null) return null; var entity = new Job(); ToEntity(source, entity); return entity; } public static void ToEntity(DT.Job source, Job target) { if ((source != null) && (target != null)) { target.JobId = source.Id; target.CoresNeeded = source.CoresNeeded; target.DateCalculated = source.DateCalculated; target.DateCreated = source.DateCreated; target.DateFinished = source.DateFinished; target.Exception = source.Exception; target.ExecutionTime = source.ExecutionTime; target.MemoryNeeded = source.MemoryNeeded; target.ParentJobId = source.ParentJobId; target.Priority = source.Priority; target.SlaveId = source.SlaveId; target.JobState = source.JobState; target.UserId = source.UserId; // target.RequiredPlugins = db.Plugins.Select(x => source.PluginsNeededIds.Contains(x.PluginId)); - this is difficult } } #endregion #region JobData public static DT.JobData ToDto(JobData source) { if (source == null) return null; return new DT.JobData { JobId = source.JobId, Data = source.Data.ToArray(), LastUpdate = source.LastUpdate }; } public static JobData ToEntity(DT.JobData source) { if (source == null) return null; var entity = new JobData(); ToEntity(source, entity); return entity; } public static void ToEntity(DT.JobData source, JobData target) { if ((source != null) && (target != null)) { target.JobId = source.JobId; target.Data = new Binary(source.Data); target.LastUpdate = source.LastUpdate; } } #endregion #region HiveExperiment public static DT.HiveExperiment ToDto(HiveExperiment source) { if (source == null) return null; return new DT.HiveExperiment { Id = source.HiveExperimentId, Description = source.Description, Name = source.Name, RootJobId = source.RootJobId, UserId = source.UserId, DateCreated = source.DateCreated }; } public static HiveExperiment ToEntity(DT.HiveExperiment source) { if (source == null) return null; var entity = new HiveExperiment(); ToEntity(source, entity); return entity; } public static void ToEntity(DT.HiveExperiment source, HiveExperiment target) { if ((source != null) && (target != null)) { target.HiveExperimentId = source.Id; target.Description = source.Description; target.Name = source.Name; target.RootJobId = source.RootJobId; target.UserId = source.UserId; target.DateCreated = source.DateCreated; } } #endregion #region Plugin public static DT.Plugin ToDto(Plugin source) { if (source == null) return null; return new DT.Plugin { Id = source.PluginId, Name = source.Name, Version = new Version(source.Version) }; } public static Plugin ToEntity(DT.Plugin source) { if (source == null) return null; var entity = new Plugin(); ToEntity(source, entity); return entity; } public static void ToEntity(DT.Plugin source, Plugin target) { if ((source != null) && (target != null)) { target.PluginId = source.Id; target.Name = source.Name; target.Version = source.Version.ToString(); } } #endregion #region PluginData public static DT.PluginData ToDto(PluginData source) { if (source == null) return null; return new DT.PluginData { PluginId = source.PluginId, Data = source.Data.ToArray(), FileName = source.FileName, PluginDataId = source.PluginDataId }; } public static PluginData ToEntity(DT.PluginData source) { if (source == null) return null; var entity = new PluginData(); ToEntity(source, entity); return entity; } public static void ToEntity(DT.PluginData source, PluginData target) { if ((source != null) && (target != null)) { target.PluginId = source.PluginId; target.Data = new Binary(source.Data); target.FileName = target.FileName; } } #endregion #region Slave public static DT.Slave ToDto(Slave source) { if (source == null) return null; return new DT.Slave { Id = source.ResourceId, ParentResourceId = source.ParentResourceId, Cores = source.Cores, CpuSpeed = source.CpuSpeed, FreeCores = source.FreeCores, FreeMemory = source.FreeMemory, IsAllowedToCalculate = source.IsAllowedToCalculate, Memory = source.Memory, Name = source.Name, SlaveState = source.SlaveState }; } public static Slave ToEntity(DT.Slave source) { if (source == null) return null; var entity = new Slave(); ToEntity(source, entity); return entity; } public static void ToEntity(DT.Slave source, Slave target) { if ((source != null) && (target != null)) { target.ResourceId = source.Id; target.ParentResourceId = source.ParentResourceId; target.Cores = source.Cores; target.CpuSpeed = source.CpuSpeed; target.FreeCores = source.FreeCores; target.FreeMemory = source.FreeMemory; target.IsAllowedToCalculate = source.IsAllowedToCalculate; target.Memory = source.Memory; target.Name = source.Name; target.SlaveState = source.SlaveState; } } #endregion #region SlaveGroup public static DT.SlaveGroup ToDto(SlaveGroup source) { if (source == null) return null; return new DT.SlaveGroup { Id = source.ResourceId, Name = source.Name, ParentResourceId = source.ParentResourceId }; } public static SlaveGroup ToEntity(DT.SlaveGroup source) { if (source == null) return null; var entity = new SlaveGroup(); ToEntity(source, entity); return entity; } public static void ToEntity(DT.SlaveGroup source, SlaveGroup target) { if ((source != null) && (target != null)) { target.ResourceId = source.Id; target.Name = source.Name; target.ParentResourceId = source.ParentResourceId; } } #endregion #region Resource public static DT.Resource ToDto(Resource source) { if (source == null) return null; return new DT.Resource { Id = source.ResourceId, Name = source.Name, ParentResourceId = source.ParentResourceId }; } public static Resource ToEntity(DT.Resource source) { if (source == null) return null; var entity = new Resource(); ToEntity(source, entity); return entity; } public static void ToEntity(DT.Resource source, Resource target) { if ((source != null) && (target != null)) { target.ResourceId = source.Id; target.Name = source.Name; target.ParentResourceId = source.ParentResourceId; } } #endregion } }