Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1497 was 1468, checked in by svonolfe, 16 years ago

Added transaction management (#527)

File size: 9.4 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 ClientGroupAdapter :
81    DataAdapterBase<dsHiveServerTableAdapters.ClientGroupTableAdapter,
82    ClientGroup,
83    dsHiveServer.ClientGroupRow>,
84    IClientGroupAdapter {
85    #region Fields
86    private dsHiveServerTableAdapters.ClientGroup_ResourceTableAdapter resourceClientGroupAdapter =
87      new dsHiveServerTableAdapters.ClientGroup_ResourceTableAdapter();
88
89    private IResourceAdapter resourceAdapter = null;
90
91    private IResourceAdapter ResAdapter {
92      get {
93        if (resourceAdapter == null)
94          resourceAdapter =
95            this.Session.GetDataAdapter<Resource, IResourceAdapter>();
96
97        return resourceAdapter;
98      }
99    }
100
101    private IClientAdapter clientAdapter = null;
102
103    private IClientAdapter ClientAdapter {
104      get {
105        if (clientAdapter == null)
106          clientAdapter =
107            this.Session.GetDataAdapter<ClientInfo, IClientAdapter>();
108
109        return clientAdapter;
110      }
111    }
112    #endregion
113
114    public ClientGroupAdapter():
115      base(new ClientGroupAdapterWrapper()) {
116    }
117
118    #region Overrides
119    protected override ClientGroup ConvertRow(dsHiveServer.ClientGroupRow row,
120      ClientGroup clientGroup) {
121      if (row != null && clientGroup != null) {
122        /*Parent - Permission Owner*/
123        clientGroup.Id = row.ResourceId;
124        ResAdapter.GetById(clientGroup);
125
126        //first check for created references
127        dsHiveServer.ClientGroup_ResourceDataTable clientGroupRows =
128            resourceClientGroupAdapter.GetDataByClientGroupId(clientGroup.Id);
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
137            where p.Id == resourceClientGroupRow.ResourceId
138            select p;
139          if (resources.Count<Resource>() == 1)
140            resource = resources.First<Resource>();
141
142          if (resource == null) {
143            Resource res =
144              ClientAdapter.GetById(resourceClientGroupRow.ResourceId);
145
146            if (res == null) {
147              //is a client group
148              res =
149                GetById(resourceClientGroupRow.ResourceId);
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) {
162          dsHiveServer.ClientGroup_ResourceDataTable found =
163            resourceClientGroupAdapter.GetDataByClientGroupResourceId(
164            clientGroup.Id,
165            resource.Id);
166
167          if (found.Count != 1) {
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
181    protected override dsHiveServer.ClientGroupRow ConvertObj(ClientGroup clientGroup,
182      dsHiveServer.ClientGroupRow row) {
183      if (clientGroup != null && row != null) {
184        row.ResourceId = clientGroup.Id;
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) {
190            ClientAdapter.Update(resource as ClientInfo);
191          } else if (resource is ClientGroup) {
192            Update(resource as ClientGroup);
193          }
194
195          //secondly check for created references
196          dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow =
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];
204
205          if (resourceClientGroupRow == null) {
206            resourceClientGroupRow =
207              found.NewClientGroup_ResourceRow();
208
209            resourceClientGroupRow.ResourceId =
210              resource.Id;
211            resourceClientGroupRow.ClientGroupId =
212              clientGroup.Id;
213
214            found.AddClientGroup_ResourceRow(resourceClientGroupRow);
215
216            resourceClientGroupAdapter.Update(
217              resourceClientGroupRow);
218          }
219        }
220
221        //thirdly check for deleted references
222        dsHiveServer.ClientGroup_ResourceDataTable clientGroupRows =
223          resourceClientGroupAdapter.GetDataByClientGroupId(clientGroup.Id);
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
235            where r.Id == resourceClientGroupRow.ResourceId
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
246        foreach (dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow in deleted) {
247          resourceClientGroupRow.Delete();
248          resourceClientGroupAdapter.Update(resourceClientGroupRow);
249        }
250      }
251
252      return row;
253    }
254    #endregion
255
256    #region IClientGroupAdapter Members
257    protected override void doUpdate(ClientGroup group) {
258      if (group != null) {
259        ResAdapter.Update(group);
260
261        base.doUpdate(group);
262      }
263    }
264
265    public ClientGroup GetByName(string name) {
266      ClientGroup group = new ClientGroup();
267      Resource res =
268        ResAdapter.GetByName(name);
269
270      if (res != null) {
271        return GetById(res.Id);
272      }
273
274      return null;
275    }
276
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 =
283         resourceClientGroupAdapter.GetDataByResourceId(resource.Id);
284
285        foreach (dsHiveServer.ClientGroup_ResourceRow clientGroupRow in
286          clientGroupRows) {
287          ClientGroup clientGroup =
288            GetById(clientGroupRow.ClientGroupId);
289          clientGroups.Add(clientGroup);
290        }
291      }
292
293      return clientGroups;
294    }
295
296    protected override bool doDelete(ClientGroup group) {
297      if (group != null) {
298        return base.doDelete(group) &&
299          ResAdapter.Delete(group);
300      }
301
302      return false;
303    }
304
305    #endregion
306  }
307}
Note: See TracBrowser for help on using the repository browser.