Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added Job Adapter (#372)

File size: 5.6 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    private IJobAdapter jobAdapter = null;
63
64    private IJobAdapter JobAdapter {
65      get {
66        if (jobAdapter == null) {
67          jobAdapter = ServiceLocator.GetJobAdapter();
68        }
69
70        return jobAdapter;
71      }
72    }
73
74    public UserAdapter() {
75      adapter.Fill(data);
76    }
77
78    protected override void Update() {
79      this.adapter.Update(this.data);
80    }
81
82    private User Convert(dsHiveServer.HiveUserRow row,
83      User user) {
84      if (row != null && user != null) {
85        /*Parent - PermissionOwner*/
86        user.PermissionOwnerId = row.PermissionOwnerId;
87        PermOwnerAdapter.GetPermissionOwnerById(user);
88
89        /*User*/
90        if (!row.IsPasswordNull())
91          user.Password = row.Password;
92        else
93          user.Password = String.Empty;
94
95        return user;
96      } else
97        return null;
98    }
99
100    private dsHiveServer.HiveUserRow Convert(User user,
101      dsHiveServer.HiveUserRow row) {
102      if (user != null && row != null) {
103        row.Password = user.Password;
104
105        return row;
106      } else
107        return null;     
108    }
109
110    #region IUserAdapter Members
111
112    [MethodImpl(MethodImplOptions.Synchronized)]
113    public void UpdateUser(User user) {
114      if (user != null) {
115        PermOwnerAdapter.UpdatePermissionOwner(user);
116
117        dsHiveServer.HiveUserRow row =
118          data.FindByPermissionOwnerId(user.PermissionOwnerId);
119        if (row == null) {
120          row = data.NewHiveUserRow();
121          row.PermissionOwnerId = user.PermissionOwnerId;
122          data.AddHiveUserRow(row);
123        }
124
125        Convert(user, row);
126      }
127    }
128
129    public User GetUserById(long userId) {
130      User user = new User();
131
132      dsHiveServer.HiveUserRow row =
133          data.FindByPermissionOwnerId(userId);
134
135      if(row != null) {
136        Convert(row, user);
137
138        return user;
139      } else {
140        return null;
141      }
142    }
143
144    public User GetUserByName(String name) {
145      User user = new User();
146
147      PermissionOwner permOwner =
148        PermOwnerAdapter.GetPermissionOwnerByName(name);
149
150      if (permOwner != null) {
151        dsHiveServer.HiveUserRow row =
152          data.FindByPermissionOwnerId(permOwner.PermissionOwnerId);
153
154        if (row != null) {
155          Convert(row, user);
156
157          return user;
158        }
159      }
160
161      return null;
162    }
163
164    public ICollection<User> GetAllUsers() {
165      ICollection<User> allUsers =
166        new List<User>();
167
168      foreach (dsHiveServer.HiveUserRow row in data) {
169        User user = new User();
170        Convert(row, user);
171        allUsers.Add(user);
172      }
173
174      return allUsers;
175    }
176
177    [MethodImpl(MethodImplOptions.Synchronized)]
178    public bool DeleteUser(User user) {
179      if (user != null) {
180        dsHiveServer.HiveUserRow row =
181          data.FindByPermissionOwnerId(user.PermissionOwnerId);
182
183        if (row != null) {
184          //Referential integrity with user groups
185          ICollection<UserGroup> userGroups =
186            UserGroupAdapter.MemberOf(user);
187          foreach (UserGroup group in userGroups) {
188            group.Members.Remove(user);
189            UserGroupAdapter.UpdateUserGroup(group);
190          }
191
192          //Referential integrity with jobs
193          ICollection<Job> jobs =
194            JobAdapter.GetJobsOf(user);
195          foreach (Job job in jobs) {
196            JobAdapter.DeleteJob(job);
197          }
198
199          data.RemoveHiveUserRow(row);
200
201          return PermOwnerAdapter.DeletePermissionOwner(user);
202        }
203      }
204
205      return false;
206    }
207
208    #endregion
209  }
210}
Note: See TracBrowser for help on using the repository browser.