//$Id: MeetingsMgr.java,v 1.1 2008/06/18 12:29:33 ahmed Exp $




//
package com.util;

//
import java.util.StringTokenizer;




/**
 * Used in managing Zoho Meetings
 */
public class MeetingsMgr
{

    //
    private static String apikey = ZohoUtil.API_KEY; 

    /** 
     * Used as prefix for all the meeting requests
     */
    private static String serverName = "http://meeting.zoho.com/"; 

    /**
     * URL for fetching my meetings list
     */
    private static String getMyMeetingsURL = 
          serverName+"api/private/json/meetings/getMeetingList";

    /**
     * URL for fetching invited meetings list
     */
    private static String getInvitedMeetingsURL = 
        serverName+"api/private/json/meetings/getInvitedMeetingList";

    /**
     * URL for fetching details of a meeting based on meeting key
     */
    private static String getMeetingDetailsURL = 
         serverName+"api/public/json/meetings/getMeetingDetails?apikey="+apikey;

    /**
     * URL for fetching status of a meeting based on meeting key
     */
    private static String getMeetingStatusURL = 
         serverName+"api/public/json/meetings/getMeetingStatus?apikey="+apikey;

    /**
     * URL for creating a meeting
     */
    private static String createMeetingURL = 
           serverName+"api/private/json/meetings/createMeeting";

    /**
     *
     */
    private static String deleteMeetingURL = 
         serverName+"api/private/json/meetings/deleteMeeting";

    /**
     * URL for stopping a meeting
     */
    private static String stopMeetingURL = 
         serverName+"api/public/json/meetings/stopMeeting?apikey="+apikey;
         //serverName+"api/private/json/meetings/sendAgentMessages";

    /**
     * URL for fetching timezone
     */
    private static String getTimezoneURL =
          serverName+"api/private/json/meetings/getTimeZone";

    /**
     * URL for setting timezone
     */
    private static String setTimezoneURL = 
          serverName+"api/private/json/meetings/setTimeZone";

    /**
     * URL for inviting into a meeting
     */
    private static String inviteMeetingURL =
         serverName+"api/private/json/meetings/inviteParticipant";

    /**
     * URL for getting the url for downloading agent zip
     */
    private static String downloadZipURL =
         serverName+"api/public/json/meetings/getZipPath?apikey="+apikey;
         //-------- For logged in users : with IAMTICKET ---
         //serverName+"api/private/json/meetings/getZipPath";

    /**
     * URL for editing a meeting
     */
    private static String editMeetingURL = 
         serverName+"api/private/json/meetings/updateMeeting";

    //
    private static String productName = "assist";
 
    /**
     * Fetches all "My Meetings" list. 
     * And returns the same in JSON format.
     *
     * @param ticket - 
     * @return String - my meetings list in JSON format
     */
    public static String getAllMyMeetings(String ticket)
    {
        String completeURL = appendKeyAndTicket(getMyMeetingsURL,ticket);
        String response =  "";
        try
        {
            response = ZohoUtil.getURLResponse(
                           completeURL , "GET" , null );
        }
        catch(Exception e)
        {
            sop("getAllMyMeetings:Exception:ticket ::"+ ticket+"*** URL::"+completeURL
                     + "*** response::"+response+"**","normal");
            e.printStackTrace();    
        }
        return response;
    }

    /**
     * Fetches all "Invited Meetings" list.
     * And returns the same in JSON format
     *
     * @param ticket - 
     * @return String -  invited meeting list in JSON format
     */
    public static String getAllInvitedMeetings(String ticket)
    {
        String completeURL = appendKeyAndTicket(getInvitedMeetingsURL,ticket);
        String response =  "";
        try
        {
            response = ZohoUtil.getURLResponse(
                           completeURL , "GET" , null );
        }
        catch(Exception e)
        {
            sop("getAllInvitedMeetings:Exception:ticket ::"+ ticket+"*** URL::"+completeURL
                     + "*** response::"+response+"**","normal");
            e.printStackTrace();    
        }
        return response;
    }


