Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.Hive.Server.LINQDataAccess/3.2/ClientGroupDao.cs @ 3931

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

added minor speedups and better transaction handling to the server (#828)

File size: 6.0 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.Resources.OfType<ClientGroup>()
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.Resources.OfType<ClientGroup>()
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.Resources.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.Resources.OfType<ClientGroup>().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.Resources.OfType<ClientGroup>().SingleOrDefault(c => c.ResourceId.Equals(clientGroupId));
57      Resource res = Context.Resources.SingleOrDefault(r => r.ResourceId.Equals(resource));
58      cg.ClientGroup_Resources_Children.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.Resources.OfType<ClientGroup>()
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_Children);       
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.Resources.OfType<ClientGroup>()
96               where cg.ClientGroup_Resources_Parents.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_Children);
103      }
104
105      //get the clients
106      var qClients = (from cl in Context.Resources.OfType<Client>()
107               where cl.ClientGroup_Resources_Parents.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.Resources.OfType<Client>().SingleOrDefault(client => client.ResourceId.Equals(clientId));
117      FindAllGroupAndParentGroupIdsForClientRecursive(c, guids);
118      return guids;
119    }
120
121    //Massivly error Prone
122    private void FindAllGroupAndParentGroupIdsForClientRecursive(Resource resource, List<Guid> guids) {
123      foreach (ClientGroup_Resource cgr in resource.ClientGroup_Resources_Parents) {
124        guids.Add(cgr.ClientGroupId);
125        FindAllGroupAndParentGroupIdsForClientRecursive(cgr.ClientGroup, guids);       
126      }
127    }
128
129    #endregion
130
131    public override ClientGroup DtoToEntity(ClientGroupDto source, ClientGroup target) {
132      if (source == null)
133        return null;
134      if (target == null)
135        target = new ClientGroup();
136
137      target.Name = source.Name;
138      target.ResourceId = source.Id;
139
140      return target;
141    }
142
143    public override ClientGroupDto EntityToDto(ClientGroup source, ClientGroupDto target) {
144      if (source == null)
145        return null;
146      if (target == null)
147        target = new ClientGroupDto();
148
149      target.Id = source.ResourceId;
150      target.Name = source.Name;
151
152      return target;
153    }
154
155    public IEnumerable<ClientGroupDto> FindByName(string res) {
156      return (from cq in Context.Resources.OfType<ClientGroup>()
157              where cq.Name == res
158              select EntityToDto(cq, null)).ToList();     
159    }
160
161
162  }
163}
Note: See TracBrowser for help on using the repository browser.