source: branches/HiveProjectManagement/HeuristicLab.Services.Hive/3.3/HiveService.cs @ 15547

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

#2839 worked on ProjectResource assignment: (cascading) assigning & unassigning (reverted changes of config files from r15546)

File size: 48.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Linq;
25using System.Security;
26using System.ServiceModel;
27using HeuristicLab.Services.Access;
28using HeuristicLab.Services.Hive.DataAccess.Interfaces;
29using HeuristicLab.Services.Hive.DataTransfer;
30using HeuristicLab.Services.Hive.Manager;
31using HeuristicLab.Services.Hive.ServiceContracts;
32using DA = HeuristicLab.Services.Hive.DataAccess;
33using DT = HeuristicLab.Services.Hive.DataTransfer;
34
35namespace HeuristicLab.Services.Hive {
36  /// <summary>
37  /// Implementation of the Hive service (interface <see cref="IHiveService"/>).
38  /// We need 'IgnoreExtensionDataObject' Attribute for the slave to work.
39  /// </summary>
40  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IgnoreExtensionDataObject = true)]
41  [HiveOperationContextBehavior]
42  public class HiveService : IHiveService {
43    private const string NOT_AUTHORIZED_PROJECTRESOURCE = "Selected project is not authorized to access the requested resource";
44    private const string NOT_AUTHORIZED_USERPROJECT = "Current user is not authorized to access the requested project";
45
46    private static readonly DA.TaskState[] CompletedStates = { DA.TaskState.Finished, DA.TaskState.Aborted, DA.TaskState.Failed };
47
48    private IPersistenceManager PersistenceManager {
49      get { return ServiceLocator.Instance.PersistenceManager; }
50    }
51
52    private IUserManager UserManager {
53      get { return ServiceLocator.Instance.UserManager; }
54    }
55
56    private IRoleVerifier RoleVerifier {
57      get { return ServiceLocator.Instance.RoleVerifier; }
58    }
59
60    private IAuthorizationManager AuthorizationManager {
61      get { return ServiceLocator.Instance.AuthorizationManager; }
62    }
63    private IEventManager EventManager {
64      get { return ServiceLocator.Instance.EventManager; }
65    }
66    private HeartbeatManager HeartbeatManager {
67      get { return ServiceLocator.Instance.HeartbeatManager; }
68    }
69
70    #region Task Methods
71    public Guid AddTask(DT.Task task, DT.TaskData taskData, IEnumerable<Guid> resourceIds) {
72      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
73      var pm = PersistenceManager;
74      using (new PerformanceLogger("AddTask")) {
75        var taskDao = pm.TaskDao;
76        var stateLogDao = pm.StateLogDao;
77
78        var newTask = task.ToEntity();
79        newTask.JobData = taskData.ToEntity();
80        newTask.JobData.LastUpdate = DateTime.Now;
81        newTask.AssignedTaskResources.AddRange(resourceIds.Select(
82          x => new DA.AssignedTaskResource {
83            ResourceId = x
84          }));
85        newTask.State = DA.TaskState.Waiting;
86        return pm.UseTransaction(() => {
87          taskDao.Save(newTask);
88          pm.SubmitChanges();
89          stateLogDao.Save(new DA.StateLog {
90            State = DA.TaskState.Waiting,
91            DateTime = DateTime.Now,
92            TaskId = newTask.TaskId,
93            UserId = UserManager.CurrentUserId,
94            SlaveId = null,
95            Exception = null
96          });
97          pm.SubmitChanges();
98          return newTask.TaskId;
99        }, false, true);
100      }
101    }
102
103    public Guid AddTask(DT.Task task, DT.TaskData taskData) {
104      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
105      var pm = PersistenceManager;
106      using (new PerformanceLogger("AddTask")) {
107        var taskDao = pm.TaskDao;
108        var stateLogDao = pm.StateLogDao;
109        var newTask = task.ToEntity();
110        newTask.JobData = taskData.ToEntity();
111        newTask.JobData.LastUpdate = DateTime.Now;
112        newTask.State = DA.TaskState.Waiting;
113        return pm.UseTransaction(() => {
114          taskDao.Save(newTask);
115          pm.SubmitChanges();
116          stateLogDao.Save(new DA.StateLog {
117            State = DA.TaskState.Waiting,
118            DateTime = DateTime.Now,
119            TaskId = newTask.TaskId,
120            UserId = UserManager.CurrentUserId,
121            SlaveId = null,
122            Exception = null
123          });
124          pm.SubmitChanges();
125          return newTask.TaskId;
126        }, false, true);
127      }
128    }
129
130    public Guid AddChildTask(Guid parentTaskId, DT.Task task, DT.TaskData taskData) {
131      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
132      task.ParentTaskId = parentTaskId;
133      return AddTask(task, taskData);
134    }
135
136    public DT.Task GetTask(Guid taskId) {
137      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
138      AuthorizationManager.AuthorizeForTask(taskId, Permission.Read);
139      var pm = PersistenceManager;
140      using (new PerformanceLogger("GetTask")) {
141        var taskDao = pm.TaskDao;
142        return pm.UseTransaction(() => {
143          var task = taskDao.GetById(taskId);
144          return task.ToDto();
145        });
146      }
147    }
148
149    public IEnumerable<DT.LightweightTask> GetLightweightJobTasks(Guid jobId) {
150      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
151      AuthorizationManager.AuthorizeForJob(jobId, Permission.Read);
152      var pm = PersistenceManager;
153      using (new PerformanceLogger("GetLightweightJobTasks")) {
154        var taskDao = pm.TaskDao;
155        return pm.UseTransaction(() => {
156          return taskDao.GetByJobId(jobId)
157            .ToList()
158            .Select(x => new DT.LightweightTask {
159              Id = x.TaskId,
160              ExecutionTime = TimeSpan.FromMilliseconds(x.ExecutionTimeMs),
161              ParentTaskId = x.ParentTaskId,
162              StateLog = x.StateLogs.OrderBy(y => y.DateTime)
163                                    .Select(z => z.ToDto())
164                                    .ToList(),
165              State = x.State.ToDto(),
166              Command = x.Command.ToDto(),
167              LastTaskDataUpdate = x.JobData.LastUpdate
168            })
169            .ToList();
170        }, false, true);
171      }
172    }
173
174    public IEnumerable<DT.LightweightTask> GetLightweightJobTasksWithoutStateLog(Guid jobId) {
175      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
176      AuthorizationManager.AuthorizeForJob(jobId, Permission.Read);
177      var pm = PersistenceManager;
178      using (new PerformanceLogger("GetLightweightJobTasksWithoutStateLog")) {
179        var taskDao = pm.TaskDao;
180        return pm.UseTransaction(() => {
181          return taskDao.GetByJobId(jobId)
182            .ToList()
183            .Select(x => new DT.LightweightTask {
184              Id = x.TaskId,
185              ExecutionTime = TimeSpan.FromMilliseconds(x.ExecutionTimeMs),
186              ParentTaskId = x.ParentTaskId,
187              StateLog = new List<DT.StateLog>(),
188              State = x.State.ToDto(),
189              Command = x.Command.ToDto(),
190              LastTaskDataUpdate = x.JobData.LastUpdate
191            })
192            .ToList();
193        }, false, true);
194      }
195    }
196
197    public DT.TaskData GetTaskData(Guid taskId) {
198      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
199      AuthorizationManager.AuthorizeForTask(taskId, Permission.Read);
200      var pm = PersistenceManager;
201      using (new PerformanceLogger("GetTaskData")) {
202        var taskDataDao = pm.TaskDataDao;
203        return pm.UseTransaction(() => taskDataDao.GetById(taskId).ToDto());
204      }
205    }
206
207    public void UpdateTask(DT.Task taskDto) {
208      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
209      AuthorizationManager.AuthorizeForTask(taskDto.Id, Permission.Full);
210      var pm = PersistenceManager;
211      using (new PerformanceLogger("UpdateTask")) {
212        var taskDao = pm.TaskDao;
213        pm.UseTransaction(() => {
214          var task = taskDao.GetById(taskDto.Id);
215          taskDto.CopyToEntity(task);
216          pm.SubmitChanges();
217        });
218      }
219    }
220
221    public void UpdateTaskData(DT.Task taskDto, DT.TaskData taskDataDto) {
222      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
223      AuthorizationManager.AuthorizeForTask(taskDto.Id, Permission.Full);
224      var pm = PersistenceManager;
225      using (new PerformanceLogger("UpdateTaskData")) {
226        var taskDao = pm.TaskDao;
227        var taskDataDao = pm.TaskDataDao;
228        pm.UseTransaction(() => {
229          var task = taskDao.GetById(taskDto.Id);
230          var taskData = taskDataDao.GetById(taskDataDto.TaskId);
231          taskDto.CopyToEntity(task);
232          taskDataDto.CopyToEntity(taskData);
233          taskData.LastUpdate = DateTime.Now;
234          pm.SubmitChanges();
235        });
236      }
237    }
238
239    public DT.Task UpdateTaskState(Guid taskId, DT.TaskState taskState, Guid? slaveId, Guid? userId, string exception) {
240      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
241      AuthorizationManager.AuthorizeForTask(taskId, Permission.Full);
242      var pm = PersistenceManager;
243      using (new PerformanceLogger("UpdateTaskState")) {
244        var taskDao = pm.TaskDao;
245        return pm.UseTransaction(() => {
246          var task = taskDao.GetById(taskId);
247          UpdateTaskState(pm, task, taskState, slaveId, userId, exception);
248          pm.SubmitChanges();
249          return task.ToDto();
250        });
251      }
252    }
253    #endregion
254
255    #region Task Control Methods
256    public void StopTask(Guid taskId) {
257      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
258      AuthorizationManager.AuthorizeForTask(taskId, Permission.Full);
259      var pm = PersistenceManager;
260      using (new PerformanceLogger("StopTask")) {
261        var taskDao = pm.TaskDao;
262        pm.UseTransaction(() => {
263          var task = taskDao.GetById(taskId);
264          if (task.State == DA.TaskState.Calculating || task.State == DA.TaskState.Transferring) {
265            task.Command = DA.Command.Stop;
266          } else if (task.State != DA.TaskState.Aborted
267                     && task.State != DA.TaskState.Finished
268                     && task.State != DA.TaskState.Failed) {
269            UpdateTaskState(pm, task, DT.TaskState.Aborted, null, null, string.Empty);
270          }
271          pm.SubmitChanges();
272        });
273      }
274    }
275
276    public void PauseTask(Guid taskId) {
277      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
278      AuthorizationManager.AuthorizeForTask(taskId, Permission.Full);
279      var pm = PersistenceManager;
280      using (new PerformanceLogger("PauseTask")) {
281        var taskDao = pm.TaskDao;
282        pm.UseTransaction(() => {
283          var task = taskDao.GetById(taskId);
284          if (task.State == DA.TaskState.Calculating || task.State == DA.TaskState.Transferring) {
285            task.Command = DA.Command.Pause;
286          } else if (task.State != DA.TaskState.Paused
287                     && task.State != DA.TaskState.Aborted
288                     && task.State != DA.TaskState.Finished
289                     && task.State != DA.TaskState.Failed) {
290            UpdateTaskState(pm, task, DT.TaskState.Paused, null, null, string.Empty);
291          }
292          pm.SubmitChanges();
293        });
294      }
295    }
296
297    public void RestartTask(Guid taskId) {
298      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
299      AuthorizationManager.AuthorizeForTask(taskId, Permission.Full);
300      var pm = PersistenceManager;
301      using (new PerformanceLogger("RestartTask")) {
302        var taskDao = pm.TaskDao;
303        pm.UseTransaction(() => {
304          var task = taskDao.GetById(taskId);
305          task.Command = null;
306          UpdateTaskState(pm, task, DT.TaskState.Waiting, null, UserManager.CurrentUserId, string.Empty);
307          pm.SubmitChanges();
308        });
309      }
310    }
311    #endregion
312
313    #region Job Methods
314    public DT.Job GetJob(Guid id) {
315      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
316      AuthorizationManager.AuthorizeForJob(id, DT.Permission.Read);
317      var pm = PersistenceManager;
318      using (new PerformanceLogger("GetJob")) {
319        var jobDao = pm.JobDao;
320        var jobPermissionDao = pm.JobPermissionDao;
321        var taskDao = pm.TaskDao;
322        var currentUserId = UserManager.CurrentUserId;
323        return pm.UseTransaction(() => {
324          var job = jobDao.GetById(id).ToDto();
325          if (job != null) {
326            var statistics = taskDao.GetByJobId(job.Id)
327              .GroupBy(x => x.JobId)
328              .Select(x => new {
329                TotalCount = x.Count(),
330                CalculatingCount = x.Count(y => y.State == DA.TaskState.Calculating),
331                FinishedCount = x.Count(y => CompletedStates.Contains(y.State))
332              }).FirstOrDefault();
333            if (statistics != null) {
334              job.JobCount = statistics.TotalCount;
335              job.CalculatingCount = statistics.CalculatingCount;
336              job.FinishedCount = statistics.FinishedCount;
337            }
338            job.OwnerUsername = UserManager.GetUserNameById(job.OwnerUserId);
339            if (currentUserId == job.OwnerUserId) {
340              job.Permission = Permission.Full;
341            } else {
342              var jobPermission = jobPermissionDao.GetByJobAndUserId(job.Id, currentUserId);
343              job.Permission = jobPermission == null ? Permission.NotAllowed : jobPermission.Permission.ToDto();
344            }
345          }
346          return job;
347        });
348      }
349    }
350
351    public IEnumerable<DT.Job> GetJobs() {
352      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
353      var pm = PersistenceManager;
354      using (new PerformanceLogger("GetJobs")) {
355        var jobDao = pm.JobDao;
356        var jobPermissionDao = pm.JobPermissionDao;
357        var taskDao = pm.TaskDao;
358        var currentUserId = UserManager.CurrentUserId;
359        return pm.UseTransaction(() => {
360          var jobs = jobDao.GetAll()
361            .Where(x => x.OwnerUserId == currentUserId
362                     || x.JobPermissions.Count(y => y.Permission != DA.Permission.NotAllowed
363                                                 && y.GrantedUserId == currentUserId) > 0)
364            .Select(x => x.ToDto())
365            .ToList();
366          var statistics = taskDao.GetAll()
367              .GroupBy(x => x.JobId)
368              .Select(x => new {
369                x.Key,
370                TotalCount = x.Count(),
371                CalculatingCount = x.Count(y => y.State == DA.TaskState.Calculating),
372                FinishedCount = x.Count(y => CompletedStates.Contains(y.State))
373              })
374              .ToList();
375          foreach (var job in jobs) {
376            var statistic = statistics.FirstOrDefault(x => x.Key == job.Id);
377            if (statistic != null) {
378              job.JobCount = statistic.TotalCount;
379              job.CalculatingCount = statistic.CalculatingCount;
380              job.FinishedCount = statistic.FinishedCount;
381            }
382            job.OwnerUsername = UserManager.GetUserNameById(job.OwnerUserId);
383            if (currentUserId == job.OwnerUserId) {
384              job.Permission = Permission.Full;
385            } else {
386              var jobPermission = jobPermissionDao.GetByJobAndUserId(job.Id, currentUserId);
387              job.Permission = jobPermission == null ? Permission.NotAllowed : jobPermission.Permission.ToDto();
388            }
389          }
390          return jobs;
391        });
392      }
393    }
394
395    public Guid AddJob(DT.Job jobDto) {
396      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
397      var pm = PersistenceManager;
398      using (new PerformanceLogger("AddJob")) {
399        var jobDao = pm.JobDao;
400        var userPriorityDao = pm.UserPriorityDao;
401        return pm.UseTransaction(() => {
402          jobDto.OwnerUserId = UserManager.CurrentUserId;
403          jobDto.DateCreated = DateTime.Now;
404          var job = jobDao.Save(jobDto.ToEntity());
405          if (userPriorityDao.GetById(jobDto.OwnerUserId) == null) {
406            userPriorityDao.Save(new DA.UserPriority {
407              UserId = jobDto.OwnerUserId,
408              DateEnqueued = jobDto.DateCreated
409            });
410          }
411          pm.SubmitChanges();
412          return job.JobId;
413        });
414      }
415    }
416
417    public Guid AddJob(DT.Job jobDto, IEnumerable<Guid> resourceIds) {
418      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
419      // check user - project
420      AuthorizationManager.AuthorizeUserForProjectUse(UserManager.CurrentUserId, jobDto.ProjectId);
421      // check project - resources
422      AuthorizationManager.AuthorizeProjectForResourcesUse(jobDto.ProjectId, resourceIds);
423      var pm = PersistenceManager;
424      using (new PerformanceLogger("AddJob")) {
425        var jobDao = pm.JobDao;
426        var userPriorityDao = pm.UserPriorityDao;
427
428        return pm.UseTransaction(() => {
429          var newJob = jobDto.ToEntity();
430          newJob.OwnerUserId = UserManager.CurrentUserId;
431          newJob.DateCreated = DateTime.Now;
432
433          // add resource assignments
434          newJob.AssignedJobResources.AddRange(resourceIds.Select(
435            x => new DA.AssignedJobResource {
436              ResourceId = x
437            }));
438
439          var job = jobDao.Save(newJob);
440          if (userPriorityDao.GetById(jobDto.OwnerUserId) == null) {
441            userPriorityDao.Save(new DA.UserPriority {
442              UserId = jobDto.OwnerUserId,
443              DateEnqueued = jobDto.DateCreated
444            });
445          }
446          pm.SubmitChanges();
447          return job.JobId;
448        });
449      }
450    }
451
452    public void UpdateJob(DT.Job jobDto) {
453      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
454      AuthorizationManager.AuthorizeForJob(jobDto.Id, DT.Permission.Full);
455      var pm = PersistenceManager;
456      using (new PerformanceLogger("UpdateJob")) {
457        bool exists = true;
458        var jobDao = pm.JobDao;
459        pm.UseTransaction(() => {
460          var job = jobDao.GetById(jobDto.Id);
461          if (job == null) {
462            exists = false;
463            job = new DA.Job();
464          }
465          jobDto.CopyToEntity(job);
466          if (!exists) {
467            jobDao.Save(job);
468          }
469          pm.SubmitChanges();
470        });
471      }
472    }
473
474    public void UpdateJob(DT.Job jobDto, IEnumerable<Guid> resourceIds) {
475      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
476      AuthorizationManager.AuthorizeForJob(jobDto.Id, DT.Permission.Full);
477      // check user - project permission
478      AuthorizationManager.AuthorizeUserForProjectUse(UserManager.CurrentUserId, jobDto.ProjectId);
479      // check project - resources permission
480      AuthorizationManager.AuthorizeProjectForResourcesUse(jobDto.ProjectId, resourceIds);
481
482      var pm = PersistenceManager;
483      using (new PerformanceLogger("UpdateJob")) {
484        bool exists = true;
485        var jobDao = pm.JobDao;
486        pm.UseTransaction(() => {
487          var job = jobDao.GetById(jobDto.Id);
488          if (job == null) {
489            exists = false;
490            job = new DA.Job();
491          }
492          jobDto.CopyToEntity(job);
493
494          // remove former resource assignments
495          job.AssignedJobResources.Clear();
496          // add resource assignments
497          job.AssignedJobResources.AddRange(resourceIds.Select(
498            x => new DA.AssignedJobResource {
499              ResourceId = x
500            }));
501
502          if (!exists) {
503            jobDao.Save(job);
504          }
505          pm.SubmitChanges();
506        });
507      }
508    }
509
510    public void DeleteJob(Guid jobId) {
511      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
512      AuthorizationManager.AuthorizeForJob(jobId, DT.Permission.Full);
513      var pm = PersistenceManager;
514      using (new PerformanceLogger("DeleteJob")) {
515        var jobDao = pm.JobDao;
516        pm.UseTransaction(() => {
517          // child task will be deleted by db-trigger
518          jobDao.Delete(jobId);
519          pm.SubmitChanges();
520        });
521      }
522    }
523    #endregion
524
525    #region JobPermission Methods
526    public void GrantPermission(Guid jobId, Guid grantedUserId, DT.Permission permission) {
527      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
528      AuthorizationManager.AuthorizeForJob(jobId, Permission.Full);
529      var pm = PersistenceManager;
530      using (new PerformanceLogger("GrantPermission")) {
531        var jobPermissionDao = pm.JobPermissionDao;
532        var currentUserId = UserManager.CurrentUserId;
533        pm.UseTransaction(() => {
534          jobPermissionDao.SetJobPermission(jobId, currentUserId, grantedUserId, permission.ToEntity());
535          pm.SubmitChanges();
536        });
537      }
538    }
539
540    public void RevokePermission(Guid jobId, Guid grantedUserId) {
541      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
542      AuthorizationManager.AuthorizeForJob(jobId, Permission.Full);
543      var pm = PersistenceManager;
544      using (new PerformanceLogger("RevokePermission")) {
545        var jobPermissionDao = pm.JobPermissionDao;
546        var currentUserId = UserManager.CurrentUserId;
547        pm.UseTransaction(() => {
548          jobPermissionDao.SetJobPermission(jobId, currentUserId, grantedUserId, DA.Permission.NotAllowed);
549          pm.SubmitChanges();
550        });
551      }
552    }
553
554    public IEnumerable<JobPermission> GetJobPermissions(Guid jobId) {
555      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
556      AuthorizationManager.AuthorizeForJob(jobId, Permission.Full);
557      var pm = PersistenceManager;
558      using (new PerformanceLogger("GetJobPermissions")) {
559        var jobPermissionDao = pm.JobPermissionDao;
560        return pm.UseTransaction(() => jobPermissionDao.GetByJobId(jobId)
561          .Select(x => x.ToDto())
562          .ToList()
563        );
564      }
565    }
566
567    // BackwardsCompatibility3.3
568    #region Backwards compatible code, remove with 3.4
569    public bool IsAllowedPrivileged() {
570      return true;
571    }
572    #endregion
573    #endregion
574
575    #region Login Methods
576    public void Hello(DT.Slave slaveInfo) {
577      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Slave);
578      if (UserManager.CurrentUser.UserName != "hiveslave") {
579        slaveInfo.OwnerUserId = UserManager.CurrentUserId;
580      }
581      var pm = PersistenceManager;
582      using (new PerformanceLogger("Hello")) {
583        var slaveDao = pm.SlaveDao;
584        pm.UseTransaction(() => {
585          var slave = slaveDao.GetById(slaveInfo.Id);
586          if (slave == null) {
587            slaveDao.Save(slaveInfo.ToEntity());
588          } else {
589            bool oldIsAllowedToCalculate = slave.IsAllowedToCalculate;
590            Guid? oldParentResourceId = slave.ParentResourceId;
591            slaveInfo.CopyToEntity(slave);
592            slave.IsAllowedToCalculate = oldIsAllowedToCalculate;
593            slave.ParentResourceId = oldParentResourceId;
594            slave.LastHeartbeat = DateTime.Now;
595            slave.SlaveState = DA.SlaveState.Idle;
596          }
597          pm.SubmitChanges();
598        });
599      }
600    }
601
602    public void GoodBye(Guid slaveId) {
603      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Slave);
604      var pm = PersistenceManager;
605      using (new PerformanceLogger("GoodBye")) {
606        var slaveDao = pm.SlaveDao;
607        pm.UseTransaction(() => {
608          var slave = slaveDao.GetById(slaveId);
609          if (slave != null) {
610            slave.SlaveState = DA.SlaveState.Offline;
611            pm.SubmitChanges();
612          }
613        });
614      }
615    }
616    #endregion
617
618    #region Heartbeat Methods
619    public List<MessageContainer> Heartbeat(DT.Heartbeat heartbeat) {
620      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Slave);
621      List<MessageContainer> result = new List<MessageContainer>();
622      try {
623        using (new PerformanceLogger("ProcessHeartbeat")) {
624          result = HeartbeatManager.ProcessHeartbeat(heartbeat);
625        }
626      } catch (Exception ex) {
627        DA.LogFactory.GetLogger(this.GetType().Namespace).Log(string.Format("Exception processing Heartbeat: {0}", ex));
628      }
629      if (HeuristicLab.Services.Hive.Properties.Settings.Default.TriggerEventManagerInHeartbeat) {
630        TriggerEventManager(false);
631      }
632      return result;
633    }
634    #endregion
635
636    #region Plugin Methods
637    public DT.Plugin GetPlugin(Guid pluginId) {
638      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
639      var pm = PersistenceManager;
640      using (new PerformanceLogger("GetPlugin")) {
641        var pluginDao = pm.PluginDao;
642        return pm.UseTransaction(() => pluginDao.GetById(pluginId).ToDto());
643      }
644    }
645
646    public Guid AddPlugin(DT.Plugin plugin, List<DT.PluginData> pluginData) {
647      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
648      var pm = PersistenceManager;
649      using (new PerformanceLogger("AddPlugin")) {
650        var pluginDao = pm.PluginDao;
651        plugin.UserId = UserManager.CurrentUserId;
652        plugin.DateCreated = DateTime.Now;
653        return pm.UseTransaction(() => {
654          var pluginEntity = pluginDao.GetByHash(plugin.Hash).SingleOrDefault();
655          if (pluginEntity != null) {
656            throw new FaultException<PluginAlreadyExistsFault>(new PluginAlreadyExistsFault(pluginEntity.PluginId));
657          }
658          pluginEntity = plugin.ToEntity();
659          foreach (var data in pluginData) {
660            data.PluginId = default(Guid); // real id will be assigned from linq2sql
661            pluginEntity.PluginData.Add(data.ToEntity());
662          }
663          pluginDao.Save(pluginEntity);
664          pm.SubmitChanges();
665          return pluginEntity.PluginId;
666        });
667      }
668    }
669
670    public IEnumerable<DT.Plugin> GetPlugins() {
671      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
672      var pm = PersistenceManager;
673      using (new PerformanceLogger("GetPlugins")) {
674        var pluginDao = pm.PluginDao;
675        return pm.UseTransaction(() => pluginDao.GetAll()
676          .Where(x => x.Hash != null)
677          .Select(x => x.ToDto())
678          .ToList()
679        );
680      }
681    }
682
683    public IEnumerable<DT.PluginData> GetPluginDatas(List<Guid> pluginIds) {
684      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
685      var pm = PersistenceManager;
686      using (new PerformanceLogger("GetPluginDatas")) {
687        var pluginDataDao = pm.PluginDataDao;
688        return pm.UseTransaction(() => pluginDataDao.GetAll()
689            .Where(x => pluginIds.Contains(x.PluginId))
690            .Select(x => x.ToDto())
691            .ToList()
692        );
693      }
694    }
695    #endregion
696
697    #region Project Methods
698    public Guid AddProject(DT.Project projectDto) {
699      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
700      var pm = PersistenceManager;
701      using (new PerformanceLogger("AddProject")) {
702        var projectDao = pm.ProjectDao;
703        return pm.UseTransaction(() => {
704          var project = projectDao.Save(projectDto.ToEntity());
705          pm.SubmitChanges();
706          return project.ProjectId;
707        });
708      }
709    }
710
711    public void UpdateProject(DT.Project projectDto) {
712      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
713      var pm = PersistenceManager;
714      using (new PerformanceLogger("UpdateProject")) {
715        var projectDao = pm.ProjectDao;
716        pm.UseTransaction(() => {
717          var project = projectDao.GetById(projectDto.Id);
718          if (project != null) {
719            projectDto.CopyToEntity(project);
720          } else {
721            projectDao.Save(projectDto.ToEntity());
722          }
723          pm.SubmitChanges();
724        });
725      }
726    }
727
728    public void DeleteProject(Guid projectId) {
729      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
730      AuthorizationManager.AuthorizeForProjectAdministration(projectId);
731      var pm = PersistenceManager;
732      using (new PerformanceLogger("DeleteProject")) {
733        var projectDao = pm.ProjectDao;
734        pm.UseTransaction(() => {
735          projectDao.Delete(projectId);
736          pm.SubmitChanges();
737        });
738      }
739    }
740
741    public DT.Project GetProject(Guid projectId) {
742      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
743      var pm = PersistenceManager;
744      using (new PerformanceLogger("GetProject")) {
745        var projectDao = pm.ProjectDao;
746        return pm.UseTransaction(() => projectDao.GetById(projectId).ToDto());
747      }
748    }
749
750    public IEnumerable<DT.Project> GetProjects() {
751      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
752      bool isAdministrator = RoleVerifier.IsInRole(HiveRoles.Administrator);
753      var pm = PersistenceManager;
754      using (new PerformanceLogger("GetProjects")) {
755        var projectDao = pm.ProjectDao;
756        var projectPermissionDao = pm.ProjectPermissionDao;
757        var currentUserId = UserManager.CurrentUserId;
758        return pm.UseTransaction(() => {
759          var projectPermissions = projectPermissionDao.GetAll();
760          return projectDao.GetAll().ToList()
761            .Where(x => isAdministrator
762              || x.OwnerUserId == currentUserId
763              || UserManager.VerifyUser(currentUserId, projectPermissions
764                  .Where(y => y.ProjectId == x.ProjectId)
765                  .Select(z => z.GrantedUserId)
766                  .ToList())
767              )
768            .Select(x => x.ToDto())
769            .ToList();
770        });
771      }
772    }
773    #endregion
774
775    #region ProjectPermission Methods
776    public void GrantProjectPermissions(Guid projectId, Guid[] grantedUserIds, bool cascading) {
777      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
778      AuthorizationManager.AuthorizeForProjectAdministration(projectId);
779      var pm = PersistenceManager;
780      using (new PerformanceLogger("GrantProjectPermissions")) {
781        var projectDao = pm.ProjectDao;
782
783        pm.UseTransaction(() => {
784          var project = projectDao.GetById(projectId);
785          var projectPermissions = project.ProjectPermissions.ToList();
786          foreach (var id in grantedUserIds) {
787            if (projectPermissions.All(x => x.GrantedUserId != id)) {
788              project.ProjectPermissions.Add(new DA.ProjectPermission {
789                GrantedUserId = id,
790                GrantedByUserId = UserManager.CurrentUserId
791              });
792            }
793          }
794          if (cascading) {
795            var childProjects = projectDao.GetChildProjectsById(projectId);
796            foreach (var p in childProjects) {
797              p.ProjectPermissions.Clear();
798              foreach (var id in grantedUserIds) {
799                p.ProjectPermissions.Add(new DA.ProjectPermission {
800                  GrantedUserId = id,
801                  GrantedByUserId = UserManager.CurrentUserId
802                });
803              }
804            }
805          }
806          pm.SubmitChanges();
807        });
808      }
809    }
810
811    public void RevokeProjectPermissions(Guid projectId, Guid[] grantedUserIds, bool cascading) {
812      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
813      AuthorizationManager.AuthorizeForProjectAdministration(projectId);
814      var pm = PersistenceManager;
815      using (new PerformanceLogger("RevokeProjectPermissions")) {
816        var projectPermissionDao = pm.ProjectPermissionDao;
817        var projectDao = pm.ProjectDao;
818        pm.UseTransaction(() => {
819          if (cascading) {
820            var childProjectIds = projectDao.GetChildProjectIdsById(projectId);
821            projectPermissionDao.DeleteByProjectIdsAndGrantedUserIds(childProjectIds, grantedUserIds);
822          }
823          projectPermissionDao.DeleteByProjectIdAndGrantedUserIds(projectId, grantedUserIds);
824          pm.SubmitChanges();
825        });
826      }
827    }
828
829    public IEnumerable<DT.ProjectPermission> GetProjectPermissions(Guid projectId) {
830      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
831      var pm = PersistenceManager;
832      using (new PerformanceLogger("GetProjectPermissions")) {
833        var projectPermissionDao = pm.ProjectPermissionDao;
834        return pm.UseTransaction(() => projectPermissionDao.GetByProjectId(projectId)
835          .Select(x => x.ToDto())
836          .ToList()
837        );
838      }
839    }
840    #endregion
841
842    #region AssignedProjectResource Methods
843    public void AssignProjectResources(Guid projectId, Guid[] resourceIds, bool cascading) {
844      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
845      AuthorizationManager.AuthorizeForProjectResourceAdministration(projectId, resourceIds);
846      var pm = PersistenceManager;
847      using (new PerformanceLogger("AssignProjectResources")) {
848        var projectDao = pm.ProjectDao;
849        pm.UseTransaction(() => {
850          var project = projectDao.GetById(projectId);
851          var assignedProjectResources = project.AssignedProjectResources.ToList();
852
853          foreach (var id in resourceIds) {
854            if (assignedProjectResources.All(x => x.ResourceId != id)) {
855              project.AssignedProjectResources.Add(new DA.AssignedProjectResource {
856                ResourceId = id
857              });
858            }
859          }
860          if (cascading) {
861            var childProjects = projectDao.GetChildProjectsById(projectId);
862            foreach (var p in childProjects) {
863              p.AssignedProjectResources.Clear();
864              foreach (var id in resourceIds) {
865                p.AssignedProjectResources.Add(new DA.AssignedProjectResource {
866                  ResourceId = id
867                });
868              }
869            }
870          }
871          pm.SubmitChanges();
872        });
873      }
874    }
875
876    public void UnassignProjectResources(Guid projectId, Guid[] resourceIds, bool cascading) {
877      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
878      AuthorizationManager.AuthorizeForProjectResourceAdministration(projectId, resourceIds);
879      var pm = PersistenceManager;
880      using (new PerformanceLogger("UnassignProjectResources")) {
881        var assignedProjectResourceDao = pm.AssignedProjectResourceDao;
882        var projectDao = pm.ProjectDao;
883        pm.UseTransaction(() => {
884          if (cascading) {
885            var childProjectIds = projectDao.GetChildProjectIdsById(projectId);
886            assignedProjectResourceDao.DeleteByProjectIdsAndResourceIds(childProjectIds, resourceIds);
887          }
888          assignedProjectResourceDao.DeleteByProjectIdAndResourceIds(projectId, resourceIds);
889          pm.SubmitChanges();
890        });
891      }
892    }
893
894    public IEnumerable<AssignedProjectResource> GetAssignedResourcesForProject(Guid projectId) {
895      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
896      var pm = PersistenceManager;
897      using (new PerformanceLogger("GetAssignedResourcesForProject")) {
898        var assignedProjectResourceDao = pm.AssignedProjectResourceDao;
899        return pm.UseTransaction(() => assignedProjectResourceDao.GetByProjectId(projectId)
900          .Select(x => x.ToDto())
901          .ToList()
902        );
903      }
904    }
905    #endregion
906
907    #region Slave Methods
908    public Guid AddSlave(DT.Slave slaveDto) {
909      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
910      var pm = PersistenceManager;
911      using (new PerformanceLogger("AddSlave")) {
912        var slaveDao = pm.SlaveDao;
913        return pm.UseTransaction(() => {
914          var slave = slaveDao.Save(slaveDto.ToEntity());
915          pm.SubmitChanges();
916          return slave.ResourceId;
917        });
918      }
919    }
920
921    public Guid AddSlaveGroup(DT.SlaveGroup slaveGroupDto) {
922      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
923      var pm = PersistenceManager;
924      using (new PerformanceLogger("AddSlaveGroup")) {
925        var slaveGroupDao = pm.SlaveGroupDao;
926        return pm.UseTransaction(() => {
927          if (slaveGroupDto.Id == Guid.Empty) {
928            slaveGroupDto.Id = Guid.NewGuid();
929          }
930          var slaveGroup = slaveGroupDao.Save(slaveGroupDto.ToEntity());
931          pm.SubmitChanges();
932          return slaveGroup.ResourceId;
933        });
934      }
935    }
936
937    public DT.Slave GetSlave(Guid slaveId) {
938      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
939      var pm = PersistenceManager;
940      using (new PerformanceLogger("GetSlave")) {
941        var slaveDao = pm.SlaveDao;
942        return pm.UseTransaction(() => slaveDao.GetById(slaveId).ToDto());
943      }
944    }
945
946    public IEnumerable<DT.Slave> GetSlaves() {
947      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
948      bool isAdministrator = RoleVerifier.IsInRole(HiveRoles.Administrator);
949      var pm = PersistenceManager;
950      using (new PerformanceLogger("GetSlaves")) {
951        var slaveDao = pm.SlaveDao;
952        var resourcePermissionDao = pm.ProjectPermissionDao;
953        var currentUserId = UserManager.CurrentUserId;
954        return pm.UseTransaction(() => {
955          var resourcePermissions = resourcePermissionDao.GetAll();
956          return slaveDao.GetAll().ToList()
957            .Where(x => isAdministrator
958              || x.OwnerUserId == null
959              || x.OwnerUserId == currentUserId
960              || UserManager.VerifyUser(currentUserId, resourcePermissions
961                  //.Where(y => y.ResourceId == x.ResourceId)
962                  .Select(z => z.GrantedUserId)
963                  .ToList())
964              )
965            .Select(x => x.ToDto())
966            .ToList();
967        });
968      }
969    }
970
971    public IEnumerable<DT.SlaveGroup> GetSlaveGroups() {
972      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
973      bool isAdministrator = RoleVerifier.IsInRole(HiveRoles.Administrator);
974      var pm = PersistenceManager;
975      using (new PerformanceLogger("GetSlaveGroups")) {
976        var slaveGroupDao = pm.SlaveGroupDao;
977        var resourcePermissionDao = pm.ProjectPermissionDao;
978        var currentUserId = UserManager.CurrentUserId;
979        return pm.UseTransaction(() => {
980          var resourcePermissions = resourcePermissionDao.GetAll();
981          return slaveGroupDao.GetAll().ToList()
982            .Where(x => isAdministrator
983              || x.OwnerUserId == null
984              || x.OwnerUserId == currentUserId
985              || UserManager.VerifyUser(currentUserId, resourcePermissions
986                  //.Where(y => y.ResourceId == x.ResourceId)
987                  .Select(z => z.GrantedUserId)
988                  .ToList())
989              )
990            .Select(x => x.ToDto())
991            .ToList();
992        });
993      }
994    }
995
996    public void UpdateSlave(DT.Slave slaveDto) {
997      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
998      var pm = PersistenceManager;
999      using (new PerformanceLogger("UpdateSlave")) {
1000        var slaveDao = pm.SlaveDao;
1001        pm.UseTransaction(() => {
1002          var slave = slaveDao.GetById(slaveDto.Id);
1003          if (slave != null) {
1004            slaveDto.CopyToEntity(slave);
1005          } else {
1006            slaveDao.Save(slaveDto.ToEntity());
1007          }
1008          pm.SubmitChanges();
1009        });
1010      }
1011    }
1012
1013    public void UpdateSlaveGroup(DT.SlaveGroup slaveGroupDto) {
1014      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
1015      var pm = PersistenceManager;
1016      using (new PerformanceLogger("UpdateSlaveGroup")) {
1017        var slaveGroupDao = pm.SlaveGroupDao;
1018        pm.UseTransaction(() => {
1019          var slaveGroup = slaveGroupDao.GetById(slaveGroupDto.Id);
1020          if (slaveGroup != null) {
1021            slaveGroupDto.CopyToEntity(slaveGroup);
1022          } else {
1023            slaveGroupDao.Save(slaveGroupDto.ToEntity());
1024          }
1025          pm.SubmitChanges();
1026        });
1027      }
1028    }
1029
1030    public void DeleteSlave(Guid slaveId) {
1031      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
1032      AuthorizationManager.AuthorizeForResourceAdministration(slaveId);
1033      var pm = PersistenceManager;
1034      using (new PerformanceLogger("DeleteSlave")) {
1035        var slaveDao = pm.SlaveDao;
1036        pm.UseTransaction(() => {
1037          slaveDao.Delete(slaveId);
1038          pm.SubmitChanges();
1039        });
1040      }
1041    }
1042
1043    public void DeleteSlaveGroup(Guid slaveGroupId) {
1044      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
1045      AuthorizationManager.AuthorizeForResourceAdministration(slaveGroupId);
1046      var pm = PersistenceManager;
1047      using (new PerformanceLogger("DeleteSlaveGroup")) {
1048        var slaveGroupDao = pm.SlaveGroupDao;
1049        pm.UseTransaction(() => {
1050          slaveGroupDao.Delete(slaveGroupId);
1051          pm.SubmitChanges();
1052        });
1053      }
1054    }
1055
1056    public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
1057      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
1058      var pm = PersistenceManager;
1059      using (new PerformanceLogger("AddResourceToGroup")) {
1060        var resourceDao = pm.ResourceDao;
1061        pm.UseTransaction(() => {
1062          var resource = resourceDao.GetById(resourceId);
1063          resource.ParentResourceId = slaveGroupId;
1064          pm.SubmitChanges();
1065        });
1066      }
1067    }
1068
1069    public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
1070      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
1071      var pm = PersistenceManager;
1072      using (new PerformanceLogger("RemoveResourceFromGroup")) {
1073        var resourceDao = pm.ResourceDao;
1074        pm.UseTransaction(() => {
1075          var resource = resourceDao.GetById(resourceId);
1076          resource.ParentResourceId = null;
1077          pm.SubmitChanges();
1078        });
1079      }
1080    }
1081
1082    public Guid GetResourceId(string resourceName) {
1083      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
1084      var pm = PersistenceManager;
1085      using (new PerformanceLogger("GetResourceId")) {
1086        var resourceDao = pm.ResourceDao;
1087        return pm.UseTransaction(() => {
1088          var resource = resourceDao.GetByName(resourceName);
1089          return resource != null ? resource.ResourceId : Guid.Empty;
1090        });
1091      }
1092    }
1093
1094    public void TriggerEventManager(bool force) {
1095      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
1096      // use a serializable transaction here to ensure not two threads execute this simultaniously (mutex-lock would not work since IIS may use multiple AppDomains)
1097      bool cleanup;
1098      var pm = PersistenceManager;
1099      using (new PerformanceLogger("TriggerEventManager")) {
1100        cleanup = false;
1101        var lifecycleDao = pm.LifecycleDao;
1102        pm.UseTransaction(() => {
1103          var lastLifecycle = lifecycleDao.GetLastLifecycle();
1104          DateTime lastCleanup = lastLifecycle != null ? lastLifecycle.LastCleanup : DateTime.MinValue;
1105          if (force || DateTime.Now - lastCleanup > HeuristicLab.Services.Hive.Properties.Settings.Default.CleanupInterval) {
1106            lifecycleDao.UpdateLifecycle();
1107            cleanup = true;
1108            pm.SubmitChanges();
1109          }
1110        }, true);
1111      }
1112      if (cleanup) {
1113        EventManager.Cleanup();
1114      }
1115    }
1116
1117    public int GetNewHeartbeatInterval(Guid slaveId) {
1118      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Slave);
1119      var pm = PersistenceManager;
1120      using (new PerformanceLogger("GetNewHeartbeatInterval")) {
1121        var slaveDao = pm.SlaveDao;
1122        return pm.UseTransaction(() => {
1123          var slave = slaveDao.GetById(slaveId);
1124          if (slave != null) {
1125            return slave.HbInterval;
1126          }
1127          return -1;
1128        });
1129      }
1130    }
1131    #endregion
1132
1133    #region Downtime Methods
1134    public Guid AddDowntime(DT.Downtime downtimeDto) {
1135      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
1136      AuthorizationManager.AuthorizeForResourceAdministration(downtimeDto.ResourceId);
1137      var pm = PersistenceManager;
1138      using (new PerformanceLogger("AddDowntime")) {
1139        var downtimeDao = pm.DowntimeDao;
1140        return pm.UseTransaction(() => {
1141          var downtime = downtimeDao.Save(downtimeDto.ToEntity());
1142          pm.SubmitChanges();
1143          return downtime.ResourceId;
1144        });
1145      }
1146    }
1147
1148    public void DeleteDowntime(Guid downtimeId) {
1149      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
1150      var pm = PersistenceManager;
1151      using (new PerformanceLogger("DeleteDowntime")) {
1152        var downtimeDao = pm.DowntimeDao;
1153        pm.UseTransaction(() => {
1154          downtimeDao.Delete(downtimeId);
1155          pm.SubmitChanges();
1156        });
1157      }
1158    }
1159
1160    public void UpdateDowntime(DT.Downtime downtimeDto) {
1161      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
1162      AuthorizationManager.AuthorizeForResourceAdministration(downtimeDto.ResourceId);
1163      var pm = PersistenceManager;
1164      using (new PerformanceLogger("UpdateDowntime")) {
1165        var downtimeDao = pm.DowntimeDao;
1166        pm.UseTransaction(() => {
1167          var downtime = downtimeDao.GetById(downtimeDto.Id);
1168          if (downtime != null) {
1169            downtimeDto.CopyToEntity(downtime);
1170          } else {
1171            downtimeDao.Save(downtimeDto.ToEntity());
1172          }
1173          pm.SubmitChanges();
1174        });
1175      }
1176    }
1177
1178    public IEnumerable<DT.Downtime> GetDowntimesForResource(Guid resourceId) {
1179      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
1180      var pm = PersistenceManager;
1181      using (new PerformanceLogger("GetDowntimesForResource")) {
1182        var downtimeDao = pm.DowntimeDao;
1183        return pm.UseTransaction(() => downtimeDao.GetByResourceId(resourceId)
1184          .Select(x => x.ToDto())
1185          .ToList()
1186        );
1187      }
1188    }
1189    #endregion
1190
1191    #region User Methods
1192    public string GetUsernameByUserId(Guid userId) {
1193      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
1194      var user = UserManager.GetUserById(userId);
1195      return user != null ? user.UserName : null;
1196    }
1197
1198    public Guid GetUserIdByUsername(string username) {
1199      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
1200      var user = ServiceLocator.Instance.UserManager.GetUserByName(username);
1201      return user != null ? (Guid?)user.ProviderUserKey ?? Guid.Empty : Guid.Empty;
1202    }
1203    #endregion
1204
1205    #region UserPriorities Methods
1206    public IEnumerable<DT.UserPriority> GetUserPriorities() {
1207      var pm = PersistenceManager;
1208      using (new PerformanceLogger("GetUserPriorities")) {
1209        var userPriorityDao = pm.UserPriorityDao;
1210        return pm.UseTransaction(() => userPriorityDao.GetAll()
1211          .Select(x => x.ToDto())
1212          .ToList()
1213        );
1214      }
1215    }
1216    #endregion
1217
1218    #region Private Helper Methods
1219    private void UpdateTaskState(IPersistenceManager pm, DA.Task task, DT.TaskState taskState, Guid? slaveId, Guid? userId, string exception) {
1220      var stateLogDao = pm.StateLogDao;
1221      var taskStateEntity = taskState.ToEntity();
1222
1223      if (task.State == DA.TaskState.Transferring && taskStateEntity == DA.TaskState.Paused && task.Command == null) {
1224        // slave paused and uploaded the task (no user-command) -> set waiting.
1225        taskStateEntity = DA.TaskState.Waiting;
1226      }
1227
1228      stateLogDao.Save(new DA.StateLog {
1229        State = taskStateEntity,
1230        DateTime = DateTime.Now,
1231        TaskId = task.TaskId,
1232        UserId = userId,
1233        SlaveId = slaveId,
1234        Exception = exception
1235      });
1236
1237      task.State = taskStateEntity;
1238
1239      if (task.Command == DA.Command.Pause && task.State == DA.TaskState.Paused
1240          || task.Command == DA.Command.Abort && task.State == DA.TaskState.Aborted
1241          || task.Command == DA.Command.Stop && task.State == DA.TaskState.Aborted) {
1242        task.Command = null;
1243      }
1244    }
1245
1246    #endregion
1247  }
1248}
Note: See TracBrowser for help on using the repository browser.