Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/07/18 15:34:23 (6 years ago)
Author:
jkarder
Message:

#2839: worked on hive project management

  • added .ToList() calls within some service methods to prevent TransactionExceptions
  • added check for InvokeRequired
  • improved RunInHiveMenuItem
  • removed .resx files from projects
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2839_HiveProjectManagement/HeuristicLab.Services.Hive/3.3/HiveService.cs

    r16057 r16060  
    333333            .Where(x => x.State == DA.JobState.Online
    334334                          && (x.OwnerUserId == currentUserId
    335                             || x.JobPermissions.Count(y => y.Permission != DA.Permission.NotAllowed 
     335                            || x.JobPermissions.Count(y => y.Permission != DA.Permission.NotAllowed
    336336                              && y.GrantedUserId == currentUserId) > 0)
    337337                          )
     
    359359            .ToList();
    360360
    361           if(administrationGrantedProjects.Select(x => x.ProjectId).Contains(projectId)) {
     361          if (administrationGrantedProjects.Select(x => x.ProjectId).Contains(projectId)) {
    362362            var jobs = jobDao.GetByProjectId(projectId)
    363363            .Select(x => x.ToDto())
     
    388388          var requestedAndGrantedProjectIds = projectIds.Intersect(administrationGrantedProjectIds);
    389389
    390           if(requestedAndGrantedProjectIds.Any()) {
     390          if (requestedAndGrantedProjectIds.Any()) {
    391391            var jobs = jobDao.GetByProjectIds(requestedAndGrantedProjectIds)
    392392              .Select(x => x.ToDto())
     
    415415      // check project - resources
    416416      AuthorizationManager.AuthorizeProjectForResourcesUse(jobDto.ProjectId, resourceIds);
    417      
     417
    418418      using (new PerformanceLogger("AddJob")) {
    419419        var jobDao = pm.JobDao;
     
    426426
    427427          // add resource assignments
    428           if (resourceIds != null && resourceIds.Any()) {           
     428          if (resourceIds != null && resourceIds.Any()) {
    429429            newJob.AssignedJobResources.AddRange(resourceIds.Select(
    430430              x => new DA.AssignedJobResource {
     
    467467            exists = false;
    468468            job = new DA.Job();
    469           } else if(job.State != DA.JobState.Online) {
     469          } else if (job.State != DA.JobState.Online) {
    470470            throw new InvalidOperationException(NO_JOB_UPDATE_POSSIBLE);
    471471          }
     
    480480                x => new DA.AssignedJobResource {
    481481                  ResourceId = x
    482               }));
     482                }));
    483483            }
    484484            jobDao.Save(job);
    485           } else if(resourceIds != null) {
     485          } else if (resourceIds != null) {
    486486            var addedJobResourceIds = resourceIds.Except(job.AssignedJobResources.Select(x => x.ResourceId));
    487487            var removedJobResourceIds = job.AssignedJobResources
     
    489489              .Except(resourceIds)
    490490              .ToArray();
    491            
     491
    492492            // remove resource assignments
    493             foreach(var rid in removedJobResourceIds) {
     493            foreach (var rid in removedJobResourceIds) {
    494494              var ajr = job.AssignedJobResources.Where(x => x.ResourceId == rid).SingleOrDefault();
    495495              if (ajr != null) job.AssignedJobResources.Remove(ajr);
     
    515515        pm.UseTransaction(() => {
    516516          var job = jobDao.GetById(jobId);
    517           if(job != null) {
     517          if (job != null) {
    518518            var jobStateEntity = jobState.ToEntity();
    519519            // note: allow solely state changes from "Online" to "StatisticsPending" = deletion request by user for HiveStatisticGenerator
     
    521521            if (job.State == DA.JobState.Online && jobStateEntity == DA.JobState.StatisticsPending) {
    522522              job.State = jobStateEntity;
    523               foreach(var task in job.Tasks
    524               .Where(x => x.State == DA.TaskState.Waiting 
    525                 || x.State == DA.TaskState.Paused 
     523              foreach (var task in job.Tasks
     524              .Where(x => x.State == DA.TaskState.Waiting
     525                || x.State == DA.TaskState.Paused
    526526                || x.State == DA.TaskState.Offline)) {
    527527                task.State = DA.TaskState.Aborted;
    528528              }
    529529              pm.SubmitChanges();
    530             } else if(job.State == DA.JobState.StatisticsPending && jobStateEntity == DA.JobState.DeletionPending) {
     530            } else if (job.State == DA.JobState.StatisticsPending && jobStateEntity == DA.JobState.DeletionPending) {
    531531              job.State = jobStateEntity;
    532532              pm.SubmitChanges();
    533             } 
     533            }
    534534          }
    535535        });
     
    549549        var projectDao = pm.ProjectDao;
    550550        pm.UseTransaction(() => {
    551           foreach(var jobId in jobIds) {
     551          foreach (var jobId in jobIds) {
    552552            var job = jobDao.GetById(jobId);
    553553            if (job != null) {
     
    560560              if (isAdministrator || administrationGrantedProjects.Contains(job.Project)) {
    561561                // note: allow solely state changes from "Online" to "StatisticsPending" = deletion request by user for HiveStatisticGenerator
    562                 if (job.State == DA.JobState.Online) { 
     562                if (job.State == DA.JobState.Online) {
    563563                  job.State = DA.JobState.StatisticsPending;
    564564                  foreach (var task in job.Tasks
     
    566566                    || x.State == DA.TaskState.Paused
    567567                    || x.State == DA.TaskState.Offline)) {
    568                       task.State = DA.TaskState.Aborted;
     568                    task.State = DA.TaskState.Aborted;
    569569                  }
    570570                  pm.SubmitChanges();
     
    772772      bool isAdmin = RoleVerifier.IsInRole(HiveRoles.Administrator);
    773773      if (!isAdmin) {
    774         if(projectDto != null && projectDto.ParentProjectId.HasValue) {
     774        if (projectDto != null && projectDto.ParentProjectId.HasValue) {
    775775          AuthorizationManager.AuthorizeForProjectAdministration(projectDto.ParentProjectId.Value, false);
    776776        } else {
     
    794794          var project = projectDao.Save(projectDto.ToEntity());
    795795
    796           var parentProjects = projectDao.GetParentProjectsById(project.ProjectId);
     796          var parentProjects = projectDao.GetParentProjectsById(project.ProjectId).ToList();
    797797          bool isParent = parentProjects.Select(x => x.OwnerUserId == UserManager.CurrentUserId).Any();
    798798
     
    826826      // check if current (non-admin) user is owner of the project or the projectDto's-parents
    827827      // note: non-admin users are not allowed to administer root projects (i.e. projects without parental entry)
    828       bool isAdmin = RoleVerifier.IsInRole(HiveRoles.Administrator);     
     828      bool isAdmin = RoleVerifier.IsInRole(HiveRoles.Administrator);
    829829      if (!isAdmin) {
    830830        if (projectDto != null && projectDto.ParentProjectId.HasValue) {
    831           AuthorizationManager.AuthorizeForProjectAdministration(projectDto.Id, false);         
     831          AuthorizationManager.AuthorizeForProjectAdministration(projectDto.Id, false);
    832832        } else {
    833833          throw new SecurityException(NOT_AUTHORIZED_USERPROJECT);
     
    836836
    837837      // check that non-admins can not be set as owner of root projects
    838       if(projectDto != null && !projectDto.ParentProjectId.HasValue) {
     838      if (projectDto != null && !projectDto.ParentProjectId.HasValue) {
    839839        var owner = UserManager.GetUserById(projectDto.OwnerUserId);
    840         if(owner == null || !RoleVerifier.IsUserInRole(owner.UserName, HiveRoles.Administrator)) {
     840        if (owner == null || !RoleVerifier.IsUserInRole(owner.UserName, HiveRoles.Administrator)) {
    841841          throw new SecurityException(NOT_AUTHORIZED_PROJECTOWNER);
    842842        }
    843843      }
    844      
     844
    845845      var pm = PersistenceManager;
    846846      using (new PerformanceLogger("UpdateProject")) {
     
    850850          var project = projectDao.GetById(projectDto.Id);
    851851          if (project != null) { // (1) update existent project
    852             var parentProjects = projectDao.GetParentProjectsById(project.ProjectId);
     852            var parentProjects = projectDao.GetParentProjectsById(project.ProjectId).ToList();
    853853            bool isParent = parentProjects.Select(x => x.OwnerUserId == UserManager.CurrentUserId).Any();
    854854
     
    893893            var newProject = projectDao.Save(projectDto.ToEntity());
    894894
    895             var parentProjects = projectDao.GetParentProjectsById(project.ProjectId);
     895            var parentProjects = projectDao.GetParentProjectsById(project.ProjectId).ToList();
    896896            bool isParent = parentProjects.Select(x => x.OwnerUserId == UserManager.CurrentUserId).Any();
    897897
     
    925925      // check if current (non-admin) user is owner of one of the projectDto's-parents
    926926      // note: non-admin users are not allowed to administer root projects (i.e. projects without parental entry)
    927       if (!RoleVerifier.IsInRole(HiveRoles.Administrator)) {       
     927      if (!RoleVerifier.IsInRole(HiveRoles.Administrator)) {
    928928        AuthorizationManager.AuthorizeForProjectAdministration(projectId, true);
    929929      }
     
    942942            .ToList();
    943943
    944           if(jobs.Count > 0) {
     944          if (jobs.Count > 0) {
    945945            throw new InvalidOperationException("There are " + jobs.Count + " job(s) using this project and/or child-projects. It is necessary to delete them before the project.");
    946946          } else {
     
    966966          .Where(x => x.ProjectId == projectId)
    967967          .Select(x => x.ToDto())
    968           .SingleOrDefault(); 
     968          .SingleOrDefault();
    969969        });
    970970      }
     
    995995      using (new PerformanceLogger("GetProjectsForAdministration")) {
    996996        var projectDao = pm.ProjectDao;
    997        
    998         return pm.UseTransaction(() => {
    999           if(isAdministrator) {
     997
     998        return pm.UseTransaction(() => {
     999          if (isAdministrator) {
    10001000            return projectDao.GetAll().Select(x => x.ToDto()).ToList();
    10011001          } else {
     
    10121012      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
    10131013      var pm = PersistenceManager;
    1014       using(new PerformanceLogger("GetProjectGenealogy")) {
     1014      using (new PerformanceLogger("GetProjectGenealogy")) {
    10151015        var projectDao = pm.ProjectDao;
    10161016        var projectAncestors = new Dictionary<Guid, HashSet<Guid>>();
     
    10201020          foreach (var p in projects) {
    10211021            var parentProject = p.ParentProject;
    1022             while(parentProject != null) {
     1022            while (parentProject != null) {
    10231023              projectAncestors[p.ProjectId].Add(parentProject.ProjectId);
    10241024              parentProject = parentProject.ParentProject;
     
    10391039          projectDao
    10401040            .GetAll().ToList()
    1041             .ForEach(p=> projectNames.Add(p.ProjectId, p.Name));
     1041            .ForEach(p => projectNames.Add(p.ProjectId, p.Name));
    10421042          return projectNames;
    10431043        });
     
    10631063
    10641064          // guarantee that project owner is always permitted
    1065           if(!grantedUserIds.Contains(project.OwnerUserId)) {
     1065          if (!grantedUserIds.Contains(project.OwnerUserId)) {
    10661066            grantedUserIds.Add(project.OwnerUserId);
    10671067          }
     
    11151115          // add project permissions
    11161116          foreach (var id in grantedUserIds) {
    1117             if(project.ProjectPermissions.All(x => x.GrantedUserId != id)) {
     1117            if (project.ProjectPermissions.All(x => x.GrantedUserId != id)) {
    11181118              project.ProjectPermissions.Add(new DA.ProjectPermission {
    11191119                GrantedUserId = id,
     
    11351135            }
    11361136
    1137             foreach(var p in childProjects) {
     1137            foreach (var p in childProjects) {
    11381138              var cpAssignedPermissions = p.ProjectPermissions.Select(x => x.GrantedUserId).ToList();
    11391139              // guarantee that project owner is always permitted
     
    11441144
    11451145              // remove left-over job assignments (for non-reassignments)
    1146               if(!reassignCascading) {
     1146              if (!reassignCascading) {
    11471147                assignedJobResourceDao.DeleteByProjectIdAndUserIds(p.ProjectId, cpRemovedPermissions);
    11481148              }
     
    11521152                p.ProjectPermissions.Clear();
    11531153              } else {
    1154                 foreach(var item in p.ProjectPermissions
    1155                   .Where(x => x.GrantedUserId != p.OwnerUserId 
     1154                foreach (var item in p.ProjectPermissions
     1155                  .Where(x => x.GrantedUserId != p.OwnerUserId
    11561156                    && (removedPermissions.Contains(x.GrantedUserId) || cpRemovedPermissions.Contains(x.GrantedUserId)))
    11571157                  .ToList()) {
     
    12371237        pm.UseTransaction(() => {
    12381238          var project = projectDao.GetById(projectId);
    1239          
    1240           var parentProjects = projectDao.GetParentProjectsById(project.ProjectId);
     1239
     1240          var parentProjects = projectDao.GetParentProjectsById(project.ProjectId).ToList();
    12411241          bool isParent = parentProjects.Select(x => x.OwnerUserId == UserManager.CurrentUserId).Any();
    12421242
     
    12461246
    12471247          // if user is admin or owner of parent project(s)
    1248           if(isAdmin || isParent) {
     1248          if (isAdmin || isParent) {
    12491249            // remove job and project assignments
    12501250            if (reassign) {
     
    12881288
    12891289              // remove left-over job assignments (for non-reassignments)
    1290               if(!reassignCascading) {
     1290              if (!reassignCascading) {
    12911291                assignedJobResourceDao.DeleteByProjectIdAndResourceIds(p.ProjectId, cpRemovedAssignments);
    12921292              }
     
    13061306              // add project assignments
    13071307              foreach (var id in resourceIds) {
    1308                 if(p.AssignedProjectResources.All(x => x.ResourceId != id)) {
     1308                if (p.AssignedProjectResources.All(x => x.ResourceId != id)) {
    13091309                  p.AssignedProjectResources.Add(new DA.AssignedProjectResource {
    13101310                    ResourceId = id
     
    13731373    public IEnumerable<DT.AssignedProjectResource> GetAssignedResourcesForProjectsAdministration(IEnumerable<Guid> projectIds) {
    13741374      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
    1375       foreach(var id in projectIds)
     1375      foreach (var id in projectIds)
    13761376        AuthorizationManager.AuthorizeForProjectAdministration(id, false);
    13771377
     
    15721572          var resources = resourceDao.GetAll().ToList();
    15731573          resources.ForEach(r => resourceAncestors.Add(r.ResourceId, new HashSet<Guid>()));
    1574          
    1575           foreach(var r in resources) {
     1574
     1575          foreach (var r in resources) {
    15761576            var parentResource = r.ParentResource;
    1577             while(parentResource != null) {
     1577            while (parentResource != null) {
    15781578              resourceAncestors[r.ResourceId].Add(parentResource.ResourceId);
    15791579              parentResource = parentResource.ParentResource;
     
    18151815      return user != null ? (Guid?)user.ProviderUserKey ?? Guid.Empty : Guid.Empty;
    18161816    }
    1817    
     1817
    18181818    public Dictionary<Guid, HashSet<Guid>> GetUserGroupTree() {
    18191819      var userGroupTree = new Dictionary<Guid, HashSet<Guid>>();
    18201820      var userGroupMapping = UserManager.GetUserGroupMapping();
    18211821
    1822       foreach(var ugm in userGroupMapping) {
     1822      foreach (var ugm in userGroupMapping) {
    18231823        if (ugm.Parent == null || ugm.Child == null) continue;
    18241824
     
    18361836      bool isAdministrator = RoleVerifier.IsInRole(HiveRoles.Administrator);
    18371837      var pm = PersistenceManager;
    1838       using(new PerformanceLogger("CheckAccessToAdminAreaGranted")) {
     1838      using (new PerformanceLogger("CheckAccessToAdminAreaGranted")) {
    18391839        if (isAdministrator) {
    18401840          return true;
     
    19071907      if (jobs == null || !jobs.Any()) return;
    19081908
    1909       var currentUserId = UserManager.CurrentUserId;     
     1909      var currentUserId = UserManager.CurrentUserId;
    19101910      var taskDao = pm.TaskDao;
    19111911      var jobPermissionDao = pm.JobPermissionDao;
Note: See TracChangeset for help on using the changeset viewer.