
The Java class that you want to use as a callable object for background execution must implement the interface com.sap.caf.eu.gp.co.api.IBackgroundCallableObject . It enables the execution of the class within the Guided Procedures (GP) framework.
For more information about the interfaces used for the implementation, see the API documentation at http://www.sdn.sap.com/irj/sdn/javadocs .
The most important aspects of the implementation include:
To be able to create the required imports in the Java class source code, you must have configured your Java development component.
More information: Creating a Java Development Component .
Create the Java Class
Edit Method getDescription()
This method returns the technical description of the callable object, which contains metadata about the object's input, output, and configuration parameters, as well as its result states.
To define the callable object:
In this example, you define a single input parameter for the user ID in the root structure. For the output parameters, you create a structure where you define the attributes for the user details that you retrieve - first and last name, telephone and fax numbers (see the coding example below).
The technical names that you use for the parameters of the callable object may only contain numbers, letters from the Latin alphabet, and underscore.
publicIGPTechnicalDescription getDescription(Locale originalLocale) { try {//create technical description instance IGPTechnicalDescription technicalDescription = GPCallableObjectFactory.newTechnicalDescription( "CO_NAME", "CO_DESCRIPTION", resourceAccessor, originalLocale); //get root structure for input parameters IGPStructureInfo input = technicalDescription.getInputStructureInfo(); //define attributes for input parameters IGPAttributeInfouserId = input.addAttribute("UserID", IGPAttributeInfo.BASE_STRING);userId.setMultiplicity(IGPAttributeInfo.MULITIPLICITY_1_1); //get root structure for output parameters IGPStructureInfo output = technicalDescription.getOutputStructureInfo(); //add a sub-structure for the output IGPStructureInfo user = output.addStructure("User");//define attributes in output sub-structure user.addAttribute("firstName", IGPAttributeInfo.BASE_STRING); user.addAttribute("lastName", IGPAttributeInfo.BASE_STRING); user.addAttribute("phone", IGPAttributeInfo.BASE_STRING); user.addAttribute("fax", IGPAttributeInfo.BASE_STRING);} ... } |
In the example below, a result state is defined that indicates successful execution.
publicIGPTechnicalDescription getDescription(Locale originalLocale) { try {... //add a result state for successful execution IGPCOResultStateInfosuccess = technicalDescription.addResultState("Success");//add result state description success.setDescriptionKey("Success_desc");return technicalDescription; } ... } |
Edit Method execute()
In this method you implement the runtime behavior of the callable object.
publicvoid execute(IGPExecutionContext executionContext) throwsGPTechnicalCallableObjectException{ try {//retrieve the runtime representation of the input structure IGPStructure input = executionContext.getInputStructure(); //retrieve the value of the input parameter String userId = (String) input.getAttributeAsString("UserID");//retrieve the runtime representation of the output structure IGPStructure output = executionContext.getOutputStructure(); IGPStructure user = output.addStructure("User"); try{//find user in user management IUser umeUser = UMFactory.getUserFactory().getUserByLogonID(userId); //set values to output parameters user.setAttributeValue("firstName", umeUser.getFirstName()); user.setAttributeValue("lastName", umeUser.getLastName()); user.setAttributeValue("phone",umeUser.getTelephone()); user.setAttributeValue("fax", umeUser.getFax());//set result state executionContext.setResultState("Success");}... //complete object execution executionContext.processingComplete(); } } |
publicvoid execute(IGPExecutionContext executionContext) throwsGPTechnicalCallableObjectException{ try {... try{... //set result state executionContext.setResultState("Success");... }... } } |
publicvoid execute(IGPExecutionContext executionContext) throwsGPTechnicalCallableObjectException{ try {... //complete object execution executionContext.processingComplete(); } } |
Implement Process Exception Handling
publicIGPTechnicalDescription getDescription(Locale originalLocale) { try {... //add a process exception for the callable object technicalDescription.addProcessException("EXCEPTION_NO_USER_FOUND");returntechnicalDescription; } ... } |
publicvoid execute(IGPExecutionContext executionContext) throwsGPTechnicalCallableObjectException{String userId = null; try {... try{... } catch (UMException e) { executionContext.setProcessException("EXCEPTION_NO_USER_FOUND");} executionContext.processingComplete(); } ... } |
Implement Technical Exception Handling
This exception is thrown if the technical names contain forbidden characters. Therefore, it should not occur unless the rules for the names are violated.
publicIGPTechnicalDescription getDescription(Locale originalLocale) { try {... } catch (GPInvocationException e) {logger.logT( Severity.ERROR, Category.APPLICATIONS, "Incorrect technical name"); logger.traceThrowableT( Severity.ERROR, "Exception while creating technical description: ", e); return null; } } |
public void execute(IGPExecutionContext executionContext) throwsGPTechnicalCallableObjectException{String userId = null; try {... } catch (GPInvocationException e) {throw newGPTechnicalCallableObjectException(logger,resourceAccessor, "ERROR_PARAMETERS", e); } catch (GPEngineException e) {throw newGPTechnicalCallableObjectException(logger,resourceAccessor, "INTERNAL_ERROR", e); } } } |
Implement Localization
The names that you have defined for the callable object parameters can be automatically used as keys for localized messages that are displayed at runtime. You can also set additional keys, as shown in the example for defining a result state description above.
To implement localization:
publicclass UserDetailsResourceAccessor extendsGPStandardResourceAccessor{/** * @param bundleName */ public UserDetailsResourceAccessor(String bundleName) {super(bundleName); } } |
CO_NAME=User Details CO_DESCRIPTION=User Details Background Callable Object UserID=User ID User=User firstName=First Name lastName=Last Name fax=Fax phone=Telephone Success=Successful execution Success_desc=The execution completed successfully ERROR_PARAMETERS=Error while setting/getting parameters INTERNAL_ERROR=Internal error USER_NOT_FOUND=User with ID {0} not found |
publicclass UserDetailsCallableObject implements IGPBackgroundCallableObject {GPStandardResourceAccessor resourceAccessor = new UserDetailsResourceAccessor("com.examples.bckgco.UserDetails");... } |
Save the files, and build your project. You have implemented a Java class that can be exposed as a callable object in GP.
Now you must create a Java Enterprise Application and deploy it to AS Java.
More Information:
UserDetailsCallableObject - the complete source code of sample class.