The first time a member registers, the session needs to be set, which normally happens in login, so I thought I'd reuse the LoginAction
without routing to it.
But the sessionmap
isn't getting instantiated.
member logging in is: model.hibernate.Member@549c8a8c
session map not instantiated
RegisterAction
public class RegisterAction extends ActionSupport implements SessionAware{
private String username, password, email;
SessionMap<String,Object> sessionmap;
MemberDAO mdao = new MemberDAO();
UsersDAO udao = new UsersDAO();
Users user = new Users();
Member member = new Member();
public RegisterAction() {
this.email = "";
this.password = "";
this.username = "";
}
// ...setters/getters...
public String execute() {
udao.addUserToDatabase(newUser);
Member newMember = new Member(username, password);
mdao.addMemberToDatabase(newMember);
member = newMember;
// perform first login (to set session member and role).
// this could be done by sending to login action.
// but then would have to track that it was first login. this is quick fix.
LoginAction firstLogin = new LoginAction(member);
firstLogin.setSession(sessionmap);
String firstLoginAttempt = firstLogin.execute();
String resultString = "";
if(firstLoginAttempt.equals(SUCCESS)){resultString = SUCCESS;}
else{
addActionError("First login attempt didnt work");
resultString = ERROR;
}
return resultString; // send user to quiz or show error
}else {
// cant add user
addActionError("Username already taken. Please choose another.");
return ERROR;
}
}
public void setSession(Map<String, Object> map) {
sessionmap=(SessionMap) map;
}
}
LoginAction
public class LoginAction extends ActionSupport implements SessionAware{
private String username, password;
MemberDAO mdao = new MemberDAO();
Member member = new Member();
SessionMap<String,Object> sessionmap;
public LoginAction() {
this.password = "";
this.username = "";
}
public LoginAction(Member m) {
this.member = m;
this.password = member.getPassword();
this.username = member.getUsername();
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String execute() {
// checks that credentials match db
setLoggedInMember(member);
setLoggedInRole(member);
}
public String logout(){
if(sessionmap!=null){
sessionmap.invalidate();
}
return "success";
}
public void setSession(Map<String, Object> map) {
sessionmap=(SessionMap) map;
}
protected void setLoggedInMember(Member m){
System.out.println("member logging in is: "+m.toString());
try{
if(sessionmap!=null){
sessionmap.put("member",m.toString());
}
else{System.out.println("session map not instantiated");}
}catch(Exception e){System.out.println(e);}
}
public Member getLoggedInMember(){
return (Member) sessionmap.get("member");
}
protected void setLoggedInRole(Member member) {
if(member.getAdmin() != null)
sessionmap.put("role", "admin");
else if(member.getAgent()!=null)
sessionmap.put("role", "agent");
else if(member.getUsers()!=null)
sessionmap.put("role", "user");
else
addActionError("Unknown member role");
}
public String getLoggedInRole(){
return (String) sessionmap.get("role");
}