diff --git a/src/java/DmWebPortal/src/java/gov/anl/aps/dm/portal/constants/RoleTypeName.java b/src/java/DmWebPortal/src/java/gov/anl/aps/dm/portal/constants/RoleTypeName.java new file mode 100644 index 0000000000000000000000000000000000000000..a7d81ff26fe8a8b56bcff89ebb4c9a18eedb6d15 --- /dev/null +++ b/src/java/DmWebPortal/src/java/gov/anl/aps/dm/portal/constants/RoleTypeName.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package gov.anl.aps.dm.portal.constants; + +/** + * + * @author bfrosik + */ +public class RoleTypeName { + public static final String ADMIN = "Administrator"; + public static final String EXPERIMENT_ADMIN = "Experiment Administrator"; + public static final String MANAGER = "Manager"; + public static final String PI = "Principal Investigator"; + public static final String USER = "User"; +} 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 46149030c6d90cbddd4db4a3dff8ec04238ccd75..9cb7c6aedfb33e718338ac5ca230003d8b8ebd12 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 @@ -1,6 +1,7 @@ package gov.anl.aps.dm.portal.controllers; +import gov.anl.aps.dm.portal.constants.RoleTypeName; import gov.anl.aps.dm.portal.exceptions.DmPortalException; import gov.anl.aps.dm.portal.exceptions.ObjectAlreadyExists; import gov.anl.aps.dm.portal.exceptions.InvalidDate; @@ -48,20 +49,16 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper @EJB private UserExperimentRoleFacade userExperimentRoleFacade; - int USER_ROLE_ID = 4; - int PI_ROLE_ID = 3; - int MANAGER_ROLE_ID = 2; - public class ExperimentUser extends CloneableEntity { String username; String name; int userId; int experimentId; - private final boolean[] roles; + private final boolean[] userRoles; public ExperimentUser(int experimentId, int userId) { - roles = new boolean[maxExperimentRoleTypeId]; + userRoles = new boolean[maxExperimentRoleTypeId]; this.experimentId = experimentId; this.userId = userId; } @@ -83,43 +80,43 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper } public boolean isIsManager() { - return roles[MANAGER_ROLE_ID]; + return userRoles[experimentRoles.get(RoleTypeName.MANAGER).getId()]; } public void setIsManager(boolean isManager) { - roles[MANAGER_ROLE_ID] = isManager; + userRoles[experimentRoles.get(RoleTypeName.MANAGER).getId()] = isManager; } public boolean isIsPI() { - return roles[PI_ROLE_ID]; + return userRoles[experimentRoles.get(RoleTypeName.PI).getId()]; } public void setIsPI(boolean isPI) { - roles[PI_ROLE_ID] = isPI; + userRoles[experimentRoles.get(RoleTypeName.PI).getId()] = isPI; } public boolean isIsUser() { - return roles[USER_ROLE_ID]; + return userRoles[experimentRoles.get(RoleTypeName.USER).getId()]; } public void setIsUser(boolean isUser) { - roles[USER_ROLE_ID] = isUser; + userRoles[experimentRoles.get(RoleTypeName.USER).getId()] = isUser; } public void setIsInRole(RoleType role, boolean isInRole) { - roles[role.getId()] = isInRole; + userRoles[role.getId()] = isInRole; } public boolean[] getRoles() { - return roles; + return userRoles; } 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); + for (RoleType role : experimentRoles.values()) { + if (userRoles[role.getId()]) { + userExperimentRole = userExperimentRoleFacade.findByUserExperimentRole(userId, experimentId, role.getId()); + userExperimentRoleFacade.remove(userExperimentRole); } } } @@ -200,6 +197,7 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper private ExperimentController.ExperimentUsersTable experimentUsersEditTable = new ExperimentController.ExperimentUsersTable(); private ExperimentController.NoExperimentUsersTypeTable noExperimentUsersTypeTable = new ExperimentController.NoExperimentUsersTypeTable(); final private Map<UserInfo, ExperimentUser> experimentUsers = new HashMap<>(); + final private Map<String, RoleType> experimentRoles = new HashMap<>(); int maxExperimentRoleTypeId = 0; public ExperimentController() { @@ -257,8 +255,11 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper private void setMaxExperimentRoleTypeId() { List<RoleType> roleTypesList = roleTypeFacade.findAll(); for (RoleType roleType : roleTypesList) { - if ((roleType.getId() > maxExperimentRoleTypeId) && !roleType.isIsSystemRole()) { - maxExperimentRoleTypeId = roleType.getId(); + if (!roleType.isIsSystemRole()) { + experimentRoles.put(roleType.getName(), roleType); + if (roleType.getId() > maxExperimentRoleTypeId) { + maxExperimentRoleTypeId = roleType.getId(); + } } } maxExperimentRoleTypeId++; @@ -340,12 +341,11 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper public void updateRemovedExperimentRoles() { UserExperimentRole userExperimentRole; - List<RoleType> roleTypesList = roleTypeFacade.findAll(); for (UserInfo user : experimentUsers.keySet()) { ExperimentUser experimentUser = experimentUsers.get(user); - for (RoleType roleType : roleTypesList) { + for (RoleType roleType : experimentRoles.values()) { int roleId = roleType.getId(); - if (!roleType.isIsSystemRole() && (!experimentUser.roles[roleId]) && (user.inRole(roleId, experimentUser.experimentId))) { + if ((!experimentUser.userRoles[roleId]) && (user.inRole(roleId, experimentUser.experimentId))) { userExperimentRole = userExperimentRoleFacade.findByUserExperimentRole(experimentUser.userId, experimentUser.experimentId, roleId); if (userExperimentRole != null) { userExperimentRoleFacade.remove(userExperimentRole); @@ -358,22 +358,21 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper @Override public String update() { boolean updated; - List<RoleType> roleTypesList = roleTypeFacade.findAll(); for (UserInfo user : experimentUsers.keySet()) { updated = false; ExperimentUser experimentUser = experimentUsers.get(user); - for (RoleType roleType : roleTypesList) { + for (RoleType roleType : experimentRoles.values()) { if (!roleType.isIsSystemRole()) { int roleId = roleType.getId(); if (!user.inRole(roleId, experimentUser.experimentId)) { - if (experimentUser.roles[roleId]) { + if (experimentUser.userRoles[roleId]) { UserExperimentRole userExperimentRole = new UserExperimentRole(experimentUser.userId, experimentUser.experimentId, roleId); user.addUserExperimentRole(userExperimentRole); updated = true; logger.debug("adding userExperimentRole " + experimentUser.userId + " " + experimentUser.experimentId + " " + roleId); } } - else if (!experimentUser.roles[roleId]) { + else if (!experimentUser.userRoles[roleId]) { user.removeExperimentRole(roleId, experimentUser.experimentId); updated = true; logger.debug("adding userExperimentRole " + experimentUser.userId + " " + experimentUser.experimentId + " " + roleId); @@ -402,7 +401,7 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper UserInfo user = findUserInExperiment(experimentUser); for (RoleType roleType : roleTypesList) { int roleId = roleType.getId(); - if (experimentUser.roles[roleId]) { + if (experimentUser.userRoles[roleId]) { user.removeExperimentRole(roleId, experimentUser.experimentId); } } @@ -437,7 +436,7 @@ public class ExperimentController extends CrudEntityController<Experiment, Exper try { UserExperimentRole userExperimentRole; for (UserInfo user : newUsers) { - userExperimentRole = new UserExperimentRole(user.getId(), current.getId(), USER_ROLE_ID); // set the User + userExperimentRole = new UserExperimentRole(user.getId(), current.getId(), experimentRoles.get(RoleTypeName.USER).getId()); // set the User user.getUserExperimentRoleList().add(userExperimentRole); userInfoFacade.edit(user); } diff --git a/src/java/DmWebPortal/web/views/roleType/roleTypeEditPanelGrid.xhtml b/src/java/DmWebPortal/web/views/roleType/roleTypeEditPanelGrid.xhtml index 57d11e1e90a807ec76abf941f767156b8cc0dc92..9bf391922d31f60752868de7da85c6deeeffe7b6 100644 --- a/src/java/DmWebPortal/web/views/roleType/roleTypeEditPanelGrid.xhtml +++ b/src/java/DmWebPortal/web/views/roleType/roleTypeEditPanelGrid.xhtml @@ -12,7 +12,7 @@ <p:panelGrid columns="2" styleClass="editEntityDetails"> <h:outputLabel for="name" value="Name" styleClass="entityDataEmphasizedLabel"/> - <h:inputText id="name" value="#{roleTypeObject.name}" title="Name" styleClass="entityDataEmphasizedText"/> + <h:outputText id="name" value="#{roleTypeObject.name}" title="Name" styleClass="entityDataEmphasizedText"/> <h:outputLabel for="description" value="Description" styleClass="entityDataLabel"/> <h:inputText id="description" value="#{roleTypeObject.description}" title="Description" styleClass="entityDataText"/>