Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.LINQDataAccess/3.2/ClientGroupDao.cs @ 3013

Last change on this file since 3013 was 3011, checked in by kgrading, 15 years ago

changed the complete DAL to LINQ 2 SQL (with the exception of the job streaming), did a lot of refactoring, Introduced DTOs (that are named DTOs for better understanding), added the spring.NET Interceptor, reintroduced transactions and cleaned up the whole JobResult thing and updated a part of the config merger (#830)

File size: 5.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Hive.Contracts.BusinessObjects;
6using HeuristicLab.Hive.Server.DataAccess;
7
8namespace HeuristicLab.Hive.Server.LINQDataAccess {
9  public class ClientGroupDao : BaseDao<ClientGroupDto, ClientGroup>, IClientGroupDao {
10
11
12    #region IGenericDao<ClientGroupDto,ClientGroup> Members
13
14    public ClientGroupDto FindById(Guid id) {
15      return (from cc in Context.ClientGroups
16              where cc.ResourceId.Equals(id)
17              select EntityToDto(cc, null)).SingleOrDefault();
18    }
19
20
21    public IEnumerable<ClientGroupDto> FindAll() {
22      return (from cc in Context.ClientGroups
23              select EntityToDto(cc, null)).ToList();
24    }
25
26    public ClientGroupDto Insert(ClientGroupDto bObj) {
27      //Auto GUID is disabled for Ressource... thx to the clients... grml     
28      if(bObj.Id == Guid.Empty)
29        bObj.Id = Guid.NewGuid();
30
31      ClientGroup cc = DtoToEntity(bObj, null);
32      Context.ClientGroups.InsertOnSubmit(cc);
33      Context.SubmitChanges();
34      bObj.Id = cc.ResourceId;
35      return bObj;
36    }
37
38    public void Delete(ClientGroupDto bObj) {
39     
40      //Deleting all references
41      Context.ClientGroup_Resources.DeleteAllOnSubmit(Context.ClientGroup_Resources.Where(cg => cg.ClientGroupId.Equals(bObj.Id)));
42
43      Resource res = Context.Resources.SingleOrDefault(c => c.ResourceId.Equals(bObj.Id));
44
45      Context.Resources.DeleteOnSubmit(res);
46      Context.SubmitChanges();
47    }
48
49    public void Update(ClientGroupDto bObj) {
50      ClientGroup client = Context.ClientGroups.SingleOrDefault(c => c.ResourceId.Equals(bObj.Id));
51      DtoToEntity(bObj, client);
52      Context.SubmitChanges();
53    }
54
55    public void AddRessourceToClientGroup(Guid resource, Guid clientGroupId) {
56      ClientGroup cg = Context.ClientGroups.SingleOrDefault(c => c.ResourceId.Equals(clientGroupId));
57      Resource res = Context.Resources.SingleOrDefault(r => r.ResourceId.Equals(resource));
58      cg.ClientGroup_Resources.Add(new ClientGroup_Resource { ClientGroup = cg, Resource = res });
59      Context.SubmitChanges();
60    }
61
62    public void RemoveRessourceFromClientGroup(Guid resource, Guid clientGroupId) {
63      ClientGroup_Resource cgr =
64        Context.ClientGroup_Resources.SingleOrDefault(
65          cg => cg.ResourceId.Equals(resource) && cg.ClientGroupId.Equals(clientGroupId));
66      Context.ClientGroup_Resources.DeleteOnSubmit(cgr);
67      Context.SubmitChanges();
68    }
69
70    public IEnumerable<ClientGroupDto> MemberOf(ClientDto client) {
71      return (from cgr in Context.ClientGroup_Resources
72              where cgr.ResourceId.Equals(client.Id)
73              select EntityToDto(cgr.ClientGroup, null)).ToList();
74    }
75
76    public IEnumerable<ClientGroupDto> FindAllWithSubGroupsAndClients() {
77      List<ClientGroupDto> groupList = new List<ClientGroupDto>();
78
79      var q = (from cg in Context.ClientGroups
80               where !Context.ClientGroup_Resources.Any(cgr => cgr.ResourceId.Equals(cg.ResourceId))
81               select cg);
82
83      foreach (ClientGroup cg in q) {
84        ClientGroupDto cgd = EntityToDto(cg, null);
85        groupList.Add(cgd);
86        FillSubGroupsAndClientsRecursivly(cgd, cg.ClientGroup_Resources);       
87      }
88      return groupList;
89    }
90
91    private void FillSubGroupsAndClientsRecursivly(ClientGroupDto parentClientGroup, System.Data.Linq.EntitySet<ClientGroup_Resource> parentResourceSet) {
92      ClientDao cd = new ClientDao();     
93      //Get all the Groups
94     
95      var qGroups = (from cg in Context.ClientGroups
96               where cg.Resource.ClientGroup_Resources.Any(cgr => cgr.ClientGroupId.Equals(parentClientGroup.Id))
97               select cg);
98
99      foreach (ClientGroup cg in qGroups) {
100        ClientGroupDto cgd = EntityToDto(cg, null);
101        parentClientGroup.Resources.Add(cgd);
102        FillSubGroupsAndClientsRecursivly(cgd, cg.ClientGroup_Resources);
103      }
104
105      //get the clients
106      var qClients = (from cl in Context.Clients
107               where cl.Resource.ClientGroup_Resources.Any(cgr => cgr.ClientGroupId.Equals(parentClientGroup.Id))
108               select cl);
109      foreach (Client client in qClients) {
110        parentClientGroup.Resources.Add(cd.EntityToDto(client, null));                       
111      }
112     
113    }
114
115    #endregion
116
117    public override ClientGroup DtoToEntity(ClientGroupDto source, ClientGroup target) {
118      if (source == null)
119        return null;
120      if (target == null)
121        target = new ClientGroup();
122      if (target.Resource == null)
123        target.Resource = new Resource();
124
125      target.Resource.Name = source.Name;
126      target.Resource.ResourceId = source.Id;
127
128      return target;
129    }
130
131    public override ClientGroupDto EntityToDto(ClientGroup source, ClientGroupDto target) {
132      if (source == null)
133        return null;
134      if (target == null)
135        target = new ClientGroupDto();
136
137      target.Id = source.ResourceId;
138      target.Name = source.Resource.Name;
139
140      return target;
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.