From 46b20c85cdff377fe94090a1f212b330fb427ca9 Mon Sep 17 00:00:00 2001
From: "Barbara B. Frosik" <bfrosik@aps.anl.gov>
Date: Wed, 21 Jan 2015 22:29:38 +0000
Subject: [PATCH]

---
 .../controllers/ExperimentController.java     | 223 +++++++++---------
 .../dm/portal/model/entities/UserInfo.java    |  13 -
 2 files changed, 111 insertions(+), 125 deletions(-)

diff --git a/src/java/DmWebPortal/src/java/gov/anl/aps/dm/portal/controllers/ExperimentController.java b/src/java/DmWebPortal/src/java/gov/anl/aps/dm/portal/controllers/ExperimentController.java
index cc5e5b4a..1a1ea26e 100644
--- a/src/java/DmWebPortal/src/java/gov/anl/aps/dm/portal/controllers/ExperimentController.java
+++ b/src/java/DmWebPortal/src/java/gov/anl/aps/dm/portal/controllers/ExperimentController.java
@@ -7,7 +7,6 @@ import gov.anl.aps.dm.portal.exceptions.InvalidDate;
 import gov.anl.aps.dm.portal.exceptions.MissingProperty;
 import gov.anl.aps.dm.portal.model.entities.Experiment;
 import gov.anl.aps.dm.portal.model.beans.ExperimentFacade;
-import gov.anl.aps.dm.portal.model.beans.ExperimentTypeFacade;
 import gov.anl.aps.dm.portal.model.beans.RoleTypeFacade;
 import gov.anl.aps.dm.portal.model.beans.UserExperimentRoleFacade;
 import gov.anl.aps.dm.portal.model.beans.UserInfoFacade;
@@ -40,9 +39,6 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper
     @EJB
     private ExperimentFacade experimentFacade;
 
-    @EJB
-    private ExperimentTypeFacade experimentTypeFacade;
-
     @EJB
     private UserInfoFacade userInfoFacade;
 
