1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
26 | using HeuristicLab.Hive.Server.DataAccess;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Hive.Server.LINQDataAccess {
|
---|
29 | public class SlaveGroupDao : BaseDao<SlaveGroupDto, SlaveGroup>, ISlaveGroupDao {
|
---|
30 |
|
---|
31 |
|
---|
32 | #region IGenericDao<SlaveGroupDto,SlaveGroup> Members
|
---|
33 |
|
---|
34 | public SlaveGroupDto FindById(Guid id) {
|
---|
35 | return (from cc in Context.Resources.OfType<SlaveGroup>()
|
---|
36 | where cc.ResourceId.Equals(id)
|
---|
37 | select EntityToDto(cc, null)).SingleOrDefault();
|
---|
38 | }
|
---|
39 |
|
---|
40 |
|
---|
41 | public IEnumerable<SlaveGroupDto> FindAll() {
|
---|
42 | return (from cc in Context.Resources.OfType<SlaveGroup>()
|
---|
43 | select EntityToDto(cc, null)).ToList();
|
---|
44 | }
|
---|
45 |
|
---|
46 | public SlaveGroupDto Insert(SlaveGroupDto bObj) {
|
---|
47 | //Auto GUID is disabled for Ressource... thx to the slaves... grml
|
---|
48 | if(bObj.Id == Guid.Empty)
|
---|
49 | bObj.Id = Guid.NewGuid();
|
---|
50 |
|
---|
51 | SlaveGroup cc = DtoToEntity(bObj, null);
|
---|
52 | Context.Resources.InsertOnSubmit(cc);
|
---|
53 | CommitChanges();
|
---|
54 | bObj.Id = cc.ResourceId;
|
---|
55 | return bObj;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public void Delete(Guid id) {
|
---|
59 |
|
---|
60 | //Deleting all references
|
---|
61 | Context.SlaveGroup_Resources.DeleteAllOnSubmit(Context.SlaveGroup_Resources.Where(cg => cg.SlaveGroupId.Equals(id)));
|
---|
62 |
|
---|
63 | Resource res = Context.Resources.SingleOrDefault(c => c.ResourceId.Equals(id));
|
---|
64 |
|
---|
65 | Context.Resources.DeleteOnSubmit(res);
|
---|
66 | CommitChanges();
|
---|
67 | }
|
---|
68 |
|
---|
69 | public void Update(SlaveGroupDto bObj) {
|
---|
70 | SlaveGroup slave = Context.Resources.OfType<SlaveGroup>().SingleOrDefault(c => c.ResourceId.Equals(bObj.Id));
|
---|
71 | DtoToEntity(bObj, slave);
|
---|
72 | CommitChanges();
|
---|
73 | }
|
---|
74 |
|
---|
75 | public void AddRessourceToSlaveGroup(Guid resource, Guid slaveGroupId) {
|
---|
76 | SlaveGroup cg = Context.Resources.OfType<SlaveGroup>().SingleOrDefault(c => c.ResourceId.Equals(slaveGroupId));
|
---|
77 | Resource res = Context.Resources.SingleOrDefault(r => r.ResourceId.Equals(resource));
|
---|
78 | cg.SlaveGroup_Resources_Children.Add(new SlaveGroup_Resource { SlaveGroup = cg, Resource = res });
|
---|
79 | CommitChanges();
|
---|
80 | }
|
---|
81 |
|
---|
82 | public void RemoveRessourceFromSlaveGroup(Guid resource, Guid slaveGroupId) {
|
---|
83 | SlaveGroup_Resource cgr =
|
---|
84 | Context.SlaveGroup_Resources.SingleOrDefault(
|
---|
85 | cg => cg.ResourceId.Equals(resource) && cg.SlaveGroupId.Equals(slaveGroupId));
|
---|
86 | Context.SlaveGroup_Resources.DeleteOnSubmit(cgr);
|
---|
87 | CommitChanges();
|
---|
88 | }
|
---|
89 |
|
---|
90 | public IEnumerable<SlaveGroupDto> MemberOf(SlaveDto slave) {
|
---|
91 | return (from cgr in Context.SlaveGroup_Resources
|
---|
92 | where cgr.ResourceId.Equals(slave.Id)
|
---|
93 | select EntityToDto(cgr.SlaveGroup, null)).ToList();
|
---|
94 | }
|
---|
95 |
|
---|
96 | public IEnumerable<SlaveGroupDto> FindAllWithSubGroupsAndSlaves() {
|
---|
97 | List<SlaveGroupDto> groupList = new List<SlaveGroupDto>();
|
---|
98 |
|
---|
99 | var q = (from cg in Context.Resources.OfType<SlaveGroup>()
|
---|
100 | where !Context.SlaveGroup_Resources.Any(cgr => cgr.ResourceId.Equals(cg.ResourceId))
|
---|
101 | select cg);
|
---|
102 |
|
---|
103 | foreach (SlaveGroup cg in q) {
|
---|
104 | SlaveGroupDto cgd = EntityToDto(cg, null);
|
---|
105 | groupList.Add(cgd);
|
---|
106 | FillSubGroupsAndSlavesRecursivly(cgd, cg.SlaveGroup_Resources_Children);
|
---|
107 | }
|
---|
108 | return groupList;
|
---|
109 | }
|
---|
110 |
|
---|
111 | private void FillSubGroupsAndSlavesRecursivly(SlaveGroupDto parentSlaveGroup, System.Data.Linq.EntitySet<SlaveGroup_Resource> parentResourceSet) {
|
---|
112 | SlaveDao cd = new SlaveDao();
|
---|
113 | //Get all the Groups
|
---|
114 |
|
---|
115 | var qGroups = (from cg in Context.Resources.OfType<SlaveGroup>()
|
---|
116 | where cg.SlaveGroup_Resources_Parents.Any(cgr => cgr.SlaveGroupId.Equals(parentSlaveGroup.Id))
|
---|
117 | select cg);
|
---|
118 |
|
---|
119 | foreach (SlaveGroup cg in qGroups) {
|
---|
120 | SlaveGroupDto cgd = EntityToDto(cg, null);
|
---|
121 | parentSlaveGroup.Resources.Add(cgd);
|
---|
122 | FillSubGroupsAndSlavesRecursivly(cgd, cg.SlaveGroup_Resources_Children);
|
---|
123 | }
|
---|
124 |
|
---|
125 | //get the slaves
|
---|
126 | var qSlaves = (from cl in Context.Resources.OfType<Slave>()
|
---|
127 | where cl.SlaveGroup_Resources_Parents.Any(cgr => cgr.SlaveGroupId.Equals(parentSlaveGroup.Id))
|
---|
128 | select cl);
|
---|
129 | foreach (Slave slave in qSlaves) {
|
---|
130 | parentSlaveGroup.Resources.Add(cd.EntityToDto(slave, null));
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | public IEnumerable<Guid> FindAllGroupAndParentGroupIdsForSlave(Guid slaveId) {
|
---|
135 | List<Guid> guids = new List<Guid>();
|
---|
136 | Slave c = Context.Resources.OfType<Slave>().SingleOrDefault(slave => slave.ResourceId.Equals(slaveId));
|
---|
137 | FindAllGroupAndParentGroupIdsForSlaveRecursive(c, guids);
|
---|
138 | return guids;
|
---|
139 | }
|
---|
140 |
|
---|
141 | //Massivly error Prone
|
---|
142 | private void FindAllGroupAndParentGroupIdsForSlaveRecursive(Resource resource, List<Guid> guids) {
|
---|
143 | foreach (SlaveGroup_Resource cgr in resource.SlaveGroup_Resources_Parents) {
|
---|
144 | guids.Add(cgr.SlaveGroupId);
|
---|
145 | FindAllGroupAndParentGroupIdsForSlaveRecursive(cgr.SlaveGroup, guids);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | #endregion
|
---|
150 |
|
---|
151 | public override SlaveGroup DtoToEntity(SlaveGroupDto source, SlaveGroup target) {
|
---|
152 | if (source == null)
|
---|
153 | return null;
|
---|
154 | if (target == null)
|
---|
155 | target = new SlaveGroup();
|
---|
156 |
|
---|
157 | target.Name = source.Name;
|
---|
158 | target.ResourceId = source.Id;
|
---|
159 |
|
---|
160 | return target;
|
---|
161 | }
|
---|
162 |
|
---|
163 | public override SlaveGroupDto EntityToDto(SlaveGroup source, SlaveGroupDto target) {
|
---|
164 | if (source == null)
|
---|
165 | return null;
|
---|
166 | if (target == null)
|
---|
167 | target = new SlaveGroupDto();
|
---|
168 |
|
---|
169 | target.Id = source.ResourceId;
|
---|
170 | target.Name = source.Name;
|
---|
171 |
|
---|
172 | return target;
|
---|
173 | }
|
---|
174 |
|
---|
175 | public IEnumerable<SlaveGroupDto> FindByName(string res) {
|
---|
176 | return (from cq in Context.Resources.OfType<SlaveGroup>()
|
---|
177 | where cq.Name == res
|
---|
178 | select EntityToDto(cq, null)).ToList();
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | }
|
---|
183 | }
|
---|