Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.Access/3.3/Convert.cs @ 13780

Last change on this file since 13780 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 12.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 DA = HeuristicLab.Services.Access.DataAccess;
26using DT = HeuristicLab.Services.Access.DataTransfer;
27
28
29namespace HeuristicLab.Services.Access {
30  public static class Convert {
31
32    #region Resource
33    public static DT.Resource ToDto(DA.Resource source) {
34      return new DT.Resource() {
35        Id = source.Id,
36        Description = source.Description,
37        Name = source.Name
38      };
39    }
40
41    public static DA.Resource ToEntity(DT.Resource source) {
42      return new DA.Resource() {
43        Id = source.Id,
44        Description = source.Description,
45        Name = source.Name
46      };
47    }
48    #endregion
49
50    #region ClientGroup
51    public static DT.ClientGroup ToDto(DA.ClientGroup source) {
52      return new DT.ClientGroup() {
53        Id = source.Id,
54        Description = source.Description,
55        Name = source.Name
56      };
57    }
58
59    public static DA.ClientGroup ToEntity(DT.ClientGroup source) {
60      return new DA.ClientGroup() {
61        Id = source.Id,
62        Description = source.Description,
63        Name = source.Name,
64
65      };
66    }
67    #endregion
68
69    #region Country
70    public static DT.Country ToDto(DA.Country source) {
71      if (source == null) {
72        return null;
73      } else {
74        return new DT.Country() {
75          Id = source.Id,
76          Name = source.Name
77        };
78      }
79    }
80
81    public static DA.Country ToEntity(DT.Country source) {
82      if (source == null) {
83        return null;
84      } else {
85        return new DA.Country() {
86          Id = source.Id,
87          Name = source.Name,
88
89        };
90      }
91    }
92    #endregion
93
94    #region OperatingSystem
95    public static DT.OperatingSystem ToDto(DA.OperatingSystem source) {
96      if (source == null) {
97        return null;
98      } else {
99        return new DT.OperatingSystem() {
100          Id = source.Id,
101          Name = source.Name
102        };
103      }
104    }
105
106    public static DA.OperatingSystem ToEntity(DT.OperatingSystem source) {
107      if (source == null) {
108        return null;
109      } else {
110        return new DA.OperatingSystem() {
111          Id = source.Id,
112          Name = source.Name,
113        };
114      }
115    }
116    #endregion
117
118    #region ClientType
119    public static DT.ClientType ToDto(DA.ClientType source) {
120      if (source == null) {
121        return null;
122      } else {
123        return new DT.ClientType() {
124          Id = source.Id,
125          Name = source.Name
126        };
127      }
128    }
129
130    public static DA.ClientType ToEntity(DT.ClientType source) {
131      if (source == null) {
132        return null;
133      } else {
134        return new DA.ClientType() {
135          Id = source.Id,
136          Name = source.Name,
137
138        };
139      }
140    }
141    #endregion
142
143    #region ClientConfiguration
144    public static DT.ClientConfiguration ToDto(DA.ClientConfiguration source) {
145      if (source == null) {
146        return null;
147      } else {
148        return new DT.ClientConfiguration() {
149          Id = source.Id,
150          Hash = source.Hash,
151          Description = source.Description
152        };
153      }
154    }
155
156    public static DA.ClientConfiguration ToEntity(DT.ClientConfiguration source) {
157      if (source == null) {
158        return null;
159      } else {
160        return new DA.ClientConfiguration() {
161          Id = source.Id,
162          Hash = source.Hash,
163          Description = source.Description
164        };
165      }
166    }
167    #endregion
168
169    #region Plugin
170    public static DT.Plugin ToDto(DA.Plugin source) {
171      return new DT.Plugin() {
172        Id = source.Id,
173        Name = source.Name,
174        StrongName = source.StrongName,
175        Version = source.Version
176      };
177    }
178
179    public static DA.Plugin ToEntity(DT.Plugin source) {
180      return new DA.Plugin() {
181        Id = source.Id,
182        Name = source.Name,
183        StrongName = source.StrongName,
184        Version = source.Version
185      };
186    }
187    #endregion
188
189    #region Client
190    public static DT.Client ToDto(DA.Client source) {
191      return new DT.Client() {
192        Id = source.Id,
193        Description = source.Description,
194        Name = source.Name,
195        ClientConfiguration = ToDto(source.ClientConfiguration),
196        HeuristicLabVersion = source.HeuristicLabVersion,
197        Country = ToDto(source.Country),
198        OperatingSystem = ToDto(source.OperatingSystem),
199        MemorySize = source.MemorySize.GetValueOrDefault(),
200        Timestamp = source.Timestamp.GetValueOrDefault(),
201        NumberOfCores = source.NumberOfCores.GetValueOrDefault(),
202        ProcessorType = source.ProcessorType,
203        PerformanceValue = source.PerformanceValue.GetValueOrDefault(),
204        ClientType = ToDto(source.ClientType)
205      };
206    }
207
208    public static DA.Client ToEntity(DT.Client source) {
209      return new DA.Client() {
210        Id = source.Id,
211        Description = source.Description,
212        Name = source.Name,
213        ClientConfiguration = ToEntity(source.ClientConfiguration),
214        HeuristicLabVersion = source.HeuristicLabVersion,
215        Country = ToEntity(source.Country),
216        OperatingSystem = ToEntity(source.OperatingSystem),
217        MemorySize = source.MemorySize,
218        Timestamp = source.Timestamp,
219        NumberOfCores = source.NumberOfCores,
220        ProcessorType = source.ProcessorType,
221        PerformanceValue = source.PerformanceValue,
222        ClientType = ToEntity(source.ClientType)
223      };
224    }
225    #endregion
226
227    #region ClientLog
228    public static DT.ClientLog ToDto(DA.ClientLog source) {
229      return new DT.ClientLog() {
230        Timestamp = source.Timestamp,
231        ResourceId = source.ResourceId,
232        Message = source.Message
233      };
234    }
235
236    public static DA.ClientLog ToEntity(DT.ClientLog source) {
237      return new DA.ClientLog() {
238        Timestamp = source.Timestamp,
239        ResourceId = source.ResourceId,
240        Message = source.Message
241      };
242    }
243    #endregion
244
245    #region ClientError
246    public static DT.ClientError ToDto(DA.ClientError source) {
247      return new DT.ClientError() {
248        Id = source.Id,
249        Timestamp = source.Timestamp,
250        Exception = source.Exception,
251        UserComment = source.UserComment,
252        ConfigDump = source.ConfigDump,
253        ClientId = source.ClientId.GetValueOrDefault(),
254        UserId = source.UserId.GetValueOrDefault()
255      };
256    }
257
258    public static DA.ClientError ToEntity(DT.ClientError source) {
259      return new DA.ClientError() {
260        Id = source.Id,
261        Timestamp = source.Timestamp,
262        Exception = source.Exception,
263        UserComment = source.UserComment,
264        ConfigDump = source.ConfigDump,
265        ClientId = source.ClientId,
266        UserId = source.UserId
267      };
268    }
269    #endregion
270
271    #region UserGroup
272    public static DT.UserGroup ToDto(DA.UserGroup source) {
273      return new DT.UserGroup() {
274        Id = source.Id,
275        Name = source.Name
276      };
277    }
278
279    public static DA.UserGroup ToEntity(DT.UserGroup source) {
280      return new DA.UserGroup() {
281        Id = source.Id,
282        Name = source.Name
283      };
284    }
285
286    public static void ToEntity(DT.UserGroup source, DA.UserGroup target) {
287      target.Name = source.Name;
288    }
289    #endregion
290
291    #region User
292
293    public static DT.LightweightUser ToDto(DA.User source, DA.aspnet_User aspUserSource, DA.aspnet_Membership aspMembershipSource, List<DA.aspnet_Role> roles, List<DA.UserGroup> groups) {
294      return new DT.LightweightUser() {
295        Id = source.Id,
296        FullName = source.FullName,
297        UserName = aspUserSource.UserName,
298        EMail = aspMembershipSource.Email,
299        //TODO: check if the roles and groups are include in source
300        Roles = roles.Select(x => Convert.ToDto(x)).ToArray(),
301        Groups = groups.Select(x => Convert.ToDto(x)).ToArray()
302      };
303    }
304
305    public static DT.User ToDto(DA.User source, DA.aspnet_User aspUserSource, DA.aspnet_Membership aspMembershipSource) {
306      return new DT.User() {
307        Id = source.Id,
308        FullName = source.FullName,
309        Comment = aspMembershipSource.Comment,
310        CreationDate = aspMembershipSource.CreateDate,
311        Email = aspMembershipSource.Email,
312        IsApproved = aspMembershipSource.IsApproved,
313        LastActivityDate = aspUserSource.LastActivityDate,
314        LastLoginDate = aspMembershipSource.LastLoginDate,
315        LastPasswordChangedDate = aspMembershipSource.LastPasswordChangedDate,
316        UserName = aspUserSource.UserName
317      };
318    }
319
320    public static DA.User ToEntity(DT.User source) {
321      return new DA.User() { Id = source.Id, FullName = source.FullName };
322    }
323
324    public static void ToEntity(DT.User source, out DA.User accessUser, out DA.aspnet_User aspUser, out DA.aspnet_Membership aspMembership, out bool userExistsInASP) {
325      userExistsInASP = false;
326      accessUser = new DA.User();
327      aspUser = new DA.aspnet_User();
328      aspMembership = new DA.aspnet_Membership();
329
330      if (source.Id != Guid.Empty) {
331        using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
332          var userCol = context.aspnet_Users.Where(s => s.UserId == source.Id);
333          var membershipCol = context.aspnet_Memberships.Where(s => s.UserId == source.Id);
334          if (userCol.Count() > 0 && membershipCol.Count() > 0) {
335            aspUser = userCol.First();
336            aspMembership = membershipCol.First();
337            userExistsInASP = true;
338          }
339        }
340      }
341
342      accessUser.Id = source.Id;
343      accessUser.FullName = source.FullName;
344
345      aspUser.UserId = source.Id;
346      aspUser.LastActivityDate = source.LastActivityDate;
347      aspUser.UserName = source.UserName;
348
349      aspMembership.UserId = source.Id;
350      aspMembership.Comment = source.Comment;
351      aspMembership.CreateDate = source.CreationDate;
352      aspMembership.Email = source.Email;
353      aspMembership.IsApproved = source.IsApproved;
354      aspMembership.LastLoginDate = source.LastLoginDate;
355      aspMembership.LastPasswordChangedDate = source.LastPasswordChangedDate;
356    }
357    #endregion
358
359    #region ClientGroupMapping
360    public static DT.ClientGroupMapping ToDto(DA.ResourceResourceGroup source) {
361      return new DT.ClientGroupMapping() {
362        Child = source.ResourceId, Parent = source.ResourceGroupId
363      };
364    }
365    #endregion
366
367    #region UserGroupBase
368    public static DT.UserGroupBase ToDto(DA.UserGroupBase source) {
369      return new DT.UserGroupBase() {
370        Id = source.Id
371      };
372    }
373    #endregion
374
375    #region UserGroupMapping
376    public static DT.UserGroupMapping ToDto(DA.UserGroupUserGroup source) {
377      return new DT.UserGroupMapping() {
378        Child = source.UserGroupId, Parent = source.UserGroupUserGroupId
379      };
380    }
381    #endregion
382
383    #region Role
384    public static DT.Role ToDto(DA.aspnet_Role r) {
385      return new DT.Role() {
386        Name = r.RoleName
387      };
388    }
389    public static DA.aspnet_Role ToEntity(DT.Role r) {
390      return new DA.aspnet_Role() {
391        RoleName = r.Name
392      };
393    }
394    #endregion
395  }
396}
Note: See TracBrowser for help on using the repository browser.