//$Id

package com.action;

//java imports
import javax.servlet.http.*;
import org.apache.struts.action.*;
import java.util.*;

//
import com.util.ZohoUtil;

/**
 * HomeAction is used to get all the user details from the server and set as cookies.
 * @version 1.0
 */
public class HomeAction extends Action 
{
	public ActionForward execute (ActionMapping mapping,
            ActionForm form,
            HttpServletRequest req,
            HttpServletResponse res) throws Exception
	{
        String ticket=req.getParameter("ticket");
        if(ticket==null)
        {
            Cookie[] cookies = req.getCookies();
            Properties cookieProp = new Properties();
            if (cookies != null)
            {
                for (int i=0; i < cookies.length; i++)
                {
                    cookieProp.put(cookies[i].getName(), cookies[i].getValue());
                }
            }


          ticket = cookieProp.getProperty("IAMTICKET") ;
        }  
        req.setAttribute("ticket",ticket);
        String urlStr="http://accounts.zoho.com/validate?ticket="+ticket;
        System.out.println("urlString :"+urlStr);  
        String response = "";
        String resp = ZohoUtil.getUserDetails(urlStr);
        System.out.println("ZohoUtil.getUserDetails resp:"+resp);  
        String temp="";
        String props[]=resp.split("\n");
        for(int i=0;props.length>i;i++)
        {
           if(props[i].startsWith("LOGIN_ID"))
           {
              temp=props[i].substring(props[i].indexOf("=")+1);
              req.setAttribute("LOGIN_ID",temp);

           }else if(props[i].startsWith("EMAILID"))
           {
              temp=props[i].substring(props[i].indexOf("=")+1);
              req.setAttribute("EMAILID",temp);
               
           }    
   
        }    
        String result = "success";
        //if failure, checks with the response
        if(resp.equals("mailId"))
        {
            result = "failure";    
        }


/**Akiyoshi Matsumoto added part**/
        String stylesheet = ZohoUtil.getStyleSheet();
 	req.setAttribute("stylesheet",stylesheet);
/**end of Akiyoshi Matsumoto added part**/

       return  mapping.findForward("home");
	}

}
