[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 |
|
---|
[1377] | 27 | using HeuristicLab.Hive.Server.DataAccess;
|
---|
[910] | 28 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
[965] | 29 | using System.Data;
|
---|
[1377] | 30 | using HeuristicLab.DataAccess.ADOHelper;
|
---|
[1468] | 31 | using HeuristicLab.Hive.Server.ADODataAccess.dsHiveServerTableAdapters;
|
---|
| 32 | using System.Data.Common;
|
---|
| 33 | using System.Data.SqlClient;
|
---|
[910] | 34 |
|
---|
| 35 | namespace HeuristicLab.Hive.Server.ADODataAccess {
|
---|
[1468] | 36 | class ClientGroupAdapterWrapper :
|
---|
| 37 | DataAdapterWrapperBase<dsHiveServerTableAdapters.ClientGroupTableAdapter,
|
---|
| 38 | ClientGroup,
|
---|
| 39 | dsHiveServer.ClientGroupRow> {
|
---|
| 40 | public override dsHiveServer.ClientGroupRow
|
---|
| 41 | InsertNewRow(ClientGroup group) {
|
---|
| 42 | dsHiveServer.ClientGroupDataTable data =
|
---|
| 43 | new dsHiveServer.ClientGroupDataTable();
|
---|
| 44 |
|
---|
| 45 | dsHiveServer.ClientGroupRow row =
|
---|
| 46 | data.NewClientGroupRow();
|
---|
| 47 |
|
---|
| 48 | row.ResourceId = group.Id;
|
---|
| 49 |
|
---|
| 50 | data.AddClientGroupRow(row);
|
---|
| 51 | TransactionalAdapter.Update(row);
|
---|
| 52 |
|
---|
| 53 | return row;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public override void
|
---|
| 57 | UpdateRow(dsHiveServer.ClientGroupRow row) {
|
---|
| 58 | TransactionalAdapter.Update(row);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public override IEnumerable<dsHiveServer.ClientGroupRow>
|
---|
| 62 | FindById(Guid id) {
|
---|
| 63 | return TransactionalAdapter.GetDataById(id);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | public override IEnumerable<dsHiveServer.ClientGroupRow>
|
---|
| 67 | FindAll() {
|
---|
| 68 | return TransactionalAdapter.GetData();
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | protected override void SetConnection(DbConnection connection) {
|
---|
| 72 | adapter.Connection = connection as SqlConnection;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | protected override void SetTransaction(DbTransaction transaction) {
|
---|
| 76 | adapter.Transaction = transaction as SqlTransaction;
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[995] | 80 | class ClientGroupAdapter :
|
---|
| 81 | DataAdapterBase<dsHiveServerTableAdapters.ClientGroupTableAdapter,
|
---|
| 82 | ClientGroup,
|
---|
| 83 | dsHiveServer.ClientGroupRow>,
|
---|
| 84 | IClientGroupAdapter {
|
---|
| 85 | #region Fields
|
---|
[965] | 86 | private dsHiveServerTableAdapters.ClientGroup_ResourceTableAdapter resourceClientGroupAdapter =
|
---|
| 87 | new dsHiveServerTableAdapters.ClientGroup_ResourceTableAdapter();
|
---|
| 88 |
|
---|
| 89 | private IResourceAdapter resourceAdapter = null;
|
---|
| 90 |
|
---|
[971] | 91 | private IResourceAdapter ResAdapter {
|
---|
[965] | 92 | get {
|
---|
| 93 | if (resourceAdapter == null)
|
---|
[1468] | 94 | resourceAdapter =
|
---|
| 95 | this.Session.GetDataAdapter<Resource, IResourceAdapter>();
|
---|
[965] | 96 |
|
---|
| 97 | return resourceAdapter;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | private IClientAdapter clientAdapter = null;
|
---|
| 102 |
|
---|
| 103 | private IClientAdapter ClientAdapter {
|
---|
| 104 | get {
|
---|
| 105 | if (clientAdapter == null)
|
---|
[1468] | 106 | clientAdapter =
|
---|
| 107 | this.Session.GetDataAdapter<ClientInfo, IClientAdapter>();
|
---|
[965] | 108 |
|
---|
| 109 | return clientAdapter;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
[995] | 112 | #endregion
|
---|
[965] | 113 |
|
---|
[1468] | 114 | public ClientGroupAdapter():
|
---|
| 115 | base(new ClientGroupAdapterWrapper()) {
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[995] | 118 | #region Overrides
|
---|
[1131] | 119 | protected override ClientGroup ConvertRow(dsHiveServer.ClientGroupRow row,
|
---|
[965] | 120 | ClientGroup clientGroup) {
|
---|
| 121 | if (row != null && clientGroup != null) {
|
---|
| 122 | /*Parent - Permission Owner*/
|
---|
[995] | 123 | clientGroup.Id = row.ResourceId;
|
---|
| 124 | ResAdapter.GetById(clientGroup);
|
---|
[965] | 125 |
|
---|
| 126 | //first check for created references
|
---|
[995] | 127 | dsHiveServer.ClientGroup_ResourceDataTable clientGroupRows =
|
---|
| 128 | resourceClientGroupAdapter.GetDataByClientGroupId(clientGroup.Id);
|
---|
[965] | 129 |
|
---|
| 130 | foreach (dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow in
|
---|
| 131 | clientGroupRows) {
|
---|
| 132 | Resource resource = null;
|
---|
| 133 |
|
---|
| 134 | IEnumerable<Resource> resources =
|
---|
| 135 | from p in
|
---|
| 136 | clientGroup.Resources
|
---|
[995] | 137 | where p.Id == resourceClientGroupRow.ResourceId
|
---|
[965] | 138 | select p;
|
---|
| 139 | if (resources.Count<Resource>() == 1)
|
---|
| 140 | resource = resources.First<Resource>();
|
---|
| 141 |
|
---|
| 142 | if (resource == null) {
|
---|
| 143 | Resource res =
|
---|
[995] | 144 | ClientAdapter.GetById(resourceClientGroupRow.ResourceId);
|
---|
[965] | 145 |
|
---|
| 146 | if (res == null) {
|
---|
| 147 | //is a client group
|
---|
| 148 | res =
|
---|
[995] | 149 | GetById(resourceClientGroupRow.ResourceId);
|
---|
[965] | 150 | }
|
---|
| 151 |
|
---|
| 152 | if (res != null)
|
---|
| 153 | clientGroup.Resources.Add(res);
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | //secondly check for deleted references
|
---|
| 158 | ICollection<Resource> deleted =
|
---|
| 159 | new List<Resource>();
|
---|
| 160 |
|
---|
| 161 | foreach (Resource resource in clientGroup.Resources) {
|
---|
[995] | 162 | dsHiveServer.ClientGroup_ResourceDataTable found =
|
---|
| 163 | resourceClientGroupAdapter.GetDataByClientGroupResourceId(
|
---|
| 164 | clientGroup.Id,
|
---|
| 165 | resource.Id);
|
---|
[965] | 166 |
|
---|
[995] | 167 | if (found.Count != 1) {
|
---|
[965] | 168 | deleted.Add(resource);
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | foreach (Resource resource in deleted) {
|
---|
| 173 | clientGroup.Resources.Remove(resource);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | return clientGroup;
|
---|
| 177 | } else
|
---|
| 178 | return null;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[1131] | 181 | protected override dsHiveServer.ClientGroupRow ConvertObj(ClientGroup clientGroup,
|
---|
[965] | 182 | dsHiveServer.ClientGroupRow row) {
|
---|
| 183 | if (clientGroup != null && row != null) {
|
---|
[995] | 184 | row.ResourceId = clientGroup.Id;
|
---|
[965] | 185 |
|
---|
| 186 | //update references
|
---|
| 187 | foreach (Resource resource in clientGroup.Resources) {
|
---|
| 188 | //first update the member to make sure it exists in the DB
|
---|
| 189 | if (resource is ClientInfo) {
|
---|
[995] | 190 | ClientAdapter.Update(resource as ClientInfo);
|
---|
[965] | 191 | } else if (resource is ClientGroup) {
|
---|
[995] | 192 | Update(resource as ClientGroup);
|
---|
[965] | 193 | }
|
---|
| 194 |
|
---|
| 195 | //secondly check for created references
|
---|
| 196 | dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow =
|
---|
[995] | 197 | null;
|
---|
| 198 | dsHiveServer.ClientGroup_ResourceDataTable found =
|
---|
| 199 | resourceClientGroupAdapter.GetDataByClientGroupResourceId(
|
---|
| 200 | clientGroup.Id,
|
---|
| 201 | resource.Id);
|
---|
| 202 | if (found.Count == 1)
|
---|
| 203 | resourceClientGroupRow = found[0];
|
---|
[965] | 204 |
|
---|
| 205 | if (resourceClientGroupRow == null) {
|
---|
| 206 | resourceClientGroupRow =
|
---|
[995] | 207 | found.NewClientGroup_ResourceRow();
|
---|
[965] | 208 |
|
---|
| 209 | resourceClientGroupRow.ResourceId =
|
---|
[995] | 210 | resource.Id;
|
---|
[991] | 211 | resourceClientGroupRow.ClientGroupId =
|
---|
[995] | 212 | clientGroup.Id;
|
---|
[965] | 213 |
|
---|
[995] | 214 | found.AddClientGroup_ResourceRow(resourceClientGroupRow);
|
---|
| 215 |
|
---|
| 216 | resourceClientGroupAdapter.Update(
|
---|
[965] | 217 | resourceClientGroupRow);
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | //thirdly check for deleted references
|
---|
[995] | 222 | dsHiveServer.ClientGroup_ResourceDataTable clientGroupRows =
|
---|
| 223 | resourceClientGroupAdapter.GetDataByClientGroupId(clientGroup.Id);
|
---|
[965] | 224 |
|
---|
| 225 | ICollection<dsHiveServer.ClientGroup_ResourceRow> deleted =
|
---|
| 226 | new List<dsHiveServer.ClientGroup_ResourceRow>();
|
---|
| 227 |
|
---|
| 228 | foreach (dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow in
|
---|
| 229 | clientGroupRows) {
|
---|
| 230 | Resource resource = null;
|
---|
| 231 |
|
---|
| 232 | IEnumerable<Resource> resources =
|
---|
| 233 | from r in
|
---|
| 234 | clientGroup.Resources
|
---|
[995] | 235 | where r.Id == resourceClientGroupRow.ResourceId
|
---|
[965] | 236 | select r;
|
---|
| 237 |
|
---|
| 238 | if (resources.Count<Resource>() == 1)
|
---|
| 239 | resource = resources.First<Resource>();
|
---|
| 240 |
|
---|
| 241 | if (resource == null) {
|
---|
| 242 | deleted.Add(resourceClientGroupRow);
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[995] | 246 | foreach (dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow in deleted) {
|
---|
| 247 | resourceClientGroupRow.Delete();
|
---|
| 248 | resourceClientGroupAdapter.Update(resourceClientGroupRow);
|
---|
[965] | 249 | }
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | return row;
|
---|
| 253 | }
|
---|
[995] | 254 | #endregion
|
---|
[965] | 255 |
|
---|
[995] | 256 | #region IClientGroupAdapter Members
|
---|
[1468] | 257 | protected override void doUpdate(ClientGroup group) {
|
---|
[995] | 258 | if (group != null) {
|
---|
| 259 | ResAdapter.Update(group);
|
---|
| 260 |
|
---|
[1468] | 261 | base.doUpdate(group);
|
---|
[965] | 262 | }
|
---|
[910] | 263 | }
|
---|
| 264 |
|
---|
[995] | 265 | public ClientGroup GetByName(string name) {
|
---|
[971] | 266 | ClientGroup group = new ClientGroup();
|
---|
| 267 | Resource res =
|
---|
[995] | 268 | ResAdapter.GetByName(name);
|
---|
[971] | 269 |
|
---|
| 270 | if (res != null) {
|
---|
[995] | 271 | return GetById(res.Id);
|
---|
[971] | 272 | }
|
---|
| 273 |
|
---|
| 274 | return null;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[965] | 277 | public ICollection<ClientGroup> MemberOf(Resource resource) {
|
---|
| 278 | ICollection<ClientGroup> clientGroups =
|
---|
| 279 | new List<ClientGroup>();
|
---|
| 280 |
|
---|
| 281 | if (resource != null) {
|
---|
| 282 | IEnumerable<dsHiveServer.ClientGroup_ResourceRow> clientGroupRows =
|
---|
[995] | 283 | resourceClientGroupAdapter.GetDataByResourceId(resource.Id);
|
---|
[965] | 284 |
|
---|
| 285 | foreach (dsHiveServer.ClientGroup_ResourceRow clientGroupRow in
|
---|
| 286 | clientGroupRows) {
|
---|
| 287 | ClientGroup clientGroup =
|
---|
[995] | 288 | GetById(clientGroupRow.ClientGroupId);
|
---|
[965] | 289 | clientGroups.Add(clientGroup);
|
---|
| 290 | }
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | return clientGroups;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[1468] | 296 | protected override bool doDelete(ClientGroup group) {
|
---|
[965] | 297 | if (group != null) {
|
---|
[1468] | 298 | return base.doDelete(group) &&
|
---|
[995] | 299 | ResAdapter.Delete(group);
|
---|
[965] | 300 | }
|
---|
| 301 |
|
---|
| 302 | return false;
|
---|
[910] | 303 | }
|
---|
| 304 |
|
---|
| 305 | #endregion
|
---|
| 306 | }
|
---|
| 307 | }
|
---|