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

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

Added Job Adapter (#372)

File size: 11.3 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;
30using System.Data;
31
32namespace HeuristicLab.Hive.Server.ADODataAccess {
33  class UserGroupAdapter: DataAdapterBase, IUserGroupAdapter {
34    private dsHiveServerTableAdapters.UserGroupTableAdapter adapter =
35        new dsHiveServerTableAdapters.UserGroupTableAdapter();
36
37    private dsHiveServer.UserGroupDataTable data =
38      new dsHiveServer.UserGroupDataTable();
39
40    private dsHiveServerTableAdapters.PermissionOwner_UserGroupTableAdapter permOwnerUserGroupAdapter =
41      new dsHiveServerTableAdapters.PermissionOwner_UserGroupTableAdapter();
42
43    private dsHiveServer.PermissionOwner_UserGroupDataTable permOwnerUserGroupData =
44      new dsHiveServer.PermissionOwner_UserGroupDataTable();
45
46    private IPermissionOwnerAdapter permOwnerAdapter = null;
47
48    private IPermissionOwnerAdapter PermOwnerAdapter {
49      get {
50        if (permOwnerAdapter == null)
51          permOwnerAdapter = ServiceLocator.GetPermissionOwnerAdapter();
52
53        return permOwnerAdapter;
54      }
55    }
56
57    private IUserAdapter userAdapter = null;
58
59    private IUserAdapter UserAdapter {
60      get {
61        if (userAdapter == null)
62          userAdapter = ServiceLocator.GetUserAdapter();
63
64        return userAdapter;
65      }
66    }
67
68    public UserGroupAdapter() {
69      adapter.Fill(data);
70      permOwnerUserGroupAdapter.Fill(permOwnerUserGroupData);
71    }
72
73    protected override void Update() {
74      this.adapter.Update(this.data);
75      this.permOwnerUserGroupAdapter.Update(permOwnerUserGroupData);
76    }
77
78    private UserGroup Convert(dsHiveServer.UserGroupRow row,
79      UserGroup userGroup) {
80      if (row != null && userGroup != null) {
81        /*Parent - Permission Owner*/
82        userGroup.PermissionOwnerId = row.PermissionOwnerId;
83        PermOwnerAdapter.GetPermissionOwnerById(userGroup);
84
85        //first check for created references
86        IEnumerable<dsHiveServer.PermissionOwner_UserGroupRow> userGroupRows =
87          from permOwner in
88            permOwnerUserGroupData.AsEnumerable<dsHiveServer.PermissionOwner_UserGroupRow>()
89          where permOwner.UserGroupId == userGroup.PermissionOwnerId
90          select permOwner;
91
92        foreach (dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow in
93          userGroupRows) {
94          PermissionOwner permOwner = null;
95         
96          IEnumerable<PermissionOwner> permOwners =
97            from p in
98              userGroup.Members
99            where p.PermissionOwnerId == permOwnerUserGroupRow.PermissionOwnerId
100            select p;
101          if (permOwners.Count<PermissionOwner>() == 1)
102            permOwner = permOwners.First<PermissionOwner>();
103
104          if (permOwner == null) {
105            PermissionOwner permissionOwner =
106              UserAdapter.GetUserById(permOwnerUserGroupRow.PermissionOwnerId);
107
108            if (permissionOwner == null) {
109              //is a user group
110              permissionOwner =
111                GetUserGroupById(permOwnerUserGroupRow.PermissionOwnerId);
112            }
113
114            if(permissionOwner != null)
115              userGroup.Members.Add(permissionOwner);
116          }
117        }
118
119        //secondly check for deleted references
120        ICollection<PermissionOwner> deleted =
121          new List<PermissionOwner>();
122
123        foreach (PermissionOwner permOwner in userGroup.Members) {
124          dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow =
125            permOwnerUserGroupData.FindByPermissionOwnerIdUserGroupId(
126              permOwner.PermissionOwnerId,
127              userGroup.PermissionOwnerId);
128
129          if (permOwnerUserGroupRow == null) {
130            deleted.Add(permOwner);
131          }
132        }
133
134        foreach (PermissionOwner permOwner in deleted) {
135          userGroup.Members.Remove(permOwner);
136        }
137
138        return userGroup;
139      } else
140        return null;
141    }
142
143    private dsHiveServer.UserGroupRow Convert(UserGroup userGroup,
144      dsHiveServer.UserGroupRow row) {
145      if (userGroup != null && row != null) {
146        row.PermissionOwnerId = userGroup.PermissionOwnerId;
147     
148        //update references
149        foreach (PermissionOwner permOwner in userGroup.Members) {         
150          //first update the member to make sure it exists in the DB
151          if (permOwner is User) {
152            UserAdapter.UpdateUser(permOwner as User);
153          } else if (permOwner is UserGroup) {
154            UpdateUserGroup(permOwner as UserGroup);
155          }
156
157          //secondly check for created references
158          dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow =
159            permOwnerUserGroupData.FindByPermissionOwnerIdUserGroupId(
160              permOwner.PermissionOwnerId,
161              userGroup.PermissionOwnerId);
162
163          if (permOwnerUserGroupRow == null) {
164            permOwnerUserGroupRow =
165              permOwnerUserGroupData.NewPermissionOwner_UserGroupRow();
166
167            permOwnerUserGroupRow.PermissionOwnerId =
168              permOwner.PermissionOwnerId;
169            permOwnerUserGroupRow.UserGroupId =
170              userGroup.PermissionOwnerId;
171
172            permOwnerUserGroupData.AddPermissionOwner_UserGroupRow(
173              permOwnerUserGroupRow);
174          }
175        }
176
177        //thirdly check for deleted references
178        IEnumerable<dsHiveServer.PermissionOwner_UserGroupRow> userGroupRows =
179          from permOwner in
180            permOwnerUserGroupData.AsEnumerable<dsHiveServer.PermissionOwner_UserGroupRow>()
181          where permOwner.UserGroupId == userGroup.PermissionOwnerId 
182          select permOwner;
183
184        ICollection<dsHiveServer.PermissionOwner_UserGroupRow> deleted =
185          new List<dsHiveServer.PermissionOwner_UserGroupRow>();
186
187        foreach (dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow in
188          userGroupRows) {
189          PermissionOwner permOwner = null;
190
191          IEnumerable<PermissionOwner> permOwners =
192            from p in
193              userGroup.Members
194            where p.PermissionOwnerId == permOwnerUserGroupRow.PermissionOwnerId
195            select p;
196
197          if (permOwners.Count<PermissionOwner>() == 1)
198            permOwner = permOwners.First<PermissionOwner>();
199
200          if (permOwner == null) {
201            deleted.Add(permOwnerUserGroupRow);
202          }
203        }
204
205        foreach (dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow in
206          deleted) {
207          permOwnerUserGroupData.RemovePermissionOwner_UserGroupRow(
208            permOwnerUserGroupRow);
209        }
210
211      }
212
213      return row;
214    }
215
216    #region IUserGroupAdapter Members
217    [MethodImpl(MethodImplOptions.Synchronized)]
218    public void UpdateUserGroup(UserGroup group) {
219      if (group != null) {
220        PermOwnerAdapter.UpdatePermissionOwner(group);
221
222        dsHiveServer.UserGroupRow row =
223          data.FindByPermissionOwnerId(group.PermissionOwnerId);
224
225        if (row == null) {
226          row = data.NewUserGroupRow();
227          row.PermissionOwnerId = group.PermissionOwnerId;
228          data.AddUserGroupRow(row);
229        }
230
231        Convert(group, row);
232      }
233    }
234
235    public UserGroup GetUserGroupById(long userGroupId) {
236      UserGroup userGroup = new UserGroup();
237
238      dsHiveServer.UserGroupRow row =
239        data.FindByPermissionOwnerId(userGroupId);
240
241      if (row != null) {
242        Convert(row, userGroup);
243
244        return userGroup;
245      } else {
246        return null;
247      }
248    }
249
250    public UserGroup GetUserGroupByName(string name) {
251      UserGroup group = new UserGroup();
252
253      PermissionOwner permOwner =
254        PermOwnerAdapter.GetPermissionOwnerByName(name);
255
256      if (permOwner != null) {
257        dsHiveServer.UserGroupRow row =
258          data.FindByPermissionOwnerId(permOwner.PermissionOwnerId);
259
260        if (row != null) {
261          Convert(row, group);
262
263          return group;
264        }
265      }
266
267      return null;
268    }
269
270    public ICollection<UserGroup> GetAllUserGroups() {
271      ICollection<UserGroup> allUserGroups =
272        new List<UserGroup>();
273
274      foreach (dsHiveServer.UserGroupRow row in data) {
275        UserGroup userGroup = new UserGroup();
276
277        Convert(row, userGroup);
278        allUserGroups.Add(userGroup);
279      }
280
281      return allUserGroups;
282    }
283
284    public ICollection<UserGroup> MemberOf(PermissionOwner permOwner) {
285      ICollection<UserGroup> userGroups =
286        new List<UserGroup>();
287     
288      if (permOwner != null) {
289        IEnumerable<dsHiveServer.PermissionOwner_UserGroupRow> userGroupRows =
290         from userGroup in
291           permOwnerUserGroupData.AsEnumerable<dsHiveServer.PermissionOwner_UserGroupRow>()
292         where userGroup.PermissionOwnerId == permOwner.PermissionOwnerId
293         select userGroup;
294
295        foreach (dsHiveServer.PermissionOwner_UserGroupRow userGroupRow in
296          userGroupRows) {
297          UserGroup userGroup =
298            GetUserGroupById(userGroupRow.UserGroupId);
299          userGroups.Add(userGroup);
300        }
301      }
302
303      return userGroups;
304    }
305
306    [MethodImpl(MethodImplOptions.Synchronized)]
307    public bool DeleteUserGroup(UserGroup group) {
308      if (group != null) {
309        dsHiveServer.UserGroupRow row =
310          data.FindByPermissionOwnerId(group.PermissionOwnerId);
311
312        if (row != null) {
313          ICollection<dsHiveServer.PermissionOwner_UserGroupRow> deleted =
314            new List<dsHiveServer.PermissionOwner_UserGroupRow>();
315         
316          foreach (dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow in
317            permOwnerUserGroupData) {
318            if (permOwnerUserGroupRow.UserGroupId == group.PermissionOwnerId ||
319              permOwnerUserGroupRow.PermissionOwnerId == group.PermissionOwnerId) {
320              deleted.Add(permOwnerUserGroupRow);
321            }
322          }
323
324          foreach (dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow in
325            deleted) {
326            permOwnerUserGroupData.RemovePermissionOwner_UserGroupRow(
327              permOwnerUserGroupRow);
328          }
329         
330          data.RemoveUserGroupRow(row);
331          return PermOwnerAdapter.DeletePermissionOwner(group);
332        }
333      }
334
335      return false;
336    }
337
338    #endregion
339  }
340}
Note: See TracBrowser for help on using the repository browser.