@@ -54,102 +50,95 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper
     
     public class ExperimentUser extends CloneableEntity {
 
-    String username;
-    String name;
-    int userId;
-    int experimentId;
-    private final boolean [] roles;
+        String username;
+        String name;
+        int userId;
+        int experimentId;
+        private final boolean[] roles;
 
-    public ExperimentUser(int experimentId, int userId) {
-        roles = new boolean[maxExperimentRoleTypeId];
-        this.experimentId = experimentId;
-        this.userId = userId;
-    }
-    
-    public String getUsername() {
-        return username;
-    }
+        public ExperimentUser(int experimentId, int userId) {
+            roles = new boolean[maxExperimentRoleTypeId];
+            this.experimentId = experimentId;
+            this.userId = userId;
+        }
 
-    public void setUsername(String username) {
-        this.username = username;
-    }
+        public String getUsername() {
+            return username;
+        }
 
-    public String getName() {
-        return name;
-    }
+        public void setUsername(String username) {
+            this.username = username;
+        }
 
-    public void setName(String name) {
-        this.name = name;
-    }
+        public String getName() {
+            return name;
+        }
 
-    public boolean isIsManager() {
-        return roles[2];
-    }
+        public void setName(String name) {
+            this.name = name;
+        }
 
-    public void setIsManager(boolean isManager) {
-        logger.debug("setting Manager role for experiment "+ experimentId + " and user " + username + " to " + isManager);
-        roles[2] = isManager;
-    }
+        public boolean isIsManager() {
+            return roles[2];
+        }
 
-    public boolean isIsPI() {
-        return roles[3];
-    }
+        public void setIsManager(boolean isManager) {
+            roles[2] = isManager;
+        }
 
-    public void setIsPI(boolean isPI) {
-        roles[3] = isPI;
-    }
+        public boolean isIsPI() {
+            return roles[3];
+        }
 
-    public boolean isIsUser() {
-        return roles[4];
-    }
+        public void setIsPI(boolean isPI) {
+            roles[3] = isPI;
+        }
 
-    public void setIsUser(boolean isUser) {
-        roles[4] = isUser;
-    }
+        public boolean isIsUser() {
+            return roles[4];
+        }
 
-    public void setIsInRole(RoleType role, boolean isInRole) {
-        roles[role.getId()] = isInRole;
-    }
- 
-    public boolean[] getRoles() {
-        return roles;
-    }
-    
-    public void removeAllExperimentRoles() {
-        UserExperimentRole userExperimentRole;
-        for (int i = 1; i <= 4; i++) {
-        if (roles[i]) {
-                userExperimentRole = userExperimentRoleFacade.findByUserExperimentRole(userId, experimentId, i);
-                userExperimentRoleFacade.remove(userExperimentRole);
-            }            
-        }       
-    }
-    
-    public void removeExperimentRole(int roleId) {
-        UserExperimentRole userExperimentRole = userExperimentRoleFacade.findByUserExperimentRole(userId, experimentId, roleId);
-        userExperimentRoleFacade.remove(userExperimentRole);
-    }
+        public void setIsUser(boolean isUser) {
+            roles[4] = isUser;
+        }
 
-    public boolean canAddUsers() {
-        return roles[2] || roles[3];            
-    }
-    
-    @Override
-    public int hashCode() {
-        return userId + experimentId * 100;
-    }
+        public void setIsInRole(RoleType role, boolean isInRole) {
+            roles[role.getId()] = isInRole;
+        }
 
-    @Override
-    public boolean equals(Object object) {
-        // TODO: Warning - this method won't work in the case the id fields are not set
-        if (!(object instanceof ExperimentUser)) {
-            return false;
+        public boolean[] getRoles() {
+            return roles;
+        }
+
+        public void removeAllExperimentRoles() {
+            UserExperimentRole userExperimentRole;
+            for (int i = 1; i < roles.length; i++) {
+                if (roles[i]) {
+                    userExperimentRole = userExperimentRoleFacade.findByUserExperimentRole(userId, experimentId, i);
+                    userExperimentRoleFacade.remove(userExperimentRole);
+                }
+            }
+        }
+
+        public boolean canAddUsers() {
+            return roles[2] || roles[3];
+        }
+
+        @Override
+        public int hashCode() {
+            return userId + experimentId * 100;
+        }
+
+        @Override
+        public boolean equals(Object object) {
+            // TODO: Warning - this method won't work in the case the id fields are not set
+            if (!(object instanceof ExperimentUser)) {
+                return false;
+            }
+            return ((userId == ((ExperimentUser) object).userId) && (experimentId == ((ExperimentUser) object).experimentId));
         }
-        return ((userId == ((ExperimentUser)object).userId) && (experimentId == ((ExperimentUser)object).experimentId));
     }
 
-}
-    
     class ExperimentUsersTable extends DataTableController<ExperimentUser> {
 
         @Override
@@ -161,7 +150,6 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper
         public List<ExperimentUser> findAll() {
             List<UserInfo> list = userInfoFacade.findUsersInExperiment(getCurrent().getId());
             return convertExperimentUsers(list);
-//            return new ArrayList<>(tempUsersList);
         }
         
         List<ExperimentUser> convertExperimentUsers(List<UserInfo> list) {
@@ -208,19 +196,13 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper
         }
     }
     
-    private ExperimentController.ExperimentUsersTable experimentUsersListTable;
-    private ExperimentController.ExperimentUsersTable experimentUsersEditTable;
-    private ExperimentController.NoExperimentUsersTypeTable noExperimentUsersTypeTable;
+    private ExperimentController.ExperimentUsersTable experimentUsersListTable = new ExperimentController.ExperimentUsersTable();
+    private ExperimentController.ExperimentUsersTable experimentUsersEditTable = new ExperimentController.ExperimentUsersTable();
+    private ExperimentController.NoExperimentUsersTypeTable noExperimentUsersTypeTable = new ExperimentController.NoExperimentUsersTypeTable();
     final private Map<UserInfo, ExperimentUser> experimentUsers = new HashMap<>();
-    int maxExperimentRoleTypeId = 5;
+    int maxExperimentRoleTypeId = 0;
 
     public ExperimentController() {
-//        List<RoleType> roleTypesList = roleTypeFacade.findAll();
-//        for (RoleType roleType : roleTypesList) {
-//            if ((roleType.getId() > maxExperimentRoleTypeId) && !roleType.isIsSystemRole()) {
-//                maxExperimentRoleTypeId = roleType.getId();
-//            }
-//        }
         this.experimentUsersListTable = new ExperimentController.ExperimentUsersTable();
         this.experimentUsersEditTable = new ExperimentController.ExperimentUsersTable();
         this.noExperimentUsersTypeTable = new ExperimentController.NoExperimentUsersTypeTable();
@@ -257,15 +239,31 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper
     @Override
     public String prepareEdit(Experiment experiment) {
         clear();
+        if (maxExperimentRoleTypeId == 0) {
+            setMaxExperimentRoleTypeId();
+        }
         return super.prepareEdit(experiment);
     }
 
     @Override
     public String prepareView(Experiment experiment) {
         clear();
+        if (maxExperimentRoleTypeId == 0) {
+            setMaxExperimentRoleTypeId();
+        }
         return super.prepareView(experiment);
     }
 
+    private void setMaxExperimentRoleTypeId() {
+        List<RoleType> roleTypesList = roleTypeFacade.findAll();
+        for (RoleType roleType : roleTypesList) {
+            if ((roleType.getId() > maxExperimentRoleTypeId) && !roleType.isIsSystemRole()) {
+                maxExperimentRoleTypeId = roleType.getId();
+            }
+        }
+        maxExperimentRoleTypeId++;
+    }
+
     @Override
     public void prepareEntityInsert(Experiment experiment) throws ObjectAlreadyExists, InvalidDate, MissingProperty {
         if ((experiment.getName() == null) || (experiment.getName().length() == 0)){
@@ -356,8 +354,25 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper
         // experiment type permission
         // return experimentUsersMap.get(username).canAddUsers();
     }
+    
+     public void updateRemovedExperimentRoles() {
+        UserExperimentRole userExperimentRole;
+        List<RoleType> roleTypesList = roleTypeFacade.findAll();
+        for (UserInfo user : experimentUsers.keySet()) {
+            ExperimentUser experimentUser = experimentUsers.get(user);
+            for (RoleType roleType : roleTypesList) {
+                int roleId = roleType.getId();
+                if (!roleType.isIsSystemRole() && (!experimentUser.roles[roleId]) && (user.inRole(roleId, experimentUser.experimentId))) {
+                    userExperimentRole = userExperimentRoleFacade.findByUserExperimentRole(experimentUser.userId, experimentUser.experimentId, roleId);
+                    if (userExperimentRole != null) {
+                        userExperimentRoleFacade.remove(userExperimentRole);
+                    }
+                }
+            }
+        }
+    }
 
-     @Override
+    @Override
     public String update() {
         boolean updated;
         List<RoleType> roleTypesList = roleTypeFacade.findAll();
@@ -388,8 +403,6 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper
         }
     
         clear();
-//        experimentUsersListTable.resetList();
-//        experimentUsersEditTable.resetList();
         return super.update();
     }
 
@@ -401,7 +414,6 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper
     }
         
     public String removeUser() {
-
         List<RoleType> roleTypesList = roleTypeFacade.findAll();
         ExperimentUser experimentUser = experimentUsersEditTable.getCurrentObject();
         UserInfo user = findUserInExperiment(experimentUser);
@@ -421,19 +433,6 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper
         experimentUser.removeAllExperimentRoles();        
     }
     
-    public void updateRemovedExperimentRoles() {
-        List<RoleType> roleTypesList = roleTypeFacade.findAll();
-        for (UserInfo user : experimentUsers.keySet()) {
-            ExperimentUser experimentUser = experimentUsers.get(user);
-            for (RoleType roleType : roleTypesList) {
-                int roleId = roleType.getId();
-                if (!roleType.isIsSystemRole() && (!experimentUser.roles[roleId]) && (user.inRole(roleId, experimentUser.experimentId))) {
-                        experimentUser.removeExperimentRole(roleId);
-                }
-            }
-        }
-    }
-
     private UserInfo findUserInExperiment(ExperimentUser experimentUser) {
         for (UserInfo user : experimentUsers.keySet()) {
             if (experimentUsers.get(user).equals(experimentUser)) {
diff --git a/src/java/DmWebPortal/src/java/gov/anl/aps/dm/portal/model/entities/UserInfo.java b/src/java/DmWebPortal/src/java/gov/anl/aps/dm/portal/model/entities/UserInfo.java
index 20815919..5f315316 100644
--- a/src/java/DmWebPortal/src/java/gov/anl/aps/dm/portal/model/entities/UserInfo.java
+++ b/src/java/DmWebPortal/src/java/gov/anl/aps/dm/portal/model/entities/UserInfo.java
@@ -6,7 +6,6 @@
 
 package gov.anl.aps.dm.portal.model.entities;
 
-import java.util.ArrayList;
 import java.util.List;
 import javax.persistence.Basic;
 import javax.persistence.CascadeType;
@@ -208,18 +207,6 @@ public class UserInfo extends CloneableEntity
         }
     }
     
-    public void removeExperiment(int experimentId) {
-        List<UserExperimentRole> rolesToRemove = new ArrayList();
-        for (UserExperimentRole userExperimentRole : userExperimentRoleList) {
-            if (userExperimentRole.getUserExperimentRolePK().getExperimentId() == experimentId) {
-                rolesToRemove.add(userExperimentRole);
-            }
-        }
-        for (UserExperimentRole userExperimentRole : rolesToRemove) {
-            userExperimentRoleList.remove(userExperimentRole);
-        }        
-    }                
-    
     public void addUserExperimentRole(UserExperimentRole userExperimentRole) {
         userExperimentRoleList.add(userExperimentRole);
     }
-- 
GitLab