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