Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Security.ADODataAccess/3.2/UserGroupAdapter.cs @ 4498

Last change on this file since 4498 was 1759, checked in by svonolfe, 16 years ago

Small refactoring of the manyToManyRelationHelper 2 (#527)

File size: 4.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.DataAccess.ADOHelper;
6using HeuristicLab.Security.Contracts.BusinessObjects;
7using HeuristicLab.Security.DataAccess;
8using HeuristicLab.Security.ADODataAccess.TableAdapterWrapper;
9
10namespace HeuristicLab.Security.ADODataAccess {
11  class UserGroupAdapter: DataAdapterBase<
12      dsSecurityTableAdapters.UserGroupTableAdapter,
13      UserGroup,
14      dsSecurity.UserGroupRow>,
15      IUserGroupAdapter {
16    public UserGroupAdapter() :
17      base(new UserGroupAdapterWrapper()) {
18    }
19
20    private ManyToManyRelationHelper<
21      dsSecurityTableAdapters.PermissionOwner_UserGroupTableAdapter,
22      dsSecurity.PermissionOwner_UserGroupRow> manyToManyRelationHelper = null;
23
24    private ManyToManyRelationHelper<dsSecurityTableAdapters.PermissionOwner_UserGroupTableAdapter,
25      dsSecurity.PermissionOwner_UserGroupRow> ManyToManyRelationHelper {
26      get {
27        if (manyToManyRelationHelper == null) {
28          manyToManyRelationHelper =
29            new ManyToManyRelationHelper<dsSecurityTableAdapters.PermissionOwner_UserGroupTableAdapter,
30              dsSecurity.PermissionOwner_UserGroupRow>(new PermissionOwner_UserGroupAdapterWrapper(), 0);
31        }
32
33        manyToManyRelationHelper.Session = Session as Session;
34
35        return manyToManyRelationHelper;
36      }
37    }
38
39    private IPermissionOwnerAdapter permOwnerAdapter = null;
40
41    private IPermissionOwnerAdapter PermOwnerAdapter {
42      get {
43        if (permOwnerAdapter == null)
44          permOwnerAdapter =
45            this.Session.GetDataAdapter<PermissionOwner, IPermissionOwnerAdapter>();
46
47        return permOwnerAdapter;
48      }
49    }
50
51    protected override dsSecurity.UserGroupRow ConvertObj(UserGroup group,
52      dsSecurity.UserGroupRow row) {
53      if (group != null && row != null) {
54        row.PermissionOwnerId = group.Id;
55
56        return row;
57      } else {
58        return null;
59      }
60    }
61
62    protected override UserGroup ConvertRow(dsSecurity.UserGroupRow row,
63      UserGroup group) {
64      if (group != null && row != null) {
65        group.Id = row.PermissionOwnerId;
66        PermOwnerAdapter.GetById(group);
67
68        ICollection<Guid> permissionOwners =
69          ManyToManyRelationHelper.GetRelationships(group.Id);
70
71        group.Members.Clear();
72        foreach (Guid permissionOwner in permissionOwners) {
73          PermissionOwner permOwner =
74            PermOwnerAdapter.GetByIdPolymorphic(permissionOwner);
75
76          group.Members.Add(permOwner);
77        }
78
79        return group;
80      } else {
81        return null;
82      }
83    }
84
85    #region IUserGroupAdapter Members
86
87    protected override void doUpdate(UserGroup group) {
88      if (group != null) {
89        PermOwnerAdapter.Update(group);
90
91        base.doUpdate(group);
92
93        List<Guid> relationships =
94          new List<Guid>();
95        foreach (PermissionOwner permOwner in group.Members) {
96          PermOwnerAdapter.UpdatePolymorphic(permOwner);
97
98          relationships.Add(permOwner.Id);
99        }
100
101        ManyToManyRelationHelper.UpdateRelationships(group.Id,
102          relationships);
103      }
104    }
105
106    protected override bool doDelete(UserGroup group) {
107      if (group != null) {
108        //delete all relationships
109        ManyToManyRelationHelper.UpdateRelationships(group.Id,
110          new List<Guid>());
111
112        return base.doDelete(group) &&
113          PermOwnerAdapter.Delete(group);
114      }
115
116      return false;
117    }
118
119    public UserGroup GetByName(string name) {
120      UserGroup group = new UserGroup();
121      PermissionOwner permOwner =
122        PermOwnerAdapter.GetByName(name);
123
124      if (permOwner != null)
125        return GetById(permOwner.Id);
126      else
127        return null;
128    }
129
130    public ICollection<UserGroup> MemberOf(Guid permissionOwnerId) {
131      return base.FindMultiple(
132        delegate() {
133          return Adapter.GetDataByMemberOf(permissionOwnerId);
134        }
135      );
136    }
137
138    #endregion
139  }
140}
Note: See TracBrowser for help on using the repository browser.