package com.cogix.vwf;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;


//	The ViewsFlash mechanism for publishing extensibility
//	Please use indicatd WriteLog message codes
//	Note addition of "definition def" to most methods 10/9/00

class PublishNotifierExtension extends PublishNotifier {

    void init (ServletConfig conf) {
        Dirs.getErrorLog().WriteLog ("3000I", "initializing " + this.getClass().toString() );
    }

    void destroy () {
    }

    void onPublishChange ( File pollfile, File resultsfile,
                           String spotname, String pollid, Date open, Date close,
                           Spot spot, definition def, Poll pol, ThisCall thiscall ) {
        Dirs.getErrorLog().WriteLog ("3001I", "onPublishChange " + pollid );

        //	Common things to access:
        PrintWriter out = thiscall.out;
        HttpServletRequest req = thiscall.req;
        HttpSession session = req.getSession (false);
        HttpServletResponse res = thiscall.res;
        ServletContext servletContext = Dirs.getServletContext ();
        ServletConfig servletConfig = Dirs.getServletConfig ();

        //	To access custom fields.
        //	note that custom c/b null, custom[x] c/b null

        String valueOfCustomA = def != null && def.custom != null  ? def.custom [0] : "";	// customa is 0, customz is 25

        //	Note that the Files being referred to here may not exist yet;
        //	They will be created at "open" time and destroyed at "close" time.
        //	No need to do anything here, but you can if you want to:
        Date now = new Date();
        if ( open != null ) {
            if ( now.after (open) ) {
                //	poll may be open
                if ( close != null ) {
                    if ( now.before (close) ) {
                        // poll is open for sure.
                    }
                    else {
                        ;//	poll is closed for sure
                    }
                }
                else {
                    //	poll is after opening date, and there is no closing date, so it's probably open.
                }
            }
            else {
                //	poll is not open yet
            }
        }
        else {
            //	no opening date, poll should be presumed closed.
        }
    }

    void onPublishOpen ( File pollfile, File resultsfile,
                         String spotname, String pollid,
                         Spot spot, definition def, Poll pol ) {
        Dirs.getErrorLog().WriteLog ("3002I", "onPublishOpen " + pollid );

        //	Include this code to send an e-mail to the recipients named in
        //	the Spot.emailnotify field ( set in Place settings ) .
        //	This is called twice; once with pollfile != null, one with resultsfile != null.
        if ( pollfile != null )	//	This avoids sending out two emails.
            emailNotification (spot, "Opened", spotname, pollid);

        //	pollfile contains the html of the voting form or page; can be null
        //	resultsfile contains results page; can be null
        //	This method called once for the poll page, once for results page.
        if ( pollfile != null ) {
            FileInputStream fis = null;
            try {
                if ( pollfile.lastModified() > 0 ) {	//	can be compared against new Date() too.
                    //	File exists
                    long size = pollfile.length();
                    fis = new FileInputStream (pollfile) ;
                    byte [] content = new byte [(int)size];
                    fis.read (content, 0, (int)size );
                    fis.close();
                    //	Now you can write the file, or do whatever needs to be done.
                }
            }
            catch (IOException ioe) {
                //	Do something like writing to the ViewsFlash log.
                //	Use xxxxI for info, xxxxE for serious error with operator e-mail.
                Dirs.getErrorLog().WriteLog("3003I", "Message " +  ioe.toString());
            }
            finally {
                if ( fis != null )
                    try { fis.close (); } catch ( IOException ignored) {}
            }
        }
        //	similar code to above for resultsfile

        //	How to get popular things:
        //	definition def is the survey/poll definition ("Poll page").
        qdefinition qdef = def.findqdefinition("maxparticipants");	//	a question's definition ("Question page")
        answer ans = (answer) qdef.answers.elementAt(0);				//	first answer to that question
        String value = ans.text;							//	value of that answer

        String pollname = def.pollname;	//	same as pollid
        String polltitle = def.polltitle;	//	poll title
        String pollheader = def.pollheader;	//	poll header
        String pollfooter = def.pollfooter;	//	poll footer

        ScheduledPoll sp = spot.getScheduledPoll(pollid);
        Date publishdate = sp.begin;	//	publish date
        Date unpublishdate = sp.end;	//	unpublish date
    }

    void onPublishClose ( File pollfile, File resultsfile,
                          String spotname, String pollid,
                          Spot spot, definition def, Poll pol ) {
        Dirs.getErrorLog().WriteLog ("3004I", "onPublishClose " + pollid );
        //	Include this code to send an e-mail to the recipients named in
        //	the Spot.emailnotify field ( set in place settings ) .
        //	This is called twice; once with pollfile != null, one with resultsfile != null.
        emailNotification (spot, "Closed and Archived", spotname, pollid);

        //	When the poll is scheduled to Close,
        //	pollfile and resultsfile - similar handling to Open, either could be null
    }

    void onPublishExtra ( File resultsfile,
                          String spotname, String pollid,
                          Spot spot, definition def, Poll pol ) {
        if ( Dirs.bLogRequests() )
            Dirs.getErrorLog().WriteLog ("3003I", "onPublishExtra " + pollid );
    }

}