Custom Publisher
The custom publisher (custom class) mechanism can be used for the following purposes:
- formatting or inserting data to the request before publishing
- taking full control of the publishing
Sample Custom Publisher for Inserting Data
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:
In this example, the form data is to be sent to database, hence the custom publisher class must be extended from CanvasReqModelDBPublisher class. If the form data is to be sent to Web Service, the custom publisher class must be extended from CanvasReqModelWSPublisher.
/* 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; //For Web Service connection, use CanvasReqModelWSPublisher, for example, //import com.intellectdesign.canvas.reqmodel.external.CanvasReqModelWSPublisher; /* In this example, the form data is to be sent to database, hence the custom publisher class must be extended from CanvasReqModelDBPublisher class. If the form data is to be sent to Web Service, the custom publisher class must be extended from CanvasReqModelWSPublisher. */ 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); } }
You must specify the custom publisher class in the Additional Params field in the request configuration. Request Modeler will then publish the data as per the custom publisher class.