Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1197 was 1175, checked in by svonolfe, 16 years ago

Refactored DAL, avoided possible race conditions (#372)

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