//$Id

/*
* @(#)ZohoMeetingsMgr.java
* Copyright (c) 2007 , All Rights Reserved.
*
*/ 

package com.action;

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

//
import com.util.ZohoUtil;
import com.util.MeetingsMgr;

/**
 * 
 *
 * @version 1.0
 * @see org.apache.struts.actions.DispatchAction
 * @see com.ea.zoho.server.util.ZohoUtil
 */
public class ZohoMeetingsMgr extends DispatchAction 
{
    /**
     *
     */
    public ActionForward getMyMeetingsList(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest req,
                                 HttpServletResponse res)
    {
        String ticket = req.getParameter("apiTicket");
        
        try
        {
            String resp = MeetingsMgr.getAllMyMeetings(ticket);
            sop("getMyMeetingsList:ticket::"+ticket+"**response::"
                       +resp+"**","Debug");
            sendResponseForAJAXCall(res,resp,"");
        }
        catch(Exception e)
        {
            sop("Exception: getMyMeetingsList:ticket::"+ticket+"**","normal");
            e.printStackTrace();    
        }
        return null;
    }

    /**
     *
     */
    public ActionForward getInvitedMeetingsList(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest req,
                                 HttpServletResponse res)
    {
        String ticket = req.getParameter("apiTicket");
        try
        {
            String resp = MeetingsMgr.getAllInvitedMeetings(ticket);
            sop("getInvitedMeetingsList:ticket:"+ticket+"**response:"
                     +resp+"**","Debug");
            sendResponseForAJAXCall(res,resp,"");
        }
        catch(Exception e)
        {
            sop("Exception: getInvitedMeetingsList:ticket:"+ticket+"**","normal");
            e.printStackTrace();    
        }
        return null;
    }

    /**
     * Creates a meeting.
     */
    public ActionForward createMeeting(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest req,
                                 HttpServletResponse res)
    {
        String ticket = req.getParameter("apiTicket");
        String topic = req.getParameter("ttopic");
        String time = req.getParameter("ttime");
        String agenda = req.getParameter("agenda");
        String presenter = req.getParameter("presenter");
        String participants = req.getParameter("participants");
        String control = req.getParameter("ct");
        String timeZone = req.getParameter("timeZone");
        String userName = req.getParameter("userName");
        String mailTime = req.getParameter("mailTime");
        String path=getApplicationPath(req);
        try
        {
            String response = MeetingsMgr.createMeeting(ticket,topic,time,
               agenda,presenter,participants,control,timeZone,userName,mailTime,path);
            sop("createMeeting::topic:"+topic+"**time:"+time+"**agenda:"+agenda+"**"+
                    "presenter:"+presenter+"**participants::"+participants+"**control:"
                    +control+"**timeZone::"+timeZone+"**userName:"+userName+"**"
                    +"mailTime::"+mailTime+"***Response::"+response+"**","Debug");
            sendResponseForAJAXCall(res , response , "" );
        }
        catch(Exception e)
        {
            sop("Exception: createMeeting::topic:"+topic+"**time:"+time+"**agenda:"+agenda+"**"+
                    "presenter:"+presenter+"**participants::"+participants+"**control:"
                    +control+"**timeZone::"+timeZone+"**userName:"+userName+"**"
                    +"mailTime::"+mailTime+"***","normal");
            e.printStackTrace();    
        }
        return null;
    }


    /**
     * Edit meeting.
     */
    public ActionForward editMeeting(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest req,
                                 HttpServletResponse res)
    {
        String ticket = req.getParameter("apiTicket");
        String topic = req.getParameter("ttopic");
        String time = req.getParameter("ttime");
        String agenda = req.getParameter("agenda");
        String presenter = req.getParameter("presenter");
        String participants = req.getParameter("participants");
        String control = req.getParameter("ct");
        String timeZone = req.getParameter("timeZone");
        String userName = req.getParameter("userName");
        String mailTime = req.getParameter("mailTime");
        String meetingKey = req.getParameter("meetingKey");
        String existingParticipants = req.getParameter("existingParticipants");
        String extPresenter = req.getParameter("extPresenter");
        String extTopic = req.getParameter("extTopic");
        String extTime = req.getParameter("extTime");
        String path=getApplicationPath(req);
        try
        {
            String response = MeetingsMgr.editMeeting(ticket,topic,time,
               agenda,presenter,participants,control,timeZone,userName,
               mailTime,meetingKey,existingParticipants,extPresenter,extTopic,extTime,path);
            sop("editMeeting::topic:"+topic+"**time:"+time+"**agenda:"+agenda+"**"+
                "presenter:"+presenter+"**participants::"+participants+"**control:"
                +control+"**timeZone::"+timeZone+"**userName:"+userName+"**"
                +"mailTime::"+mailTime+"***meetingKey:"+meetingKey+"**"+
                "existingParticipants::"+existingParticipants+"**"+
                "extPresenter::"+extPresenter+"**extTopic:"+extTopic+"**"+
                "extTime:"+extTime+"**Response::"+response+"***","Debug");
            sendResponseForAJAXCall(res , response , "" );
        }
        catch(Exception e)
        {
            sop("Exception : editMeeting::topic:"+topic+"**time:"+time+"**agenda:"+agenda+"**"+
                    "presenter:"+presenter+"**participants::"+participants+"**control:"
                    +control+"**timeZone::"+timeZone+"**userName:"+userName+"**"
                    +"mailTime::"+mailTime+"***meetingKey:"+meetingKey+"**"+
                    "existingParticipants::"+existingParticipants+"**"+
                    "extPresenter::"+extPresenter+"**extTopic:"+extTopic+"**"+
                    "extTime:"+extTime+"**","normal");
            e.printStackTrace();    
        }
        return null;
    }


