Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15530 was 15530, checked in by jzenisek, 6 years ago

#2839

  • worked on Job operations add&update
  • worked on ProjectPermission handling
  • worked on Project-Resource assignment
File size: 5.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 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    #region Compiled queries
65    private static readonly Func<DataContext, Guid, IEnumerable<AssignedProjectResource>> GetByProjectIdGetByIdQuery =
66      CompiledQuery.Compile((DataContext db, Guid projectId) =>
67        from projectPermission in db.GetTable<AssignedProjectResource>()
68        where projectPermission.ProjectId == projectId
69        select projectPermission);
70    #endregion
71
72    #region String queries
73    private const string DeleteByGrantedUserQuery =
74      @"DELETE FROM [AssignedProjectResource]
75         WHERE ProjectId = '{0}'
76           AND ResourceId IN ({1});";
77    private const string CheckProjectGrantedForResourcesQueryString = @"
78    WITH rtree AS
79    (
80      SELECT ResourceId, ParentResourceId
81      FROM [Resource]
82      UNION ALL
83      SELECT rt.ResourceId, r.ParentResourceId
84      FROM [Resource] r
85      JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
86    )
87    SELECT r.ResourceId
88    FROM [Resource] r
89    WHERE r.ResourceId IN ({1})
90    EXCEPT
91    (
92      SELECT rtree.ResourceId
93      FROM rtree, [AssignedProjectResource] apr
94      WHERE rtree.ParentResourceId = apr.ResourceId
95      AND apr.ProjectId = {0}
96      UNION
97      SELECT apr.ResourceId
98      FROM [AssignedProjectResource] apr
99      WHERE apr.ProjectId = {0}
100    )
101    ";
102    private const string GetAllGrantedResourcesByProjectIdQueryString = @"
103      WITH rtree AS
104      (
105        SELECT ResourceId, ParentResourceId
106        FROM [Resource]
107        UNION ALL
108        SELECT rt.ResourceId, r.ParentResourceId
109        FROM [Resource] r
110        JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
111      )
112      SELECT res.*
113      FROM rtree, [AssignedProjectResource] apr, [Resource] res
114      WHERE rtree.ParentResourceId = apr.ResourceId
115      AND rtree.ResourceId = res.ResourceId
116      AND apr.ProjectId = {0}
117      UNION
118      SELECT res.*
119      FROM [AssignedProjectResource] apr, [Resource] res
120      WHERE apr.ResourceId = res.ResourceId
121      AND apr.ProjectId = {0}
122    ";
123    private const string GetAllGrantedResourceIdsByProjectIdQueryString = @"
124    WITH rtree AS
125    (
126      SELECT ResourceId, ParentResourceId
127      FROM [Resource]
128      UNION ALL
129      SELECT rt.ResourceId, r.ParentResourceId
130      FROM [Resource] r
131      JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
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    #endregion
143  }
144}
Note: See TracBrowser for help on using the repository browser.