Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/AssignedJobResourceDao.cs @ 17564

Last change on this file since 17564 was 17059, checked in by mkommend, 5 years ago

#2839: Merged 16117, 16122, 16173, 16184, 16185, 16186, 16187, 16202, 16203, 16204, 16205, 16208, 16211, 16209, 16211, 16219, 16257, 16247 into stable.

File size: 9.0 KB
RevLine 
[15528]1using System;
2using System.Collections.Generic;
3using System.Data.Linq;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7
8namespace HeuristicLab.Services.Hive.DataAccess.Daos {
9  public class AssignedJobResourceDao : GenericDao<Guid, AssignedJobResource> {
10    public AssignedJobResourceDao(DataContext dataContext) : base(dataContext) { }
11
12    public override AssignedJobResource GetById(Guid id) {
13      throw new NotImplementedException();
14    }
15
16    public IQueryable<AssignedJobResource> GetByJobId(Guid jobId) {
17      return Table.Where(x => x.JobId == jobId);
18    }
19
[15552]20    public void DeleteByProjectId(Guid projectId) {
21      DataContext.ExecuteCommand(DeleteByProjectIdQueryString, projectId);
22    }
23
24    public void DeleteByProjectIdAndUserIds(Guid projectId, IEnumerable<Guid> userIds) {
25      string paramUserIds = string.Join(",", userIds.Select(x => string.Format("'{0}'", x)));
26      if (!string.IsNullOrWhiteSpace(paramUserIds)) {
27        string queryString = string.Format(DeleteByProjectIdAndUserIdsQueryString, projectId, paramUserIds);
28        DataContext.ExecuteCommand(queryString);
29      }
30    }
31
[15577]32    public void DeleteByProjectIds(IEnumerable<Guid> projectIds) {
33      string paramProjectIds = string.Join(",", projectIds.Select(x => string.Format("'{0}'", x)));
34      if (!string.IsNullOrWhiteSpace(paramProjectIds)) {
35        string queryString = string.Format(DeleteByProjectIdsQueryString, paramProjectIds);
36        DataContext.ExecuteCommand(queryString);
37      }
38    }
39
[15552]40    public void DeleteByProjectIdsAndUserIds(IEnumerable<Guid> projectIds, IEnumerable<Guid> userIds) {
41      string paramProjectIds = string.Join(",", projectIds.Select(x => string.Format("'{0}'", x)));
[15577]42      string paramUserIds = string.Join(",", userIds.ToList().Select(x => string.Format("'{0}'", x)));
[15552]43      if (!string.IsNullOrWhiteSpace(paramProjectIds) && !string.IsNullOrWhiteSpace(paramUserIds)) {
44        string queryString = string.Format(DeleteByProjectIdsAndUserIdsQueryString, paramProjectIds, paramUserIds);
45        DataContext.ExecuteCommand(queryString);
46      }
47    }
48
49    public void DeleteByProjectIdAndResourceIds(Guid projectId, IEnumerable<Guid> resourceIds) {
50      string paramResourceIds = string.Join(",", resourceIds.Select(x => string.Format("'{0}'", x)));
51      if (!string.IsNullOrWhiteSpace(paramResourceIds)) {
52        string queryString = string.Format(DeleteByProjectIdAndResourceIdsQueryString, projectId, paramResourceIds);
53        DataContext.ExecuteCommand(queryString);
54      }
55    }
56
57    public void DeleteByProjectIdsAndResourceIds(IEnumerable<Guid> projectIds, IEnumerable<Guid> resourceIds) {
58      string paramProjectIds = string.Join(",", projectIds.Select(x => string.Format("'{0}'", x)));
59      string paramResourceIds = string.Join(",", resourceIds.Select(x => string.Format("'{0}'", x)));
60      if (!string.IsNullOrWhiteSpace(paramProjectIds) && !string.IsNullOrWhiteSpace(paramResourceIds)) {
61        string queryString = string.Format(DeleteByProjectIdsAndResourceIdsQueryString, paramProjectIds, paramResourceIds);
62        DataContext.ExecuteCommand(queryString);
63      }
64    }
65
[15528]66    public bool CheckJobGrantedForResource(Guid jobId, Guid resourceId) {
67      return DataContext.ExecuteQuery<int>(CheckJobGrantedForResourceQueryString, jobId, resourceId).First() > 0;
68    }
69
[15530]70    public bool CheckJobGrantedForResources(Guid jobId, IEnumerable<Guid> resourceIds) {
71      string paramResourceIds = string.Join(",", resourceIds.Select(x => string.Format("'{0}'", x)));
72      if (!string.IsNullOrWhiteSpace(paramResourceIds)) {
73        string queryString = string.Format(CheckJobGrantedForResourcesQueryString, jobId, paramResourceIds);
74        return DataContext.ExecuteQuery<int>(queryString).Count() == 0;
75      }
76      return false;
[15528]77    }
78
79    public bool CheckTaskGrantedForResource(Guid taskId, Guid resourceId) {
80      return DataContext.ExecuteQuery<int>(CheckTaskGrantedForResourceQueryString, taskId, resourceId).First() > 0;
81    }
82
83    public IEnumerable<Resource> GetAllGrantedResourcesByJobId(Guid jobId) {
84      return DataContext.ExecuteQuery<Resource>(GetAllGrantedResourcesByJobIdQueryString, jobId);
85    }
86
87    public IEnumerable<Guid> GetAllGrantedResourceIdsByJobId(Guid jobId) {
88      return DataContext.ExecuteQuery<Guid>(GetAllGrantedResourceIdsByJobIdQueryString, jobId);
89    }
90
91    #region String queries
[15552]92    private const string DeleteByProjectIdQueryString = @"
[15577]93      DELETE FROM [AssignedJobResource]
94      WHERE JobId IN
[15552]95        (
96          SELECT j.JobId
97          FROM [Job] j
[15737]98          WHERE j.ProjectId = {0}
[15552]99        )
100    ";
101    private const string DeleteByProjectIdAndUserIdsQueryString = @"
[15577]102      DELETE FROM [AssignedJobResource]
103      WHERE JobId IN
[15552]104        (
105          SELECT j.JobId
106          FROM [Job] j
[15760]107          WHERE j.ProjectId = '{0}'
[15552]108          AND j.OwnerUserId IN ({1})
109        )
110    ";
[15577]111    private const string DeleteByProjectIdsQueryString = @"
112      DELETE FROM [AssignedJobResource]
113      WHERE JobId IN
114        (
115          SELECT j.JobId
116          FROM [Job] j
117          WHERE j.ProjectId IN ({0})
118        )
119    ";
[15552]120    private const string DeleteByProjectIdsAndUserIdsQueryString = @"
[15577]121      DELETE FROM [AssignedJobResource]
122      WHERE JobId IN
[15552]123        (
124          SELECT j.JobId
125          FROM [Job] j
126          WHERE j.ProjectId IN ({0})
127          AND j.OwnerUserId IN ({1})
128        )
129    ";
130    private const string DeleteByProjectIdAndResourceIdsQueryString = @"
[15577]131      DELETE FROM [AssignedJobResource]
132      WHERE JobId IN
[15552]133        (
134          SELECT j.JobId
135          FROM [Job] j
[15577]136          WHERE j.ProjectId = '{0}'
[15552]137        )
[15577]138      AND ResourceId IN ({1})
[15552]139    ";
140    private const string DeleteByProjectIdsAndResourceIdsQueryString = @"
[15577]141      DELETE FROM [AssignedJobResource]
142      WHERE JobId IN
[15552]143        (
144          SELECT j.JobId
145          FROM [Job] j
146          WHERE j.ProjectId IN ({0})
147        )
[15577]148      AND ResourceId IN ({1})
[15552]149    ";
[15528]150    private const string CheckJobGrantedForResourceQueryString = @"
151      WITH rbranch AS (
152        SELECT ResourceId, ParentResourceId
153        FROM [Resource]
[15530]154        WHERE ResourceId = {1}
[15528]155        UNION ALL
156        SELECT r.ResourceId, r.ParentResourceId
157        FROM [Resource] r
158        JOIN rbranch rb ON rb.ParentResourceId = r.ResourceId
159      )
160      SELECT COUNT(ajr.JobId)
[15630]161      FROM rbranch, [AssignedJobResource] ajr
[15528]162      WHERE rbranch.ResourceId = ajr.ResourceId
[15530]163      AND ajr.JobId = {0}
[15528]164    ";
165    private const string CheckTaskGrantedForResourceQueryString = @"
166      WITH rbranch AS (
167        SELECT ResourceId, ParentResourceId
168        FROM [Resource]
[15530]169        WHERE ResourceId = {1}
[15528]170        UNION ALL
171        SELECT r.ResourceId, r.ParentResourceId
172        FROM [Resource] r
173        JOIN rbranch rb ON rb.ParentResourceId = r.ResourceId
174      )
175      SELECT COUNT(ajr.JobId)
[15630]176      FROM rbranch, [AssignedJobResource] ajr, [Task] t
[15528]177      WHERE rbranch.ResourceId = ajr.ResourceId
178      AND ajr.JobId = t.JobId
[15530]179      AND t.JobId = {0}
[15528]180    ";
181    private const string CheckJobGrantedForResourcesQueryString = @"
182      WITH rtree AS
183      (
184        SELECT ResourceId, ParentResourceId
185        FROM [Resource]
186        UNION ALL
187        SELECT rt.ResourceId, r.ParentResourceId
188        FROM [Resource] r
189        JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
190      )
191      SELECT r.ResourceId
192      FROM [Resource] r
193      WHERE r.ResourceId IN ({1})
194      EXCEPT
195      (
196        SELECT rtree.ResourceId
197        FROM rtree, [AssignedJobResource] ajr
198        WHERE rtree.ParentResourceId = ajr.ResourceId
199        AND ajr.JobId = {0}
200        UNION
201        SELECT ajr.ResourceId
202        FROM [AssignedJobResource] ajr
203        WHERE ajr.JobId = {0}
204      )
205    ";
206    private const string GetAllGrantedResourcesByJobIdQueryString = @"
207      WITH rtree AS
208      (
209        SELECT ResourceId, ParentResourceId
210        FROM [Resource]
211        UNION ALL
212        SELECT rt.ResourceId, r.ParentResourceId
213        FROM [Resource] r
214        JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
215      )
216      SELECT res.*
217      FROM rtree, [AssignedJobResource] ajr, [Resource] res
218      WHERE rtree.ParentResourceId = ajr.ResourceId
219      AND rtree.ResourceId = res.ResourceId
220      AND ajr.JobId = {0}
221      UNION
222      SELECT res.*
223      FROM [AssignedJobResource] ajr, [Resource] res
224      WHERE ajr.ResourceId = res.ResourceId
225      AND ajr.JobId = {0}
226    ";
227    private const string GetAllGrantedResourceIdsByJobIdQueryString = @"
228    WITH rtree AS
229    (
230      SELECT ResourceId, ParentResourceId
231      FROM [Resource]
232      UNION ALL
233      SELECT rt.ResourceId, r.ParentResourceId
234      FROM [Resource] r
235      JOIN rtree rt ON rt.ParentResourceId = r.ResourceId
236    )
237    SELECT rtree.ResourceId
238    FROM rtree, [AssignedJobResource] ajr
239    WHERE rtree.ParentResourceId = ajr.ResourceId
240    AND ajr.JobId = {0}
241    UNION
242    SELECT ajr.ResourceId
243    FROM [AssignedJobResource] ajr
244    WHERE ajr.JobId = {0}
245    ";
246    #endregion
247  }
248}
Note: See TracBrowser for help on using the repository browser.