    /**
     * Deletes a meeting.
     */
    public ActionForward deleteMeeting(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest req,
                                 HttpServletResponse res)
    {
        String ticket = req.getParameter("apiTicket");
        String meetingKey = req.getParameter("kkey");
        try
        {
            String response = MeetingsMgr.deleteMeeting(ticket,meetingKey);
            sop("deleteMeeting: apiTicket:"+ticket
                    +"*meetingKey:"+meetingKey+"**response::"+response+"**","debug");
            sendResponseForAJAXCall(res , response , "" );
        }
        catch(Exception e)
        {
            sop("Exception: deleteMeeting: apiTicket:"+ticket
                    +"*meetingKey:"+meetingKey+"**","normal");
            e.printStackTrace();    
        }
        return null;
    }

    /**
     *
     */
    public ActionForward getDownloadZipURL(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest req,
                                 HttpServletResponse res)
    {
        //String ticket = req.getParameter("apiTicket");
        String meetingKey = req.getParameter("meetingKey");
        try
        {
             String response = MeetingsMgr.getDownloadZipURL(meetingKey);
             sop("getDownloadZipURL:meetingKey:"+meetingKey
                     +"**Response:"+response+"***","Debug");
             sendResponseForAJAXCall(res , response , "");
        }
        catch(Exception e)
        {
            sop("Exception: getDownloadZipURL:meetingKey:"+meetingKey
                      +"**","normal");
            e.printStackTrace();    
        }
        return null;
    }
    
    /**
     *
     */
    public ActionForward getEMailId(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest req,
                                 HttpServletResponse res)
    {
        String userId =  req.getParameter("userId");
        try
        {
            String emailId = "dummy@mummy.com";//ZohoUtil.fetchEMailId(userId);
            sop("getEMailId:userId::"+userId+"***emailId:"
                  +emailId+"**","Debug");
            sendResponseForAJAXCall(res, emailId , "text");
        }
        catch(Exception e)
        {
            sop("Exception getEMailId:userId::"+userId+"***","normal");
            e.printStackTrace();    
        }
        return null;
    }

    /**
     *
     */
    public ActionForward getMeetingStatus(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest req,
                                 HttpServletResponse res)
    {
        //String ticket = req.getParameter("apiTicket");
        String meetingKey = req.getParameter("key");
        try
        {
            String resp = MeetingsMgr.getMeetingStatus(meetingKey);
             sop("getMeetingStatus::meetingKey:"+meetingKey+
                   "**Response:"+resp+"***","Debug");
            sendResponseForAJAXCall(res,resp,"");
        }
        catch(Exception e)
        {
            sop("Exception: getMeetingStatus::meetingKey:"
                      +meetingKey+"**","normal");
            e.printStackTrace();    
        }
        return null;
    }

    /**
     *
     */
    public ActionForward stopMeeting(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest req,
                                 HttpServletResponse res)
    {
        //String ticket = req.getParameter("apiTicket");
        String meetingKey = req.getParameter("key");
        try
        {
            String resp = MeetingsMgr.stopMeeting(meetingKey);
            sop("stopMeeting::meetingKey::"+meetingKey+
                     "***Response::"+resp,"Debug");
            sendResponseForAJAXCall(res,resp,"");
        }
        catch(Exception e)
        {
            sop("Exception: stopMeeting::meetingKey::"+meetingKey+"***","normal");
            e.printStackTrace();    
        }
        return null;
    }

