Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The custom publisher (custom class) mechanism can be used for the following purposes:

...

Consider a scenario where you need to pass data that is not captured on the form or data is not provided by the customer when filling the form. Let's take the earlier example provided for database request where Credit Card details such as Card Number, Current Balance, Currency, Card Type and Minimum Payment are to be entered. Let's assume that data for Currency is not provided by the customer. However, you would like to pass the value for Currency in the request and publish the updated value to the database. You can achieve this using the custom publisher class as shown in the code sample:

Code Block
languagejava
/* This is a sample custom publisher Java class that inserts a value for Currency column, which is not entered on the form by the customer. */

...

 
import java.util.ArrayList;

...


import java.util.HashMap;

...


import com.intellectdesign.canvas.model.StateModelException;

...


import com.intellectdesign.canvas.reqmodel.external.CanvasPublishContext;

...


import com.intellectdesign.canvas.reqmodel.external.CanvasReqModelDBPublisher;

...

 


/* In this example, the form data is to be sent to database, hence the

...

 
custom publisher class must be extended from CanvasReqModelDBPublisher class. */

...

 
public class CustomPublisher extends CanvasReqModelDBPublisher

...

 
{ 
	/* The prepareDataToBePublished method prepares the data to be sent

...

 
	to the database and can be used for any data manipulation or

...

 
	formatting needs. */

...


	@Override

...


	public void prepareDataToBePublished(CanvasPublishContext publishContext) throws StateModelException

...


	{
		super.prepareDataToBePublished(publishContext);

...

 
		// Form data is stored in an array list.

...


		ArrayList data = (ArrayList) publishContext.getFormattedData();

...

 
		/* Two array list variables are used to store field IDs

...

 
		and field values separately. */

...


		ArrayList columnList, valueList;

...


		columnList =(ArrayList) ((HashMap)data.get(0)).get("COLUMN_IDS");

...


		valueList =(ArrayList) ((HashMap)data.get(0)).get("VALUES");

...

 
		/* The getUserData method fetches the user details such as

...

 
		GCIF, user ID, first/last name etc. */

...


		publishContext.getUserData();

...

 
		/* The index number of the Currency field in the

...

 
		array list is stored in a variable. */

...


		int ind = columnList.indexOf("CURRENCY");

...

 
		/* The current value of the field is removed and

...

 
		new value is assigned to it. */

...


		valueList.remove(ind);

...


		valueList.add(ind,"USD");

...

 
		/* The setFormattedData method sets the data

...

 
		in the CanvasPublishContext object. */

...


		publishContext.setFormattedData(data);

...


	} 
	/* The publish method publishes the formatted data

...

 
	in the CanvasPublishContext object to the database. */

...


	@Override

...


	public boolean publish(CanvasPublishContext publishContext) throws StateModelException

...

 
	{
		return super.publish(publishContext);

...


	}

...


}