Free cookie consent management tool by TermsFeed Policy Generator

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

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

fixed race condition issues, improved performance (#372)

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