    /**
     *
     */
    public ActionForward getMeetingDetails(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest req,
                                 HttpServletResponse res)
    {
        //String ticket = req.getParameter("apiTicket");
        String meetingKey = req.getParameter("key");
        try
        {
            String resp = MeetingsMgr.getMeetingDetails(meetingKey);
            sop("getMeetingDetails:meetingKey:"+meetingKey+
                  "****resp::"+resp+"***","Debug");
            sendResponseForAJAXCall(res,resp,"");
        }
        catch(Exception e)
        {
            sop("Exception:getMeetingDetails:meetingKey:"+meetingKey
                      +"**","normal");
            e.printStackTrace();    
        }
        return null;
    }


    /**
     *
     */
    public ActionForward getTimezone(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest req,
                                 HttpServletResponse res)
    {
        String ticket = req.getParameter("apiTicket");
        String offsetHr = req.getParameter("offsetHr");
        try
        {
            String resp = MeetingsMgr.getTimezone(ticket,offsetHr);
            sop("getTimezone::ticket::"+ticket+"**response:"
                       +resp+"***offsetHr:"+offsetHr+"**","Debug");
            sendResponseForAJAXCall(res,resp,"");
        }
        catch(Exception e)
        {
            sop("Exception:getTimezone:ticket:"+ticket+"**","normal");
            e.printStackTrace();    
        }
        return null;
    }


    /**
     *
     */
    public ActionForward inviteMeeting(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest req,
                                 HttpServletResponse res)
    {
        String ticket = req.getParameter("apiTicket");
        String emails = req.getParameter("emails");
        String topic = req.getParameter("topic");
        String time = req.getParameter("ttime");
        String agenda = req.getParameter("agenda");
        String userName = req.getParameter("userName");
        String timezone = req.getParameter("timezone");
        String meetingKey = req.getParameter("key");
        String presenter = req.getParameter("presenter");
        String path=getApplicationPath(req);
        try
        {
            String resp = MeetingsMgr.inviteMeeting(meetingKey,ticket,emails,
                            topic,time,agenda,userName,timezone,presenter,path);
            sop("inviteMeeting:::emails:"+emails
                    +"***meetingKey::"+meetingKey+"***topic::"+topic+"**"+
                    "time::"+time+"**agenda::"+agenda+"**userName::"+userName+
                    "***timezone::"+timezone+"**presenter::"+presenter
                    +"**Response::"+resp+"**","Debug");
            sendResponseForAJAXCall(res,resp,"");
        }
        catch(Exception e)
        {
            sop("inviteMeeting:::emails:"+emails
                    +"***meetingKey::"+meetingKey+"***topic::"+topic+"**"+
                    "time::"+time+"**agenda::"+agenda+"**userName::"+userName+
                    "***timezone::"+timezone+"**presenter::"+presenter+"**","normal");
            e.printStackTrace();    
        }
        return null;
    }


    /**
     *
     */
    public ActionForward setTimezone(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest req,
                                 HttpServletResponse res)
    {
        String ticket = req.getParameter("apiTicket");
        String timezone = req.getParameter("timezone");
        try
        {
            String resp = MeetingsMgr.setTimezone(timezone,ticket);
            sop("setTimezone::apiTicket::"+ticket+"**timezone::"+
                    timezone+"**Response:"+resp+"**","Debug");
            sendResponseForAJAXCall(res,resp,"");
        }
        catch(Exception e)
        {
            sop("Exception:setTimezone:ticket:"+ticket+"::"+
                    "timezone::"+timezone+"*","normal");
            e.printStackTrace();    
        }
        return null;
    }


    /**
     *
     */
    public ActionForward unspecified(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest req,
                                 HttpServletResponse res)
    {
        //By default fetching all "My Meetings" list
        return mapping.findForward("meetingPage");
    }

    //write the response in HTTPServletResponse PrintWriter
    private void sendResponseForAJAXCall(HttpServletResponse res, 
                   String response , String type)
    {
        try
        {
            PrintWriter pw = res.getWriter();
            StringBuffer sb = new StringBuffer();
            sb.append(response);
            if(type.trim().equalsIgnoreCase("xml"))
            {
                res.setContentType("text/xml");
            }
            res.setContentLength(sb.length());
            pw.println(sb);
        }
        catch(Exception e)
        {
            e.printStackTrace();    
        }
    }

    //Prints the given message in log
    private void sop(String message,String type)
    {
       // ZohoUtil.printInLog("ZohoMeetingsMgr:: "+message,type);
    }
    private String getApplicationPath(HttpServletRequest request)
    {
        String url=request.getRequestURL().toString();
        String spath=request.getServletPath();
        int indx=url.indexOf(spath);
       return url.substring(0,indx);
      
    }    
}

