Index: branches/HiveProjectManagement/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/JobDao.cs
===================================================================
--- branches/HiveProjectManagement/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/JobDao.cs	(revision 15642)
+++ branches/HiveProjectManagement/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/JobDao.cs	(revision 15643)
@@ -45,4 +45,8 @@
     }
 
+    public IEnumerable<Job> GetJobsReadyForDeletion() {
+      return GetJobsReadyForDeletionQuery(DataContext);
+    }
+
 
     #region Compiled queries
@@ -62,4 +66,14 @@
          where job.State == jobState
          select job.JobId).ToList());
+    private static readonly Func<DataContext, IEnumerable<Job>> GetJobsReadyForDeletionQuery =
+      CompiledQuery.Compile((DataContext db) =>
+        (from job in db.GetTable<Job>()
+         where job.State == JobState.StatisticsPending
+         && (from task in db.GetTable<Task>()
+             where task.JobId == job.JobId
+             select task.State == TaskState.Finished
+              || task.State == TaskState.Aborted
+              || task.State == TaskState.Failed).All(x => x)
+         select job).ToList());
     #endregion
 
@@ -69,4 +83,13 @@
       WHERE JobState = {0}
     ";
+    private const string GetStatisticsPendingJobs = @"
+      SELECT DISTINCT j.*
+      FROM [Job] j
+      WHERE j.JobState = 'StatisticsPending'
+      AND 'Calculating' NOT IN (
+        SELECT t.TaskState 
+        FROM [Task] t 
+        WHERE t.JobId = j.JobId)
+    ";
     #endregion
   }
Index: branches/HiveProjectManagement/HeuristicLab.Services.Hive.DataAccess/3.3/HiveDataContext.designer.cs
===================================================================
--- branches/HiveProjectManagement/HeuristicLab.Services.Hive.DataAccess/3.3/HiveDataContext.designer.cs	(revision 15642)
+++ branches/HiveProjectManagement/HeuristicLab.Services.Hive.DataAccess/3.3/HiveDataContext.designer.cs	(revision 15643)
@@ -390,5 +390,5 @@
 		}
 		
-		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Resource_AssignedProjectResource", Storage="_Resource", ThisKey="ResourceId", OtherKey="ResourceId", IsForeignKey=true, DeleteRule="CASCADE")]
+		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Resource_AssignedProjectResource", Storage="_Resource", ThisKey="ResourceId", OtherKey="ResourceId", IsForeignKey=true, DeleteRule="CASCADE", DeleteOnNull=true)]
 		public Resource Resource
 		{
@@ -424,5 +424,5 @@
 		}
 		
-		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Project_AssignedProjectResource", Storage="_Project", ThisKey="ProjectId", OtherKey="ProjectId", IsForeignKey=true, DeleteRule="CASCADE")]
+		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Project_AssignedProjectResource", Storage="_Project", ThisKey="ProjectId", OtherKey="ProjectId", IsForeignKey=true, DeleteRule="CASCADE", DeleteOnNull=true)]
 		public Project Project
 		{
@@ -1098,5 +1098,5 @@
 		}
 		
-		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Resource_AssignedProjectResource", Storage="_AssignedResources", ThisKey="ResourceId", OtherKey="ResourceId")]
+		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Resource_AssignedProjectResource", Storage="_AssignedResources", ThisKey="ResourceId", OtherKey="ResourceId", DeleteRule="CASCADE")]
 		public EntitySet<AssignedProjectResource> AssignedProjectResources
 		{
@@ -1163,5 +1163,5 @@
 		}
 		
-		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Resource_AssignedJobResource", Storage="_AssignedJobResources", ThisKey="ResourceId", OtherKey="ResourceId")]
+		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Resource_AssignedJobResource", Storage="_AssignedJobResources", ThisKey="ResourceId", OtherKey="ResourceId", DeleteRule="CASCADE")]
 		public EntitySet<AssignedJobResource> AssignedJobResources
 		{
@@ -2669,5 +2669,5 @@
 		}
 		
-		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Job_Task", Storage="_Jobs", ThisKey="JobId", OtherKey="JobId")]
+		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Job_Task", Storage="_Jobs", ThisKey="JobId", OtherKey="JobId", DeleteRule="CASCADE")]
 		public EntitySet<Task> Tasks
 		{
@@ -2682,5 +2682,5 @@
 		}
 		