    /**
     * Edit meeting 
     */
    public static String editMeeting(String ticket, 
               String topic , String time , String agenda, String presenter,
               String participants , String control , String timeZone ,String userName,
               String mailTime,String meetingKey, String existingParticipants,
               String extPresenter, String extTopic,String extTime,String path)
    {
        sop("editMeeting::topic:"+topic+"**time:"+time+"**agenda:"+agenda+"**"+
                "presenter:"+presenter+"**participants::"+participants+"**control:"
                +control+"**timeZone::"+timeZone+"**userName::"+userName+"**"+
                "mailTime:"+mailTime+"***meetingKey:"+meetingKey+"**"+
                "existingPartiicipants::"+existingParticipants+"***"+
                "extPresenter:"+extPresenter+"**extTopic:"+extTopic+"**"
                +"extTime:"+extTime+"**","Debug");
        //
         String participantsWithSkype = participants;
         //Only one mail id
         if(!participantsWithSkype.trim().equals("") )
         {
             participantsWithSkype = participantsWithSkype.replaceAll("," , ":,");
             participantsWithSkype = participantsWithSkype+":";
         }

         String completeURL = appendKeyAndTicket(editMeetingURL,ticket);
         completeURL = completeURL+"&topic="+topic+"&meetingKey="+meetingKey 
                           + "&presenter="+presenter
                           +"&participants="+participantsWithSkype
                           +"&date="+time+"&agenda="+agenda
                           +"&skype=no&agentType="+control+"&sendMail=no";
         String response = "";
         try
         {
             response = 
                ZohoUtil.getURLResponse(completeURL , "POST" , "");
             if(response != null && !response.trim().equals(""))
             {
                 participants = participants+":::existing_Participant:::"
                                +existingParticipants;
                 presenter = presenter + ":::existing_Presenter:::"
                                +extPresenter;
                 topic = topic +":::123_existing_topic_123:::"+extTopic;
                 mailTime = mailTime + ":::existing_Time:::"+extTime;
                 SendMeetingLink sml = new SendMeetingLink(response, 
                          presenter , participants , topic , mailTime , agenda , 
                          timeZone , userName,meetingKey,path);
             }
         }
         catch(Exception e)
         {
             sop("editMeeting::Exception:topic:"+topic+"**time:"+time+"**agenda:"+agenda+"**"+
                     "presenter:"+presenter+"**participants::"+participants+"**control:"
                     +control+"**timeZone::"+timeZone+"**userName::"+userName+"**"+
                     "mailTime:"+mailTime+"***meetingKey:"+meetingKey+"**"+
                     "existingPartiicipants::"+existingParticipants+"***"+
                     "extPresenter:"+extPresenter+"**extTopic:"+extTopic+"**"
                     +"extTime:"+extTime+"**","normal");
             sop("editMeeting:: Exception:URL:"+completeURL+"*** Response:"+response+"**","normal");
             e.printStackTrace();    
         }
         sop("editMeeting:: URL:"+completeURL+"*** Response:"+response+"**","Debug");
         return response;
    }

    /**
     * Creates a new meeting 
     */
    public static String createMeeting(String ticket, 
               String topic , String time , String agenda, String presenter,
               String participants , String control , String timeZone ,String userName,
               String mailTime,String path)
    {
        sop("createMeeting::topic:"+topic+"**time:"+time+"**agenda:"+agenda+"**"+
                "presenter:"+presenter+"**participants::"+participants+"**control:"
                +control+"**timeZone::"+timeZone+"**userName::"+userName+"**"+
                "mailTime:"+mailTime+"***","Debug");
         //
         String participantsWithSkype = participants;
         //Only one mail id
         if(!participantsWithSkype.trim().equals("") )
         {
             participantsWithSkype = participantsWithSkype.replaceAll("," , ":,");
             participantsWithSkype = participantsWithSkype+":";
         }

         String completeURL = appendKeyAndTicket(createMeetingURL,ticket);
         completeURL = completeURL+"&topic="+topic 
                           + "&presenter="+presenter
                           +"&participants="+participantsWithSkype
                           +"&source="+productName+"&date="+time+"&agenda="+agenda
                           +"&skype=no&agentType="+control+"&sendMail=no";
         String response = "";
         try
         {
             response =ZohoUtil.getURLResponse(completeURL , "POST" , "");
             if(response != null && !response.trim().equals(""))
             {
                 SendMeetingLink sml = new SendMeetingLink(response, 
                          presenter , participants , topic , mailTime , agenda , 
                          timeZone , userName,"",path);
             }
         }
         catch(Exception e)
         {
            sop("createMeeting:Exception:topic:"+topic+"**time:"+time+"**agenda:"+agenda+"**"+
               "presenter:"+presenter+"**participants::"+participants+"**control:"
               +control+"**timeZone::"+timeZone+"**userName::"+userName+"**"+
               "mailTime:"+mailTime+"***","normal");
             sop("createMeeting:Exception: URL:"+completeURL+"*** Response:"+response+"**","normal");
             e.printStackTrace();    
         }
         sop("createMeeting:: URL:"+completeURL+"*** Response:"+response+"**","Debug");
         return response;
    }

