Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1648 worked on user and client information singletons

File size: 11.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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        PerformanceValue = source.PerformanceValue.GetValueOrDefault(),
203        ClientType = ToDto(source.ClientType)
204      };
205    }
206
207    public static DA.Client ToEntity(DT.Client source) {
208      return new DA.Client() {
209        Id = source.Id,
210        Description = source.Description,
211        Name = source.Name,
212        ClientConfiguration = ToEntity(source.ClientConfiguration),
213        HeuristicLabVersion = source.HeuristicLabVersion,
214        Country = ToEntity(source.Country),
215        OperatingSystem = ToEntity(source.OperatingSystem),
216        MemorySize = source.MemorySize,
217        Timestamp = source.Timestamp,
218        NumberOfCores = source.NumberOfCores,
219        ProcessorType = source.ProcessorType,
220        PerformanceValue = source.PerformanceValue,
221        ClientType = ToEntity(source.ClientType)
222      };
223    }
224    #endregion
225
226    #region ClientLog
227    public static DT.ClientLog ToDto(DA.ClientLog source) {
228      return new DT.ClientLog() {
229        Timestamp = source.Timestamp,
230        ResourceId = source.ResourceId,
231        Message = source.Message
232      };
233    }
234
235    public static DA.ClientLog ToEntity(DT.ClientLog source) {
236      return new DA.ClientLog() {
237        Timestamp = source.Timestamp,
238        ResourceId = source.ResourceId,
239        Message = source.Message
240      };
241    }
242    #endregion
243
244    #region ClientError
245    public static DT.ClientError ToDto(DA.ClientError source) {
246      return new DT.ClientError() {
247        Id = source.Id,
248        Timestamp = source.Timestamp,
249        Exception = source.Exception,
250        UserComment = source.UserComment,
251        ConfigDump = source.ConfigDump,
252        ClientId = source.ClientId.GetValueOrDefault(),
253        UserId = source.UserId.GetValueOrDefault()
254      };
255    }
256
257    public static DA.ClientError ToEntity(DT.ClientError source) {
258      return new DA.ClientError() {
259        Id = source.Id,
260        Timestamp = source.Timestamp,
261        Exception = source.Exception,
262        UserComment = source.UserComment,
263        ConfigDump = source.ConfigDump,
264        ClientId = source.ClientId,
265        UserId = source.UserId
266      };
267    }
268    #endregion
269
270    #region UserGroup
271    public static DT.UserGroup ToDto(DA.UserGroup source) {
272      return new DT.UserGroup() {
273        Id = source.Id,
274        Name = source.Name
275      };
276    }
277
278    public static DA.UserGroup ToEntity(DT.UserGroup source) {
279      return new DA.UserGroup() {
280        Id = source.Id,
281        Name = source.Name
282      };
283    }
284    #endregion
285
286    #region User
287
288    public static DT.LightweightUser ToDto(DA.User source, DA.aspnet_User aspUserSource) {
289      return new DT.LightweightUser() {
290        Id = source.Id,
291        FullName = source.FullName,
292        UserName = aspUserSource.UserName
293      };
294    }
295
296    public static DT.User ToDto(DA.User source, DA.aspnet_User aspUserSource, DA.aspnet_Membership aspMembershipSource) {
297      return new DT.User() {
298        Id = source.Id,
299        FullName = source.FullName,
300        Comment = aspMembershipSource.Comment,
301        CreationDate = aspMembershipSource.CreateDate,
302        Email = aspMembershipSource.Email,
303        IsApproved = aspMembershipSource.IsApproved,
304        LastActivityDate = aspUserSource.LastActivityDate,
305        LastLoginDate = aspMembershipSource.LastLoginDate,
306        LastPasswordChangedDate = aspMembershipSource.LastPasswordChangedDate,
307        UserName = aspUserSource.UserName
308      };
309    }
310
311    public static DA.User ToEntity(DT.User source) {
312      return new DA.User() { Id = source.Id, FullName = source.FullName };
313    }
314
315    public static void ToEntity(DT.User source, out DA.User accessUser, out DA.aspnet_User aspUser, out DA.aspnet_Membership aspMembership, out bool userExistsInASP) {
316      userExistsInASP = false;
317      accessUser = new DA.User();
318      aspUser = new DA.aspnet_User();
319      aspMembership = new DA.aspnet_Membership();
320
321      if (source.Id != Guid.Empty) {
322        using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
323          var userCol = context.aspnet_Users.Where(s => s.UserId == source.Id);
324          var membershipCol = context.aspnet_Memberships.Where(s => s.UserId == source.Id);
325          if (userCol.Count() > 0 && membershipCol.Count() > 0) {
326            aspUser = userCol.First();
327            aspMembership = membershipCol.First();
328            userExistsInASP = true;
329          }
330        }
331      }
332
333      accessUser.Id = source.Id;
334      accessUser.FullName = source.FullName;
335
336      aspUser.UserId = source.Id;
337      aspUser.LastActivityDate = source.LastActivityDate;
338      aspUser.UserName = source.UserName;
339
340      aspMembership.UserId = source.Id;
341      aspMembership.Comment = source.Comment;
342      aspMembership.CreateDate = source.CreationDate;
343      aspMembership.Email = source.Email;
344      aspMembership.IsApproved = source.IsApproved;
345      aspMembership.LastLoginDate = source.LastLoginDate;
346      aspMembership.LastPasswordChangedDate = source.LastPasswordChangedDate;
347    }
348    #endregion
349
350    #region ClientGroupMapping
351    public static DT.ClientGroupMapping ToDto(DA.ResourceResourceGroup source) {
352      return new DT.ClientGroupMapping() {
353        Child = source.ResourceId, Parent = source.ResourceGroupId
354      };
355    }
356    #endregion
357
358    #region UserGroupBase
359    public static DT.UserGroupBase ToDto(DA.UserGroupBase source) {
360      return new DT.UserGroupBase() {
361        Id = source.Id
362      };
363    }
364    #endregion
365
366    #region UserGroupMapping
367    public static DT.UserGroupMapping ToDto(DA.UserGroupUserGroup source) {
368      return new DT.UserGroupMapping() {
369        Child = source.UserGroupId, Parent = source.UserGroupUserGroupId
370      };
371    }
372    #endregion
373
374    #region Role
375    public static DT.Role ToDto(DA.aspnet_Role r) {
376      return new DT.Role() {
377        Name = r.RoleName
378      };
379    }
380    public static DA.aspnet_Role ToEntity(DT.Role r) {
381      return new DA.aspnet_Role() {
382        RoleName = r.Name
383      };
384    }
385    #endregion
386  }
387}
Note: See TracBrowser for help on using the repository browser.