-		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Job_JobPermission", Storage="_HiveExperimentPermissions", ThisKey="JobId", OtherKey="JobId")]
+		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Job_JobPermission", Storage="_HiveExperimentPermissions", ThisKey="JobId", OtherKey="JobId", DeleteRule="CASCADE")]
 		public EntitySet<JobPermission> JobPermissions
 		{
@@ -2695,5 +2695,5 @@
 		}
 		
-		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Job_AssignedJobResource", Storage="_AssignedJobResources", ThisKey="JobId", OtherKey="JobId")]
+		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Job_AssignedJobResource", Storage="_AssignedJobResources", ThisKey="JobId", OtherKey="JobId", DeleteRule="CASCADE")]
 		public EntitySet<AssignedJobResource> AssignedJobResources
 		{
@@ -3533,5 +3533,5 @@
 		}
 		
-		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Job_JobPermission", Storage="_HiveExperiment", ThisKey="JobId", OtherKey="JobId", IsForeignKey=true)]
+		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Job_JobPermission", Storage="_HiveExperiment", ThisKey="JobId", OtherKey="JobId", IsForeignKey=true, DeleteRule="CASCADE", DeleteOnNull=true)]
 		public Job Job
 		{
@@ -5833,5 +5833,5 @@
 		}
 		
-		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Project_AssignedProjectResource", Storage="_AssignedResources", ThisKey="ProjectId", OtherKey="ProjectId")]
+		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Project_AssignedProjectResource", Storage="_AssignedResources", ThisKey="ProjectId", OtherKey="ProjectId", DeleteRule="CASCADE")]
 		public EntitySet<AssignedProjectResource> AssignedProjectResources
 		{
@@ -5872,5 +5872,5 @@
 		}
 		
-		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Project_ProjectPermission", Storage="_ProjectPermissions", ThisKey="ProjectId", OtherKey="ProjectId")]
+		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Project_ProjectPermission", Storage="_ProjectPermissions", ThisKey="ProjectId", OtherKey="ProjectId", DeleteRule="CASCADE")]
 		public EntitySet<ProjectPermission> ProjectPermissions
 		{
@@ -6084,5 +6084,5 @@
 		}
 		
-		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Project_ProjectPermission", Storage="_Project", ThisKey="ProjectId", OtherKey="ProjectId", IsForeignKey=true)]
+		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Project_ProjectPermission", Storage="_Project", ThisKey="ProjectId", OtherKey="ProjectId", IsForeignKey=true, DeleteRule="CASCADE", DeleteOnNull=true)]
 		public Project Project
 		{
@@ -6386,5 +6386,5 @@
 		}
 		
-		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Resource_AssignedJobResource", Storage="_Resource", ThisKey="ResourceId", OtherKey="ResourceId", IsForeignKey=true)]
+		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Resource_AssignedJobResource", Storage="_Resource", ThisKey="ResourceId", OtherKey="ResourceId", IsForeignKey=true, DeleteRule="CASCADE", DeleteOnNull=true)]
 		public Resource Resource
 		{
@@ -6420,5 +6420,5 @@
 		}
 		
