Skip to content

Instantly share code, notes, and snippets.

@theresajayne
Created November 1, 2011 15:43
Show Gist options
  • Save theresajayne/1330864 to your computer and use it in GitHub Desktop.
Save theresajayne/1330864 to your computer and use it in GitHub Desktop.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.inbrand.actions;
import com.opensymphony.xwork2.ActionSupport;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import uk.co.inbrand.dto.AddressBase;
import uk.co.inbrand.dto.OrganisationBase;
import uk.co.inbrand.dto.UserRolesBase;
import uk.co.inbrand.dto.UsersBase;
import uk.co.inbrand.service.Service;
import uk.co.inbrand.utilities.MD5Utils;
/**
*
* @author Thersa Jayne Forster
*/
public class UserAction extends ActionSupport implements StrutsStatics,ServletRequestAware,ServletResponseAware{
private Service appservice;
private UsersBase userbean;
private OrganisationBase orgbean;
private List<UsersBase> userslistbean;
private HttpServletRequest request;
private HttpServletResponse response;
private String user_password;
private List<UserRolesBase> userroleslist;
private String[] userroleresponse;
private static final Logger LOGGER = Logger.getLogger(UserAction.class);
public UserAction(Service service) {
this.appservice = service;
}
public String list() throws Exception {
String orgString =request.getParameter("org");
int org_id = 0;
if(orgString!=null) {
org_id = Integer.parseInt(orgString);
} else {
//try to get it from the session.
setOrgbean((OrganisationBase)request.getSession().getAttribute("organisation"));
org_id = getOrgbean().getOrg_id();
}
if(org_id != 0) {
setOrgbean(getAppservice().getOrganisationById(org_id));
setUserslistbean(getAppservice().getUsersForOrg(getOrgbean()));
request.getSession().setAttribute("organisation", getOrgbean());
}
return SUCCESS;
}
public String delete() throws Exception {
//The supplied form is deleting an organisation
//we need to check if it is a Root org or a sub office
String tempid = request.getParameter("id");
if(tempid!= null) {
int id = Integer.parseInt(tempid);
appservice.deleteUser(id);
}
return SUCCESS;
}
private void populateList() {
setUserroleslist(appservice.getUserRoles());
}
public String create() throws Exception {
populateList();
UsersBase dto = new UsersBase();
AddressBase addr_dto = new AddressBase();
dto.setUser_id(0);
if(getOrgbean().getOrg_address()!= null) {
addr_dto = getOrgbean().getOrg_address(); //Set the users initial details to the same as the organisation then set it as a new record
}
addr_dto.setAddr_id(0);
dto.setUser_address(addr_dto);
dto.setUser_organisation(getOrgbean());
this.setUserbean(dto);
return "input";
}
public String edit() throws Exception {
populateList();
String tempid = request.getParameter("id-edit");
if(tempid != null) {
int id = Integer.parseInt(tempid);
userbean = appservice.getUsersById(id);
request.getSession().setAttribute("organisation", userbean.getUser_organisation());
return "input";
} else {
//we are saving the update
//userbean.setUser_organisation(getOrgbean());
orgbean = (OrganisationBase)request.getSession().getAttribute("organisation");
userbean.setUser_organisation(orgbean);
if(userbean.getUser_password()!= null) {
//Encrypt the password
userbean.setUser_password(MD5Utils.getMD5(userbean.getUser_password(), LOGGER));
}
processRoles();
appservice.saveUsers(userbean);
return SUCCESS;
}
}
private void processRoles() {
Set<UserRolesBase> roles = new HashSet<UserRolesBase>();
//Process the roles for the system.
for(int a=0;a< userroleresponse.length;a++) {
UserRolesBase dto = appservice.getUserRoleById(Integer.parseInt(userroleresponse[a]));
roles.add(dto);
}
userbean.setUser_roles(roles);
}
public void validateEdit(){
if(request.getParameter("id-edit")== null) {
if(userbean.getUser_name().length()==0) {
addFieldError("userbean.user_name", "You must have a login name");
}
if(userbean.getUser_password().compareTo("") !=0 ) {
if((getUser_password() == null) || (getUser_password().compareTo(userbean.getUser_password())!=0))
addFieldError("userbean.user_password", "Passwords do not match");
}
}
}
@Override
public void setServletRequest(HttpServletRequest hsr) {
this.request = hsr;
}
@Override
public void setServletResponse(HttpServletResponse hsr) {
this.response = hsr;
}
/**
* @return the appservice
*/
public Service getAppservice() {
return appservice;
}
/**
* @param appservice the appservice to set
*/
public void setAppservice(Service appservice) {
this.appservice = appservice;
}
/**
* @return the userbean
*/
public UsersBase getUserbean() {
return userbean;
}
/**
* @param userbean the userbean to set
*/
public void setUserbean(UsersBase userbean) {
this.userbean = userbean;
}
/**
* @return the userslistbean
*/
public List<UsersBase> getUserslistbean() {
return userslistbean;
}
/**
* @param userslistbean the userslistbean to set
*/
public void setUserslistbean(List<UsersBase> userslistbean) {
this.userslistbean = userslistbean;
}
/**
* @return the orgbean
*/
public OrganisationBase getOrgbean() {
return orgbean;
}
/**
* @param orgbean the orgbean to set
*/
public void setOrgbean(OrganisationBase orgbean) {
this.orgbean = orgbean;
}
/**
* @return the user_password
*/
public String getUser_password() {
return user_password;
}
/**
* @param user_password the user_password to set
*/
public void setUser_password(String user_password) {
this.user_password = user_password;
}
/**
* @return the userroleslist
*/
public List<UserRolesBase> getUserroleslist() {
return userroleslist;
}
/**
* @param userroleslist the userroleslist to set
*/
public void setUserroleslist(List<UserRolesBase> userroleslist) {
this.userroleslist = userroleslist;
}
/**
* @return the userroleresponse
*/
public String[] getUserroleresponse() {
return userroleresponse;
}
/**
* @param userroleresponse the userroleresponse to set
*/
public void setUserroleresponse(String[] userroleresponse) {
this.userroleresponse = userroleresponse;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment