1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 |
|
---|
22 | using System.Linq;
|
---|
23 | using System.Security.Cryptography;
|
---|
24 | using DA = HeuristicLab.Services.OKB.DataAccess;
|
---|
25 | using DT = HeuristicLab.Services.OKB.Administration.DataTransfer;
|
---|
26 |
|
---|
27 | namespace 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, DataTypeName = source.DataType.Name, DataTypeTypeName = source.DataType.TypeName };
|
---|
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.DataTypeName, source.DataTypeTypeName, 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.DataTypeName, source.DataTypeTypeName, 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, DataTypeName = source.DataType.Name, DataTypeTypeName = source.DataType.TypeName };
|
---|
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.DataTypeName, source.DataTypeTypeName, 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.DataTypeName, source.DataTypeTypeName, okb);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | #endregion
|
---|
108 |
|
---|
109 | #region DataType
|
---|
110 | private static DA.DataType ToEntity(string dataTypeName, string dataTypeTypeName, DA.OKBDataContext okb) {
|
---|
111 | if ((dataTypeName == null) || (dataTypeTypeName == null)) return null;
|
---|
112 | var entity = okb.DataTypes.Where(x => (x.Name == dataTypeName) && (x.TypeName == dataTypeTypeName)).FirstOrDefault();
|
---|
113 | if (entity == null)
|
---|
114 | entity = new DA.DataType() { Id = 0, Name = dataTypeName, TypeName = dataTypeTypeName };
|
---|
115 | return entity;
|
---|
116 | }
|
---|
117 | #endregion
|
---|
118 |
|
---|
119 | #region BinaryData
|
---|
120 | internal static DA.BinaryData ToEntity(byte[] data, DA.OKBDataContext okb) {
|
---|
121 | if (data == null) return null;
|
---|
122 | byte[] hash;
|
---|
123 | using (SHA1 sha1 = SHA1.Create()) {
|
---|
124 | hash = sha1.ComputeHash(data);
|
---|
125 | }
|
---|
126 | var entity = okb.BinaryDatas.Where(x => x.Hash.Equals(hash)).FirstOrDefault();
|
---|
127 | if (entity == null)
|
---|
128 | entity = new DA.BinaryData() { Id = 0, Data = data, Hash = hash };
|
---|
129 | return entity;
|
---|
130 | }
|
---|
131 | #endregion
|
---|
132 | }
|
---|
133 | }
|
---|