Starting a Business Service — Service Connector API Example (Java)

An example is provided that illustrates starting a business service using the method calls available in the Service Connector API.

The step numbers in the comments correspond to the steps in the illustration Starting a Business Service.

private void startBusinessServiceExample()
	{
		try
		{
			// Get all the business service categories that are available
			ListCategoriesResponse categories = serviceConnectorInstance.getBusinessService().listCategories(					Long.valueOf(0), Long.valueOf(0), true, null, true, null);
			// Step 1: For this example we will pick the first category
			if (categories.getCategoryArray() != null && categories.getCategoryArray().length > 0)
			{
				// Query the business services for a particular category ( and all child categories by adding "/**" )
				QueryBusinessServicesResponse response = serviceConnectorInstance.getBusinessService()
						.queryBusinessServices(categories.getCategoryArray(0).getName() + "/**", null, true);
				if (response.getBusinessServiceTemplateArray() != null
						&& response.getBusinessServiceTemplateArray().length > 0)
				{
					// Get the first business service for the category
					BusinessServiceTemplate template = response.getBusinessServiceTemplateArray(0);
					// Step 2: Start the first business service
					StartBusinessServiceResponse startResponse = serviceConnectorInstance.getBusinessService()
							.startBusinessService(template, null, PayloadModeType.XML, Long.valueOf(0));
					// Now we would use the forms API to open the Business Service and allow users to enter data
					// After form processing is complete update the business service with the new data
					// Build up a request context for the update call
					RequestContext requestContext = RequestContext.Factory.newInstance();
					// We need a Process Reference for the context
					ProcessReference ref = ProcessReference.Factory.newInstance();
					ref.setId(startResponse.getPageResponse().getContext().getProcessReference().getId());
					// We also need an Activity Reference for the context
					// For this example we will update the first activity in the business service
					ActivityReference actRef = ActivityReference.Factory.newInstance();
					actRef.setActivityId(startResponse.getPageResponse().getPageDataArray(0).getPageReference()
							.getActivityId());
					actRef.setActivityModelId(startResponse.getPageResponse().getPageDataArray(0).getPageReference()
							.getActivityModelId());
					actRef.setActivityName(startResponse.getPageResponse().getPageDataArray(0).getPageReference()
							.getActivityName());
					actRef.setModuleName(startResponse.getPageResponse().getPageDataArray(0).getPageReference()
							.getModuleName());
					actRef.setModuleVersion(startResponse.getPageResponse().getPageDataArray(0).getPageReference()
							.getModuleVersion());
					actRef.setProcessName(startResponse.getPageResponse().getPageDataArray(0).getPageReference()
							.getProcessName());
					// update the request context with the Process Reference and Activity Reference
					requestContext.setProcessReference(ref);
					requestContext.setActivityReference(actRef);
					// setup the data payload with the new business service data, normally we would get this back when the form is closed or submitted
					DataPayload payload = DataPayload.Factory.newInstance();
					payload.setPayloadMode(PayloadModeType.JSON);
					payload.setSerializedPayload("");
					// Step 3: Update the business service
					UpdateBusinessServiceResponse updateResponse = this.serviceConnectorInstance.getBusinessService()
							.updateBusinessService(requestContext, null, PayloadModeType.XML);
					print(updateResponse);
				}
			}
		}
		catch (com.tibco.n2.busserv.services.InternalServiceFault e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (com.tibco.n2.busserv.services.InvalidArgumentFault e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (com.tibco.n2.busserv.services.PageFlowExecutionFault e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}