source: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/UserAdapter.cs @ 948

Last change on this file since 948 was 948, checked in by svonolfe, 13 years ago

Refactored DAL (#372)

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
26
27using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
28using HeuristicLab.Hive.Contracts.BusinessObjects;
29using System.Runtime.CompilerServices;
30
31namespace HeuristicLab.Hive.Server.ADODataAccess {
32  class UserAdapter : DataAdapterBase, IUserAdapter {
33    private dsHiveServerTableAdapters.HiveUserTableAdapter adapter =
34        new dsHiveServerTableAdapters.HiveUserTableAdapter();
35
36    private dsHiveServer.HiveUserDataTable data =
37        new dsHiveServer.HiveUserDataTable();
38
39    private IPermissionOwnerAdapter permOwnerAdapter = null;
40
41    private IPermissionOwnerAdapter PermOwnerAdapter {
42      get {
43        if (permOwnerAdapter == null)
44          permOwnerAdapter = ServiceLocator.GetPermissionOwnerAdapter();
45
46        return permOwnerAdapter;
47      }
48    }
49
50    private IUserGroupAdapter userGroupAdapter = null;
51
52    private IUserGroupAdapter UserGroupAdapter {
53      get {
54        if(userGroupAdapter == null) {
55          userGroupAdapter = ServiceLocator.GetUserGroupAdapter();
56        }
57
58        return userGroupAdapter;
59      }
60    }
61
62    public UserAdapter() {
63      adapter.Fill(data);
64    }
65
66    protected override void Update() {
67      this.adapter.Update(this.data);
68    }
69
70    private User Convert(dsHiveServer.HiveUserRow row,
71      User user) {
72      if (row != null && user != null) {
73        /*Parent - PermissionOwner*/
74        user.PermissionOwnerId = row.PermissionOwnerId;
75        PermOwnerAdapter.GetPermissionOwnerById(user);
76
77        /*User*/
78        if (!row.IsPasswordNull())
79          user.Password = row.Password;
80        else
81          user.Password = String.Empty;
82
83        return user;
84      } else
85        return null;
86    }
87
88    private dsHiveServer.HiveUserRow Convert(User user,
89      dsHiveServer.HiveUserRow row) {
90      if (user != null && row != null) {
91        row.Password = user.Password;
92
93        return row;
94      } else
95        return null;     
96    }
97
98    #region IUserAdapter Members
99
100    [MethodImpl(MethodImplOptions.Synchronized)]
101    public void UpdateUser(User user) {
102      if (user != null) {
103        PermOwnerAdapter.UpdatePermissionOwner(user);
104
105        dsHiveServer.HiveUserRow row =
106          data.FindByPermissionOwnerId(user.PermissionOwnerId);
107        if (row == null) {
108          row = data.NewHiveUserRow();
109          row.PermissionOwnerId = user.PermissionOwnerId;
110          data.AddHiveUserRow(row);
111        }
112
113        Convert(user, row);
114      }
115    }
116
117    public User GetUserById(long userId) {
118      User user = new User();
119
120      dsHiveServer.HiveUserRow row =
121          data.FindByPermissionOwnerId(userId);
122
123      if(row != null) {
124        Convert(row, user);
125
126        return user;
127      } else {
128        return null;
129      }
130    }
131
132    public User GetUserByName(String name) {
133      User user = new User();
134
135      PermissionOwner permOwner =
136        PermOwnerAdapter.GetPermissionOwnerByName(name);
137
138      if (permOwner != null) {
139        dsHiveServer.HiveUserRow row =
140          data.FindByPermissionOwnerId(permOwner.PermissionOwnerId);
141
142        if (row != null) {
143          Convert(row, user);
144
145          return user;
146        }
147      }
148
149      return null;
150    }
151
152    public ICollection<User> GetAllUsers() {
153      ICollection<User> allUsers =
154        new List<User>();
155
156      foreach (dsHiveServer.HiveUserRow row in data) {
157        User user = new User();
158        Convert(row, user);
159        allUsers.Add(user);
160      }
161
162      return allUsers;
163    }
164
165    [MethodImpl(MethodImplOptions.Synchronized)]
166    public bool DeleteUser(User user) {
167      if (user != null) {
168        dsHiveServer.HiveUserRow row =
169          data.FindByPermissionOwnerId(user.PermissionOwnerId);
170
171        if (row != null) {
172          ICollection<UserGroup> userGroups =
173            UserGroupAdapter.MemberOf(user);
174
175          foreach (UserGroup group in userGroups) {
176            group.Members.Remove(user);
177            UserGroupAdapter.UpdateUserGroup(group);
178          }
179
180          data.RemoveHiveUserRow(row);
181
182          return PermOwnerAdapter.DeletePermissionOwner(user);
183        }
184      }
185
186      return false;
187    }
188
189    #endregion
190  }
191}
Note: See TracBrowser for help on using the repository browser.