-		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Job_AssignedJobResource", Storage="_Job", ThisKey="JobId", OtherKey="JobId", IsForeignKey=true)]
+		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Job_AssignedJobResource", Storage="_Job", ThisKey="JobId", OtherKey="JobId", IsForeignKey=true, DeleteRule="CASCADE", DeleteOnNull=true)]
 		public Job Job
 		{
Index: branches/HiveProjectManagement/HeuristicLab.Services.Hive/3.3/HiveService.cs
===================================================================
--- branches/HiveProjectManagement/HeuristicLab.Services.Hive/3.3/HiveService.cs	(revision 15642)
+++ branches/HiveProjectManagement/HeuristicLab.Services.Hive/3.3/HiveService.cs	(revision 15643)
@@ -507,5 +507,8 @@
           } else {
             var addedJobResourceIds = resourceIds.Except(job.AssignedJobResources.Select(x => x.ResourceId));
-            var removedJobResourceIds = job.AssignedJobResources.Select(x => x.ResourceId).Except(resourceIds);
+            var removedJobResourceIds = job.AssignedJobResources
+              .Select(x => x.ResourceId)
+              .Except(resourceIds)
+              .ToArray();
             
             // remove resource assignments
@@ -855,5 +858,5 @@
       AuthorizationManager.AuthorizeForProjectAdministration(projectId);
       var pm = PersistenceManager;
-      using (new PerformanceLogger("GrantProjectPermissions")) {
+      using (new PerformanceLogger("SaveProjectPermissions")) {
         var projectDao = pm.ProjectDao;
         var projectPermissionDao = pm.ProjectPermissionDao;
@@ -863,7 +866,6 @@
           var project = projectDao.GetById(projectId);
           if (project == null) return;
-          //if (project.ProjectPermissions == null) project.ProjectPermissions = new System.Data.Linq.EntitySet<DA.ProjectPermission>();
-          var projectPermissions = project.ProjectPermissions.Select(x => x.GrantedUserId);
-          var addedPermissions = grantedUserIds.Except(projectPermissions);
+          var projectPermissions = project.ProjectPermissions.Select(x => x.GrantedUserId).ToArray();
+          //var addedPermissions = grantedUserIds.Except(projectPermissions);
           var removedPermissions = projectPermissions.Except(grantedUserIds);
 
@@ -874,5 +876,7 @@
           } else {
             assignedJobResourceDao.DeleteByProjectIdAndUserIds(project.ProjectId, removedPermissions);
-            foreach(var item in project.ProjectPermissions.ToList().Where(x => removedPermissions.Contains(x.GrantedUserId))) {
+            foreach(var item in project.ProjectPermissions
+              .Where(x => removedPermissions.Contains(x.GrantedUserId))
+              .ToList()) {
               project.ProjectPermissions.Remove(item);
             }
@@ -907,5 +911,7 @@
                 p.ProjectPermissions.Clear();
               } else {
-                foreach(var item in p.ProjectPermissions.ToList().Where(x => removedPermissions.Contains(x.GrantedUserId))) {
+                foreach(var item in p.ProjectPermissions
+                  .Where(x => removedPermissions.Contains(x.GrantedUserId))
+                  .ToList()) {
                   p.ProjectPermissions.Remove(item);
                 }
@@ -979,5 +985,5 @@
       AuthorizationManager.AuthorizeForProjectResourceAdministration(projectId, resourceIds);
       var pm = PersistenceManager;
-      using (new PerformanceLogger("AssignProjectResources")) {
+      using (new PerformanceLogger("SaveProjectResourceAssignments")) {
         var projectDao = pm.ProjectDao;
         var assignedProjectResourceDao = pm.AssignedProjectResourceDao;
@@ -985,6 +991,6 @@
         pm.UseTransaction(() => {
           var project = projectDao.GetById(projectId);
-          var assignedResources = project.AssignedProjectResources.Select(x => x.ResourceId);
-          var addedAssignments = resourceIds.Except(assignedResources);
+          var assignedResources = project.AssignedProjectResources.Select(x => x.ResourceId).ToArray();
+          //var addedAssignments = resourceIds.Except(assignedResources);
           var removedAssignments = assignedResources.Except(resourceIds);
 
@@ -992,10 +998,10 @@
           if (reassign) {
             assignedJobResourceDao.DeleteByProjectId(project.ProjectId);
-            //assignedProjectResourceDao.DeleteByProjectIds(new List<Guid> { projectId });
             project.AssignedProjectResources.Clear();
           } else {
             assignedJobResourceDao.DeleteByProjectIdAndResourceIds(projectId, removedAssignments);
-            //assignedProjectResourceDao.DeleteByProjectIdAndResourceIds(projectId, removedAssignments);
-            foreach (var item in project.AssignedProjectResources.ToList().Where(x => removedAssignments.Contains(x.ResourceId))) {
+            foreach (var item in project.AssignedProjectResources
+              .Where(x => removedAssignments.Contains(x.ResourceId))
+              .ToList()) {
               project.AssignedProjectResources.Remove(item);
             }
@@ -1030,5 +1036,12 @@
               } else {
                 //assignedProjectResourceDao.DeleteByProjectIdAndResourceIds(p.ProjectId, removedAssignments);
-                foreach (var item in p.AssignedProjectResources.ToList().Where(x => removedAssignments.Contains(x.ResourceId))) {
+                //for(int i = p.AssignedProjectResources.Count -1; i >= 0; i--) {
+                //  if(removedAssignments.Contains(p.AssignedProjectResources[i].ResourceId)) {
+                //    p.AssignedProjectResources.RemoveAt(i);
+                //  }
+                //}
+                foreach (var item in p.AssignedProjectResources
+                  .Where(x => removedAssignments.Contains(x.ResourceId))
+                  .ToList()) {
                   p.AssignedProjectResources.Remove(item);
                 }
