package hirondelle.fish.access.role;
import hirondelle.web4j.action.ActionTemplateListAndEdit;
import hirondelle.web4j.action.ResponsePage;
import hirondelle.web4j.config.ConnectionSrc;
import hirondelle.web4j.database.DAOException;
import hirondelle.web4j.database.DuplicateException;
import hirondelle.web4j.database.SqlId;
import hirondelle.web4j.model.Id;
import hirondelle.web4j.model.ModelCtorException;
import hirondelle.web4j.model.ModelFromRequest;
import hirondelle.web4j.request.RequestParameter;
import hirondelle.web4j.request.RequestParser;
import hirondelle.web4j.security.SafeText;
import java.util.ArrayList;
import java.util.List;
public final class RoleAction extends ActionTemplateListAndEdit {
public RoleAction(RequestParser aRequestParser){
super(FORWARD, REDIRECT_TO_LISTING, aRequestParser);
}
public static final SqlId ROLES_FETCH = new SqlId(ConnectionSrc.ACCESS_CONTROL, "ROLES_FETCH");
public static final SqlId ROLES_LIST = new SqlId(ConnectionSrc.ACCESS_CONTROL, "ROLES_LIST");
public static final SqlId ROLES_DELETE = new SqlId(ConnectionSrc.ACCESS_CONTROL, "ROLES_DELETE");
public static final SqlId ROLES_ADD = new SqlId(ConnectionSrc.ACCESS_CONTROL, "ROLES_ADD");
public static final RequestParameter NAME = RequestParameter.withLengthCheck("UserName");
public static final RequestParameter ROLES = RequestParameter.withRegexCheck(
"Roles", "(user-general|user-president|webmaster|translator|access-control)"
);
protected void doList() throws DAOException {
addToRequest(ITEMS_FOR_LISTING, fRoleDAO.list());
}
protected void validateUserInput() {
try {
ModelFromRequest builder = new ModelFromRequest(getRequestParser());
List<Id> roles = new ArrayList<Id>( getRequestParser().toIds(ROLES) );
fUserRole = builder.build(UserRole.class, NAME, roles);
}
catch (ModelCtorException ex){
addError(ex);
}
}
protected void attemptAdd() throws DAOException {
throw new UnsupportedOperationException();
}
protected void attemptFetchForChange() throws DAOException {
SafeText name = getParam(NAME);
UserRole userRole = fRoleDAO.fetch(name);
if( userRole == null ){
addError("Item no longer exists. Likely deleted by another user.");
}
else {
addToRequest(ITEM_FOR_EDIT, userRole);
}
}
protected void attemptChange() throws DAOException {
try {
boolean success = fRoleDAO.change(fUserRole);
if (success){
addMessage("_1_ changed successfully.", fUserRole.getUserName());
}
else {
addError("No update occurred.");
}
}
catch(DuplicateException ex){
addError("Problem occurred with possible duplicate Role.");
}
}
protected void attemptDelete() throws DAOException {
throw new UnsupportedOperationException();
}
private UserRole fUserRole;
private RoleDAO fRoleDAO = new RoleDAO();
private static final ResponsePage FORWARD = new ResponsePage("Roles", "Role.jsp", RoleAction.class);
private static final ResponsePage REDIRECT_TO_LISTING = new ResponsePage("RoleAction.list");
}