Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Services.OKB/3.3/RunCreation/Convert.cs @ 11733

Last change on this file since 11733 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Collections.Generic;
24using System.Linq;
25using System.Security.Cryptography;
26using DA = HeuristicLab.Services.OKB.DataAccess;
27using DT = HeuristicLab.Services.OKB.RunCreation.DataTransfer;
28
29namespace HeuristicLab.Services.OKB.RunCreation {
30  public static class Convert {
31    #region ToDto
32    public static DT.Algorithm ToDto(DA.Algorithm source) {
33      if (source == null) return null;
34      return new DT.Algorithm { Id = source.Id, Name = source.Name, Description = source.Description, AlgorithmClass = Convert.ToDto(source.AlgorithmClass), DataType = Convert.ToDto(source.DataType) };
35    }
36
37    public static DT.Problem ToDto(DA.Problem source) {
38      if (source == null) return null;
39      return new DT.Problem { Id = source.Id, Name = source.Name, Description = source.Description, ProblemClass = Convert.ToDto(source.ProblemClass), DataType = Convert.ToDto(source.DataType) };
40    }
41
42    private static DT.DataType ToDto(DA.DataType source) {
43      if (source == null) return null;
44      return new DT.DataType { Name = source.Name, TypeName = source.TypeName };
45    }
46
47    private static DT.AlgorithmClass ToDto(DA.AlgorithmClass source) {
48      if (source == null) return null;
49      return new DT.AlgorithmClass { Name = source.Name, Description = source.Description };
50    }
51
52    private static DT.ProblemClass ToDto(DA.ProblemClass source) {
53      if (source == null) return null;
54      return new DT.ProblemClass { Name = source.Name, Description = source.Description };
55    }
56    #endregion
57
58    #region ToEntity
59    public static DA.Run ToEntity(DT.Run source, DA.OKBDataContext okb) {
60      if (source == null) return null;
61
62      List<DA.BinaryData> binCache = new List<DA.BinaryData>();
63
64      DA.Run entity = new DA.Run { Id = 0, AlgorithmId = source.AlgorithmId, ProblemId = source.ProblemId, CreatedDate = source.CreatedDate, UserId = source.UserId, ClientId = source.ClientId };
65      foreach (var value in source.ParameterValues)
66        entity.Values.Add(Convert.ToEntity(value, entity, DA.ValueNameCategory.Parameter, okb, binCache));
67      foreach (var value in source.ResultValues)
68        entity.Values.Add(Convert.ToEntity(value, entity, DA.ValueNameCategory.Result, okb, binCache));
69      return entity;
70    }
71
72    private static DA.Value ToEntity(DT.Value source, DA.Run run, DA.ValueNameCategory category, DA.OKBDataContext okb, List<DA.BinaryData> binCache) {
73      if (source == null) return null;
74      var entity = new DA.Value();
75      entity.Run = run;
76      entity.DataType = Convert.ToEntity(source.DataType, okb);
77      if (source is DT.BoolValue) {
78        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Bool, okb);
79        entity.BoolValue = ((DT.BoolValue)source).Value;
80      } else if (source is DT.IntValue) {
81        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Int, okb);
82        entity.IntValue = ((DT.IntValue)source).Value;
83      } else if (source is DT.TimeSpanValue) {
84        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.TimeSpan, okb);
85        entity.LongValue = ((DT.TimeSpanValue)source).Value;
86      } else if (source is DT.LongValue) {
87        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Long, okb);
88        entity.LongValue = ((DT.LongValue)source).Value;
89      } else if (source is DT.FloatValue) {
90        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Float, okb);
91        entity.FloatValue = ((DT.FloatValue)source).Value;
92      } else if (source is DT.DoubleValue) {
93        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Double, okb);
94        entity.DoubleValue = ((DT.DoubleValue)source).Value;
95      } else if (source is DT.PercentValue) {
96        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Percent, okb);
97        entity.DoubleValue = ((DT.PercentValue)source).Value;
98      } else if (source is DT.StringValue) {
99        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.String, okb);
100        entity.StringValue = ((DT.StringValue)source).Value;
101      } else if (source is DT.BinaryValue) {
102        entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Binary, okb);
103        entity.BinaryData = Convert.ToEntity(((DT.BinaryValue)source).Value, okb, binCache);
104      } else {
105        throw new ArgumentException("Unknown value type.", "source");
106      }
107      return entity;
108    }
109
110    private static DA.DataType ToEntity(DT.DataType source, DA.OKBDataContext okb) {
111      if (source == null) return null;
112      var entity = okb.DataTypes.Where(x => (x.Name == source.Name) && (x.TypeName == source.TypeName)).FirstOrDefault();
113      if (entity == null)
114        entity = new DA.DataType() { Id = 0, Name = source.Name, TypeName = source.TypeName };
115      return entity;
116    }
117
118    private static DA.ValueName ToEntity(string name, DA.ValueNameCategory category, DA.ValueNameType type, DA.OKBDataContext okb) {
119      if (string.IsNullOrEmpty(name)) return null;
120      var entity = okb.ValueNames.Where(x => (x.Name == name) && (x.Category == category) && (x.Type == type)).FirstOrDefault();
121      if (entity == null)
122        entity = new DA.ValueName() { Id = 0, Name = name, Category = category, Type = type };
123      return entity;
124    }
125
126    private static DA.BinaryData ToEntity(byte[] data, DA.OKBDataContext okb, List<DA.BinaryData> binCache) {
127      if (data == null) return null;
128      byte[] hash;
129      using (SHA1 sha1 = SHA1.Create()) {
130        hash = sha1.ComputeHash(data);
131      }
132
133      var cachedBinaryData = binCache.Where(x => x.Hash.SequenceEqual(hash)).FirstOrDefault();
134      if (cachedBinaryData != null)
135        return cachedBinaryData;
136
137      var entity = okb.BinaryDatas.Where(x => x.Hash.Equals(hash)).FirstOrDefault();
138      if (entity == null) {
139        entity = new DA.BinaryData() { Id = 0, Data = data, Hash = hash };
140        binCache.Add(entity);
141      }
142
143      return entity;
144    }
145    #endregion
146  }
147}
Note: See TracBrowser for help on using the repository browser.