Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1103 was 995, checked in by svonolfe, 16 years ago

Refactored DAL, Improved Caching (#372)

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