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

Last change on this file since 971 was 971, checked in by svonolfe, 12 years ago

Added Job Adapter (#372)

File size: 10.9 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 : DataAdapterBase, IClientGroupAdapter {
34    private dsHiveServerTableAdapters.ClientGroupTableAdapter adapter =
35        new dsHiveServerTableAdapters.ClientGroupTableAdapter();
36
37    private dsHiveServer.ClientGroupDataTable data =
38      new dsHiveServer.ClientGroupDataTable();
39
40    private dsHiveServerTableAdapters.ClientGroup_ResourceTableAdapter resourceClientGroupAdapter =
41      new dsHiveServerTableAdapters.ClientGroup_ResourceTableAdapter();
42
43    private dsHiveServer.ClientGroup_ResourceDataTable resourceClientGroupData =
44      new dsHiveServer.ClientGroup_ResourceDataTable();
45
46    private IResourceAdapter resourceAdapter = null;
47
48    private IResourceAdapter ResAdapter {
49      get {
50        if (resourceAdapter == null)
51          resourceAdapter = ServiceLocator.GetResourceAdapter();
52
53        return resourceAdapter;
54      }
55    }
56
57    private IClientAdapter clientAdapter = null;
58
59    private IClientAdapter ClientAdapter {
60      get {
61        if (clientAdapter == null)
62          clientAdapter = ServiceLocator.GetClientAdapter();
63
64        return clientAdapter;
65      }
66    }
67
68    public ClientGroupAdapter() {
69      adapter.Fill(data);
70      resourceClientGroupAdapter.Fill(resourceClientGroupData);
71    }
72
73    protected override void Update() {
74      this.adapter.Update(this.data);
75      this.resourceClientGroupAdapter.Update(resourceClientGroupData);
76    }
77
78    private ClientGroup Convert(dsHiveServer.ClientGroupRow row,
79      ClientGroup clientGroup) {
80      if (row != null && clientGroup != null) {
81        /*Parent - Permission Owner*/
82        clientGroup.ResourceId = row.ResourceId;
83        ResAdapter.GetResourceById(clientGroup);
84
85        //first check for created references
86        IEnumerable<dsHiveServer.ClientGroup_ResourceRow> clientGroupRows =
87          from resource in
88            resourceClientGroupData.AsEnumerable<dsHiveServer.ClientGroup_ResourceRow>()
89          where resource.ClientGroupResource == clientGroup.ResourceId
90          select resource;
91
92        foreach (dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow in
93          clientGroupRows) {
94          Resource resource = null;
95
96          IEnumerable<Resource> resources =
97            from p in
98              clientGroup.Resources
99            where p.ResourceId == resourceClientGroupRow.ResourceId
100            select p;
101          if (resources.Count<Resource>() == 1)
102            resource = resources.First<Resource>();
103
104          if (resource == null) {
105            Resource res =
106              ClientAdapter.GetClientById(resourceClientGroupRow.ResourceId);
107
108            if (res == null) {
109              //is a client group
110              res =
111                GetClientGroupById(resourceClientGroupRow.ResourceId);
112            }
113
114            if (res != null)
115              clientGroup.Resources.Add(res);
116          }
117        }
118
119        //secondly check for deleted references
120        ICollection<Resource> deleted =
121          new List<Resource>();
122
123        foreach (Resource resource in clientGroup.Resources) {
124          dsHiveServer.ClientGroup_ResourceRow permOwnerUserGroupRow =
125            resourceClientGroupData.FindByClientGroupResourceResourceId(
126              clientGroup.ResourceId,
127              resource.ResourceId);
128
129          if (permOwnerUserGroupRow == null) {
130            deleted.Add(resource);
131          }
132        }
133
134        foreach (Resource resource in deleted) {
135          clientGroup.Resources.Remove(resource);
136        }
137
138        return clientGroup;
139      } else
140        return null;
141    }
142
143    private dsHiveServer.ClientGroupRow Convert(ClientGroup clientGroup,
144      dsHiveServer.ClientGroupRow row) {
145      if (clientGroup != null && row != null) {
146        row.ResourceId = clientGroup.ResourceId;
147
148        //update references
149        foreach (Resource resource in clientGroup.Resources) {
150          //first update the member to make sure it exists in the DB
151          if (resource is ClientInfo) {
152            ClientAdapter.UpdateClient(resource as ClientInfo);
153          } else if (resource is ClientGroup) {
154            UpdateClientGroup(resource as ClientGroup);
155          }
156
157          //secondly check for created references
158          dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow =
159            resourceClientGroupData.FindByClientGroupResourceResourceId(
160                            clientGroup.ResourceId,
161                            resource.ResourceId);
162
163          if (resourceClientGroupRow == null) {
164            resourceClientGroupRow =
165              resourceClientGroupData.NewClientGroup_ResourceRow();
166
167            resourceClientGroupRow.ResourceId =
168              resource.ResourceId;
169            resourceClientGroupRow.ClientGroupResource =
170              clientGroup.ResourceId;
171
172            resourceClientGroupData.AddClientGroup_ResourceRow(
173              resourceClientGroupRow);
174          }
175        }
176
177        //thirdly check for deleted references
178        IEnumerable<dsHiveServer.ClientGroup_ResourceRow> clientGroupRows =
179          from permOwner in
180            resourceClientGroupData.AsEnumerable<dsHiveServer.ClientGroup_ResourceRow>()
181          where permOwner.ClientGroupResource == clientGroup.ResourceId
182          select permOwner;
183
184        ICollection<dsHiveServer.ClientGroup_ResourceRow> deleted =
185          new List<dsHiveServer.ClientGroup_ResourceRow>();
186
187        foreach (dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow in
188          clientGroupRows) {
189          Resource resource = null;
190
191          IEnumerable<Resource> resources =
192            from r in
193              clientGroup.Resources
194            where r.ResourceId == resourceClientGroupRow.ResourceId
195            select r;
196
197          if (resources.Count<Resource>() == 1)
198            resource = resources.First<Resource>();
199
200          if (resource == null) {
201            deleted.Add(resourceClientGroupRow);
202          }
203        }
204
205        foreach (dsHiveServer.ClientGroup_ResourceRow resoruceClientGroupRow in
206          deleted) {
207          resourceClientGroupData.RemoveClientGroup_ResourceRow(
208            resoruceClientGroupRow);
209        }
210
211      }
212
213      return row;
214    }
215
216    #region IClientGroupAdapter Members
217    [MethodImpl(MethodImplOptions.Synchronized)]
218    public void UpdateClientGroup(ClientGroup group) {
219      if (group != null) {
220        ResAdapter.UpdateResource(group);
221
222        dsHiveServer.ClientGroupRow row =
223          data.FindByResourceId(group.ResourceId);
224
225        if (row == null) {
226          row = data.NewClientGroupRow();
227          row.ResourceId = group.ResourceId;
228          data.AddClientGroupRow(row);
229        }
230
231        Convert(group, row);
232      }
233    }
234
235    public ClientGroup GetClientGroupById(long clientGroupId) {
236      ClientGroup clientGroup = new ClientGroup();
237
238      dsHiveServer.ClientGroupRow row =
239        data.FindByResourceId(clientGroupId);
240
241      if (row != null) {
242        Convert(row, clientGroup);
243
244        return clientGroup;
245      } else {
246        return null;
247      }
248    }
249
250    public ClientGroup GetClientGroupByName(string name) {
251      ClientGroup group = new ClientGroup();
252
253      Resource res =
254        ResAdapter.GetResourceByName(name);
255
256      if (res != null) {
257        dsHiveServer.ClientGroupRow row =
258          data.FindByResourceId(res.ResourceId);
259
260        if (row != null) {
261          Convert(row, group);
262
263          return group;
264        }
265      }
266
267      return null;
268    }
269
270    public ICollection<ClientGroup> GetAllClientGroups() {
271      ICollection<ClientGroup> allClientGroups =
272        new List<ClientGroup>();
273
274      foreach (dsHiveServer.ClientGroupRow row in data) {
275        ClientGroup clientGroup = new ClientGroup();
276
277        Convert(row, clientGroup);
278        allClientGroups.Add(clientGroup);
279      }
280
281      return allClientGroups;
282    }
283
284    public ICollection<ClientGroup> MemberOf(Resource resource) {
285      ICollection<ClientGroup> clientGroups =
286        new List<ClientGroup>();
287
288      if (resource != null) {
289        IEnumerable<dsHiveServer.ClientGroup_ResourceRow> clientGroupRows =
290         from clientGroup in
291           resourceClientGroupData.AsEnumerable<dsHiveServer.ClientGroup_ResourceRow>()
292         where clientGroup.ResourceId == resource.ResourceId
293         select clientGroup;
294
295        foreach (dsHiveServer.ClientGroup_ResourceRow clientGroupRow in
296          clientGroupRows) {
297          ClientGroup clientGroup =
298            GetClientGroupById(clientGroupRow.ClientGroupResource);
299          clientGroups.Add(clientGroup);
300        }
301      }
302
303      return clientGroups;
304    }
305
306    [MethodImpl(MethodImplOptions.Synchronized)]
307    public bool DeleteClientGroup(ClientGroup group) {
308      if (group != null) {
309        dsHiveServer.ClientGroupRow row =
310          data.FindByResourceId(group.ResourceId);
311
312        if (row != null) {
313          ICollection<dsHiveServer.ClientGroup_ResourceRow> deleted =
314            new List<dsHiveServer.ClientGroup_ResourceRow>();
315
316          foreach (dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow in
317            resourceClientGroupData) {
318            if (resourceClientGroupRow.ClientGroupResource == group.ResourceId ||
319              resourceClientGroupRow.ResourceId == group.ResourceId) {
320              deleted.Add(resourceClientGroupRow);
321            }
322          }
323
324          foreach (dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow in
325            deleted) {
326            resourceClientGroupData.RemoveClientGroup_ResourceRow(
327              resourceClientGroupRow);
328          }
329
330          data.RemoveClientGroupRow(row);
331          return ResAdapter.DeleteResource(group);
332        }
333      }
334
335      return false;
336    }
337
338    #endregion
339  }
340}
Note: See TracBrowser for help on using the repository browser.