Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1209 was 1175, checked in by svonolfe, 16 years ago

Refactored DAL, avoided possible race conditions (#372)

File size: 8.8 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.Data;
30
31namespace HeuristicLab.Hive.Server.ADODataAccess {
32  class UserGroupAdapter:
33    DataAdapterBase<
34      dsHiveServerTableAdapters.UserGroupTableAdapter,
35      UserGroup,
36      dsHiveServer.UserGroupRow>,
37    IUserGroupAdapter {
38
39    #region Fields
40    private dsHiveServerTableAdapters.PermissionOwner_UserGroupTableAdapter permOwnerUserGroupAdapter =
41      new dsHiveServerTableAdapters.PermissionOwner_UserGroupTableAdapter();
42
43    private IPermissionOwnerAdapter permOwnerAdapter = null;
44
45    private IPermissionOwnerAdapter PermOwnerAdapter {
46      get {
47        if (permOwnerAdapter == null)
48          permOwnerAdapter = ServiceLocator.GetPermissionOwnerAdapter();
49
50        return permOwnerAdapter;
51      }
52    }
53
54    private IUserAdapter userAdapter = null;
55
56    private IUserAdapter UserAdapter {
57      get {
58        if (userAdapter == null)
59          userAdapter = ServiceLocator.GetUserAdapter();
60
61        return userAdapter;
62      }
63    }
64    #endregion
65
66    #region Overrides
67    protected override UserGroup ConvertRow(dsHiveServer.UserGroupRow row,
68      UserGroup userGroup) {
69      if (row != null && userGroup != null) {
70        /*Parent - Permission Owner*/
71        userGroup.Id = row.PermissionOwnerId;
72        PermOwnerAdapter.GetById(userGroup);
73
74        //first check for created references
75        dsHiveServer.PermissionOwner_UserGroupDataTable userGroupRows =
76          permOwnerUserGroupAdapter.GetDataByUserGroupId(userGroup.Id);
77
78        foreach (dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow in
79          userGroupRows) {
80          PermissionOwner permOwner = null;
81         
82          IEnumerable<PermissionOwner> permOwners =
83            from p in
84              userGroup.Members
85            where p.Id == permOwnerUserGroupRow.PermissionOwnerId
86            select p;
87          if (permOwners.Count<PermissionOwner>() == 1)
88            permOwner = permOwners.First<PermissionOwner>();
89
90          if (permOwner == null) {
91            PermissionOwner permissionOwner =
92              UserAdapter.GetById(permOwnerUserGroupRow.PermissionOwnerId);
93
94            if (permissionOwner == null) {
95              //is a user group
96              permissionOwner =
97                GetById(permOwnerUserGroupRow.PermissionOwnerId);
98            }
99
100            if(permissionOwner != null)
101              userGroup.Members.Add(permissionOwner);
102          }
103        }
104
105        //secondly check for deleted references
106        ICollection<PermissionOwner> deleted =
107          new List<PermissionOwner>();
108
109        foreach (PermissionOwner permOwner in userGroup.Members) {
110          dsHiveServer.PermissionOwner_UserGroupDataTable found =
111            permOwnerUserGroupAdapter.GetDataByPermownerUsergroupId(
112              permOwner.Id,
113              userGroup.Id);
114          if (found.Count != 1) {
115            deleted.Add(permOwner);
116          }
117        }
118
119        foreach (PermissionOwner permOwner in deleted) {
120          userGroup.Members.Remove(permOwner);
121        }
122
123        return userGroup;
124      } else
125        return null;
126    }
127
128    protected override dsHiveServer.UserGroupRow ConvertObj(UserGroup userGroup,
129      dsHiveServer.UserGroupRow row) {
130      if (userGroup != null && row != null) {
131        row.PermissionOwnerId = userGroup.Id;
132     
133        //update references
134        foreach (PermissionOwner permOwner in userGroup.Members) {         
135          //first update the member to make sure it exists in the DB
136          if (permOwner is User) {
137            UserAdapter.Update(permOwner as User);
138          } else if (permOwner is UserGroup) {
139            Update(permOwner as UserGroup);
140          }
141
142          //secondly check for created references
143          dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow =
144            null;
145          dsHiveServer.PermissionOwner_UserGroupDataTable found =
146            permOwnerUserGroupAdapter.GetDataByPermownerUsergroupId(
147              permOwner.Id,
148              userGroup.Id);
149          if (found.Count == 1)
150            permOwnerUserGroupRow = found[0];
151
152          if (permOwnerUserGroupRow == null) {
153            permOwnerUserGroupRow =
154              found.NewPermissionOwner_UserGroupRow();
155
156            permOwnerUserGroupRow.PermissionOwnerId =
157              permOwner.Id;
158            permOwnerUserGroupRow.UserGroupId =
159              userGroup.Id;
160
161            found.AddPermissionOwner_UserGroupRow(permOwnerUserGroupRow);
162
163            permOwnerUserGroupAdapter.Update(permOwnerUserGroupRow);
164          }
165        }
166
167        //thirdly check for deleted references
168        dsHiveServer.PermissionOwner_UserGroupDataTable userGroupRows =
169            permOwnerUserGroupAdapter.GetDataByUserGroupId(userGroup.Id);
170
171        ICollection<dsHiveServer.PermissionOwner_UserGroupRow> deleted =
172          new List<dsHiveServer.PermissionOwner_UserGroupRow>();
173
174        foreach (dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow in
175          userGroupRows) {
176          PermissionOwner permOwner = null;
177
178          IEnumerable<PermissionOwner> permOwners =
179            from p in
180              userGroup.Members
181            where p.Id == permOwnerUserGroupRow.PermissionOwnerId
182            select p;
183
184          if (permOwners.Count<PermissionOwner>() == 1)
185            permOwner = permOwners.First<PermissionOwner>();
186
187          if (permOwner == null) {
188            deleted.Add(permOwnerUserGroupRow);
189          }
190        }
191
192        foreach (dsHiveServer.PermissionOwner_UserGroupRow
193          permOwnerUserGroupRow in deleted) {
194          permOwnerUserGroupRow.Delete();
195          permOwnerUserGroupAdapter.Update(permOwnerUserGroupRow);
196        }
197      }
198
199      return row;
200    }
201
202    protected override dsHiveServer.UserGroupRow
203      InsertNewRow(UserGroup group) {
204      dsHiveServer.UserGroupDataTable data =
205        new dsHiveServer.UserGroupDataTable();
206
207      dsHiveServer.UserGroupRow row =
208        data.NewUserGroupRow();
209
210      row.PermissionOwnerId = group.Id;
211
212      data.AddUserGroupRow(row);
213      Adapter.Update(row);
214
215      return row;
216    }
217
218    protected override void
219     UpdateRow(dsHiveServer.UserGroupRow row) {
220      Adapter.Update(row);
221    }
222
223    protected override IEnumerable<dsHiveServer.UserGroupRow>
224      FindById(long id) {
225      return Adapter.GetDataById(id);
226    }
227
228    protected override IEnumerable<dsHiveServer.UserGroupRow>
229      FindAll() {
230      return Adapter.GetData();
231    }
232    #endregion
233
234    #region IUserGroupAdapter Members
235    public override void Update(UserGroup group) {
236      if (group != null) {
237        PermOwnerAdapter.Update(group);
238
239        base.Update(group);
240      }
241    }
242
243    public UserGroup GetByName(String name) {
244      if (name != null) {
245        return base.FindSingle(
246          delegate() {
247            return Adapter.GetDataByName(name);
248          });
249      }
250
251      return null;
252    }
253
254    public ICollection<UserGroup> MemberOf(PermissionOwner permOwner) {
255      ICollection<UserGroup> userGroups =
256        new List<UserGroup>();
257     
258      if (permOwner != null) {
259        dsHiveServer.PermissionOwner_UserGroupDataTable userGroupRows =
260          permOwnerUserGroupAdapter.GetDataByPermissionOwnerId(permOwner.Id);
261
262        foreach (dsHiveServer.PermissionOwner_UserGroupRow userGroupRow in
263          userGroupRows) {
264          UserGroup userGroup =
265            GetById(userGroupRow.UserGroupId);
266          userGroups.Add(userGroup);
267        }
268      }
269
270      return userGroups;
271    }
272
273    public override bool Delete(UserGroup group) {
274      if (group != null) {
275        return base.Delete(group) &&
276          PermOwnerAdapter.Delete(group);
277      }
278
279      return false;
280    }
281
282    #endregion
283  }
284}
Note: See TracBrowser for help on using the repository browser.