package Controllers;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import toolbox.JSONResponse;
import toolbox.Sha;
import models.User;
import flexjson.JSONSerializer;
import MVC.Controller;

/**
 * @author Alexander
 *
 */
public class UserCtl extends Controller {

    @Override
    public void doRequest(String actionName, Integer id) throws Exception {

        EntityManager em = this.getServletContext().getEM();
        JSONSerializer serializer = new JSONSerializer();

        // if(actionName.equals("create")) {
        //
        // User u = new User();
        //
        // // u.setEmail(getRequest().getParameter("email"));
        //
        // em.getTransaction().begin();
        // em.persist(u);
        // em.getTransaction().commit();

        //
        if (actionName.equals("get")) {
            List<?> list = em.createNamedQuery("User.findAll").getResultList();

            
            this.getRequest().setAttribute("json",
                    serializer.serialize(list));

            forward("/json.jsp");

        }
        
        if(actionName.equals("logout"))
        {
            JSONResponse response;
            try
            {
                this.getRequest().getSession().removeAttribute("user");
                response = new JSONResponse(true, null, null);
            }
            catch(Exception ex)
            {
                response = new JSONResponse(ex.getMessage());
            }
            
            this.getRequest().setAttribute("json", serializer.serialize(response));
            this.forward("/json.jsp");
        }
        
        if(actionName.equals("login"))
        {
            JSONResponse response = null;
            try
            {
                Query qry = em.createNamedQuery("User.findByEmailAndPassword");
                qry.setParameter("email", this.getRequest().getParameter("email"));
                qry.setParameter("password", Sha.hash256(this.getRequest().getParameter("password")));
                User user = (User) qry.getSingleResult();
                this.getRequest().getSession().setAttribute("user", user);
                response = new JSONResponse(user);
            }
            catch (NoResultException ex)
            {
                response = new JSONResponse("Invalid email address or password");
            }
            catch (Exception ex)
            {
                response = new JSONResponse(ex.getMessage());
            }
            
            this.getRequest().setAttribute("json", serializer.serialize(response));
            forward("/json.jsp");
        }

    }

}

@@ -1170,24 +1170,560 @@
 n.jsp%22);%0D%0A%0D%0A
+        %7D%0D%0A        %0D%0A        if(actionName.equals(%22isLoggedIn%22))%0D%0A        %7B%0D%0A            JSONResponse response;%0D%0A            if(this.getRequest().getSession().getAttribute(%22user%22) == null)%0D%0A            %7B%0D%0A                response = new JSONResponse(true, null, true);%0D%0A            %7D%0D%0A            else%0D%0A            %7B%0D%0A                response = new JSONResponse(true, null, false);%0D%0A            %7D%0D%0A            %0D%0A            this.getRequest().setAttribute(%22json%22, serializer.serialize(response));%0D%0A            this.forward(%22/json.jsp%22);%0D%0A
         %7D%0D%0A 

Implement isLoggedIn method