Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • DM/dm-docs
  • hammonds/dm-docs
  • hparraga/dm-docs
3 results
Show changes
Showing
with 3568 additions and 0 deletions
# 'USERNAME' string will be replaced by the actual username
dm.portal.ldapUrl=ldaps://phoebusldap.aps.anl.gov:636
dm.portal.ldapDnString=uid=USERNAME,ou=people,o=aps.anl.gov,dc=aps,dc=anl,dc=gov
package gov.anl.aps.dm.portal.constants;
/**
* Status codes.
*/
public class DmStatus
{
public static final int DM_OK = 0;
public static final int DM_ERROR = 1;
public static final int DM_DB_ERROR = 2;
public static final int DM_TIMEOUT = 3;
public static final int DM_INVALID_ARGUMENT = 4;
public static final int DM_INVALID_OBJECT_STATE = 5;
public static final int DM_OBJECT_ALREADY_EXISTS = 6;
public static final int DM_OBJECT_NOT_FOUND = 7;
public static final int DM_INVALID_DATE = 8;
public static final int DM_MISSING_PROPERTY = 9;
}
\ No newline at end of file
/*
* 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";
}
package gov.anl.aps.dm.portal.controllers;
import gov.anl.aps.dm.portal.exceptions.DmPortalException;
import gov.anl.aps.dm.portal.exceptions.MissingProperty;
import gov.anl.aps.dm.portal.exceptions.ObjectAlreadyExists;
import gov.anl.aps.dm.portal.model.entities.AllowedPolicyValue;
import gov.anl.aps.dm.portal.model.beans.AllowedPolicyValueFacade;
import gov.anl.aps.dm.portal.model.entities.PolicyProperty;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.apache.log4j.Logger;
@Named("allowedPolicyValueController")
@SessionScoped
public class AllowedPolicyValueController extends CrudEntityController<AllowedPolicyValue, AllowedPolicyValueFacade>
{
private static final Logger logger = Logger.getLogger(AllowedPolicyValueController.class.getName());
class ValueCopy {
private final String value;
private final String description;
ValueCopy(String value, String description) {
this.value = value;
this.description = description;
}
boolean isUpdated(AllowedPolicyValue editedAllowedPolicyValue) {
return (!editedAllowedPolicyValue.getPolicyValue().equals(value) || !editedAllowedPolicyValue.getDescription().equals(description));
}
String getValue() {
return value;
}
String getDescription() {
return description;
}
}
@EJB
private AllowedPolicyValueFacade allowedPolicyValueFacade;
private final Map<AllowedPolicyValue, ValueCopy> editMap = new HashMap<>();
class AllowedPolicyValueInfoTable extends DataTableController<AllowedPolicyValue> {
@Override
public String getClassName() {
return "AllowedPolicyValue";
}
@Override
public List<AllowedPolicyValue> findAll() {
return allowedPolicyValueFacade.findByPolicyPropertyId(getPolicyProperty().getId());
}
}
private AllowedPolicyValueInfoTable allowedPolicyValueInfoTable = new AllowedPolicyValueInfoTable();
private PolicyProperty policyProperty;
private int rows = 25;
public AllowedPolicyValueController() {
}
public AllowedPolicyValueInfoTable getAllowedPolicyValueInfoTable() {
return allowedPolicyValueInfoTable;
}
public void setAllowedPolicyValueInfoTable(AllowedPolicyValueInfoTable allowedPolicyValueInfoTable) {
this.allowedPolicyValueInfoTable = allowedPolicyValueInfoTable;
}
public PolicyProperty getPolicyProperty() {
return policyProperty;
}
public void setPolicyProperty(PolicyProperty policyProperty) {
clear();
this.policyProperty = policyProperty;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
@Override
protected AllowedPolicyValueFacade getFacade() {
return allowedPolicyValueFacade;
}
@Override
public DataModel createListDataModel() {
List<AllowedPolicyValue> allowedValueList = allowedPolicyValueFacade.findByPolicyPropertyId(policyProperty.getId());
for (AllowedPolicyValue allowedPolicyValue : allowedValueList) {
editMap.put(allowedPolicyValue, new ValueCopy(allowedPolicyValue.getPolicyValue(), allowedPolicyValue.getDescription()));
}
return new ListDataModel((List) allowedValueList);
}
@Override
public List<AllowedPolicyValue> getAvailableItems() {
List<AllowedPolicyValue> allowedValueList = allowedPolicyValueFacade.findByPolicyPropertyId(policyProperty.getId());
return (allowedValueList);
}
@Override
protected AllowedPolicyValue createEntityInstance() {
return new AllowedPolicyValue();
}
@Override
public String getEntityTypeName() {
return "AllowedPolicyValue";
}
@Override
public String getCurrentEntityInstanceName() {
if (getCurrent() != null) {
return getCurrent().getPolicyValue();
}
return "";
}
@Override
public String prepareEdit(AllowedPolicyValue allowedPolicyValue) {
super.prepareEdit(allowedPolicyValue);
return "/views/allowedPolicyValue/edit?faces-redirect=true";
}
@Override
public String prepareView(AllowedPolicyValue allowedPolicyValue) {
current = allowedPolicyValue;
super.prepareView(allowedPolicyValue);
return "/views/allowedPolicyValue/view?faces-redirect=true";
}
@Override
public String prepareCreate() {
super.prepareCreate();
return "/views/allowedPolicyValue/create?faces-redirect=true";
}
@Override
public String create() {
AllowedPolicyValue newValue = current;
if (super.create() == null) {
return null;
}
policyProperty.getAllowedPolicyValueList().add(newValue);
allowedPolicyValueInfoTable.resetList();
return null;
}
@Override
public String destroy() {
AllowedPolicyValue propertyValue = current;
super.destroy();
policyProperty.getAllowedPolicyValueList().remove(propertyValue);
clear();
return null;
}
@Override
public void prepareEntityInsert(AllowedPolicyValue allowedPolicyValue) throws ObjectAlreadyExists, MissingProperty {
current.setPolicyProperty(policyProperty);
if ((allowedPolicyValue.getPolicyValue() == null) || (allowedPolicyValue.getPolicyValue().length() == 0)) {
throw new MissingProperty("Policy Value is missing.");
}
List<AllowedPolicyValue> propertyAllowedPolicyValueList = allowedPolicyValueFacade.findByPolicyPropertyId(policyProperty.getId());
for (AllowedPolicyValue policyValue : propertyAllowedPolicyValueList) {
if ((allowedPolicyValue.getPolicyValue().equals(policyValue.getPolicyValue()))) {
throw new ObjectAlreadyExists("allowed policy value " + allowedPolicyValue.getPolicyValue() + " already exists for this property.");
}
}
}
@Override
public void prepareEntityUpdate(AllowedPolicyValue allowedPolicyValue) throws DmPortalException {
if ((allowedPolicyValue.getPolicyValue() == null) || (allowedPolicyValue.getPolicyValue().length() == 0)) {
throw new MissingProperty("Policy Value is missing.");
}
ValueCopy tempValueCopy = editMap.get(allowedPolicyValue);
editMap.remove(allowedPolicyValue);
Collection<ValueCopy> propertyAllowedPolicyValueList = editMap.values();
for (ValueCopy valueCopy : propertyAllowedPolicyValueList) {
if ((valueCopy.getValue().equals(allowedPolicyValue.getPolicyValue()))) {
throw new ObjectAlreadyExists("allowed policy value " + allowedPolicyValue.getPolicyValue() + " already exists for this property.");
}
}
editMap.put(allowedPolicyValue, tempValueCopy);
}
@Override
public String prepareList() {
logger.debug("Preparing list");
resetListDataModel();
return "/views/policyProperty/view?faces-redirect=true";
}
@Override
protected String getObjectAlreadyExistMessage(AllowedPolicyValue entity) {
return null;
}
@Override
public void clear() {
allowedPolicyValueInfoTable.resetList();
resetList();
editMap.clear();
}
@Override
public String update() {
Iterator<AllowedPolicyValue> iterator = getListDataModel().iterator();
while (iterator.hasNext()) {
AllowedPolicyValue editedAllowedPolicyValue = iterator.next();
ValueCopy valueCopy = editMap.get(editedAllowedPolicyValue);
if ((valueCopy != null) && (valueCopy.isUpdated(editedAllowedPolicyValue))) {
current = editedAllowedPolicyValue;
super.update();
}
}
clear();
return null;
}
}
\ No newline at end of file
package gov.anl.aps.dm.portal.controllers;
import gov.anl.aps.dm.portal.exceptions.DmPortalException;
import gov.anl.aps.dm.portal.model.beans.AbstractFacade;
import gov.anl.aps.dm.portal.model.entities.CloneableEntity;
import gov.anl.aps.dm.portal.utilities.CollectionUtility;
import gov.anl.aps.dm.portal.utilities.SessionUtility;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import org.apache.log4j.Logger;
import org.primefaces.component.datatable.DataTable;
public abstract class CrudEntityController<EntityType extends CloneableEntity, FacadeType extends AbstractFacade<EntityType>> implements Serializable
{
private static final Logger logger = Logger.getLogger(CrudEntityController.class.getName());
protected EntityType current = null;
private DataModel listDataModel = null;
private DataTable listDataTable = null;
private boolean listDataModelReset = true;
private List<EntityType> filteredObjectList = null;
private DataModel selectDataModel = null;
private DataTable selectDataTable = null;
private boolean selectDataModelReset = false;
private List<EntityType> selectedObjectList = null;
public CrudEntityController() {
}
@PostConstruct
public void initialize() {
}
protected abstract FacadeType getFacade();
protected abstract EntityType createEntityInstance();
public abstract String getEntityTypeName();
public String getDisplayEntityTypeName() {
return getEntityTypeName();
}
public abstract String getCurrentEntityInstanceName();
public EntityType getCurrent() {
return current;
}
public void setCurrent(EntityType current) {
this.current = current;
}
public void selectByRequestParams() {
}
public EntityType getSelected() {
if (current == null) {
current = createEntityInstance();
}
return current;
}
public String resetList() {
logger.debug("Resetting list");
return prepareList();
}
public String prepareList() {
logger.debug("Preparing list");
resetListDataModel();
return "list?faces-redirect=true";
}
public DataTable getListDataTable() {
if (listDataTable == null) {
logger.debug("Recreating data table");
listDataTable = new DataTable();
}
return listDataTable;
}
public void setListDataTable(DataTable listDataTable) {
this.listDataTable = listDataTable;
}
public DataTable getSelectDataTable() {
return selectDataTable;
}
public void setSelectDataTable(DataTable selectDataTable) {
this.selectDataTable = selectDataTable;
}
public boolean isAnyListFilterSet() {
if (listDataTable == null) {
return false;
}
Map<String, Object> filterMap = listDataTable.getFilters();
for (Object filter : filterMap.values()) {
if ((String)filter != null && !((String)filter).isEmpty()) {
return true;
}
}
return false;
}
public String prepareView(EntityType entity) {
clear();
logger.debug("Preparing view");
current = entity;
return "view?faces-redirect=true";
}
public void clear() {
}
public String view() {
return "view?faces-redirect=true";
}
public String prepareCreate() {
current = createEntityInstance();
return "create?faces-redirect=true";
}
public EntityType cloneEntityInstance(EntityType entity) {
EntityType clonedEntity;
try {
clonedEntity = (EntityType) (entity.clone());
}
catch (CloneNotSupportedException ex) {
logger.error("Object cannot be cloned: " + ex);
clonedEntity = createEntityInstance();
}
return clonedEntity;
}
public String prepareClone(EntityType entity) {
current = cloneEntityInstance(entity);
return "create?faces-redirect=true";
}
protected void prepareEntityInsert(EntityType entity) throws DmPortalException {
}
public String create() {
try {
EntityType newEntity = current;
prepareEntityInsert(current);
getFacade().create(current);
SessionUtility.addInfoMessage("Success", "Created " + getDisplayEntityTypeName() + " " + getCurrentEntityInstanceName() + ".");
resetListDataModel();
current = newEntity;
return prepareList();
}
catch (DmPortalException | RuntimeException ex) {
SessionUtility.addErrorMessage("Error", "Could not create " + getDisplayEntityTypeName() + ": " + ex.getMessage());
return null;
}
}
public String prepareEdit(EntityType entity) {
current = entity;
return edit();
}
public String edit() {
resetSelectDataModel();
return "edit?faces-redirect=true";
}
protected void prepareEntityUpdate(EntityType entity) throws DmPortalException {
}
public String update() {
try {
logger.debug("Updating " + getDisplayEntityTypeName() + " " + getCurrentEntityInstanceName());
prepareEntityUpdate(current);
EntityType updatedEntity = getFacade().edit(current);
SessionUtility.addInfoMessage("Success", "Updated " + getDisplayEntityTypeName() + " " + getCurrentEntityInstanceName() + ".");
resetListDataModel();
current = updatedEntity;
return view();
}
catch (DmPortalException ex) {
SessionUtility.addErrorMessage("Error", "Could not update " + getDisplayEntityTypeName() + ": " + ex.getMessage());
return null;
}
catch (RuntimeException ex) {
SessionUtility.addErrorMessage("Error", "Could not update " + getDisplayEntityTypeName() + ": " + getObjectAlreadyExistMessage(current));
return null;
}
}
abstract protected String getObjectAlreadyExistMessage(EntityType entity);
public void destroy(EntityType entity) {
current = entity;
destroy();
}
public String destroy() {
if (current == null) {
logger.warn("Current item is not set");
// Do nothing if current item is not set.
return null;
}
try {
logger.debug("Destroying " + getCurrentEntityInstanceName());
getFacade().remove(current);
SessionUtility.addInfoMessage("Success", "Deleted " + getDisplayEntityTypeName() + " " + getCurrentEntityInstanceName() + ".");
resetListDataModel();
return prepareList();
}
catch (Exception ex) {
SessionUtility.addErrorMessage("Error", "Could not delete " + getDisplayEntityTypeName() + ": " + ex.getMessage());
return null;
}
}
public DataModel createListDataModel() {
return new ListDataModel(getFacade().findAll());
}
public DataModel getListDataModel() {
if (listDataModel == null) {
listDataModel = createListDataModel();
}
return listDataModel;
}
public void prepareEntityListForSelection(List<EntityType> selectEntityList) {
}
public DataModel createSelectDataModel() {
List<EntityType> selectEntityList = getFacade().findAll();
prepareEntityListForSelection(selectEntityList);
return new ListDataModel(selectEntityList);
}
public DataModel getSelectDataModel() {
if (selectDataModel == null) {
selectDataModel = createSelectDataModel();
}
return selectDataModel;
}
public DataModel getItems() {
return getListDataModel();
}
public List<EntityType> getSelectedObjectListAndResetSelectDataModel() {
List<EntityType> returnList = selectedObjectList;
resetSelectDataModel();
return returnList;
}
public List<EntityType> getSelectedObjectList() {
return selectedObjectList;
}
public List<EntityType> getFilteredObjectList() {
return filteredObjectList;
}
public List<EntityType> getFilteredItems() {
return filteredObjectList;
}
public void resetSelectedObjectList() {
selectedObjectList = null;
}
public void setSelectedObjectList(List<EntityType> selectedObjectList) {
this.selectedObjectList = selectedObjectList;
}
public void setFilteredObjectList(List<EntityType> filteredObjectList) {
this.filteredObjectList = filteredObjectList;
}
public void setFilteredItems(List<EntityType> filteredItems) {
this.filteredObjectList = filteredItems;
}
public void resetListDataModel() {
listDataModel = null;
listDataTable = null;
listDataModelReset = true;
filteredObjectList = null;
current = null;
}
public void resetSelectDataModel() {
selectDataModel = null;
selectDataTable = null;
selectedObjectList = null;
selectDataModelReset = true;
}
public List<EntityType> getAvailableItems() {
return getFacade().findAll();
}
public EntityType getEntity(Integer id) {
return getFacade().find(id);
}
public SelectItem[] getAvailableItemsForSelectMany() {
return CollectionUtility.getSelectItems(getFacade().findAll(), false);
}
public SelectItem[] getAvailableItemsForSelectOne() {
return CollectionUtility.getSelectItems(getFacade().findAll(), true);
}
public String getCurrentViewId() {
return SessionUtility.getCurrentViewId();
}
public static String displayEntityList(List<?> entityList) {
String itemDelimiter = ", ";
return CollectionUtility.displayItemListWithoutOutsideDelimiters(entityList, itemDelimiter);
}
public boolean isListDataModelReset() {
if (listDataModelReset) {
listDataModelReset = false;
return true;
}
return false;
}
public boolean isSelectDataModelReset() {
if (selectDataModelReset) {
selectDataModelReset = false;
return true;
}
return false;
}
}
package gov.anl.aps.dm.portal.controllers;
import gov.anl.aps.dm.portal.model.entities.CloneableEntity;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.apache.log4j.Logger;
import org.primefaces.component.datatable.DataTable;
public abstract class DataTableController<EntityType extends CloneableEntity> implements Serializable
{
private static final Logger logger = Logger.getLogger(DataTableController.class.getName());
protected EntityType currentObject = null;
private DataModel listDataModel = null;
private DataTable listDataTable = null;
private boolean listDataModelReset = true;
private List<EntityType> filteredObjectList = null;
private List<EntityType> selectedObjectList = null;
public DataTableController() {
}
@PostConstruct
public void initialize() {
}
public abstract String getClassName();
public EntityType getCurrentObject() {
return currentObject;
}
public void setCurrentObject(EntityType current) {
this.currentObject = current;
}
public DataModel createListDataModel() {
return new ListDataModel(findAll());
}
public DataModel getListDataModel() {
if (listDataModel == null) {
listDataModel = createListDataModel();
}
return listDataModel;
}
public abstract List<EntityType> findAll();
public String getTableName() {
return "deafault";
}
public void resetList() {
logger.debug("Resetting list"+" "+getTableName());
listDataModel = null;
listDataTable = null;
listDataModelReset = true;
filteredObjectList = null;
selectedObjectList = null;
currentObject = null;
}
public DataTable getListDataTable() {
if (listDataTable == null) {
logger.debug("Recreating data table"+" "+getTableName());
listDataTable = new DataTable();
}
return listDataTable;
}
public void setListDataTable(DataTable listDataTable) {
this.listDataTable = listDataTable;
}
public boolean isAnyListFilterSet() {
if (listDataTable == null) {
return false;
}
Map<String, Object> filterMap = listDataTable.getFilters();
for (Object filter : filterMap.values()) {
if ((String)filter != null && !((String)filter).isEmpty()) {
return true;
}
}
return false;
}
public List<EntityType> getFilteredObjectList() {
return filteredObjectList;
}
public void setFilteredObjectList(List<EntityType> filteredObjectList) {
this.filteredObjectList = filteredObjectList;
}
public List<EntityType> getFilteredItems() {
return filteredObjectList;
}
public void setFilteredItems(List<EntityType> filteredItems) {
this.filteredObjectList = filteredItems;
}
public void resetFilterList() {
filteredObjectList = null;
}
public boolean isListDataModelReset() {
if (listDataModelReset) {
listDataModelReset = false;
return true;
}
return false;
}
public List<EntityType> getSelectedObjectList() {
return selectedObjectList;
}
public void setSelectedObjectList(List<EntityType> selectedObjectList) {
this.selectedObjectList = selectedObjectList;
}
}
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;
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.ExperimentPolicyFacade;
import gov.anl.aps.dm.portal.model.beans.ExperimentPolicyPropertyValueFacade;
import gov.anl.aps.dm.portal.model.beans.PolicyTypeFacade;
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;
import gov.anl.aps.dm.portal.model.entities.CloneableEntity;
import gov.anl.aps.dm.portal.model.entities.ExperimentPolicy;
import gov.anl.aps.dm.portal.model.entities.ExperimentPolicyPropertyValue;
import gov.anl.aps.dm.portal.model.entities.PolicyProperty;
import gov.anl.aps.dm.portal.model.entities.PolicyType;
import gov.anl.aps.dm.portal.model.entities.RoleType;
import gov.anl.aps.dm.portal.model.entities.UserExperimentRole;
import gov.anl.aps.dm.portal.model.entities.UserInfo;
import gov.anl.aps.dm.portal.utilities.SessionUtility;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.apache.log4j.Logger;
@Named("experimentController")
@SessionScoped
public class ExperimentController extends CrudEntityController<Experiment, ExperimentFacade>
{
private static final Logger logger = Logger.getLogger(ExperimentController.class.getName());
@EJB
private ExperimentFacade experimentFacade;
@EJB
private UserInfoFacade userInfoFacade;
@EJB
private RoleTypeFacade roleTypeFacade;
@EJB
private UserExperimentRoleFacade userExperimentRoleFacade;
@EJB
private PolicyTypeFacade policyTypeFacade;
@EJB
private ExperimentPolicyFacade experimentPolicyFacade;
@EJB
private ExperimentPolicyPropertyValueFacade experimentPolicyPropertyValueFacade;
public class ExperimentUser extends CloneableEntity {
String name;
int experimentId;
private final UserInfo user;
private final boolean[] userRoles;
public ExperimentUser(int experimentId, UserInfo user) {
userRoles = new boolean[maxExperimentRoleTypeId + 1];
this.experimentId = experimentId;
this.user = user;
}
public String getBadge() {
return user.getBadge();
}
public UserInfo getUser() {
return user;
}
public String getGlobusUsername() {
return user.getGlobusUsername();
}
public String getUsername() {
return user.getUsername();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isIsManager() {
return userRoles[experimentRoles.get(RoleTypeName.MANAGER).getId()];
}
public void setIsManager(boolean isManager) {
userRoles[experimentRoles.get(RoleTypeName.MANAGER).getId()] = isManager;
}
public boolean isIsPI() {
return userRoles[experimentRoles.get(RoleTypeName.PI).getId()];
}
public void setIsPI(boolean isPI) {
userRoles[experimentRoles.get(RoleTypeName.PI).getId()] = isPI;
}
public boolean isIsUser() {
return userRoles[experimentRoles.get(RoleTypeName.USER).getId()];
}
public void setIsUser(boolean isUser) {
userRoles[experimentRoles.get(RoleTypeName.USER).getId()] = isUser;
}
public void setIsInRole(RoleType role, boolean isInRole) {
userRoles[role.getId()] = isInRole;
}
public boolean[] getRoles() {
return userRoles;
}
@Override
public int hashCode() {
return user.getId() + 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 ((user.getId() == ((ExperimentUser) object).user.getId()) && (experimentId == ((ExperimentUser) object).experimentId));
}
}
@SessionScoped
class ExperimentUsersTable extends DataTableController<ExperimentUser> {
@Override
public String getClassName() {
return "ExperimentUser";
}
@Override
public List<ExperimentUser> findAll() {
List<UserInfo> list = userInfoFacade.findUsersInExperiment(getCurrent().getId());
return convertExperimentUsers(list);
}
List<ExperimentUser> convertExperimentUsers(List<UserInfo> list) {
if (!initialized) {
initializeTables();
}
logger.debug("converting ExperimentUser ");
ExperimentUser experimentUser;
for (UserInfo user : list) {
if ((experimentUser = experimentUsers.get(user)) == null) {
experimentUser = new ExperimentUser(getCurrent().getId(), user);
experimentUser.setName(user.getLastName() + ", " + user.getFirstName());
experimentUsers.put(user, experimentUser);
}
for (RoleType roleType : experimentRoles.values()) {
boolean inRole = (user.getExperimentRole(roleType.getId(), getCurrent().getId()) != null);
experimentUser.setIsInRole(roleType, inRole);
}
}
return new ArrayList<>(experimentUsers.values());
}
@Override
public String getTableName() {
return "experimentUsersTable";
}
}
@SessionScoped
class NoExperimentUsersTypeTable extends DataTableController<UserInfo> {
@Override
public String getClassName() {
return "UserInfo";
}
@Override
public List<UserInfo> findAll() {
resetList();
return userInfoFacade.findNoUsersInExperiment(getCurrent().getId());
}
@Override
public String getTableName() {
return "noExperimentUsersTable";
}
}
@SessionScoped
class ExperimentPolicyTable extends DataTableController<ExperimentPolicy> {
boolean forEdit;
ExperimentPolicyTable(boolean forEdit) {
this.forEdit = forEdit;
}
@Override
public String getClassName() {
return "ExperimentPolicy";
}
@Override
public List<ExperimentPolicy> findAll() {
resetList();
List<ExperimentPolicy> experimentPolicies = experimentPolicyFacade.findByExperimentId(current.getId());
if (forEdit) {
experimentPropertiesMap.clear();
for (ExperimentPolicy experimentPolicy : experimentPolicies) {
for (ExperimentPolicyPropertyValue experimentPolicyPropertyValue : experimentPolicy.getExperimentPolicyPropertyValueList()) {
experimentPropertiesMap.put(experimentPolicyPropertyValue.getId(), experimentPolicyPropertyValue.getPolicyPropertyValue());
}
}
}
return experimentPolicies;
}
@Override
public String getTableName() {
return "experimentPolicyTable";
}
}
public class PolicyPropertyValue extends CloneableEntity {
private PolicyProperty policyProperty;
private String value;
public PolicyProperty getPolicyProperty() {
return policyProperty;
}
public void setPolicyProperty(PolicyProperty policyProperty) {
this.policyProperty = policyProperty;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public boolean hasAllowedValues() {
return (policyProperty.getAllowedPolicyValueList() != null) && (policyProperty.getAllowedPolicyValueList().size() > 0);
}
public int getId() {
return policyProperty.getId();
}
}
public class MissingPolicyType extends CloneableEntity {
private List<PolicyPropertyValue> noPropertyList = new ArrayList<>();
private final int id;
private final String name;
MissingPolicyType(int id, String name) {
this.id = id;
this.name = name;
}
public List<PolicyPropertyValue> getNoPropertyList() {
return noPropertyList;
}
public void setNoPropertyList(List<PolicyPropertyValue> noPropertyList) {
this.noPropertyList = noPropertyList;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
protected void clear() {
noPropertyList.clear();
}
protected boolean isEmpty() {
return noPropertyList.isEmpty();
}
}
@SessionScoped
public class NoPolicyValueTable extends DataTableController<PolicyPropertyValue> {
NoPolicyValueTable() {
}
@Override
public String getClassName() {
return "PolicyPropertyValue";
}
@Override
public List<PolicyPropertyValue> findAll() {
setMissingProperties(experimentPolicyFacade.findByExperimentId(current.getId()), policyTypeFacade.findAll());
List<PolicyPropertyValue> missingProperties = new ArrayList<>();
for (MissingPolicyType missingPolicyType : missingPolicyTypes) {
missingProperties.addAll(missingPolicyType.getNoPropertyList());
}
return missingProperties;
}
}
private final int rows = 25;
private ExperimentPolicyPropertyValue experimentPropertyToDelete;
private ExperimentController.ExperimentUsersTable experimentUsersListTable = new ExperimentController.ExperimentUsersTable();
private ExperimentController.ExperimentUsersTable experimentUsersEditTable = new ExperimentController.ExperimentUsersTable();
private ExperimentController.NoExperimentUsersTypeTable noExperimentUsersTypeTable = new ExperimentController.NoExperimentUsersTypeTable();
private ExperimentController.ExperimentPolicyTable experimentPolicyListTable = new ExperimentController.ExperimentPolicyTable(false);
private ExperimentController.ExperimentPolicyTable experimentPolicyEditTable = new ExperimentController.ExperimentPolicyTable(true);
private ExperimentController.NoPolicyValueTable noPolicyValueTable = new ExperimentController.NoPolicyValueTable();
final private Map<UserInfo, ExperimentUser> experimentUsers = new HashMap<>();
final private Map<String, RoleType> experimentRoles = new HashMap<>();
int maxExperimentRoleTypeId = 0;
boolean initialized = false;
private List<MissingPolicyType> missingPolicyTypes = new ArrayList<>();
private final Map<Integer, String> experimentPropertiesMap = new HashMap<>();
public ExperimentController() {
}
@Override
public void clear() {
experimentUsersListTable.resetList();
experimentUsersEditTable.resetList();
experimentUsers.clear();
noExperimentUsersTypeTable.resetList();
experimentPolicyListTable.resetList();
missingPolicyTypes.clear();
experimentPolicyEditTable.resetList();
noPolicyValueTable.resetList();
experimentPropertyToDelete = null;
}
@Override
protected ExperimentFacade getFacade() {
return experimentFacade;
}
@Override
protected Experiment createEntityInstance() {
Experiment newExperiment = new Experiment();
if (!initialized) {
initializeTables();
}
return newExperiment;
}
@Override
public String getEntityTypeName() {
return "experiment";
}
@Override
public String getCurrentEntityInstanceName() {
if (getCurrent() != null) {
return getCurrent().getName();
}
return "";
}
@Override
public List<Experiment> getAvailableItems() {
return super.getAvailableItems();
}
@Override
public String prepareEdit(Experiment experiment) {
clear();
if (!initialized) {
initializeTables();
}
return super.prepareEdit(experiment);
}
private void initializeTables() {
List<RoleType> roleTypesList = roleTypeFacade.findAll();
for (RoleType roleType : roleTypesList) {
if (!roleType.isIsSystemRole()) {
experimentRoles.put(roleType.getName(), roleType);
if (roleType.getId() > maxExperimentRoleTypeId) {
maxExperimentRoleTypeId = roleType.getId();
}
}
}
initialized = true;
}
@Override
public void prepareEntityInsert(Experiment experiment) throws ObjectAlreadyExists, InvalidDate, MissingProperty {
if ((experiment.getName() == null) || (experiment.getName().length() == 0)){
throw new MissingProperty("Experiment name is missing.");
}
Experiment existingExperiment = experimentFacade.findByName(experiment.getName());
if (existingExperiment != null) {
throw new ObjectAlreadyExists("Experiment " + experiment.getName() + " already exists.");
}
verifyExperiment(experiment);
logger.debug("Inserting new experiment " + experiment.getName());
}
@Override
public void prepareEntityUpdate(Experiment experiment) throws DmPortalException {
if ((experiment.getName() == null) || (experiment.getName().length() == 0)) {
throw new MissingProperty("Experiment name is missing.");
}
verifyExperiment(experiment);
logger.debug("Updating experiment " + experiment.getName());
}
@Override
protected String getObjectAlreadyExistMessage(Experiment experiment) {
if (experiment == null) {
return null;
}
return "Experiment " + experiment.getName() + " already exists.";
}
private void verifyExperiment(Experiment experiment) throws InvalidDate, MissingProperty {
if (experiment.getExperimentType() == null) {
throw new MissingProperty("Experiment Type is missing.");
}
if ((experiment.getStartDate() != null) && (experiment.getEndDate() != null) && (experiment.getEndDate().before(experiment.getStartDate()))) {
throw new InvalidDate("Experiment end date is before start date.");
}
}
@Override
public String update() {
boolean updated;
for (UserInfo user : experimentUsers.keySet()) {
updated = false;
ExperimentUser experimentUser = experimentUsers.get(user);
for (RoleType roleType : experimentRoles.values()) {
if (!roleType.isIsSystemRole()) {
int roleId = roleType.getId();
if (user.getExperimentRole(roleId, experimentUser.experimentId) == null) {
if (experimentUser.userRoles[roleId]) {
UserExperimentRole userExperimentRole = new UserExperimentRole(user.getId(), experimentUser.experimentId, roleId);
user.addUserExperimentRole(userExperimentRole);
updated = true;
logger.debug("adding userExperimentRole " + user.getId() + " " + experimentUser.experimentId + " " + roleId);
}
} else if (!experimentUser.userRoles[roleId]) {
user.removeExperimentRole(roleId, experimentUser.experimentId);
updated = true;
logger.debug("adding userExperimentRole " + user.getId() + " " + experimentUser.experimentId + " " + roleId);
}
}
if (updated) {
userInfoFacade.edit(user);
}
}
}
Iterator<ExperimentPolicy> iterator = experimentPolicyEditTable.getListDataModel().iterator();
while (iterator.hasNext()) {
ExperimentPolicy experimentPolicy = iterator.next();
for (ExperimentPolicyPropertyValue experimentPolicyPropertyValue : experimentPolicy.getExperimentPolicyPropertyValueList()) {
String oldValue = experimentPropertiesMap.get(experimentPolicyPropertyValue.getId());
String newValue = experimentPolicyPropertyValue.getPolicyPropertyValue();
if ((oldValue != null) && !oldValue.equals(experimentPolicyPropertyValue.getPolicyPropertyValue())) {
experimentPolicyPropertyValue.setModifiedBy(((UserInfo) SessionUtility.getUser()).getUsername());
experimentPolicyPropertyValue.setModifiedDate(new Date());
experimentPolicyPropertyValue.setPolicyPropertyValue(newValue);
experimentPolicyPropertyValueFacade.edit(experimentPolicyPropertyValue);
}
}
}
clear();
return super.update();
}
void setMissingProperties(List<ExperimentPolicy> experimentPolicies, List<PolicyType> policyTypes) {
for (PolicyType policyType : policyTypes) {
ExperimentPolicy theExperimentPolicy = null;
boolean missingProperties = true;
for (ExperimentPolicy experimentPolicy : experimentPolicies) {
if (experimentPolicy.getPolicyType().getId() == policyType.getId()) {
theExperimentPolicy = experimentPolicy;
if (theExperimentPolicy.getExperimentPolicyPropertyValueList().size() == policyType.getPolicyPropertyList().size()) {
missingProperties = false;
}
break;
}
}
if (missingProperties) {
MissingPolicyType missingPolicyType = new MissingPolicyType(policyType.getId(), policyType.getName());
missingPolicyTypes.add(missingPolicyType);
for (PolicyProperty policyProperty : policyType.getPolicyPropertyList()) {
if ((theExperimentPolicy == null) || (!hasProperty(theExperimentPolicy, policyProperty))) {
PolicyPropertyValue policyPropertyValue = new PolicyPropertyValue();
policyPropertyValue.policyProperty = policyProperty;
policyPropertyValue.value = policyProperty.getDefaultValue();
missingPolicyType.getNoPropertyList().add(policyPropertyValue);
}
}
}
}
}
private boolean hasProperty(ExperimentPolicy experimentPolicy, PolicyProperty policyProperty) {
for (ExperimentPolicyPropertyValue experimentPolicyPropertyValue : experimentPolicy.getExperimentPolicyPropertyValueList()) {
if (experimentPolicyPropertyValue.getPolicyProperty().equals(policyProperty)) {
return true;
}
}
return false;
}
public void updateRemovedExperimentRoles() {
UserExperimentRole userExperimentRole;
for (UserInfo user : experimentUsers.keySet()) {
ExperimentUser experimentUser = experimentUsers.get(user);
for (RoleType roleType : experimentRoles.values()) {
int roleId = roleType.getId();
userExperimentRole = user.getExperimentRole(roleId, experimentUser.experimentId);
if ((userExperimentRole != null ) && (!experimentUser.userRoles[roleId]) ) {
userExperimentRoleFacade.remove(userExperimentRole);
}
}
}
}
public String getRemovedUserName() {
if (experimentUsersEditTable.getCurrentObject() != null) {
return experimentUsersEditTable.getCurrentObject().getUsername();
}
return "";
}
public String getRemovedPolicyName() {
if (experimentPropertyToDelete != null) {
return experimentPropertyToDelete.getPolicyProperty().getName();
}
return "";
}
public void removeUserRoles() {
ExperimentUser experimentUser = experimentUsersEditTable.getCurrentObject();
UserInfo user = experimentUser.getUser();
List<UserExperimentRole> roleList = user.getUserExperimentRoles(experimentUser.experimentId);
for (UserExperimentRole userExperimentRole : roleList) {
userExperimentRoleFacade.remove(userExperimentRole);
}
}
public String removeUser() {
ExperimentUser experimentUser = experimentUsersEditTable.getCurrentObject();
UserInfo user = experimentUser.getUser();
for (RoleType roleType : experimentRoles.values()) {
int roleId = roleType.getId();
if (experimentUser.userRoles[roleId]) {
user.removeExperimentRole(roleId, experimentUser.experimentId);
}
}
userInfoFacade.edit(user);
experimentUsersListTable.resetList();
experimentUsersEditTable.resetList();
experimentUsers.clear();
noExperimentUsersTypeTable.resetList();
return "edit?faces-redirect=true";
}
public void removePolicy() {
experimentPolicyPropertyValueFacade.remove(experimentPropertyToDelete);
}
public String updateRemovedExperimentPolicy() {
ExperimentPolicy experimentPolicy = experimentPropertyToDelete.getExperimentPolicy();
experimentPropertiesMap.remove(experimentPropertyToDelete.getId());
experimentPolicy.getExperimentPolicyPropertyValueList().remove(experimentPropertyToDelete);
experimentPolicyFacade.edit(experimentPolicy);
current = experimentFacade.find(current.getId());
experimentPolicyListTable.resetList();
missingPolicyTypes.clear();
experimentPolicyEditTable.resetList();
noPolicyValueTable.resetList();
return "edit?faces-redirect=true";
}
public String addExperimentUser() {
List<UserInfo> newUsers = noExperimentUsersTypeTable.getSelectedObjectList();
if (newUsers == null) {
logger.debug("null selected list");
return null;
} else if (newUsers.isEmpty()) {
logger.debug("empty selected list");
return null;
} else {
try {
UserExperimentRole userExperimentRole;
for (UserInfo user : newUsers) {
userExperimentRole = new UserExperimentRole(user.getId(), current.getId(), experimentRoles.get(RoleTypeName.USER).getId()); // set the User
user.getUserExperimentRoleList().add(userExperimentRole);
userInfoFacade.edit(user);
}
experimentUsersListTable.resetList();
experimentUsersEditTable.resetList();
experimentUsers.clear();
noExperimentUsersTypeTable.resetList();
return "edit?faces-redirect=true";
} catch (RuntimeException ex) {
SessionUtility.addErrorMessage("Error", "Could not update UserInfo" + ": " + ex.getMessage());
return null;
}
}
}
public String addExperimentPolicyPropertyValues() {
List<PolicyPropertyValue> newProperties = noPolicyValueTable.getSelectedObjectList();
if (newProperties == null) {
logger.debug("null selected list");
return null;
} else if (newProperties.isEmpty()) {
logger.debug("empty selected list");
return null;
} else {
try {
ExperimentPolicyPropertyValue experimentPolicyPropertyValue;
for (PolicyPropertyValue property : newProperties) {
PolicyProperty policyProperty = property.getPolicyProperty();
experimentPolicyPropertyValue = new ExperimentPolicyPropertyValue();
experimentPolicyPropertyValue.setPolicyProperty(policyProperty);
experimentPolicyPropertyValue.setPolicyPropertyValue(property.getValue());
experimentPolicyPropertyValue.setModifiedBy(((UserInfo) SessionUtility.getUser()).getUsername());
experimentPolicyPropertyValue.setModifiedDate(new Date());
ExperimentPolicy experimentPolicy = getOrCreateExperimentPolicy(policyProperty.getPolicyType());
experimentPolicyPropertyValue.setExperimentPolicy(experimentPolicy);
experimentPolicy.getExperimentPolicyPropertyValueList().add(experimentPolicyPropertyValue);
}
super.update();
current = experimentFacade.find(current.getId());
experimentPolicyListTable.resetList();
missingPolicyTypes.clear();
experimentPolicyEditTable.resetList();
noPolicyValueTable.resetList();
return "edit?faces-redirect=true";
} catch (RuntimeException ex) {
SessionUtility.addErrorMessage("Error", "Could not update Experiment Policy" + ": " + ex.getMessage());
return null;
}
}
}
private ExperimentPolicy getOrCreateExperimentPolicy(PolicyType policyType) {
List<ExperimentPolicy> experimentPolicyList = current.getExperimentPolicyList();
for (ExperimentPolicy experimentPolicy: experimentPolicyList) {
if (experimentPolicy.getPolicyType().equals(policyType)) {
return experimentPolicy;
}
}
ExperimentPolicy newExperimentPolicy = new ExperimentPolicy();
newExperimentPolicy.setExperiment(current);
newExperimentPolicy.setPolicyType(policyType);
experimentPolicyList.add(newExperimentPolicy);
policyType.getExperimentPolicyList().add(newExperimentPolicy);
experimentPolicyFacade.create(newExperimentPolicy);
return newExperimentPolicy;
}
public boolean canEditExperiment(Experiment experiment) {
UserInfo logged = (UserInfo) SessionUtility.getUser();
if (logged == null) {
return false;
}
if (experiment == null) {
return false;
}
if (!initialized) {
initializeTables();
}
// user that is Manager or PI can edit experiment
int managerRoleId = experimentRoles.get(RoleTypeName.MANAGER).getId();
int piRoleId = experimentRoles.get(RoleTypeName.PI).getId();
return (logged.getExperimentRole(managerRoleId, experiment.getId()) != null) || (logged.getExperimentRole(piRoleId, experiment.getId()) != null);
}
public boolean isRestricted() {
UserInfo logged = (UserInfo) SessionUtility.getUser();
if (logged == null) {
return false;
}
if (current == null) {
return true;
}
int managerRoleId = experimentRoles.get(RoleTypeName.MANAGER).getId();
int piRoleId = experimentRoles.get(RoleTypeName.PI).getId();
return (logged.getExperimentRole(managerRoleId, current.getId()) == null) && (logged.getExperimentRole(piRoleId, current.getId()) == null);
}
public boolean canDeleteExperiment(Experiment experiment) {
UserInfo logged = (UserInfo) SessionUtility.getUser();
if (logged == null) {
return false;
}
if (experiment == null) {
return false;
}
if (!initialized) {
initializeTables();
}
int managerRoleId = experimentRoles.get(RoleTypeName.MANAGER).getId();
return logged.getExperimentRole(managerRoleId, experiment.getId()) != null;
}
public boolean canAddManager() {
ExperimentUser logged = experimentUsers.get((UserInfo) SessionUtility.getUser());
if (logged == null) {
return false;
}
return logged.isIsManager();
}
public boolean canAddPiAndUser() {
ExperimentUser logged = experimentUsers.get((UserInfo) SessionUtility.getUser());
if (logged == null) {
return false;
}
return logged.isIsManager() || logged.isIsPI();
}
public boolean notSelected() {
return (current == null);
}
public int getRows() {
return rows;
}
public ExperimentController.ExperimentUsersTable getExperimentUsersListTable() {
return experimentUsersListTable;
}
public void setExperimentUsersListTable(ExperimentController.ExperimentUsersTable experimentUsersTable) {
this.experimentUsersListTable = experimentUsersTable;
}
public ExperimentController.ExperimentUsersTable getExperimentUsersEditTable() {
return experimentUsersEditTable;
}
public void setExperimentUsersEditTable(ExperimentController.ExperimentUsersTable experimentUsersTable) {
this.experimentUsersEditTable = experimentUsersTable;
}
public ExperimentController.NoExperimentUsersTypeTable getNoExperimentUsersTypeTable() {
return noExperimentUsersTypeTable;
}
public void setNoExperimentUsersTypeTable(ExperimentController.NoExperimentUsersTypeTable noExperimentUsersTypeTable) {
this.noExperimentUsersTypeTable = noExperimentUsersTypeTable;
}
public ExperimentPolicyTable getExperimentPolicyListTable() {
return experimentPolicyListTable;
}
public void setExperimentPolicyListTable(ExperimentPolicyTable experimentPolicyTable) {
this.experimentPolicyListTable = experimentPolicyTable;
}
public ExperimentPolicyTable getExperimentPolicyEditTable() {
return experimentPolicyEditTable;
}
public void setExperimentPolicyEditTable(ExperimentPolicyTable experimentPolicyEditTable) {
this.experimentPolicyEditTable = experimentPolicyEditTable;
}
public NoPolicyValueTable getNoPolicyValueTable() {
return noPolicyValueTable;
}
public void setNoPolicyValueTable(NoPolicyValueTable noPolicyValueTable) {
this.noPolicyValueTable = noPolicyValueTable;
}
public List<MissingPolicyType> getMissingPolicyTypes() {
return missingPolicyTypes;
}
public void setMissingPolicyTypes(List<MissingPolicyType> missingPolicyTypes) {
this.missingPolicyTypes = missingPolicyTypes;
}
public ExperimentPolicyPropertyValue getExperimentPropertyToDelete() {
return experimentPropertyToDelete;
}
public void setExperimentPropertyToDelete(ExperimentPolicyPropertyValue experimentPropertyToDelete) {
this.experimentPropertyToDelete = experimentPropertyToDelete;
}
@FacesConverter(forClass = Experiment.class)
public static class ExperimentControllerConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
ExperimentController controller = (ExperimentController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "experimentController");
return controller.getEntity(getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuilder sb = new StringBuilder();
sb.append(value);
return sb.toString();
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof Experiment) {
Experiment o = (Experiment) object;
return getStringKey(o.getId());
}
else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Experiment.class.getName());
}
}
}
}
\ No newline at end of file
package gov.anl.aps.dm.portal.controllers;
import gov.anl.aps.dm.portal.exceptions.DmPortalException;
import gov.anl.aps.dm.portal.exceptions.MissingProperty;
import gov.anl.aps.dm.portal.exceptions.ObjectAlreadyExists;
import gov.anl.aps.dm.portal.model.entities.ExperimentPolicy;
import gov.anl.aps.dm.portal.model.beans.ExperimentPolicyFacade;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.apache.log4j.Logger;
@Named("experimentPolicyController")
@SessionScoped
public class ExperimentPolicyController extends CrudEntityController<ExperimentPolicy, ExperimentPolicyFacade>
{
private static final Logger logger = Logger.getLogger(ExperimentPolicyController.class.getName());
@EJB
private ExperimentPolicyFacade experimentPolicyFacade;
public ExperimentPolicyController() {
}
@Override
protected ExperimentPolicyFacade getFacade() {
return experimentPolicyFacade;
}
@Override
protected ExperimentPolicy createEntityInstance() {
return new ExperimentPolicy();
}
@Override
public String getEntityTypeName() {
return "experimentPolicy";
}
@Override
public String getCurrentEntityInstanceName() {
return "";
}
@Override
public List<ExperimentPolicy> getAvailableItems() {
return super.getAvailableItems();
}
@Override
public String prepareEdit(ExperimentPolicy experimentPolicy) {
return super.prepareEdit(experimentPolicy);
}
@Override
public void prepareEntityInsert(ExperimentPolicy experimentPolicy) throws ObjectAlreadyExists, MissingProperty {
logger.debug("Inserting new experiment policy ");
}
@Override
public void prepareEntityUpdate(ExperimentPolicy experimentPolicy) throws DmPortalException {
}
@Override
protected String getObjectAlreadyExistMessage(ExperimentPolicy experimentPolicy) {
if (experimentPolicy == null) {
return null;
}
return "Experiment Policy " + " already exists.";
}
@FacesConverter(forClass = ExperimentPolicy.class)
public static class ExperimentPolicyControllerConverter implements Converter
{
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
ExperimentPolicyController controller = (ExperimentPolicyController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "experimentPolicyController");
return controller.getEntity(getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuilder sb = new StringBuilder();
sb.append(value);
return sb.toString();
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof ExperimentPolicy) {
ExperimentPolicy o = (ExperimentPolicy) object;
return getStringKey(o.getId());
}
else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + ExperimentPolicy.class.getName());
}
}
}
}
\ No newline at end of file
package gov.anl.aps.dm.portal.controllers;
import gov.anl.aps.dm.portal.exceptions.DmPortalException;
import gov.anl.aps.dm.portal.exceptions.MissingProperty;
import gov.anl.aps.dm.portal.exceptions.ObjectAlreadyExists;
import gov.anl.aps.dm.portal.model.entities.ExperimentType;
import gov.anl.aps.dm.portal.model.beans.ExperimentTypeFacade;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.apache.log4j.Logger;
@Named("experimentTypeController")
@SessionScoped
public class ExperimentTypeController extends CrudEntityController<ExperimentType, ExperimentTypeFacade>
{
private static final Logger logger = Logger.getLogger(ExperimentTypeController.class.getName());
private final int rows = 25;
@EJB
private ExperimentTypeFacade experimentTypeFacade;
public ExperimentTypeController() {
}
public int getRows() {
return rows;
}
@Override
protected ExperimentTypeFacade getFacade() {
return experimentTypeFacade;
}
@Override
protected ExperimentType createEntityInstance() {
return new ExperimentType();
}
@Override
public String getEntityTypeName() {
return "experimentType";
}
@Override
public String getCurrentEntityInstanceName() {
if (getCurrent() != null) {
return getCurrent().getName();
}
return "";
}
@Override
public List<ExperimentType> getAvailableItems() {
return super.getAvailableItems();
}
@Override
public String prepareEdit(ExperimentType experimentType) {
return super.prepareEdit(experimentType);
}
@Override
public void prepareEntityInsert(ExperimentType experimentType) throws ObjectAlreadyExists, MissingProperty {
if ((experimentType.getName() == null) || (experimentType.getName().length() == 0)) {
throw new MissingProperty("Name is missing.");
}
ExperimentType existingExperimentType = experimentTypeFacade.findByName(experimentType.getName());
if (existingExperimentType != null) {
throw new ObjectAlreadyExists("Experiment type " + experimentType.getName() + " already exists.");
}
logger.debug("Inserting new experiment type " + experimentType.getName());
}
@Override
public void prepareEntityUpdate(ExperimentType experimentType) throws DmPortalException {
if ((experimentType.getName() == null) || (experimentType.getName().length() == 0)) {
throw new MissingProperty("Name is missing.");
}
}
@Override
protected String getObjectAlreadyExistMessage(ExperimentType experimentType) {
if (experimentType == null) {
return null;
}
return "Experiment Type " + experimentType.getName() + " already exists.";
}
public boolean notSelected() {
return current == null;
}
@FacesConverter(forClass = ExperimentType.class)
public static class ExperimentTypeControllerConverter implements Converter
{
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
ExperimentTypeController controller = (ExperimentTypeController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "experimentTypeController");
return controller.getEntity(getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuilder sb = new StringBuilder();
sb.append(value);
return sb.toString();
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof ExperimentType) {
ExperimentType o = (ExperimentType) object;
return getStringKey(o.getId());
}
else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + ExperimentType.class.getName());
}
}
}
}
\ No newline at end of file
/*
* 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.controllers;
import gov.anl.aps.dm.portal.constants.RoleTypeName;
import gov.anl.aps.dm.portal.model.beans.RoleTypeFacade;
import gov.anl.aps.dm.portal.model.beans.UserInfoFacade;
import gov.anl.aps.dm.portal.model.entities.UserInfo;
import gov.anl.aps.dm.portal.utilities.CryptUtility;
import gov.anl.aps.dm.portal.utilities.LdapUtility;
import gov.anl.aps.dm.portal.utilities.SessionUtility;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import org.apache.log4j.Logger;
/**
* Login controller.
*/
@Named("loginController")
@SessionScoped
public class LoginController implements Serializable
{
@EJB
private UserInfoFacade userInfoFacade;
@EJB
private RoleTypeFacade roleTypeFacade;
private String username = null;
private String password = null;
private boolean loggedIn = false;
private UserInfo user = null;
private boolean isAdmin;
private static final Logger logger = Logger.getLogger(LoginController.class.getName());
/**
* Constructor.
*/
public LoginController() {
}
/**
* Get password.
*
* @return login password
*/
public String getPassword() {
return password;
}
/**
* Set password.
*
* @param password login password
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Get username.
*
* @return login username
*/
public String getUsername() {
return username;
}
/**
* Set username.
*
* @param username login username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* Check if user is logged in.
*
* @return true if admin is logged in, false otherwise
*/
public boolean isLoggedIn() {
return loggedIn;
}
/**
* Login action.
*
* @return url to service home page if login is successful, or null in case
* of errors
*/
public String login() {
loggedIn = false;
if (username == null || password == null || username.isEmpty() || password.isEmpty()) {
SessionUtility.addWarningMessage(null, "Incomplete Input. Please enter both username and password.");
return (username = password = null);
}
user = userInfoFacade.findByUsername(username);
if (user == null) {
SessionUtility.addErrorMessage(null, "Username " + username + " is not registered. Contact administrator.");
return (username = password = null);
}
boolean validCredentials = false;
if (user.getPassword() != null && CryptUtility.verifyPasswordWithPbkdf2(password, user.getPassword())) {
logger.debug("User " + username + " is authorized by DM DB");
validCredentials = true;
}
else if (LdapUtility.validateCredentials(username, password)) {
logger.debug("User " + username + " is authorized by DM LDAP");
validCredentials = true;
}
else {
logger.debug("User " + username + " is not authorized.");
}
if (validCredentials) {
SessionUtility.setUser(user);
loggedIn = true;
SessionUtility.addInfoMessage("Successful Login", "User " + username + " is logged in.");
isAdmin = roleTypeFacade.findByName(RoleTypeName.ADMIN).isAdmin(username);
return getLandingPage();
}
else {
SessionUtility.addErrorMessage(null, "Invalid Credentials. Username/password combination could not be verified.");
SessionUtility.addErrorMessage(null, "If you are having difficulty logging in, please see our <a href=\"/dm/views/loginHelp.xhtml\">login help</a> page.");
return (username = password = null);
}
}
public String getLandingPage() {
String landingPage = SessionUtility.getCurrentViewId() + "?faces-redirect=true";
if (landingPage.contains("login")) {
landingPage = "/views/home?faces-redirect=true";
}
logger.debug("Landing page: " + landingPage);
return landingPage;
}
public String displayUsername() {
if (isLoggedIn()) {
return username;
}
else {
return "Not Logged In";
}
}
public boolean isUserWriteable(UserInfo user) {
if (!isLoggedIn()) {
return false;
}
return isLoggedIn() || this.user.getId() == user.getId();
}
/**
* Logout action.
*
* @return url to logout page
*/
public String logout() {
SessionUtility.clearSession();
ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
context.invalidateSession();
loggedIn = false;
user = null;
return "/views/login?faces-redirect=true";
}
public boolean isAdmin() {
return isAdmin;
}
}
/*
* 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.controllers;
import gov.anl.aps.dm.portal.exceptions.DmPortalException;
import gov.anl.aps.dm.portal.exceptions.MissingProperty;
import gov.anl.aps.dm.portal.exceptions.ObjectAlreadyExists;
import gov.anl.aps.dm.portal.model.beans.PolicyPropertyFacade;
import gov.anl.aps.dm.portal.model.entities.PolicyProperty;
import gov.anl.aps.dm.portal.model.entities.PolicyType;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.inject.Named;
import org.apache.log4j.Logger;
/**
*
* @author bfrosik
*/
@Named("policyPropertyController")
@SessionScoped
public class PolicyPropertyController extends CrudEntityController<PolicyProperty, PolicyPropertyFacade>
{
private PolicyType policyType;
private int rows = 25;
private static final Logger logger = Logger.getLogger(PolicyPropertyController.class.getName());
@EJB
private PolicyPropertyFacade policyPropertyFacade;
class PolicyPropertyInfoTable extends DataTableController<PolicyProperty> {
@Override
public String getClassName() {
return "PolicyProperty";
}
@Override
public List<PolicyProperty> findAll() {
return policyPropertyFacade.findByPolicyTypeId(policyType.getId());
}
}
private PolicyPropertyInfoTable policyPropertyInfoTable = new PolicyPropertyInfoTable();
public PolicyPropertyController() {
}
public PolicyPropertyInfoTable getPolicyPropertyInfoTable() {
return policyPropertyInfoTable;
}
public void setPolicyPropertyInfoTable(PolicyPropertyInfoTable policyPropertyInfoTable) {
this.policyPropertyInfoTable = policyPropertyInfoTable;
}
@Override
protected PolicyPropertyFacade getFacade() {
return policyPropertyFacade;
}
@Override
protected PolicyProperty createEntityInstance() {
return new PolicyProperty();
}
@Override
public String getCurrentEntityInstanceName() {
if (getCurrent() != null) {
return getCurrent().getName();
}
return "";
}
@Override
public DataModel createListDataModel() {
return new ListDataModel((List) policyPropertyFacade.findByPolicyTypeId(policyType.getId()));
}
@Override
public List<PolicyProperty> getAvailableItems() {
return (List<PolicyProperty>) policyPropertyFacade.findByPolicyTypeId(policyType.getId());
}
@Override
public String prepareEdit(PolicyProperty policyProperty) {
super.prepareEdit(policyProperty);
return "/views/policyProperty/edit?faces-redirect=true";
}
@Override
public String prepareView(PolicyProperty policyProperty) {
super.prepareView(policyProperty);
return "/views/policyProperty/view?faces-redirect=true";
}
public String prepareView() {
return super.prepareView(current);
}
@Override
public void prepareEntityInsert(PolicyProperty policyProperty) throws ObjectAlreadyExists, MissingProperty {
current.setPolicyType(policyType);
if ((policyProperty.getName() == null) || (policyProperty.getName().length() == 0)) {
throw new MissingProperty("Name is missing.");
}
List<PolicyProperty> policyPropertyList = policyPropertyFacade.findByPolicyTypeId(policyType.getId());
for (PolicyProperty property : policyPropertyList) {
if ((property.getName().equals(policyProperty.getName()))) {
throw new ObjectAlreadyExists("policy property " + policyProperty.getName() + " already exists for this policy type.");
}
}
logger.debug("Inserting new policy property " + policyProperty.getName());
}
@Override
public void prepareEntityUpdate(PolicyProperty policyProperty) throws DmPortalException {
current.setPolicyType(policyType);
}
@Override
protected String getObjectAlreadyExistMessage(PolicyProperty policyProperty) {
if (policyProperty == null) {
return null;
}
return "Policy Property " + policyProperty.getName() + " already exists.";
}
@Override
public String getEntityTypeName() {
return "PolicyProperty";
}
@Override
public void clear() {
policyPropertyInfoTable.resetList();
resetList();
}
@Override
public String prepareCreate() {
super.prepareCreate();
return "/views/policyProperty/create?faces-redirect=true";
}
@Override
public String create() {
if (super.create() == null) {
return null;
}
policyPropertyInfoTable.resetList();
return "/views/policyType/view?faces-redirect=true";
}
@Override
public String prepareList() {
logger.debug("Preparing list");
resetListDataModel();
return "/views/policyType/view?faces-redirect=true";
}
@Override
public String destroy() {
String view = super.destroy();
clear();
return view;
}
public PolicyType getPolicyType() {
return policyType;
}
public void setPolicyType(PolicyType policyType) {
logger.info("setting policy type ");
clear();
this.policyType = policyType;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public boolean hasAllowedValues(PolicyProperty policyProperty) {
return ((policyProperty.getAllowedPolicyValueList() != null) && !policyProperty.getAllowedPolicyValueList().isEmpty());
}
public String updateAllowedValueList() {
policyPropertyFacade.edit(current);
return "/views/policyProperty/edit?faces-redirect=true";
}
public boolean notSelected() {
return current == null;
}
@FacesConverter(forClass = PolicyProperty.class)
public static class PolicyPropertyControllerConverter implements Converter
{
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
PolicyPropertyController controller = (PolicyPropertyController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "policyPropertyController");
return controller.getEntity(getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuilder sb = new StringBuilder();
sb.append(value);
return sb.toString();
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof PolicyProperty) {
PolicyProperty o = (PolicyProperty) object;
return getStringKey(o.getId());
}
else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + PolicyProperty.class.getName());
}
}
}
}
\ No newline at end of file
package gov.anl.aps.dm.portal.controllers;
import gov.anl.aps.dm.portal.exceptions.DmPortalException;
import gov.anl.aps.dm.portal.exceptions.MissingProperty;
import gov.anl.aps.dm.portal.exceptions.ObjectAlreadyExists;
import gov.anl.aps.dm.portal.model.entities.PolicyType;
import gov.anl.aps.dm.portal.model.beans.PolicyTypeFacade;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.apache.log4j.Logger;
@Named("policyTypeController")
@SessionScoped
public class PolicyTypeController extends CrudEntityController<PolicyType, PolicyTypeFacade>
{
private static final Logger logger = Logger.getLogger(PolicyTypeController.class.getName());
@EJB
private PolicyTypeFacade policyTypeFacade;
public PolicyTypeController() {
}
@Override
protected PolicyTypeFacade getFacade() {
return policyTypeFacade;
}
@Override
protected PolicyType createEntityInstance() {
return new PolicyType();
}
@Override
public String getEntityTypeName() {
return "policyType";
}
@Override
public String getCurrentEntityInstanceName() {
if (getCurrent() != null) {
return getCurrent().getName();
}
return "";
}
@Override
public List<PolicyType> getAvailableItems() {
return super.getAvailableItems();
}
@Override
public String prepareEdit(PolicyType policyType) {
return super.prepareEdit(policyType);
}
@Override
public void prepareEntityInsert(PolicyType policyType) throws ObjectAlreadyExists, MissingProperty {
if ((policyType.getName() == null) || (policyType.getName().length() == 0)) {
throw new MissingProperty("Name is missing.");
}
PolicyType existingPolicyType = policyTypeFacade.findByName(policyType.getName());
if (existingPolicyType != null) {
throw new ObjectAlreadyExists("Experiment type " + policyType.getName() + " already exists.");
}
logger.debug("Inserting new experiment type " + policyType.getName());
}
@Override
public void prepareEntityUpdate(PolicyType policyType) throws DmPortalException {
if ((policyType.getName() == null) || (policyType.getName().length() == 0)) {
throw new MissingProperty("Name is missing.");
}
}
@Override
protected String getObjectAlreadyExistMessage(PolicyType policyType) {
if (policyType == null) {
return null;
}
return "Experiment Policy " + policyType.getName() + " already exists.";
}
public String prepareView() {
return super.prepareView(current);
}
public boolean notSelected() {
return current == null;
}
@FacesConverter(forClass = PolicyType.class)
public static class PolicyTypeControllerConverter implements Converter
{
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
PolicyTypeController controller = (PolicyTypeController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "policyTypeController");
return controller.getEntity(getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuilder sb = new StringBuilder();
sb.append(value);
return sb.toString();
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof PolicyType) {
PolicyType o = (PolicyType) object;
return getStringKey(o.getId());
}
else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + PolicyType.class.getName());
}
}
}
}
\ No newline at end of file
package gov.anl.aps.dm.portal.controllers;
import gov.anl.aps.dm.portal.exceptions.DmPortalException;
import gov.anl.aps.dm.portal.exceptions.MissingProperty;
import gov.anl.aps.dm.portal.exceptions.ObjectAlreadyExists;
import gov.anl.aps.dm.portal.model.beans.RoleTypeFacade;
import gov.anl.aps.dm.portal.model.entities.RoleType;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Named;
import org.apache.log4j.Logger;
@Named("roleTypeController")
@SessionScoped
public class RoleTypeController extends CrudEntityController<RoleType, RoleTypeFacade>
{
private static final Logger logger = Logger.getLogger(RoleTypeController.class.getName());
private final int rows = 25;
@EJB
private RoleTypeFacade roleTypeFacade;
public RoleTypeController() {
}
public int getRows() {
return rows;
}
@Override
protected RoleTypeFacade getFacade() {
return roleTypeFacade;
}
@Override
protected RoleType createEntityInstance() {
return new RoleType();
}
@Override
public String getEntityTypeName() {
return "roleType";
}
@Override
public String getCurrentEntityInstanceName() {
if (getCurrent() != null) {
return getCurrent().getName();
}
return "";
}
@Override
public List<RoleType> getAvailableItems() {
return super.getAvailableItems();
}
@Override
public String prepareEdit(RoleType roleType) {
return super.prepareEdit(roleType);
}
@Override
public void prepareEntityInsert(RoleType roleType) throws ObjectAlreadyExists, MissingProperty {
if ((roleType.getName() == null) || (roleType.getName().length() == 0)) {
throw new MissingProperty("Name is missing.");
}
RoleType existingRoleType = roleTypeFacade.findByName(roleType.getName());
if (existingRoleType != null) {
throw new ObjectAlreadyExists("Role Type " + roleType.getName() + " already exists.");
}
logger.debug("Inserting new roleType " + roleType.getName());
}
@Override
public void prepareEntityUpdate(RoleType roleType) throws DmPortalException {
if ((roleType.getName() == null) || (roleType.getName().length() == 0)) {
throw new MissingProperty("Name is missing.");
}
}
@Override
protected String getObjectAlreadyExistMessage(RoleType roleType) {
if (roleType == null) {
return null;
}
return "Role Type " + roleType.getName() + " already exists.";
}
public boolean isRoleSystemType(RoleType roleType) {
if (roleType == null) {
logger.warn("Current row is not set");
// Do nothing if current item is not set.
return false;
}
return roleType.isIsSystemRole();
}
public boolean notSelected() {
return current == null;
}
@FacesConverter(forClass = RoleType.class)
public static class RoleTypeControllerConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
RoleTypeController controller = (RoleTypeController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "roleTypeController");
return controller.getEntity(getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuilder sb = new StringBuilder();
sb.append(value);
return sb.toString();
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof RoleType) {
RoleType o = (RoleType) object;
return getStringKey(o.getId());
}
else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + RoleType.class.getName());
}
}
}
}
\ No newline at end of file
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.MissingProperty;
import gov.anl.aps.dm.portal.exceptions.ObjectAlreadyExists;
import gov.anl.aps.dm.portal.model.beans.RoleTypeFacade;
import gov.anl.aps.dm.portal.model.entities.UserInfo;
import gov.anl.aps.dm.portal.model.beans.UserInfoFacade;
import gov.anl.aps.dm.portal.model.entities.CloneableEntity;
import gov.anl.aps.dm.portal.model.entities.Experiment;
import gov.anl.aps.dm.portal.model.entities.RoleType;
import gov.anl.aps.dm.portal.utilities.CryptUtility;
import gov.anl.aps.dm.portal.utilities.SessionUtility;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Named;
import org.apache.log4j.Logger;
@Named("userInfoController")
@SessionScoped
public class UserInfoController extends CrudEntityController<UserInfo, UserInfoFacade> {
private static final Logger logger = Logger.getLogger(UserInfoController.class.getName());
private final int rows = 25;
@EJB
private UserInfoFacade userInfoFacade;
@EJB
private RoleTypeFacade roleTypeFacade;
private String passwordEntry = null;
public class ExperimentUser extends CloneableEntity {
String experimentName;
String description;
int userId;
int experimentId;
private final boolean[] userRoles;
public ExperimentUser(int experimentId, int userId) {
userRoles = new boolean[maxExperimentRoleTypeId + 1];
this.experimentId = experimentId;
this.userId = userId;
}
void setExperimentName(String experimentName) {
this.experimentName = experimentName;
}
void setDescription(String description) {
this.description = description;
}
public String getExperimentName() {
return experimentName;
}
public String getDescription() {
return description;
}
public boolean isIsManager() {
return userRoles[experimentRoles.get(RoleTypeName.MANAGER).getId()];
}
public boolean isIsPI() {
return userRoles[experimentRoles.get(RoleTypeName.PI).getId()];
}
public boolean isIsUser() {
return userRoles[experimentRoles.get(RoleTypeName.USER).getId()];
}
public void setIsInRole(RoleType role, boolean isInRole) {
userRoles[role.getId()] = isInRole;
}
public boolean[] getRoles() {
return userRoles;
}
@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));
}
}
class UserExperimentsTable extends DataTableController<ExperimentUser> {
@Override
public String getClassName() {
return "UserExperiments";
}
@Override
public List<ExperimentUser> findAll() {
List<Experiment> list = userInfoFacade.findExperimentsInUser(getCurrent().getId());
return convertUserExperiments(list);
}
List<ExperimentUser> convertUserExperiments(List<Experiment> list) {
if (!initialized) {
initializeRoleTypes();
}
userExperiments.clear();
logger.debug("converting UserExperiments ");
Collection<RoleType> roleTypes;
roleTypes = experimentRoles.values();
ExperimentUser experimentUser;
for (Experiment experiment : list) {
experimentUser = new ExperimentUser(experiment.getId(), getCurrent().getId());
experimentUser.setExperimentName(experiment.getName());
experimentUser.setDescription(experiment.getDescription());
userExperiments.add(experimentUser);
for (RoleType roleType : roleTypes) {
boolean inRole = (getCurrent().getExperimentRole(roleType.getId(), experiment.getId()) != null);
experimentUser.setIsInRole(roleType, inRole);
}
}
return userExperiments;
}
@Override
public String getTableName() {
return "UserExperimentsTable";
}
}
private UserInfoController.UserExperimentsTable userExperimentsListTable = new UserInfoController.UserExperimentsTable();
private final List<ExperimentUser> userExperiments = new ArrayList<>();
private final Map<String, RoleType> experimentRoles = new HashMap<>();
int maxExperimentRoleTypeId;
boolean initialized = false;
public UserInfoController() {
}
public int getRows() {
return rows;
}
private void initializeRoleTypes() {
List<RoleType> roleTypesList = roleTypeFacade.findAll();
for (RoleType roleType : roleTypesList) {
if (!roleType.isIsSystemRole()) {
experimentRoles.put(roleType.getName(), roleType);
if (roleType.getId() > maxExperimentRoleTypeId) {
maxExperimentRoleTypeId = roleType.getId();
}
}
}
initialized = true;
}
@Override
public void clear() {
userExperimentsListTable.resetList();
userExperiments.clear();
}
public UserExperimentsTable getUserExperimentsListTable() {
return userExperimentsListTable;
}
public void setUserExperimentsListTable(UserExperimentsTable userExperimentsListTable) {
this.userExperimentsListTable = userExperimentsListTable;
}
@Override
protected UserInfoFacade getFacade() {
return userInfoFacade;
}
@Override
protected UserInfo createEntityInstance() {
return new UserInfo();
}
@Override
public String getEntityTypeName() {
return "userInfo";
}
@Override
public String getCurrentEntityInstanceName() {
if (getCurrent() != null) {
return getCurrent().getUsername();
}
return "";
}
@Override
public List<UserInfo> getAvailableItems() {
return super.getAvailableItems();
}
@Override
public String prepareEdit(UserInfo user) {
passwordEntry = null;
return super.prepareEdit(user);
}
@Override
public void prepareEntityInsert(UserInfo user) throws ObjectAlreadyExists, MissingProperty {
verifyMandatoryParam(user);
UserInfo existingUser = userInfoFacade.findByUsername(user.getUsername());
if (existingUser != null) {
throw new ObjectAlreadyExists("User " + user.getUsername() + " already exists.");
}
if (passwordEntry != null && !passwordEntry.isEmpty()) {
logger.debug("Will crypt provided password for user " + user.getUsername());
String cryptedPassword = CryptUtility.cryptPasswordWithPbkdf2(passwordEntry);
user.setPassword(cryptedPassword);
}
passwordEntry = null;
logger.debug("Inserting new user " + user.getUsername());
}
private void verifyMandatoryParam(UserInfo user) throws MissingProperty {
int missing = 0;
if ((user.getUsername() == null) || (user.getUsername().length() == 0)) {
missing = 1;
}
if ((user.getLastName() == null) || (user.getLastName().length() == 0)) {
missing += 2;
}
if ((user.getFirstName() == null) || (user.getFirstName().length() == 0)) {
missing += 4;
}
if (missing > 0) {
switch (missing) {
case 7:
throw new MissingProperty("Username, last name, and first name are missing.");
case 6:
throw new MissingProperty("last name, and first name are missing.");
case 5:
throw new MissingProperty("Username and first name are missing.");
case 4:
throw new MissingProperty("first name is missing.");
case 3:
throw new MissingProperty("Username and last name are missing.");
case 2:
throw new MissingProperty("last name is missing.");
case 1:
throw new MissingProperty("Username is missing.");
}
}
}
@Override
public void prepareEntityUpdate(UserInfo user) throws DmPortalException {
verifyMandatoryParam(user);
if (passwordEntry != null && !passwordEntry.isEmpty()) {
logger.debug("Will crypt provided password for user " + user.getUsername());
String cryptedPassword = CryptUtility.cryptPasswordWithPbkdf2(passwordEntry);
user.setPassword(cryptedPassword);
}
passwordEntry = null;
}
@Override
protected String getObjectAlreadyExistMessage(UserInfo user) {
if (user == null) {
return null;
}
return "User " + user.getUsername() + " already exists.";
}
public String prepareSessionUserEdit(String viewPath) {
UserInfo sessionUser = (UserInfo) SessionUtility.getUser();
if (sessionUser == null) {
return null;
}
prepareEdit(sessionUser);
return viewPath + "?faces-redirect=true";
}
public String getPasswordEntry() {
return passwordEntry;
}
public void setPasswordEntry(String passwordEntry) {
this.passwordEntry = passwordEntry;
}
public boolean notSelected() {
return current == null;
}
@FacesConverter(forClass = UserInfo.class)
public static class UserInfoControllerConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
UserInfoController controller = (UserInfoController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "userInfoController");
return controller.getEntity(getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuilder sb = new StringBuilder();
sb.append(value);
return sb.toString();
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof UserInfo) {
UserInfo o = (UserInfo) object;
return getStringKey(o.getId());
} else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + UserInfo.class.getName());
}
}
}
}
/*
* 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.controllers;
import gov.anl.aps.dm.portal.model.beans.RoleTypeFacade;
import gov.anl.aps.dm.portal.model.beans.UserInfoFacade;
import gov.anl.aps.dm.portal.model.entities.RoleType;
import gov.anl.aps.dm.portal.model.entities.UserInfo;
import gov.anl.aps.dm.portal.utilities.SessionUtility;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import org.apache.log4j.Logger;
@Named("userSystemRoleController")
@SessionScoped
public class UserSystemRoleController implements Serializable
{
@EJB
private UserInfoFacade userInfoFacade;
@EJB
private RoleTypeFacade roleTypeFacade;
private int rows = 25;
class SystemRoleTypeTable extends DataTableController<UserInfo> {
@Override
public String getClassName() {
return "UserInfo";
}
@Override
public List<UserInfo> findAll() {
return userInfoFacade.findSystemRoleUsers(systemRole.getId());
}
}
class NoSystemRoleTypeTable extends DataTableController<UserInfo> {
@Override
public String getClassName() {
return "UserInfo";
}
@Override
public List<UserInfo> findAll() {
return userInfoFacade.findNoSystemRoleUsers(systemRole.getId());
}
}
private SystemRoleTypeTable systemRoleTable;
private NoSystemRoleTypeTable noSystemRoleTable;
private RoleType systemRole;
private static final Logger logger = Logger.getLogger(UserSystemRoleController.class.getName());
public UserSystemRoleController() {
this.systemRoleTable = new SystemRoleTypeTable();
this.noSystemRoleTable = new NoSystemRoleTypeTable();
}
public int getRows() {
return rows;
}
public RoleType getSystemRole() {
return systemRole;
}
public void setSystemRole(RoleType systemRole) {
this.systemRole = systemRole;
}
public SystemRoleTypeTable getSystemRoleTable() {
return systemRoleTable;
}
public void setSystemRoleTable(SystemRoleTypeTable systemRoleTable) {
this.systemRoleTable = systemRoleTable;
}
public NoSystemRoleTypeTable getNoSystemRoleTable() {
return noSystemRoleTable;
}
public void setNoSystemRoleTable(NoSystemRoleTypeTable noSystemRoleTable) {
this.noSystemRoleTable = noSystemRoleTable;
}
public String getSystemRoleName() {
return systemRole.getName();
}
public String resetSystemRoleUserList() {
logger.debug("Resetting System Role User list");
systemRoleTable.resetList();
return "/views/userSystemRole/list?faces-redirect=true";
}
public void resetNoSystemRoleUserList() {
logger.debug("Resetting System Role Add User list");
noSystemRoleTable.resetList();
}
public String prepareSystemRoleView() {
logger.debug("Preparing system role view");
noSystemRoleTable.resetList();
return resetSystemRoleUserList();
}
public String addSystemRole() {
List<UserInfo> uiList = noSystemRoleTable.getSelectedObjectList();
if (uiList == null) {
logger.debug("null selected list");
} else if (uiList.isEmpty()) {
logger.debug("empty selected list");
} else {
for (UserInfo userInfo : uiList) {
systemRole.addRoleTypeToUser(userInfo);
}
}
try {
roleTypeFacade.edit(systemRole);
resetNoSystemRoleUserList();
return resetSystemRoleUserList();
} catch (RuntimeException ex) {
SessionUtility.addErrorMessage("Error", "Could not update RoleType" + ": " + ex.getMessage());
return null;
}
}
public String removeSystemRole() {
logger.debug("removing system role from user");
systemRole.removeRoleTypeFromUser(systemRoleTable.currentObject);
try {
roleTypeFacade.edit(systemRole);
resetNoSystemRoleUserList();
return resetSystemRoleUserList();
}
catch (RuntimeException ex) {
SessionUtility.addErrorMessage("Error", "Could not update RoleType" + ": " + ex.getMessage());
return null;
}
}
}
/*
* 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.exceptions;
import gov.anl.aps.dm.portal.constants.DmStatus;
/**
* Generic DAQ exception.
*/
public class DmPortalException extends Exception
{
/**
* Exception signature key.
*/
public static final String SignatureKey = "__dm_portal_exception__";
/**
* Exception type key.
*/
public static final String TypeKey = "type";
/**
* Exception code key.
*/
public static final String CodeKey = "code";
/**
* Exception args key.
*/
public static final String ArgsKey = "args";
/**
* Error code.
*/
private int errorCode = DmStatus.DM_ERROR;
/**
* Error message.
*/
private String error = null;
/**
* Constructor.
*/
public DmPortalException()
{
super();
}
/**
* Constructor.
*
* @param message Error message
*/
public DmPortalException(String message)
{
super(message);
}
/**
* Constructor.
*
* @param message Error message
* @param errorCode Error code
*/
public DmPortalException(String message, int errorCode)
{
super(message);
this.errorCode = errorCode;
}
/**
* Constructor.
*
* @param throwable Throwable object
*/
public DmPortalException(Throwable throwable)
{
super(throwable);
}
/**
* Constructor.
*
* @param message Error message
* @param throwable Throwable object
*/
public DmPortalException(String message, Throwable throwable)
{
super(message, throwable);
}
/**
* Set error code.
*
* @param errorCode Error code
*/
public void setErrorCode(int errorCode)
{
this.errorCode = errorCode;
}
/**
* Get error code.
*
* @return Error code
*/
public int getErrorCode()
{
return errorCode;
}
/**
* Set error message.
*
* @param error Error message
*/
public void setErrorMessage(String error)
{
this.error = error;
}
/**
* Get error message.
*
* @return Error message
*/
public String getErrorMessage()
{
return error;
}
/**
* Override string output if error message is set.
*
* @return error message
*/
@Override
public String toString()
{
if(error != null) {
return error;
}
else {
return super.toString();
}
}
}
/*
* 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.exceptions;
import gov.anl.aps.dm.portal.constants.DmStatus;
/**
* Object already exists exception class.
*/
public class InvalidDate extends DmPortalException
{
/**
* Constructor.
*/
public InvalidDate()
{
super();
setErrorCode(DmStatus.DM_INVALID_DATE);
}
/**
* Constructor.
*
* @param message Error message
*/
public InvalidDate(String message)
{
super(message);
setErrorCode(DmStatus.DM_INVALID_DATE);
}
/**
* Constructor.
*
* @param throwable Throwable object
*/
public InvalidDate(Throwable throwable)
{
super(throwable);
setErrorCode(DmStatus.DM_INVALID_DATE);
}
/**
* Constructor.
*
* @param message Error message
* @param throwable Throwable object
*/
public InvalidDate(String message, Throwable throwable)
{
super(message, throwable);
setErrorCode(DmStatus.DM_INVALID_DATE);
}
}
\ No newline at end of file
/*
* 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.exceptions;
import gov.anl.aps.dm.portal.constants.DmStatus;
/**
*
* @author bfrosik
*/
public class MissingProperty extends DmPortalException
{
/**
* Constructor.
*/
public MissingProperty()
{
super();
setErrorCode(DmStatus.DM_MISSING_PROPERTY);
}
/**
* Constructor.
*
* @param message Error message
*/
public MissingProperty(String message)
{
super(message);
setErrorCode(DmStatus.DM_MISSING_PROPERTY);
}
/**
* Constructor.
*
* @param throwable Throwable object
*/
public MissingProperty(Throwable throwable)
{
super(throwable);
setErrorCode(DmStatus.DM_MISSING_PROPERTY);
}
/**
* Constructor.
*
* @param message Error message
* @param throwable Throwable object
*/
public MissingProperty(String message, Throwable throwable)
{
super(message, throwable);
setErrorCode(DmStatus.DM_MISSING_PROPERTY);
}
}
/*
* 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.exceptions;
import gov.anl.aps.dm.portal.constants.DmStatus;
/**
* Object already exists exception class.
*/
public class ObjectAlreadyExists extends DmPortalException
{
/**
* Constructor.
*/
public ObjectAlreadyExists()
{
super();
setErrorCode(DmStatus.DM_OBJECT_ALREADY_EXISTS);
}
/**
* Constructor.
*
* @param message Error message
*/
public ObjectAlreadyExists(String message)
{
super(message);
setErrorCode(DmStatus.DM_OBJECT_ALREADY_EXISTS);
}
/**
* Constructor.
*
* @param throwable Throwable object
*/
public ObjectAlreadyExists(Throwable throwable)
{
super(throwable);
setErrorCode(DmStatus.DM_OBJECT_ALREADY_EXISTS);
}
/**
* Constructor.
*
* @param message Error message
* @param throwable Throwable object
*/
public ObjectAlreadyExists(String message, Throwable throwable)
{
super(message, throwable);
setErrorCode(DmStatus.DM_OBJECT_ALREADY_EXISTS);
}
}
\ No newline at end of file
/*
* 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.model.beans;
import java.util.List;
import javax.persistence.EntityManager;
/**
*
* @author sveseli
*/
public abstract class AbstractFacade<T>
{
private Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public void create(T entity) {
getEntityManager().persist(entity);
}
public T edit(T entity) {
return getEntityManager().merge(entity);
}
public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
public T find(Object id) {
return getEntityManager().find(entityClass, id);
}
public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
public List<T> findRange(int[] range) {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
javax.persistence.Query q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0] + 1);
q.setFirstResult(range[0]);
return q.getResultList();
}
public int count() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
javax.persistence.Query q = getEntityManager().createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
}
}