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

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

#2839 worked on ProjectResource assignment: (cascading) assigning & unassigning

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