package gov.nih.nci.cadsr.formloader.struts2;


import gov.nih.nci.ncicb.cadsr.common.persistence.dao.UserManagerDAO;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport implements SessionAware, ServletRequestAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private WebApplicationContext applicationContext;
    private HttpServletRequest servletRequest;
    
	private String username;
	private String password;
	private String appname; 
	private String [] apps = {"FormBuilder", "FormLoader" };
	SessionMap<String, String> sessionmap;
	boolean clear = false;
	
	public String execute() {
		applicationContext =
				WebApplicationContextUtils.getRequiredWebApplicationContext(
	                                    ServletActionContext.getServletContext()
	                        );
		UserManagerDAO userManagerDAOV2 = (UserManagerDAO)applicationContext.getBean("userManagerDAO");

		if (userManagerDAOV2.validUser(username, password)==true) {
			Map session = ActionContext.getContext().getSession();
			setSession(session);
			return SUCCESS;
		} else
		{
			return ERROR;
		}

	}
	
	public String cancel() {
		return SUCCESS;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}


	public List<String> getAppList() {
		
		return Arrays.asList(apps);
		
	}
	
	public String [] getApps() {
		return this.apps;
	}


	public String getAppname() {
		return appname;
	}


	public void setAppname(String appname) {
		this.appname = appname;
	}

	@Override
	public void setSession(Map<String, Object> map) {
		sessionmap = (SessionMap)map;
		sessionmap.put("login","true");
		sessionmap.put("username",getUsername());
	}
	
	public String clear()
	{
		sessionmap.invalidate();
		return SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		servletRequest = arg0;
		
	}
	
	public void validateClear(){
		clear = true;
	}
	
}

@@ -1066,16 +1066,18 @@
 der%22 %7D;%0A
+%09%0A
 %09Session
@@ -1519,16 +1519,18 @@
 e) %7B%0A%09%09%09
+//
 Map sess
@@ -1579,16 +1579,18 @@
 n();%0A%09%09%09
+//
 setSessi
@@ -1602,16 +1602,160 @@
 ssion);%0A
+%09%09%09this.sessionmap.put(FormLoaderActionConstants.KEY_LOGIN, %22true%22);%0A%09%09%09this.sessionmap.put(FormLoaderActionConstants.KEY_USER_NAME, username);%0A
 %09%09%09retur
@@ -1777,12 +1777,65 @@
 else
+ %7B
 %0A%09%09
-%7B
+%09addActionError(%22Invalid login. Please try again.%22);
 %0A%09%09%09
@@ -2523,87 +2523,13 @@
 p;%0A%09
-%09sessionmap.put(%22login%22,%22true%22);%0A%09%09sessionmap.put(%22username%22,getUsername());
+%7D%0A%09
 %0A%09
-%7D
 %0A%09%0A%09

put username to session in execute()