Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored DAL, Improved Caching (#372)

File size: 8.8 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.Runtime.CompilerServices;
30using System.Data;
31
32namespace HeuristicLab.Hive.Server.ADODataAccess {
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();
41
42    private dsHiveServerTableAdapters.ClientGroup_ResourceTableAdapter resourceClientGroupAdapter =
43      new dsHiveServerTableAdapters.ClientGroup_ResourceTableAdapter();
44
45    private IResourceAdapter resourceAdapter = null;
46
47    private IResourceAdapter ResAdapter {
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    }
66    #endregion
67
68    #region Overrides
69    protected override ClientGroup Convert(dsHiveServer.ClientGroupRow row,
70      ClientGroup clientGroup) {
71      if (row != null && clientGroup != null) {
72        /*Parent - Permission Owner*/
73        clientGroup.Id = row.ResourceId;
74        ResAdapter.GetById(clientGroup);
75
76        //first check for created references
77        dsHiveServer.ClientGroup_ResourceDataTable clientGroupRows =
78            resourceClientGroupAdapter.GetDataByClientGroupId(clientGroup.Id);
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
87            where p.Id == resourceClientGroupRow.ResourceId
88            select p;
89          if (resources.Count<Resource>() == 1)
90            resource = resources.First<Resource>();
91
92          if (resource == null) {
93            Resource res =
94              ClientAdapter.GetById(resourceClientGroupRow.ResourceId);
95
96            if (res == null) {
97              //is a client group
98              res =
99                GetById(resourceClientGroupRow.ResourceId);
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) {
112          dsHiveServer.ClientGroup_ResourceDataTable found =
113            resourceClientGroupAdapter.GetDataByClientGroupResourceId(
114            clientGroup.Id,
115            resource.Id);
116
117          if (found.Count != 1) {
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
131    protected override dsHiveServer.ClientGroupRow Convert(ClientGroup clientGroup,
132      dsHiveServer.ClientGroupRow row) {
133      if (clientGroup != null && row != null) {
134        row.ResourceId = clientGroup.Id;
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) {
140            ClientAdapter.Update(resource as ClientInfo);
141          } else if (resource is ClientGroup) {
142            Update(resource as ClientGroup);
143          }
144
145          //secondly check for created references
146          dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow =
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];
154
155          if (resourceClientGroupRow == null) {
156            resourceClientGroupRow =
157              found.NewClientGroup_ResourceRow();
158
159            resourceClientGroupRow.ResourceId =
160              resource.Id;
161            resourceClientGroupRow.ClientGroupId =
162              clientGroup.Id;
163
164            found.AddClientGroup_ResourceRow(resourceClientGroupRow);
165
166            resourceClientGroupAdapter.Update(
167              resourceClientGroupRow);
168          }
169        }
170
171        //thirdly check for deleted references
172        dsHiveServer.ClientGroup_ResourceDataTable clientGroupRows =
173          resourceClientGroupAdapter.GetDataByClientGroupId(clientGroup.Id);
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
185            where r.Id == resourceClientGroupRow.ResourceId
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
196        foreach (dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow in deleted) {
197          resourceClientGroupRow.Delete();
198          resourceClientGroupAdapter.Update(resourceClientGroupRow);
199        }
200      }
201
202      return row;
203    }
204
205    protected override dsHiveServer.ClientGroupRow
206      InsertNewRow(ClientGroup group) {
207      dsHiveServer.ClientGroupRow row =
208        data.NewClientGroupRow();
209
210      row.ResourceId = group.Id;
211
212      data.AddClientGroupRow(row);
213      adapter.Update(row);
214
215      return row;
216    }
217
218    protected override void
219      UpdateRow(dsHiveServer.ClientGroupRow row) {
220      adapter.Update(row);
221    }
222
223    protected override IEnumerable<dsHiveServer.ClientGroupRow>
224      FindById(long id) {
225      return adapter.GetDataById(id);
226    }
227
228    protected override IEnumerable<dsHiveServer.ClientGroupRow>
229      FindAll() {
230      return adapter.GetData();
231    }
232    #endregion
233
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);
241      }
242    }
243
244    public ClientGroup GetByName(string name) {
245      ClientGroup group = new ClientGroup();
246      Resource res =
247        ResAdapter.GetByName(name);
248
249      if (res != null) {
250        return GetById(res.Id);
251      }
252
253      return null;
254    }
255
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 =
262         resourceClientGroupAdapter.GetDataByResourceId(resource.Id);
263
264        foreach (dsHiveServer.ClientGroup_ResourceRow clientGroupRow in
265          clientGroupRows) {
266          ClientGroup clientGroup =
267            GetById(clientGroupRow.ClientGroupId);
268          clientGroups.Add(clientGroup);
269        }
270      }
271
272      return clientGroups;
273    }
274
275    [MethodImpl(MethodImplOptions.Synchronized)]
276    public override bool Delete(ClientGroup group) {
277      if (group != null) {
278        return base.Delete(group) &&
279          ResAdapter.Delete(group);
280      }
281
282      return false;
283    }
284
285    #endregion
286  }
287}
Note: See TracBrowser for help on using the repository browser.