Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1172 was 1166, checked in by svonolfe, 15 years ago

Improved memory consumption, fixed bug that already calculated jobs where reset (#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    private dsHiveServerTableAdapters.ClientGroup_ResourceTableAdapter resourceClientGroupAdapter =
40      new dsHiveServerTableAdapters.ClientGroup_ResourceTableAdapter();
41
42    private IResourceAdapter resourceAdapter = null;
43
44    private IResourceAdapter ResAdapter {
45      get {
46        if (resourceAdapter == null)
47          resourceAdapter = ServiceLocator.GetResourceAdapter();
48
49        return resourceAdapter;
50      }
51    }
52
53    private IClientAdapter clientAdapter = null;
54
55    private IClientAdapter ClientAdapter {
56      get {
57        if (clientAdapter == null)
58          clientAdapter = ServiceLocator.GetClientAdapter();
59
60        return clientAdapter;
61      }
62    }
63    #endregion
64
65    #region Overrides
66    protected override ClientGroup ConvertRow(dsHiveServer.ClientGroupRow row,
67      ClientGroup clientGroup) {
68      if (row != null && clientGroup != null) {
69        /*Parent - Permission Owner*/
70        clientGroup.Id = row.ResourceId;
71        ResAdapter.GetById(clientGroup);
72
73        //first check for created references
74        dsHiveServer.ClientGroup_ResourceDataTable clientGroupRows =
75            resourceClientGroupAdapter.GetDataByClientGroupId(clientGroup.Id);
76
77        foreach (dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow in
78          clientGroupRows) {
79          Resource resource = null;
80
81          IEnumerable<Resource> resources =
82            from p in
83              clientGroup.Resources
84            where p.Id == resourceClientGroupRow.ResourceId
85            select p;
86          if (resources.Count<Resource>() == 1)
87            resource = resources.First<Resource>();
88
89          if (resource == null) {
90            Resource res =
91              ClientAdapter.GetById(resourceClientGroupRow.ResourceId);
92
93            if (res == null) {
94              //is a client group
95              res =
96                GetById(resourceClientGroupRow.ResourceId);
97            }
98
99            if (res != null)
100              clientGroup.Resources.Add(res);
101          }
102        }
103
104        //secondly check for deleted references
105        ICollection<Resource> deleted =
106          new List<Resource>();
107
108        foreach (Resource resource in clientGroup.Resources) {
109          dsHiveServer.ClientGroup_ResourceDataTable found =
110            resourceClientGroupAdapter.GetDataByClientGroupResourceId(
111            clientGroup.Id,
112            resource.Id);
113
114          if (found.Count != 1) {
115            deleted.Add(resource);
116          }
117        }
118
119        foreach (Resource resource in deleted) {
120          clientGroup.Resources.Remove(resource);
121        }
122
123        return clientGroup;
124      } else
125        return null;
126    }
127
128    protected override dsHiveServer.ClientGroupRow ConvertObj(ClientGroup clientGroup,
129      dsHiveServer.ClientGroupRow row) {
130      if (clientGroup != null && row != null) {
131        row.ResourceId = clientGroup.Id;
132
133        //update references
134        foreach (Resource resource in clientGroup.Resources) {
135          //first update the member to make sure it exists in the DB
136          if (resource is ClientInfo) {
137            ClientAdapter.Update(resource as ClientInfo);
138          } else if (resource is ClientGroup) {
139            Update(resource as ClientGroup);
140          }
141
142          //secondly check for created references
143          dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow =
144            null;
145          dsHiveServer.ClientGroup_ResourceDataTable found =
146            resourceClientGroupAdapter.GetDataByClientGroupResourceId(
147              clientGroup.Id,
148              resource.Id);
149          if (found.Count == 1)
150            resourceClientGroupRow = found[0];
151
152          if (resourceClientGroupRow == null) {
153            resourceClientGroupRow =
154              found.NewClientGroup_ResourceRow();
155
156            resourceClientGroupRow.ResourceId =
157              resource.Id;
158            resourceClientGroupRow.ClientGroupId =
159              clientGroup.Id;
160
161            found.AddClientGroup_ResourceRow(resourceClientGroupRow);
162
163            resourceClientGroupAdapter.Update(
164              resourceClientGroupRow);
165          }
166        }
167
168        //thirdly check for deleted references
169        dsHiveServer.ClientGroup_ResourceDataTable clientGroupRows =
170          resourceClientGroupAdapter.GetDataByClientGroupId(clientGroup.Id);
171
172        ICollection<dsHiveServer.ClientGroup_ResourceRow> deleted =
173          new List<dsHiveServer.ClientGroup_ResourceRow>();
174
175        foreach (dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow in
176          clientGroupRows) {
177          Resource resource = null;
178
179          IEnumerable<Resource> resources =
180            from r in
181              clientGroup.Resources
182            where r.Id == resourceClientGroupRow.ResourceId
183            select r;
184
185          if (resources.Count<Resource>() == 1)
186            resource = resources.First<Resource>();
187
188          if (resource == null) {
189            deleted.Add(resourceClientGroupRow);
190          }
191        }
192
193        foreach (dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow in deleted) {
194          resourceClientGroupRow.Delete();
195          resourceClientGroupAdapter.Update(resourceClientGroupRow);
196        }
197      }
198
199      return row;
200    }
201
202    protected override dsHiveServer.ClientGroupRow
203      InsertNewRow(ClientGroup group) {
204      dsHiveServer.ClientGroupDataTable data =
205         new dsHiveServer.ClientGroupDataTable();
206
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.