Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3578 was 3578, checked in by kgrading, 14 years ago

Removed References to HiveLogging and updated the default logging mechanism (#991)

File size: 5.9 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      CommitChanges();
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      CommitChanges();
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      CommitChanges();
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      CommitChanges();
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      CommitChanges();
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    public IEnumerable<Guid> FindAllGroupAndParentGroupIdsForClient(Guid clientId) {
115      List<Guid> guids = new List<Guid>();
116      Client c = Context.Clients.SingleOrDefault(client => client.ResourceId.Equals(clientId));
117      FindAllGroupAndParentGroupIdsForClientRecursive(c.Resource, guids);
118      return guids;
119    }
120
121    private void FindAllGroupAndParentGroupIdsForClientRecursive(Resource resource, List<Guid> guids) {
122      foreach (ClientGroup_Resource cgr in resource.ClientGroup_Resources) {
123        guids.Add(cgr.ClientGroupId);
124        FindAllGroupAndParentGroupIdsForClientRecursive(cgr.ClientGroup.Resource, guids);       
125      }
126    }
127
128    #endregion
129
130    public override ClientGroup DtoToEntity(ClientGroupDto source, ClientGroup target) {
131      if (source == null)
132        return null;
133      if (target == null)
134        target = new ClientGroup();
135      if (target.Resource == null)
136        target.Resource = new Resource();
137
138      target.Resource.Name = source.Name;
139      target.Resource.ResourceId = source.Id;
140
141      return target;
142    }
143
144    public override ClientGroupDto EntityToDto(ClientGroup source, ClientGroupDto target) {
145      if (source == null)
146        return null;
147      if (target == null)
148        target = new ClientGroupDto();
149
150      target.Id = source.ResourceId;
151      target.Name = source.Resource.Name;
152
153      return target;
154    }
155
156    public IEnumerable<ClientGroupDto> FindByName(string res) {
157      return (from cq in Context.ClientGroups
158              where cq.Resource.Name == res
159              select EntityToDto(cq, null)).ToList();     
160    }
161
162
163  }
164}
Note: See TracBrowser for help on using the repository browser.