Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/AssignedProjectResourceDao.cs @ 17928

Last change on this file since 17928 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 9.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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    private const string CheckProjectGrantedForResourcesQueryString = @"
119    WITH rtree AS
120    (
121      SELECT ResourceId, ParentResourceId
122      FROM [Resource]
123      UNION ALL
124      SELECT rt.ResourceId, r.ParentResourceId
125      FROM [Resource] r
126      JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
127    )
128    SELECT r.ResourceId
129    FROM [Resource] r
130    WHERE r.ResourceId IN ({1})
131    EXCEPT
132    (
133      SELECT rtree.ResourceId
134      FROM rtree, [AssignedProjectResource] apr
135      WHERE rtree.ParentResourceId = apr.ResourceId
136      AND apr.ProjectId = '{0}'
137      UNION
138      SELECT apr.ResourceId
139      FROM [AssignedProjectResource] apr
140      WHERE apr.ProjectId = '{0}'
141    )
142    ";
143    private const string GetAllGrantedResourcesByProjectIdQueryString = @"
144      WITH rtree AS
145      (
146        SELECT ResourceId, ParentResourceId
147        FROM [Resource]
148        UNION ALL
149        SELECT rt.ResourceId, r.ParentResourceId
150        FROM [Resource] r
151        JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
152      )
153      SELECT res.*
154      FROM rtree, [AssignedProjectResource] apr, [Resource] res
155      WHERE rtree.ParentResourceId = apr.ResourceId
156      AND rtree.ResourceId = res.ResourceId
157      AND apr.ProjectId = '{0}'
158      UNION
159      SELECT res.*
160      FROM [AssignedProjectResource] apr, [Resource] res
161      WHERE apr.ResourceId = res.ResourceId
162      AND apr.ProjectId = '{0}'
163    ";
164    private const string GetAllGrantedResourceIdsByProjectIdQueryString = @"
165    WITH rtree AS
166    (
167      SELECT ResourceId, ParentResourceId
168      FROM [Resource]
169      UNION ALL
170      SELECT rt.ResourceId, r.ParentResourceId
171      FROM [Resource] r
172      JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
173    )
174    SELECT rtree.ResourceId
175    FROM rtree, [AssignedProjectResource] apr
176    WHERE rtree.ParentResourceId = apr.ResourceId
177    AND apr.ProjectId = '{0}'
178    UNION
179    SELECT apr.ResourceId
180    FROM [AssignedProjectResource] apr
181    WHERE apr.ProjectId = '{0}'
182    ";
183    private const string GetAllGrantedResourcesByProjectIdsQueryString = @"
184      WITH 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
192      )
193      SELECT res.*
194      FROM rtree, [AssignedProjectResource] apr, [Resource] res
195      WHERE rtree.ParentResourceId = apr.ResourceId
196      AND rtree.ResourceId = res.ResourceId
197      AND apr.ProjectId IN ({0})
198      UNION
199      SELECT res.*
200      FROM [AssignedProjectResource] apr, [Resource] res
201      WHERE apr.ResourceId = res.ResourceId
202      AND apr.ProjectId IN ({0})
203    ";
204    private const string GetAllGrantedResourceIdsOfOwnedParentProjectsQueryString = @"
205      WITH pbranch AS
206      (
207        SELECT ProjectId, ParentProjectId
208        FROM [Project]
209        UNION ALL
210        SELECT pb.ProjectId, p.ParentProjectId
211        FROM [Project] p
212        JOIN pbranch pb ON pb.ParentProjectId = p.ProjectId AND p.ParentProjectId <> p.ProjectId AND pb.ParentProjectId <> pb.ProjectId
213      ),
214      rtree AS
215      (
216        SELECT ResourceId, ParentResourceId
217        FROM [Resource]
218        UNION ALL
219        SELECT rt.ResourceId, r.ParentResourceId
220        FROM [Resource] r
221        JOIN rtree rt ON rt.ParentResourceId = r.ResourceId AND r.ParentResourceId <> r.ResourceId AND rt.ParentResourceId <> rt.ResourceId
222      )
223      SELECT DISTINCT rtree.ResourceId
224      FROM pbranch, rtree, [Project] pro, [AssignedProjectResource] apr
225      WHERE pbranch.ProjectId = {0}
226      AND pbranch.ParentProjectId = pro.ProjectId
227      AND pro.OwnerUserId = {1}
228      AND pbranch.ParentProjectId = apr.ProjectId
229      AND apr.ResourceId = rtree.ParentResourceId
230    ";
231    #endregion
232  }
233}
Note: See TracBrowser for help on using the repository browser.