Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClientUserManagement/HeuristicLab.Services.Access/3.3/Convert.cs @ 6858

Last change on this file since 6858 was 6858, checked in by ascheibe, 13 years ago

#1648 worked on webservice and added more unit tests

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