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

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

#2839 added checks for the administration of project-resource assignments

File size: 6.6 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 DeleteByProjectAndGrantedUserId(Guid projectId, IEnumerable<Guid> resourceIds) {
40      string paramIds = string.Join(",", resourceIds.Select(x => string.Format("'{0}'", x)));
41      if (!string.IsNullOrWhiteSpace(paramIds)) {
42        string query = string.Format(DeleteByGrantedUserQuery, projectId, paramIds);
43        DataContext.ExecuteCommand(query);
44      }
45    }
46
47    public bool CheckProjectGrantedForResources(Guid projectId, IEnumerable<Guid> resourceIds) {
48      string paramResourceIds = string.Join(",", resourceIds.Select(x => string.Format("'{0}'", x)));
49      if (!string.IsNullOrWhiteSpace(paramResourceIds)) {
50        string queryString = string.Format(CheckProjectGrantedForResourcesQueryString, projectId, paramResourceIds);
51        return DataContext.ExecuteQuery<int>(queryString).Count() == 0;
52      }
53      return false;
54    }
55
56    public IEnumerable<Resource> GetAllGrantedResourcesByProjectId(Guid projectId) {
57      return DataContext.ExecuteQuery<Resource>(GetAllGrantedResourcesByProjectIdQueryString, projectId);
58    }
59
60    public IEnumerable<Guid> GetAllGrantedResourceIdsByProjectId(Guid projectId) {
61      return DataContext.ExecuteQuery<Guid>(GetAllGrantedResourceIdsByProjectIdQueryString, projectId);
62    }
63
64    public IEnumerable<Guid> GetAllGrantedResourceIdsOfOwnedParentProjects(Guid projectId, Guid userId) {
65      return DataContext.ExecuteQuery<Guid>(GetAllGrantedResourceIdsOfOwnedParentProjectsQueryString, projectId, userId);
66    }
67
68
69    #region Compiled queries
70    private static readonly Func<DataContext, Guid, IEnumerable<AssignedProjectResource>> GetByProjectIdGetByIdQuery =
71      CompiledQuery.Compile((DataContext db, Guid projectId) =>
72        from projectPermission in db.GetTable<AssignedProjectResource>()
73        where projectPermission.ProjectId == projectId
74        select projectPermission);
75    #endregion
76
77    #region String queries
78    private const string DeleteByGrantedUserQuery =
79      @"DELETE FROM [AssignedProjectResource]
80         WHERE ProjectId = '{0}'
81           AND ResourceId IN ({1});";
82    private const string CheckProjectGrantedForResourcesQueryString = @"
83    WITH rtree AS
84    (
85      SELECT ResourceId, ParentResourceId
86      FROM [Resource]
87      UNION ALL
88      SELECT rt.ResourceId, r.ParentResourceId
89      FROM [Resource] r
90      JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
91    )
92    SELECT r.ResourceId
93    FROM [Resource] r
94    WHERE r.ResourceId IN ({1})
95    EXCEPT
96    (
97      SELECT rtree.ResourceId
98      FROM rtree, [AssignedProjectResource] apr
99      WHERE rtree.ParentResourceId = apr.ResourceId
100      AND apr.ProjectId = {0}
101      UNION
102      SELECT apr.ResourceId
103      FROM [AssignedProjectResource] apr
104      WHERE apr.ProjectId = {0}
105    )
106    ";
107    private const string GetAllGrantedResourcesByProjectIdQueryString = @"
108      WITH rtree AS
109      (
110        SELECT ResourceId, ParentResourceId
111        FROM [Resource]
112        UNION ALL
113        SELECT rt.ResourceId, r.ParentResourceId
114        FROM [Resource] r
115        JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
116      )
117      SELECT res.*
118      FROM rtree, [AssignedProjectResource] apr, [Resource] res
119      WHERE rtree.ParentResourceId = apr.ResourceId
120      AND rtree.ResourceId = res.ResourceId
121      AND apr.ProjectId = {0}
122      UNION
123      SELECT res.*
124      FROM [AssignedProjectResource] apr, [Resource] res
125      WHERE apr.ResourceId = res.ResourceId
126      AND apr.ProjectId = {0}
127    ";
128    private const string GetAllGrantedResourceIdsByProjectIdQueryString = @"
129    WITH rtree AS
130    (
131      SELECT ResourceId, ParentResourceId
132      FROM [Resource]
133      UNION ALL
134      SELECT rt.ResourceId, r.ParentResourceId
135      FROM [Resource] r
136      JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
137    )
138    SELECT rtree.ResourceId
139    FROM rtree, [AssignedProjectResource] apr
140    WHERE rtree.ParentResourceId = apr.ResourceId
141    AND apr.ProjectId = {0}
142    UNION
143    SELECT apr.ResourceId
144    FROM [AssignedProjectResource] apr
145    WHERE apr.ProjectId = {0}
146    ";
147    private const string GetAllGrantedResourceIdsOfOwnedParentProjectsQueryString = @"
148      WITH pbranch AS
149      (
150        SELECT ProjectId, ParentProjectId
151        FROM [Project]
152        UNION ALL
153        SELECT pb.ProjectId, p.ParentProjectId
154        FROM [Project] p
155        JOIN pbranch pb ON pb.ParentProjectId = p.ProjectId AND p.ParentProjectId <> p.ProjectId AND pb.ParentProjectId <> pb.ProjectId
156      ),
157      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 AND r.ParentResourceId <> r.ResourceId AND rt.ParentResourceId <> rt.ResourceId
165      )
166      SELECT DISTINCT rtree.ResourceId
167      FROM pbranch, rtree, [Project] pro, [AssignedProjectResource] apr
168      WHERE pbranch.ProjectId = {0}
169      AND pbranch.ParentProjectId = pro.ProjectId
170      AND pro.OwnerUserId = {1}
171      AND pbranch.ParentProjectId = apr.ProjectId
172      AND apr.ResourceId = rtree.ParentResourceId
173    ";
174    #endregion
175  }
176}
Note: See TracBrowser for help on using the repository browser.