Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2115 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

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