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

Last change on this file since 15577 was 15577, checked in by jzenisek, 4 years ago

#2839 worked on service side mgmt of project-resource assignments and project-user permissions

File size: 7.9 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<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    private const string CheckProjectGrantedForResourcesQueryString = @"
110    WITH rtree AS
111    (
112      SELECT ResourceId, ParentResourceId
113      FROM [Resource]
114      UNION ALL
115      SELECT rt.ResourceId, r.ParentResourceId
116      FROM [Resource] r
117      JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
118    )
119    SELECT r.ResourceId
120    FROM [Resource] r
121    WHERE r.ResourceId IN ({1})
122    EXCEPT
123    (
124      SELECT rtree.ResourceId
125      FROM rtree, [AssignedProjectResource] apr
126      WHERE rtree.ParentResourceId = apr.ResourceId
127      AND apr.ProjectId = {0}
128      UNION
129      SELECT apr.ResourceId
130      FROM [AssignedProjectResource] apr
131      WHERE apr.ProjectId = {0}
132    )
133    ";
134    private const string GetAllGrantedResourcesByProjectIdQueryString = @"
135      WITH rtree AS
136      (
137        SELECT ResourceId, ParentResourceId
138        FROM [Resource]
139        UNION ALL
140        SELECT rt.ResourceId, r.ParentResourceId
141        FROM [Resource] r
142        JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
143      )
144      SELECT res.*
145      FROM rtree, [AssignedProjectResource] apr, [Resource] res
146      WHERE rtree.ParentResourceId = apr.ResourceId
147      AND rtree.ResourceId = res.ResourceId
148      AND apr.ProjectId = {0}
149      UNION
150      SELECT res.*
151      FROM [AssignedProjectResource] apr, [Resource] res
152      WHERE apr.ResourceId = res.ResourceId
153      AND apr.ProjectId = {0}
154    ";
155    private const string GetAllGrantedResourceIdsByProjectIdQueryString = @"
156    WITH rtree AS
157    (
158      SELECT ResourceId, ParentResourceId
159      FROM [Resource]
160      UNION ALL
161      SELECT rt.ResourceId, r.ParentResourceId
162      FROM [Resource] r
163      JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
164    )
165    SELECT rtree.ResourceId
166    FROM rtree, [AssignedProjectResource] apr
167    WHERE rtree.ParentResourceId = apr.ResourceId
168    AND apr.ProjectId = {0}
169    UNION
170    SELECT apr.ResourceId
171    FROM [AssignedProjectResource] apr
172    WHERE apr.ProjectId = {0}
173    ";
174    private const string GetAllGrantedResourceIdsOfOwnedParentProjectsQueryString = @"
175      WITH pbranch AS
176      (
177        SELECT ProjectId, ParentProjectId
178        FROM [Project]
179        UNION ALL
180        SELECT pb.ProjectId, p.ParentProjectId
181        FROM [Project] p
182        JOIN pbranch pb ON pb.ParentProjectId = p.ProjectId AND p.ParentProjectId <> p.ProjectId AND pb.ParentProjectId <> pb.ProjectId
183      ),
184      rtree AS
185      (
186        SELECT ResourceId, ParentResourceId
187        FROM [Resource]
188        UNION ALL
189        SELECT rt.ResourceId, r.ParentResourceId
190        FROM [Resource] r
191        JOIN rtree rt ON rt.ParentResourceId = r.ResourceId AND r.ParentResourceId <> r.ResourceId AND rt.ParentResourceId <> rt.ResourceId
192      )
193      SELECT DISTINCT rtree.ResourceId
194      FROM pbranch, rtree, [Project] pro, [AssignedProjectResource] apr
195      WHERE pbranch.ProjectId = {0}
196      AND pbranch.ParentProjectId = pro.ProjectId
197      AND pro.OwnerUserId = {1}
198      AND pbranch.ParentProjectId = apr.ProjectId
199      AND apr.ResourceId = rtree.ParentResourceId
200    ";
201    #endregion
202  }
203}
Note: See TracBrowser for help on using the repository browser.