    /**
     *
     */
    public static String deleteMeeting(String ticket , String meetingKey)
    {
        String completeURL = appendKeyAndTicket(deleteMeetingURL,ticket);
        completeURL = completeURL + "&meetingKey="+meetingKey;
        String response = "";
        try
        {
            response = ZohoUtil.
                   getURLResponse(completeURL , "POST" , "");
        }
        catch(Exception e)
        {
            sop("deleteMeeting:Excepiton: completeURL:"+completeURL
                   +"** respone:"+response+"**","normal");
            e.printStackTrace();    
        }
        sop("deleteMeeting:: completeURL:"+completeURL
                +"** respone:"+response+"**","Debug");
        return response;
    }

    /**
     * Returns the meeting details in JSON format.
     */
    public static String getMeetingDetails(String meetingKey)
    {
        String baseUrl = getMeetingDetailsURL+"&meetingKey=" + meetingKey;
        String completeURL = baseUrl; //appendKeyAndTicket(baseUrl,ticket);
        String response =  "";
        try
        {
            response = ZohoUtil.getURLResponse(
                           completeURL , "GET" , null );
        }
        catch(Exception e)
        {
            sop("getMeetingDetails: Exception:*** URL::"+completeURL
                     + "*** response::"+response+"**","normal");
            e.printStackTrace();    
        }
        sop("getMeetingDetails: Exception:*** URL::"+completeURL
                + "*** response::"+response+"**","Debug");
        return response;
    }


    /**
     * Returns the meeting status in Text format.
     */
    public static String getMeetingStatus(String meetingKey)
    {
        String completeURL = getMeetingStatusURL;
        completeURL = completeURL+"&meetingKey="+meetingKey;
        String response =  "";
        try
        {
            response = ZohoUtil.getURLResponse(
                           completeURL , "GET" , null );
        }
        catch(Exception e)
        {
            sop("getMeetingStatus: Exception*** URL::"+completeURL
                     + "*** response::"+response+"**","normal");
            e.printStackTrace();    
        }
        sop("getMeetingStatus: *** URL::"+completeURL
                + "*** response::"+response+"**","Debug");
        return response;
    }


    /**
     * Stops the meeting.
     */
    public static String stopMeeting(String meetingKey)
    {
        String completeURL = stopMeetingURL;
        completeURL = completeURL+"&meetingKey="+meetingKey;
        String response =  "";
        try
        {
            response = ZohoUtil.getURLResponse(
                           completeURL , "GET" , null );
        }
        catch(Exception e)
        {
            sop("stopMeeting: *** Excepiton:URL::"+completeURL
                     + "*** response::"+response+"**","normal");
            e.printStackTrace();    
        }
        sop("stopMeeting: *** URL::"+completeURL
                + "*** response::"+response+"**","Debug");
        return response;
    }

