Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/ClientGroupAdapter.cs @ 1529

Last change on this file since 1529 was 1515, checked in by svonolfe, 15 years ago

Improved handling of binary relations (#527)

File size: 8.2 KB
Line 
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.DataAccess;
28using HeuristicLab.Hive.Contracts.BusinessObjects;
29using System.Data;
30using HeuristicLab.DataAccess.ADOHelper;
31using HeuristicLab.Hive.Server.ADODataAccess.dsHiveServerTableAdapters;
32using System.Data.Common;
33using System.Data.SqlClient;
34
35namespace HeuristicLab.Hive.Server.ADODataAccess {
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
80  class ClientGroup_ResourceAdapterWrapper :
81  DataAdapterWrapperBase<dsHiveServerTableAdapters.ClientGroup_ResourceTableAdapter,
82  BinaryRelation,
83  dsHiveServer.ClientGroup_ResourceRow> {
84    public override dsHiveServer.ClientGroup_ResourceRow
85     InsertNewRow(BinaryRelation relation) {
86      dsHiveServer.ClientGroup_ResourceDataTable data =
87         new dsHiveServer.ClientGroup_ResourceDataTable();
88
89      dsHiveServer.ClientGroup_ResourceRow row =
90        data.NewClientGroup_ResourceRow();
91
92      row.ClientGroupId = relation.Id;
93      row.ResourceId = relation.Id2;
94
95      data.AddClientGroup_ResourceRow(row);
96      TransactionalAdapter.Update(row);
97
98      return row;
99    }
100
101    public override void
102      UpdateRow(dsHiveServer.ClientGroup_ResourceRow row) {
103      TransactionalAdapter.Update(row);
104    }
105
106    public override IEnumerable<dsHiveServer.ClientGroup_ResourceRow>
107      FindById(Guid id) {
108      return TransactionalAdapter.GetDataByClientGroupId(id);
109    }
110
111    public override IEnumerable<dsHiveServer.ClientGroup_ResourceRow>
112      FindAll() {
113      return TransactionalAdapter.GetData();
114    }
115
116    protected override void SetConnection(DbConnection connection) {
117      adapter.Connection = connection as SqlConnection;
118    }
119
120    protected override void SetTransaction(DbTransaction transaction) {
121      adapter.Transaction = transaction as SqlTransaction;
122    }
123  }
124 
125  class ClientGroupAdapter :
126    DataAdapterBase<dsHiveServerTableAdapters.ClientGroupTableAdapter,
127    ClientGroup,
128    dsHiveServer.ClientGroupRow>,
129    IClientGroupAdapter {
130    #region Fields
131    private BinaryRelationHelper<
132      dsHiveServerTableAdapters.ClientGroup_ResourceTableAdapter,
133      dsHiveServer.ClientGroup_ResourceRow> binaryRelationHelper = null;
134
135    private BinaryRelationHelper<dsHiveServerTableAdapters.ClientGroup_ResourceTableAdapter,
136      dsHiveServer.ClientGroup_ResourceRow> BinaryRelationHelper {
137      get {
138        if (binaryRelationHelper == null) {
139          binaryRelationHelper =
140            new BinaryRelationHelper<dsHiveServerTableAdapters.ClientGroup_ResourceTableAdapter,
141              dsHiveServer.ClientGroup_ResourceRow>(new ClientGroup_ResourceAdapterWrapper());
142        }
143
144        binaryRelationHelper.Session = Session as Session;
145
146        return binaryRelationHelper;
147      }
148    }
149
150    private IResourceAdapter resourceAdapter = null;
151
152    private IResourceAdapter ResAdapter {
153      get {
154        if (resourceAdapter == null)
155          resourceAdapter =
156            this.Session.GetDataAdapter<Resource, IResourceAdapter>();
157
158        return resourceAdapter;
159      }
160    }
161
162    private IClientAdapter clientAdapter = null;
163
164    private IClientAdapter ClientAdapter {
165      get {
166        if (clientAdapter == null)
167          clientAdapter =
168            this.Session.GetDataAdapter<ClientInfo, IClientAdapter>();
169
170        return clientAdapter;
171      }
172    }
173    #endregion
174
175    public ClientGroupAdapter():
176      base(new ClientGroupAdapterWrapper()) {
177    }
178
179    #region Overrides
180    protected override ClientGroup ConvertRow(dsHiveServer.ClientGroupRow row,
181      ClientGroup clientGroup) {
182      if (row != null && clientGroup != null) {
183        /*Parent - Permission Owner*/
184        clientGroup.Id = row.ResourceId;
185        ResAdapter.GetById(clientGroup.Id);
186
187        ICollection<Guid> resources =
188          BinaryRelationHelper.GetRelationships(clientGroup.Id);
189
190       clientGroup.Resources.Clear();
191        foreach(Guid resource in resources) {
192          ClientInfo client =
193            ClientAdapter.GetById(resource);
194
195          if (client == null) {
196            //client group
197            ClientGroup group =
198              GetById(resource);
199
200            clientGroup.Resources.Add(group);
201          } else {
202            clientGroup.Resources.Add(client);
203          }         
204        }
205
206        return clientGroup;
207      } else
208        return null;
209    }
210
211    protected override dsHiveServer.ClientGroupRow ConvertObj(ClientGroup clientGroup,
212      dsHiveServer.ClientGroupRow row) {
213      if (clientGroup != null && row != null) {
214        row.ResourceId = clientGroup.Id;
215      }
216
217      return row;
218    }
219    #endregion
220
221    #region IClientGroupAdapter Members
222    protected override void doUpdate(ClientGroup group) {
223      if (group != null) {
224        ResAdapter.Update(group);
225
226        base.doUpdate(group);
227
228        List<Guid> relationships =
229          new List<Guid>();
230        foreach(Resource res in group.Resources) {
231          if (res is ClientInfo) {
232            ClientAdapter.Update(res as ClientInfo);
233          } else if (res is ClientGroup) {
234            Update(res as ClientGroup);
235          } else {
236            ResAdapter.Update(res);
237          }
238
239          relationships.Add(res.Id);
240        }
241
242        BinaryRelationHelper.UpdateRelationships(group.Id,
243          relationships);
244      }
245    }
246
247    public ClientGroup GetByName(string name) {
248      ClientGroup group = new ClientGroup();
249      Resource res =
250        ResAdapter.GetByName(name);
251
252      if (res != null) {
253        return GetById(res.Id);
254      }
255
256      return null;
257    }
258
259    public ICollection<ClientGroup> MemberOf(Resource resource) {
260      throw new NotImplementedException();
261    }
262
263    protected override bool doDelete(ClientGroup group) {
264      if (group != null) {
265        //delete all relationships
266        BinaryRelationHelper.UpdateRelationships(group.Id,
267          new List<Guid>());
268
269        return base.doDelete(group) &&
270          ResAdapter.Delete(group);
271      }
272
273      return false;
274    }
275
276    #endregion
277  }
278}
Note: See TracBrowser for help on using the repository browser.