1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2017 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.Data.Linq;
|
---|
25 | using System.Linq;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Services.Hive.DataAccess.Daos {
|
---|
28 | public class AssignedProjectResourceDao : GenericDao<Guid, AssignedProjectResource> {
|
---|
29 | public AssignedProjectResourceDao(DataContext dataContext) : base(dataContext) { }
|
---|
30 |
|
---|
31 | public override AssignedProjectResource GetById(Guid id) {
|
---|
32 | throw new NotImplementedException();
|
---|
33 | }
|
---|
34 |
|
---|
35 | public IQueryable<AssignedProjectResource> GetByProjectId(Guid projectId) {
|
---|
36 | return Table.Where(x => x.ProjectId == projectId);
|
---|
37 | }
|
---|
38 |
|
---|
39 | public void DeleteByProjectIdAndResourceIds(Guid projectId, IEnumerable<Guid> resourceIds) {
|
---|
40 | string paramIds = string.Join(",", resourceIds.ToList().Select(x => string.Format("'{0}'", x)));
|
---|
41 | if (!string.IsNullOrWhiteSpace(paramIds)) {
|
---|
42 | string query = string.Format(DeleteByProjectIdAndResourceIdsQueryString, projectId, paramIds);
|
---|
43 | DataContext.ExecuteCommand(query);
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public void DeleteByProjectIdsAndResourceIds(IEnumerable<Guid> projectIds, IEnumerable<Guid> resourceIds) {
|
---|
48 | string paramProjectIds = string.Join(",", projectIds.ToList().Select(x => string.Format("'{0}'", x)));
|
---|
49 | string paramResourceIds = string.Join(",", resourceIds.ToList().Select(x => string.Format("'{0}'", x)));
|
---|
50 | if (!string.IsNullOrWhiteSpace(paramProjectIds) && !string.IsNullOrWhiteSpace(paramResourceIds)) {
|
---|
51 | string query = string.Format(DeleteByProjectIdsAndResourceIdsQueryString, paramProjectIds, paramResourceIds);
|
---|
52 | DataContext.ExecuteCommand(query);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void DeleteByProjectIds(IEnumerable<Guid> projectIds) {
|
---|
57 | string paramProjectIds = string.Join(",", projectIds.ToList().Select(x => string.Format("'{0}'", x)));
|
---|
58 | if (!string.IsNullOrWhiteSpace(paramProjectIds)) {
|
---|
59 | string query = string.Format(DeleteByProjectIdsQueryString, paramProjectIds);
|
---|
60 | DataContext.ExecuteCommand(query);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public bool CheckProjectGrantedForResources(Guid projectId, IEnumerable<Guid> resourceIds) {
|
---|
65 | string paramResourceIds = string.Join(",", resourceIds.ToList().Select(x => string.Format("'{0}'", x)));
|
---|
66 | if (!string.IsNullOrWhiteSpace(paramResourceIds)) {
|
---|
67 | string queryString = string.Format(CheckProjectGrantedForResourcesQueryString, projectId, paramResourceIds);
|
---|
68 | return DataContext.ExecuteQuery<int>(queryString).Count() == 0;
|
---|
69 | }
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public IEnumerable<Resource> GetAllGrantedResourcesByProjectId(Guid projectId) {
|
---|
74 | return DataContext.ExecuteQuery<Resource>(GetAllGrantedResourcesByProjectIdQueryString, projectId);
|
---|
75 | }
|
---|
76 |
|
---|
77 | public IEnumerable<Guid> GetAllGrantedResourceIdsByProjectId(Guid projectId) {
|
---|
78 | return DataContext.ExecuteQuery<Guid>(GetAllGrantedResourceIdsByProjectIdQueryString, projectId);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public IEnumerable<Guid> GetAllGrantedResourceIdsOfOwnedParentProjects(Guid projectId, Guid userId) {
|
---|
82 | return DataContext.ExecuteQuery<Guid>(GetAllGrantedResourceIdsOfOwnedParentProjectsQueryString, projectId, userId);
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | #region Compiled queries
|
---|
87 | private static readonly Func<DataContext, Guid, IEnumerable<AssignedProjectResource>> GetByProjectIdGetByIdQuery =
|
---|
88 | CompiledQuery.Compile((DataContext db, Guid projectId) =>
|
---|
89 | from projectPermission in db.GetTable<AssignedProjectResource>()
|
---|
90 | where projectPermission.ProjectId == projectId
|
---|
91 | select projectPermission);
|
---|
92 | #endregion
|
---|
93 |
|
---|
94 | #region String queries
|
---|
95 | private const string DeleteByProjectIdAndResourceIdsQueryString = @"
|
---|
96 | DELETE FROM [AssignedProjectResource]
|
---|
97 | WHERE ProjectId = '{0}'
|
---|
98 | AND ResourceId IN ({1});
|
---|
99 | ";
|
---|
100 | private const string DeleteByProjectIdsAndResourceIdsQueryString = @"
|
---|
101 | DELETE FROM [AssignedProjectResource]
|
---|
102 | WHERE ProjectId IN ({0})
|
---|
103 | AND ResourceId IN ({1});
|
---|
104 | ";
|
---|
105 | private const string DeleteByProjectIdsQueryString = @"
|
---|
106 | DELETE FROM [AssignedProjectResource]
|
---|
107 | WHERE ProjectId IN ({0})
|
---|
108 | ";
|
---|
109 | // tested
|
---|
110 | private const string CheckProjectGrantedForResourcesQueryString = @"
|
---|
111 | WITH rtree AS
|
---|
112 | (
|
---|
113 | SELECT ResourceId, ParentResourceId
|
---|
114 | FROM [Resource]
|
---|
115 | UNION ALL
|
---|
116 | SELECT rt.ResourceId, r.ParentResourceId
|
---|
117 | FROM [Resource] r
|
---|
118 | JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
|
---|
119 | )
|
---|
120 | SELECT r.ResourceId
|
---|
121 | FROM [Resource] r
|
---|
122 | WHERE r.ResourceId IN ({1})
|
---|
123 | EXCEPT
|
---|
124 | (
|
---|
125 | SELECT rtree.ResourceId
|
---|
126 | FROM rtree, [AssignedProjectResource] apr
|
---|
127 | WHERE rtree.ParentResourceId = apr.ResourceId
|
---|
128 | AND apr.ProjectId = '{0}'
|
---|
129 | UNION
|
---|
130 | SELECT apr.ResourceId
|
---|
131 | FROM [AssignedProjectResource] apr
|
---|
132 | WHERE apr.ProjectId = '{0}'
|
---|
133 | )
|
---|
134 | ";
|
---|
135 | private const string GetAllGrantedResourcesByProjectIdQueryString = @"
|
---|
136 | WITH rtree AS
|
---|
137 | (
|
---|
138 | SELECT ResourceId, ParentResourceId
|
---|
139 | FROM [Resource]
|
---|
140 | UNION ALL
|
---|
141 | SELECT rt.ResourceId, r.ParentResourceId
|
---|
142 | FROM [Resource] r
|
---|
143 | JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
|
---|
144 | )
|
---|
145 | SELECT res.*
|
---|
146 | FROM rtree, [AssignedProjectResource] apr, [Resource] res
|
---|
147 | WHERE rtree.ParentResourceId = apr.ResourceId
|
---|
148 | AND rtree.ResourceId = res.ResourceId
|
---|
149 | AND apr.ProjectId = '{0}'
|
---|
150 | UNION
|
---|
151 | SELECT res.*
|
---|
152 | FROM [AssignedProjectResource] apr, [Resource] res
|
---|
153 | WHERE apr.ResourceId = res.ResourceId
|
---|
154 | AND apr.ProjectId = '{0}'
|
---|
155 | ";
|
---|
156 | private const string GetAllGrantedResourceIdsByProjectIdQueryString = @"
|
---|
157 | WITH rtree AS
|
---|
158 | (
|
---|
159 | SELECT ResourceId, ParentResourceId
|
---|
160 | FROM [Resource]
|
---|
161 | UNION ALL
|
---|
162 | SELECT rt.ResourceId, r.ParentResourceId
|
---|
163 | FROM [Resource] r
|
---|
164 | JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
|
---|
165 | )
|
---|
166 | SELECT rtree.ResourceId
|
---|
167 | FROM rtree, [AssignedProjectResource] apr
|
---|
168 | WHERE rtree.ParentResourceId = apr.ResourceId
|
---|
169 | AND apr.ProjectId = '{0}'
|
---|
170 | UNION
|
---|
171 | SELECT apr.ResourceId
|
---|
172 | FROM [AssignedProjectResource] apr
|
---|
173 | WHERE apr.ProjectId = '{0}'
|
---|
174 | ";
|
---|
175 | private const string GetAllGrantedResourceIdsOfOwnedParentProjectsQueryString = @"
|
---|
176 | WITH pbranch AS
|
---|
177 | (
|
---|
178 | SELECT ProjectId, ParentProjectId
|
---|
179 | FROM [Project]
|
---|
180 | UNION ALL
|
---|
181 | SELECT pb.ProjectId, p.ParentProjectId
|
---|
182 | FROM [Project] p
|
---|
183 | JOIN pbranch pb ON pb.ParentProjectId = p.ProjectId AND p.ParentProjectId <> p.ProjectId AND pb.ParentProjectId <> pb.ProjectId
|
---|
184 | ),
|
---|
185 | rtree AS
|
---|
186 | (
|
---|
187 | SELECT ResourceId, ParentResourceId
|
---|
188 | FROM [Resource]
|
---|
189 | UNION ALL
|
---|
190 | SELECT rt.ResourceId, r.ParentResourceId
|
---|
191 | FROM [Resource] r
|
---|
192 | JOIN rtree rt ON rt.ParentResourceId = r.ResourceId AND r.ParentResourceId <> r.ResourceId AND rt.ParentResourceId <> rt.ResourceId
|
---|
193 | )
|
---|
194 | SELECT DISTINCT rtree.ResourceId
|
---|
195 | FROM pbranch, rtree, [Project] pro, [AssignedProjectResource] apr
|
---|
196 | WHERE pbranch.ProjectId = {0}
|
---|
197 | AND pbranch.ParentProjectId = pro.ProjectId
|
---|
198 | AND pro.OwnerUserId = {1}
|
---|
199 | AND pbranch.ParentProjectId = apr.ProjectId
|
---|
200 | AND apr.ResourceId = rtree.ParentResourceId
|
---|
201 | ";
|
---|
202 | #endregion
|
---|
203 | }
|
---|
204 | }
|
---|