Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 925 was 925, checked in by svonolfe, 15 years ago

Added caching, thread safety to DataAccess layer (#372)

File size: 4.4 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 =
40       ServiceLocator.GetPermissionOwnerAdapter();
41
42    public UserAdapter() {
43      adapter.Fill(data);
44    }
45
46    protected override void Update() {
47      this.adapter.Update(this.data);
48    }
49
50    private User Convert(dsHiveServer.HiveUserRow row,
51      User user) {
52      if (row != null && user != null) {
53        /*Parent - PermissionOwner*/
54        user.PermissionOwnerId = row.PermissionOwnerId;
55        permOwnerAdapter.GetPermissionOwnerById(user);
56
57        /*User*/
58        if (!row.IsPasswordNull())
59          user.Password = row.Password;
60        else
61          user.Password = String.Empty;
62
63        return user;
64      } else
65        return null;
66    }
67
68    private dsHiveServer.HiveUserRow Convert(User user,
69      dsHiveServer.HiveUserRow row) {
70      if (user != null && row != null) {
71        row.Password = user.Password;
72
73        return row;
74      } else
75        return null;     
76    }
77
78    #region IUserAdapter Members
79
80    [MethodImpl(MethodImplOptions.Synchronized)]
81    public void UpdateUser(User user) {
82      if (user != null) {
83        permOwnerAdapter.UpdatePermissionOwner(user);
84
85        dsHiveServer.HiveUserRow row =
86          data.FindByPermissionOwnerId(user.PermissionOwnerId);
87        if (row == null) {
88          row = data.NewHiveUserRow();
89          row.PermissionOwnerId = user.PermissionOwnerId;
90          data.AddHiveUserRow(row);
91        }
92
93        Convert(user, row);
94      }
95    }
96
97    public User GetUserById(long userId) {
98      User user = new User();
99
100      dsHiveServer.HiveUserRow row =
101          data.FindByPermissionOwnerId(userId);
102
103      if(row != null) {
104        Convert(row, user);
105
106        return user;
107      } else {
108        return null;
109      }
110    }
111
112    public User GetUserByName(String name) {
113      User user = new User();
114
115      PermissionOwner permOwner =
116        permOwnerAdapter.GetPermissionOwnerByName(name);
117
118      if (permOwner != null) {
119        dsHiveServer.HiveUserRow row =
120          data.FindByPermissionOwnerId(permOwner.PermissionOwnerId);
121
122        if (row != null) {
123          Convert(row, user);
124
125          return user;
126        }
127      }
128
129      return null;
130    }
131
132    public ICollection<User> GetAllUsers() {
133      ICollection<User> allUsers =
134        new List<User>();
135
136      foreach (dsHiveServer.HiveUserRow row in data) {
137        User user = new User();
138        Convert(row, user);
139        allUsers.Add(user);
140      }
141
142      return allUsers;
143    }
144
145    [MethodImpl(MethodImplOptions.Synchronized)]
146    public bool DeleteUser(User user) {
147      if (user != null) {
148        dsHiveServer.HiveUserRow row =
149          data.FindByPermissionOwnerId(user.PermissionOwnerId);
150
151        if (row != null) {
152          data.RemoveHiveUserRow(row);
153
154          return permOwnerAdapter.DeletePermissionOwner(user);
155        }
156      }
157
158      return false;
159    }
160
161    #endregion
162  }
163}
Note: See TracBrowser for help on using the repository browser.