package com.cogix.vwf;

/*
	Sample Custom Action class to illustrate best practices
	Revised 12/2/02
	Requires ViewsFlash 3.5 or later
	
	See Extensibility in the ViewsFlash documentation:
	http://www.cogix.com/viewsflash/docs?Extensible.html

	Construct a poll with 5 text fields.
	Name them destination, source1, source2, returnvalue, action.
	Add a Custom Action question and use the following 
		to activate this class:
		
		testAction destination,source1,source2,returnvalue,action
		
		These five parameters are the names of the 5 questions
		defined in the test survey.
	
	Experiment with this class in both single page and multi-page surveys.
	
	This example class will compare the contents of source1 with source2.
	and use the comparison to set validity.
	
	It will use the returnvalue field to return true or false to the application
	Enter t or f.
	
	It will also call setAction() with the value of the action field.
	Enter a number from these values:
		ActionUseInvalidSettingsonSecurityPage = 0;
		ActionShowPage	= 1;		
		ActionRedirect	= 2;
		ActionAcceptWhatThereIs	= 3;
		ActionRefill = 4;
		AcceptAndRedirect = 5;

	It also uses setParameterValue to set destination to the
	concatentation of source1 and source2.
*/


class testAction  extends Validator {


	//	If necessary, add a throws clause.  If an exception is thrown,
	//	a message will be written to the poll log.
	//	It's best to not throw exceptions in a production environment.
	boolean onPageReceived (RequestParams reqx, String questionname, String spec) {

		//	Enable the following to write to the Poll log, for debugging:
		//	Dirs.getErrorLog().WriteLog ( pollid , "606I","At testAction # 1");
		//	Dirs.getErrorLog().WriteLog ( pollid , "606E","At testAction # 1");
		//	Messages ending in E are emailed to administrator immediately

				//	Perform integrity check on all parameters first.
		//	Return false to indicate that the check is indeterminate,
		//	i.e., ignored.
		String [] s = Misc.cogixParse (spec, true);
		if ( s == null || s.length < 4 ) {
			WriteLog ("301E","testAction didn't have enough parameters");
			return false;	//	do nothing
			}

		String destination = reqx.getParameter ( s [0] );
		String source1 = reqx.getParameter ( s [1] );
		String source2 = reqx.getParameter ( s [2] );
		boolean bSame = Misc.isSame ( source1, source2 );
		String retvalue = reqx.getParameter ( s [3] );
		String actionp = reqx.getParameter ( s [4] );
		boolean bReturn = ! ( ! Misc.isEmpty ( retvalue ) && retvalue.charAt (0) == 'f' );
		String setto = source1 + source2;

		//	Use this to set a value. Value can be saved, piped, and tabulated.
		setParameterValue (  destination, setto );

		//	Use this to indicate that the action has detected no problems (true)
		//	or not (false).
		//	If not called, the default is false !
		setValid ( bSame );

		//	Examples of other methods for certain cases.
		setMessage ("Custom Action 'testAction' finds data inValid.");
		setAction ( Integer.parseInt ( actionp ) ) ;	//	can throw NumberFormatException
		setRedir ( "http://www.yahoo.com");	//	easily visible
		switch ( action ) {
			case Validator.ActionRefill:
				setErrorTemplate (null);	//	null means use poll's Style Template
											//	can be used to use special Poll Style Template
				//	compose list of questions that need refilling,
				//	surrounded by blanks on both sides
				String missentries = " " + source1 + " " + source2 + " ";
				setMissingEntries (missentries);
				setMessage ("Correct marked fields ");
				break;
			case Validator.ActionShowPage:
				setErrorTemplate ("viewsflash/vfincomplete.html");
				break;
			default:;
			}
		//	A return of false means to ignore whatever this Action says to do.
		//	think of it as "Cancel".
		return bReturn;
		}
    }