    /**
     *
     */
    public static String getTimezone(String ticket, String offsetHr)
    {
        String completeURL = appendKeyAndTicket(getTimezoneURL,ticket);
        completeURL = completeURL+"&offsetHr="+offsetHr;
        String response = "";
        try
        {
            response = ZohoUtil.getURLResponse(
                          completeURL , "GET" , null );
        }
        catch(Exception e)
        {
            sop("getTimezone:: Exception: URL::"+completeURL+"** respone::"
                 +response+"**","normal");
            e.printStackTrace();    
        }
        sop("getTimezone:: URL::"+completeURL+"** respone::"+response+"**","Debug");
        return response;
    }

    /**
     *
     */
    public static String setTimezone(String timezone, String ticket)
    {
        String completeURL = appendKeyAndTicket(setTimezoneURL , ticket);
        completeURL = completeURL+"&userTimeZone="+timezone;
        String response = "";
        try
        {
            response = ZohoUtil.getURLResponse(
                          completeURL , "POST" , "" );
        }
        catch(Exception e)
        {
            sop("setTimezone URL:Exception:"+completeURL+"** respone::"
                  +response+"**","normal");
            e.printStackTrace();    
        }
        sop("setTimezone URL::"+completeURL+"** respone::"+response+"**","Debug");
        return response;
    }

    /**
     *
     */
    public static String inviteMeeting(String meetingKey , 
              String ticket , String emails,String topic , String time ,
              String agenda, String userName , String timezone, String presenter,String path)
    {
        String response = "";
        try
        {
                String completeURL = appendKeyAndTicket(inviteMeetingURL , ticket);
                completeURL = completeURL+"&meetingKey="+meetingKey
                    +"&inviteeMailId="+emails+"&sendMail=no";
                try
                {
                    response = ZohoUtil.getURLResponse(
                            completeURL , "POST" , "" );
                    if(response != null && !response.trim().equals(""))
                    {
                        SendMeetingLink sml = new SendMeetingLink(response, 
                                presenter , emails , topic , time , agenda , 
                                timezone , userName,meetingKey,path);
                    }
                }
                catch(Exception e)
                {
                    e.printStackTrace();    
                }
        }
        catch(Exception ex)
        {
            sop("inviteMeeting emails:Excepiton:"+emails+"** respone::"
                    +response+"**","normal");
            ex.printStackTrace();    
        }
        if(response == null || response.trim().equals(""))
        {
            response = "{}";
        }
        response = "{\"emails\":\""+emails+"\",\"zohoResp\":"+response+"}";
        sop("inviteMeeting emails::"+emails+"** respone::"+response+"**","Debug");
        return response;
    }

    /*
     * -------- With IAMTICKET:: For Logged-in users -------
    public static String getDownloadZipURL(String meetingKey , String ticket)
    {
        String response = "";
        String completeURL = appendKeyAndTicket(downloadZipURL , ticket);
        try
        {
            completeURL = completeURL + "&meetingKey="+meetingKey
                             +"&productName=assist";
            response = ZohoUtil.getURLResponse(completeURL,
                             "GET",null);
             
        }
        catch(Exception ex)
        {
            ex.printStackTrace();    
        }
        sop("getDownloadZipURL::completeURL:"+completeURL
                    +":::::::::response::"+response+"**");
        return response;
    }
    */


    /**
     * 
     */
    public static String getDownloadZipURL(String meetingKey )
    {
        String response = "";
        String completeURL = downloadZipURL ;
        try
        {
            completeURL = completeURL + "&meetingKey="+meetingKey
                             +"&productName="+productName;
            response = ZohoUtil.getURLResponse(completeURL,
                             "GET",null);
             
        }
        catch(Exception ex)
        {
            sop("getDownloadZipURL:Exception:completeURL:"+completeURL
                    +":::::::::response::"+response+"**","normal");
            ex.printStackTrace();    
        }
        sop("getDownloadZipURL::completeURL:"+completeURL
                +":::::::::response::"+response+"**","Debug");
        return response;
    }
 
    //
    private static String appendKeyAndTicket(String urll,String ticket)
    {
        String temp = urll + "?apikey="+apikey+"&ticket="+ticket;
        return temp;
    }

    //Prints the given message in log
    private static void sop(String message,String type)
    {
       // ZohoUtil.printInLog("MeetingsMgr:: "+ message,type);
    }

}
