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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 |
|
---|
27 | using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
|
---|
28 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
29 | using System.Runtime.CompilerServices;
|
---|
30 | using System.Data;
|
---|
31 |
|
---|
32 | namespace 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 | userGroup.Members.Add(permissionOwner);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | //secondly check for deleted references
|
---|
119 | ICollection<PermissionOwner> deleted =
|
---|
120 | new List<PermissionOwner>();
|
---|
121 |
|
---|
122 | foreach (PermissionOwner permOwner in userGroup.Members) {
|
---|
123 | dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow =
|
---|
124 | permOwnerUserGroupData.FindByPermissionOwnerIdUserGroupId(
|
---|
125 | permOwner.PermissionOwnerId,
|
---|
126 | userGroup.PermissionOwnerId);
|
---|
127 |
|
---|
128 | if (permOwnerUserGroupRow == null) {
|
---|
129 | deleted.Add(permOwner);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | foreach (PermissionOwner permOwner in deleted) {
|
---|
134 | userGroup.Members.Remove(permOwner);
|
---|
135 | }
|
---|
136 |
|
---|
137 | return userGroup;
|
---|
138 | } else
|
---|
139 | return null;
|
---|
140 | }
|
---|
141 |
|
---|
142 | private dsHiveServer.UserGroupRow Convert(UserGroup userGroup,
|
---|
143 | dsHiveServer.UserGroupRow row) {
|
---|
144 | if (userGroup != null && row != null) {
|
---|
145 | row.PermissionOwnerId = userGroup.PermissionOwnerId;
|
---|
146 |
|
---|
147 | //update references
|
---|
148 | foreach (PermissionOwner permOwner in userGroup.Members) {
|
---|
149 | //first update the member to make sure it exists in the DB
|
---|
150 | if (permOwner is User) {
|
---|
151 | UserAdapter.UpdateUser(permOwner as User);
|
---|
152 | } else if (permOwner is UserGroup) {
|
---|
153 | UpdateUserGroup(permOwner as UserGroup);
|
---|
154 | }
|
---|
155 |
|
---|
156 | //secondly check for created references
|
---|
157 | dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow =
|
---|
158 | permOwnerUserGroupData.FindByPermissionOwnerIdUserGroupId(
|
---|
159 | permOwner.PermissionOwnerId,
|
---|
160 | userGroup.PermissionOwnerId);
|
---|
161 |
|
---|
162 | if (permOwnerUserGroupRow == null) {
|
---|
163 | permOwnerUserGroupRow =
|
---|
164 | permOwnerUserGroupData.NewPermissionOwner_UserGroupRow();
|
---|
165 |
|
---|
166 | permOwnerUserGroupRow.PermissionOwnerId =
|
---|
167 | permOwner.PermissionOwnerId;
|
---|
168 | permOwnerUserGroupRow.UserGroupId =
|
---|
169 | userGroup.PermissionOwnerId;
|
---|
170 |
|
---|
171 | permOwnerUserGroupData.AddPermissionOwner_UserGroupRow(
|
---|
172 | permOwnerUserGroupRow);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | //thirdly check for deleted references
|
---|
177 | IEnumerable<dsHiveServer.PermissionOwner_UserGroupRow> userGroupRows =
|
---|
178 | from permOwner in
|
---|
179 | permOwnerUserGroupData.AsEnumerable<dsHiveServer.PermissionOwner_UserGroupRow>()
|
---|
180 | where permOwner.UserGroupId == userGroup.PermissionOwnerId
|
---|
181 | select permOwner;
|
---|
182 |
|
---|
183 | ICollection<dsHiveServer.PermissionOwner_UserGroupRow> deleted =
|
---|
184 | new List<dsHiveServer.PermissionOwner_UserGroupRow>();
|
---|
185 |
|
---|
186 | foreach (dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow in
|
---|
187 | userGroupRows) {
|
---|
188 | PermissionOwner permOwner = null;
|
---|
189 |
|
---|
190 | IEnumerable<PermissionOwner> permOwners =
|
---|
191 | from p in
|
---|
192 | userGroup.Members
|
---|
193 | where p.PermissionOwnerId == permOwnerUserGroupRow.PermissionOwnerId
|
---|
194 | select p;
|
---|
195 |
|
---|
196 | if (permOwners.Count<PermissionOwner>() == 1)
|
---|
197 | permOwner = permOwners.First<PermissionOwner>();
|
---|
198 |
|
---|
199 | if (permOwner == null) {
|
---|
200 | deleted.Add(permOwnerUserGroupRow);
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | foreach (dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow in
|
---|
205 | deleted) {
|
---|
206 | permOwnerUserGroupData.RemovePermissionOwner_UserGroupRow(
|
---|
207 | permOwnerUserGroupRow);
|
---|
208 | }
|
---|
209 |
|
---|
210 | }
|
---|
211 |
|
---|
212 | return row;
|
---|
213 | }
|
---|
214 |
|
---|
215 | #region IUserGroupAdapter Members
|
---|
216 | [MethodImpl(MethodImplOptions.Synchronized)]
|
---|
217 | public void UpdateUserGroup(UserGroup group) {
|
---|
218 | if (group != null) {
|
---|
219 | PermOwnerAdapter.UpdatePermissionOwner(group);
|
---|
220 |
|
---|
221 | dsHiveServer.UserGroupRow row =
|
---|
222 | data.FindByPermissionOwnerId(group.PermissionOwnerId);
|
---|
223 |
|
---|
224 | if (row == null) {
|
---|
225 | row = data.NewUserGroupRow();
|
---|
226 | row.PermissionOwnerId = group.PermissionOwnerId;
|
---|
227 | data.AddUserGroupRow(row);
|
---|
228 | }
|
---|
229 |
|
---|
230 | Convert(group, row);
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | public UserGroup GetUserGroupById(long userGroupId) {
|
---|
235 | UserGroup userGroup = new UserGroup();
|
---|
236 |
|
---|
237 | dsHiveServer.UserGroupRow row =
|
---|
238 | data.FindByPermissionOwnerId(userGroupId);
|
---|
239 |
|
---|
240 | if (row != null) {
|
---|
241 | Convert(row, userGroup);
|
---|
242 |
|
---|
243 | return userGroup;
|
---|
244 | } else {
|
---|
245 | return null;
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | public ICollection<UserGroup> GetAllUserGroups() {
|
---|
250 | ICollection<UserGroup> allUserGroups =
|
---|
251 | new List<UserGroup>();
|
---|
252 |
|
---|
253 | foreach (dsHiveServer.UserGroupRow row in data) {
|
---|
254 | UserGroup userGroup = new UserGroup();
|
---|
255 |
|
---|
256 | Convert(row, userGroup);
|
---|
257 | allUserGroups.Add(userGroup);
|
---|
258 | }
|
---|
259 |
|
---|
260 | return allUserGroups;
|
---|
261 | }
|
---|
262 |
|
---|
263 | public ICollection<UserGroup> MemberOf(PermissionOwner permOwner) {
|
---|
264 | ICollection<UserGroup> userGroups =
|
---|
265 | new List<UserGroup>();
|
---|
266 |
|
---|
267 | if (permOwner != null) {
|
---|
268 | IEnumerable<dsHiveServer.PermissionOwner_UserGroupRow> userGroupRows =
|
---|
269 | from userGroup in
|
---|
270 | permOwnerUserGroupData.AsEnumerable<dsHiveServer.PermissionOwner_UserGroupRow>()
|
---|
271 | where userGroup.PermissionOwnerId == permOwner.PermissionOwnerId
|
---|
272 | select userGroup;
|
---|
273 |
|
---|
274 | foreach (dsHiveServer.PermissionOwner_UserGroupRow userGroupRow in
|
---|
275 | userGroupRows) {
|
---|
276 | UserGroup userGroup =
|
---|
277 | GetUserGroupById(userGroupRow.UserGroupId);
|
---|
278 | userGroups.Add(userGroup);
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | return userGroups;
|
---|
283 | }
|
---|
284 |
|
---|
285 | [MethodImpl(MethodImplOptions.Synchronized)]
|
---|
286 | public bool DeleteUserGroup(UserGroup group) {
|
---|
287 | if (group != null) {
|
---|
288 | dsHiveServer.UserGroupRow row =
|
---|
289 | data.FindByPermissionOwnerId(group.PermissionOwnerId);
|
---|
290 |
|
---|
291 | if (row != null) {
|
---|
292 | ICollection<dsHiveServer.PermissionOwner_UserGroupRow> deleted =
|
---|
293 | new List<dsHiveServer.PermissionOwner_UserGroupRow>();
|
---|
294 |
|
---|
295 | foreach (dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow in
|
---|
296 | permOwnerUserGroupData) {
|
---|
297 | if (permOwnerUserGroupRow.UserGroupId == group.PermissionOwnerId ||
|
---|
298 | permOwnerUserGroupRow.PermissionOwnerId == group.PermissionOwnerId) {
|
---|
299 | deleted.Add(permOwnerUserGroupRow);
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | foreach (dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow in
|
---|
304 | deleted) {
|
---|
305 | permOwnerUserGroupData.RemovePermissionOwner_UserGroupRow(
|
---|
306 | permOwnerUserGroupRow);
|
---|
307 | }
|
---|
308 |
|
---|
309 | data.RemoveUserGroupRow(row);
|
---|
310 | return PermOwnerAdapter.DeletePermissionOwner(group);
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | return false;
|
---|
315 | }
|
---|
316 |
|
---|
317 | #endregion
|
---|
318 | }
|
---|
319 | }
|
---|