Versions Compared

Key

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

...

Note

In this example, the form data is to be sent to web service, hence the custom publisher class must be extended from CanvasReqModelWSPublisher class. If the form data is to be sent to database, the custom publisher class must be extended from CanvasReqModelDBPublisher. The default publisher class to send the form data to a database is CanvasReqModelDBPublisher, whereas the default publisher class to send the form data to a web service is CanvasReqModelWSPublisher. You can even set your own custom publisher class to send the data for both web services and database individually.   To process the response data that needs to be sent to the web service, you can override the prepareDataToBePublished method in the custom publisher class, whereas if you want to set the context for the response data before publishing the data to the web service, then you can override the publish method while specifying the custom publisher class.


Code Block
languagejava
/* This is a sample custom publisher Java class that inserts
the form data to the web service. */ 
import com.intellectdesign.canvas.config.ConfigurationManager;
import com.intellectdesign.canvas.config.SystemPreferenceDescriptor;
import com.intellectdesign.canvas.connectors.ICanvasConnector;
import com.intellectdesign.canvas.contextmanager.ExecutionContext;
import com.intellectdesign.canvas.logger.Logger;
import com.intellectdesign.canvas.model.StateModelException;
import com.intellectdesign.canvas.reqmodel.external.CanvasPublishContext;
import com.intellectdesign.canvas.reqmodel.external.CanvasReqModelWSPublisher;
import com.intellectdesign.canvas.utils.ResourceLoaderUtils;
import com.intellectdesign.canvas.utils.StringUtils;
import org.json.JSONObject;

import java.util.Map;

/* In this example, the form data is to be sent to web service, hence the 
custom publisher class must be extended from CanvasReqModelWSPublisher class. 

If the form data is to be sent to database, the custom publisher
class must be extended from CanvasReqModelDBPublisher. */

public class SampleWSPublisher extends CanvasReqModelWSPublisher {
    @Override
    public boolean publish(CanvasPublishContext publishContext) throws StateModelException {
        boolean status = false;
        try {
            ExecutionContext context = (ExecutionContext)publishContext.getData().get("EXECUTION_CONTEXT");
            Map datasourceData = context.getDataSourceData();
            Map detailObject = (Map)publishContext.getFormattedData();
            JSONObject processedData = new JSONObject(detailObject);
            // setting the prepared data in execution context
            context.setRequestData(processedData);
            context.setDataSource((String) datasourceData.get("DATA_SRC_ID"));
            SystemPreferenceDescriptor sysPrefDesc = ConfigurationManager.getInstance().getSystemPrefDescriptor();
            String canvasConnectorClass = sysPrefDesc.getCanvasConnectorClass();
            ICanvasConnector connector = null;
            if (!StringUtils.isEmpty(canvasConnectorClass))
            {
                connector = (ICanvasConnector) ResourceLoaderUtils.createInstance(canvasConnectorClass);
            }
            // fetching data using the connector
            context = connector.execute(context);
            JSONObject responseWSData = context.getResponseData();
            Object respObj = responseWSData.get("responseData");
            //This is a sample where the publisher sets any response from
            // the web service in the context and sends it to the client.
            //The context of the response data can be set here.
            //Any logic to process the response can also be done here.
            context.setResponseData((JSONObject)respObj);
            status = true;
        } catch (Exception e) {
            String debugInfo = publishContext.toString();
            StateModelException se = new StateModelException("CTREQM0221",
                    "Exception while publishing data for web service : " + debugInfo, e);
            logger.cterror("CTREQM0221", se);
            throw se;
        }
        return status;
    }
    private static Logger logger = Logger.getLogger(SampleWSPublisher.class);
}

...