Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Services.OKB/3.3/Administration/Convert.cs @ 5482

Last change on this file since 5482 was 5482, checked in by swagner, 14 years ago

Worked on OKB (#1174)

File size: 6.6 KB
RevLine 
[5482]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.Linq;
23using System.Security.Cryptography;
24using DA = HeuristicLab.Services.OKB.DataAccess;
25using DT = HeuristicLab.Services.OKB.Administration.DataTransfer;
26
27namespace HeuristicLab.Services.OKB.Administration {
28  public static class Convert {
29    #region Platform
30    public static DT.Platform ToDto(DA.Platform source) {
31      if (source == null) return null;
32      return new DT.Platform { Id = source.Id, Name = source.Name, Description = source.Description };
33    }
34    public static DA.Platform ToEntity(DT.Platform source) {
35      if (source == null) return null;
36      return new DA.Platform { Id = source.Id, Name = source.Name, Description = source.Description };
37    }
38    public static void ToEntity(DT.Platform source, DA.Platform target) {
39      if ((source != null) && (target != null)) {
40        target.Id = source.Id; target.Name = source.Name; target.Description = source.Description;
41      }
42    }
43    #endregion
44
45    #region AlgorithmClass
46    public static DT.AlgorithmClass ToDto(DA.AlgorithmClass source) {
47      if (source == null) return null;
48      return new DT.AlgorithmClass { Id = source.Id, Name = source.Name, Description = source.Description };
49    }
50    public static DA.AlgorithmClass ToEntity(DT.AlgorithmClass source) {
51      if (source == null) return null;
52      return new DA.AlgorithmClass { Id = source.Id, Name = source.Name, Description = source.Description };
53    }
54    public static void ToEntity(DT.AlgorithmClass source, DA.AlgorithmClass target) {
55      if ((source != null) && (target != null)) {
56        target.Id = source.Id; target.Name = source.Name; target.Description = source.Description;
57      }
58    }
59    #endregion
60
61    #region Algorithm
62    public static DT.Algorithm ToDto(DA.Algorithm source) {
63      if (source == null) return null;
64      return new DT.Algorithm { Id = source.Id, Name = source.Name, Description = source.Description, PlatformId = source.PlatformId, AlgorithmClassId = source.AlgorithmClassId, DataType = Convert.ToDto(source.DataType) };
65    }
66    public static DA.Algorithm ToEntity(DT.Algorithm source, DA.OKBDataContext okb) {
67      if (source == null) return null;
68      return new DA.Algorithm { Id = source.Id, Name = source.Name, Description = source.Description, PlatformId = source.PlatformId, AlgorithmClassId = source.AlgorithmClassId, DataType = Convert.ToEntity(source.DataType, okb) };
69    }
70    public static void ToEntity(DT.Algorithm source, DA.Algorithm target, DA.OKBDataContext okb) {
71      if ((source != null) && (target != null)) {
72        target.Id = source.Id; target.Name = source.Name; target.Description = source.Description; target.PlatformId = source.PlatformId; target.AlgorithmClassId = source.AlgorithmClassId; target.DataType = Convert.ToEntity(source.DataType, okb);
73      }
74    }
75    #endregion
76
77    #region ProblemClass
78    public static DT.ProblemClass ToDto(DA.ProblemClass source) {
79      if (source == null) return null;
80      return new DT.ProblemClass { Id = source.Id, Name = source.Name, Description = source.Description };
81    }
82    public static DA.ProblemClass ToEntity(DT.ProblemClass source) {
83      if (source == null) return null;
84      return new DA.ProblemClass { Id = source.Id, Name = source.Name, Description = source.Description };
85    }
86    public static void ToEntity(DT.ProblemClass source, DA.ProblemClass target) {
87      if ((source != null) && (target != null)) {
88        target.Id = source.Id; target.Name = source.Name; target.Description = source.Description;
89      }
90    }
91    #endregion
92
93    #region Problem
94    public static DT.Problem ToDto(DA.Problem source) {
95      if (source == null) return null;
96      return new DT.Problem { Id = source.Id, Name = source.Name, Description = source.Description, PlatformId = source.PlatformId, ProblemClassId = source.ProblemClassId, DataType = Convert.ToDto(source.DataType) };
97    }
98    public static DA.Problem ToEntity(DT.Problem source, DA.OKBDataContext okb) {
99      if (source == null) return null;
100      return new DA.Problem { Id = source.Id, Name = source.Name, Description = source.Description, PlatformId = source.PlatformId, ProblemClassId = source.ProblemClassId, DataType = Convert.ToEntity(source.DataType, okb) };
101    }
102    public static void ToEntity(DT.Problem source, DA.Problem target, DA.OKBDataContext okb) {
103      if ((source != null) && (target != null)) {
104        target.Id = source.Id; target.Name = source.Name; target.Description = source.Description; target.PlatformId = source.PlatformId; target.ProblemClassId = source.ProblemClassId; target.DataType = Convert.ToEntity(source.DataType, okb);
105      }
106    }
107    #endregion
108
109    #region DataType
110    private static DT.DataType ToDto(DA.DataType source) {
111      if (source == null) return null;
112      return new DT.DataType { Name = source.Name, TypeName = source.TypeName };
113    }
114    private static DA.DataType ToEntity(DT.DataType source, DA.OKBDataContext okb) {
115      if (source == null) return null;
116      var entity = okb.DataTypes.Where(x => (x.Name == source.Name) && (x.TypeName == source.TypeName)).FirstOrDefault();
117      if (entity == null)
118        entity = new DA.DataType() { Id = 0, Name = source.Name, TypeName = source.TypeName };
119      return entity;
120    }
121    #endregion
122
123    #region BinaryData
124    internal static DA.BinaryData ToEntity(byte[] data, DA.OKBDataContext okb) {
125      if (data == null) return null;
126      byte[] hash;
127      using (SHA1 sha1 = SHA1.Create()) {
128        hash = sha1.ComputeHash(data);
129      }
130      var entity = okb.BinaryDatas.Where(x => x.Hash.Equals(hash)).FirstOrDefault();
131      if (entity == null)
132        entity = new DA.BinaryData() { Id = 0, Data = data };
133      return entity;
134    }
135    #endregion
136  }
137}
Note: See TracBrowser for help on using the repository browser.