source: branches/HiveProjectManagement/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/AssignedProjectResourceDao.cs @ 15658

Last change on this file since 15658 was 15658, checked in by jzenisek, 3 years ago

#2839

  • worked on (restricted) accessibility of hive's administration area for non-admin roles
  • adapted HiveClient & HiveAdminClient entity loading (client- & service-side)
File size: 9.2 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Data.Linq;
25using System.Linq;
26
27namespace 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<Resource> GetAllGrantedResourcesByProjectIds(IEnumerable<Guid> projectIds) {
82      string paramProjectIds = string.Join(",", projectIds.ToList().Select(x => string.Format("'{0}'", x)));
83      if (!string.IsNullOrWhiteSpace(paramProjectIds)) {
84        string queryString = string.Format(GetAllGrantedResourcesByProjectIdsQueryString, paramProjectIds);
85        return DataContext.ExecuteQuery<Resource>(queryString);
86      }
87      return Enumerable.Empty<Resource>();
88    }
89
90    public IEnumerable<Guid> GetAllGrantedResourceIdsOfOwnedParentProjects(Guid projectId, Guid userId) {
91      return DataContext.ExecuteQuery<Guid>(GetAllGrantedResourceIdsOfOwnedParentProjectsQueryString, projectId, userId);
92    }
93
94
95    #region Compiled queries
96    private static readonly Func<DataContext, Guid, IEnumerable<AssignedProjectResource>> GetByProjectIdGetByIdQuery =
97      CompiledQuery.Compile((DataContext db, Guid projectId) =>
98        from projectPermission in db.GetTable<AssignedProjectResource>()
99        where projectPermission.ProjectId == projectId
100        select projectPermission);
101    #endregion
102
103    #region String queries
104    private const string DeleteByProjectIdAndResourceIdsQueryString = @"
105      DELETE FROM [AssignedProjectResource]
106        WHERE ProjectId = '{0}'
107        AND ResourceId IN ({1});
108    ";
109    private const string DeleteByProjectIdsAndResourceIdsQueryString = @"
110      DELETE FROM [AssignedProjectResource]
111        WHERE ProjectId IN ({0})
112        AND ResourceId IN ({1});
113    ";
114    private const string DeleteByProjectIdsQueryString = @"
115      DELETE FROM [AssignedProjectResource]
116        WHERE ProjectId IN ({0})
117    ";
118    // tested
119    private const string CheckProjectGrantedForResourcesQueryString = @"
120    WITH rtree AS
121    (
122      SELECT ResourceId, ParentResourceId
123      FROM [Resource]
124      UNION ALL
125      SELECT rt.ResourceId, r.ParentResourceId
126      FROM [Resource] r
127      JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
128    )
129    SELECT r.ResourceId
130    FROM [Resource] r
131    WHERE r.ResourceId IN ({1})
132    EXCEPT
133    (
134      SELECT rtree.ResourceId
135      FROM rtree, [AssignedProjectResource] apr
136      WHERE rtree.ParentResourceId = apr.ResourceId
137      AND apr.ProjectId = '{0}'
138      UNION
139      SELECT apr.ResourceId
140      FROM [AssignedProjectResource] apr
141      WHERE apr.ProjectId = '{0}'
142    )
143    ";
144    private const string GetAllGrantedResourcesByProjectIdQueryString = @"
145      WITH rtree AS
146      (
147        SELECT ResourceId, ParentResourceId
148        FROM [Resource]
149        UNION ALL
150        SELECT rt.ResourceId, r.ParentResourceId
151        FROM [Resource] r
152        JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
153      )
154      SELECT res.*
155      FROM rtree, [AssignedProjectResource] apr, [Resource] res
156      WHERE rtree.ParentResourceId = apr.ResourceId
157      AND rtree.ResourceId = res.ResourceId
158      AND apr.ProjectId = '{0}'
159      UNION
160      SELECT res.*
161      FROM [AssignedProjectResource] apr, [Resource] res
162      WHERE apr.ResourceId = res.ResourceId
163      AND apr.ProjectId = '{0}'
164    ";
165    private const string GetAllGrantedResourceIdsByProjectIdQueryString = @"
166    WITH rtree AS
167    (
168      SELECT ResourceId, ParentResourceId
169      FROM [Resource]
170      UNION ALL
171      SELECT rt.ResourceId, r.ParentResourceId
172      FROM [Resource] r
173      JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
174    )
175    SELECT rtree.ResourceId
176    FROM rtree, [AssignedProjectResource] apr
177    WHERE rtree.ParentResourceId = apr.ResourceId
178    AND apr.ProjectId = '{0}'
179    UNION
180    SELECT apr.ResourceId
181    FROM [AssignedProjectResource] apr
182    WHERE apr.ProjectId = '{0}'
183    ";
184    private const string GetAllGrantedResourcesByProjectIdsQueryString = @"
185      WITH 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
193      )
194      SELECT res.*
195      FROM rtree, [AssignedProjectResource] apr, [Resource] res
196      WHERE rtree.ParentResourceId = apr.ResourceId
197      AND rtree.ResourceId = res.ResourceId
198      AND apr.ProjectId IN ({0})
199      UNION
200      SELECT res.*
201      FROM [AssignedProjectResource] apr, [Resource] res
202      WHERE apr.ResourceId = res.ResourceId
203      AND apr.ProjectId IN ({0})
204    ";
205    private const string GetAllGrantedResourceIdsOfOwnedParentProjectsQueryString = @"
206      WITH pbranch AS
207      (
208        SELECT ProjectId, ParentProjectId
209        FROM [Project]
210        UNION ALL
211        SELECT pb.ProjectId, p.ParentProjectId
212        FROM [Project] p
213        JOIN pbranch pb ON pb.ParentProjectId = p.ProjectId AND p.ParentProjectId <> p.ProjectId AND pb.ParentProjectId <> pb.ProjectId
214      ),
215      rtree AS
216      (
217        SELECT ResourceId, ParentResourceId
218        FROM [Resource]
219        UNION ALL
220        SELECT rt.ResourceId, r.ParentResourceId
221        FROM [Resource] r
222        JOIN rtree rt ON rt.ParentResourceId = r.ResourceId AND r.ParentResourceId <> r.ResourceId AND rt.ParentResourceId <> rt.ResourceId
223      )
224      SELECT DISTINCT rtree.ResourceId
225      FROM pbranch, rtree, [Project] pro, [AssignedProjectResource] apr
226      WHERE pbranch.ProjectId = {0}
227      AND pbranch.ParentProjectId = pro.ProjectId
228      AND pro.OwnerUserId = {1}
229      AND pbranch.ParentProjectId = apr.ProjectId
230      AND apr.ResourceId = rtree.ParentResourceId
231    ";
232    #endregion
233  }
234}
Note: See TracBrowser for help on using the repository browser.