Show TOC Start of Content Area

Procedure documentation Implementing a Web Dynpro Component  Locate the document in its SAP Library structure

Use

To create a Web Dynpro component that can be exposed as a callable object in Guided Procedures (GP), you must implement the IGPWebDynproCO interface. It enables the execution of the component in the context of a GP process.

Note

For more information about the interfaces used for the implementation, see the API documentation at http://www.sdn.sap.com/irj/sdn/javadocs.

Prerequisites

You must have added all required dependencies.

For more information, see Creating and Configuring the Web Dynpro Project.

Procedure

1. Create a Web Dynpro Component

...

       1.      Open the Web Dynpro perspective, expand the Web Dynpro project that you have created and select Components.

       2.      Open the context menu and choose Create Component.

Enter a name for the component – for example, CUserDetails. Enter a package name – for example, com.sap.examples.wdpco.

Deselect the option Default Window and Views, select Implemented Interfaces and choose Next.

       3.      To specify the interfaces you are going to use, choose Add.

       4.      Select IGPWebDynproCO – com.sap.caf.eu.gp.co.webdynpro.

Confirm your choice with Finish.

2. Edit the Component Controller Implementation

...

       1.      Open the component controller by choosing Open Controller Editor from its context menu.

       2.      In the Context tab page, define attributes for the callable object parameters.

For this example, you define the following attributes:

       fax

       firstName

       lastName

       phone

       userId

       3.      In the Methods tab page, declare a new method complete.

Methods execute and getDescription are inherited from interface IGPWebDynproCO.

       4.      Open the implementation of the component controller by choosing Open Java Editor from its context menu and make the following modifications:

                            a.      Add a private variable for the callable object execution context. You need the execution context to retrieve runtime data, such as parameter values, and so on.

Example

//@@begin others

   private IGPExecutionContext executionContext;

   private IWDTextAccessor textAccessor;

   private GPWebDynproResourceAccessor resourceAccessor;

//@@end

                            b.      Method getDescription()

This method returns the technical description of the callable object that contains metadata for the object’s input, output, and configuration parameters, as well as for its result states.

Using the coding example below, you:

§         Define the input parameter userId as an attribute of the pre-existing root structure.

§         Define a sub-structure user for the output parameters, and add attributes to it.

§         Create a result state Success.

§         Implement invocation exception handling in the catch clause of the try-catch block.

Note

Invocation exception occurs if the technical names contain forbidden characters (only numbers, Latin letters, and underscore are allowed).

In the example, the method returns null if there is an error.

To log the error message, you use an instance of com.sap.tc.logging.Location, which is automatically available in Web Dynpro.

Example

public com.sap.caf.eu.gp.co.api.IGPTechnicalDescription getDescription(java.util.Locale locale)  {

    //@@begin getDescription()

    try {

         IGPTechnicalDescription technicalDescription =

            GPCallableObjectFactory.createTechnicalDescription(

               "CO_NAME",

               "CO_DESCRIPTION",

               resourceAccessor,

               locale);

 

         // Pre-existing input structure

         IGPStructureInfo input =

            technicalDescription.getInputStructureInfo();

         IGPAttributeInfo userId =

            input.addAttribute("UserID", IGPAttributeInfo.BASE_STRING);

         userId.setMultiplicity(IGPAttributeInfo.MULITIPLICITY_1_1);

 

         //Pre-existing structure for output parameters

         IGPStructureInfo output =

            technicalDescription.getOutputStructureInfo();

 

         //Create the attributes in the output structure

         IGPStructureInfo userStructure = output.addStructure("User");

         userStructure.addAttribute(

            "firstName",

            IGPAttributeInfo.BASE_STRING);

         userStructure.addAttribute(

            "lastName",

            IGPAttributeInfo.BASE_STRING);

         userStructure.addAttribute("phone", IGPAttributeInfo.BASE_STRING);

         userStructure.addAttribute("fax", IGPAttributeInfo.BASE_STRING);

 

         //add result state

         IGPCOResultStateInfo success =

            technicalDescription.addResultState("Success");

         success.setDescriptionKey("Success_desc");  

 

         return technicalDescription;

 

      } catch (GPInvocationException e) {

         logger.logT(

            Severity.ERROR,

            Category.APPLICATIONS,

            "Incorrect technical name");

         logger.traceThrowableT(

            Severity.ERROR,

            "Exception while creating technical description: ",

            e);

         return null;

      }

    //@@end

  }

                            c.      Method  execute()

In this method, you can implement any functions that the callable object should execute. For example, you can process the input parameters, if any, interact with the system, and so on.

To retrieve the object parameters, you use the execution context.

In the example below, the method parses the input parameter (user ID), and searches user management for the relevant user. Then it retrieves the user details, such as first and last name, phone and fax numbers. The values are then assigned to the relevant context attributes.

If there is an invocation or UME error, the method logs a technical exception.

Example

public void execute( com.sap.caf.eu.gp.co.api.IGPExecutionContext executionContext )

  {

    //@@begin execute()

      String userId = null;

      try {

         this.executionContext = executionContext;

 

         //Process the input parameters  

         IGPStructure input = executionContext.getInputStructure();

         userId = (String) input.getAttributeAsString("UserID");

         wdContext.currentContextElement().setUserId(userId); 

 

         //Retrieve user data

         IUserFactory factory = UMFactory.getUserFactory();

         IUser user = factory.getUserByLogonID(userId);

         String lastName = user.getLastName();

         String firstName = user.getFirstName();

         String phone = user.getTelephone();

         String fax = user.getFax();

 

         //Set the new values in the context

         IGPStructure output = executionContext.getOutputStructure();

         wdContext.currentContextElement().setAttributeValue(

            "firstName",

            firstName);

         wdContext.currentContextElement().setAttributeValue(

            "lastName",

            lastName);

         wdContext.currentContextElement().setAttributeValue("phone", phone);

         wdContext.currentContextElement().setAttributeValue("fax", fax);

      } catch (GPInvocationException e) {

         String localizedMessage = textAccessor.getText("ERROR_GETTING_PARAMETERS");

         wdThis.wdFireEventTechnicalException(

            new GPTechnicalCallableObjectException(

               logger,

               localizedMessage,

               e));

      } catch (UMException ex) {

         String localizedMessage = textAccessor.getText("USER_NOT_FOUND", new Object[]{userId});

         wdThis.wdFireEventTechnicalException(

            new GPTechnicalCallableObjectException(

               logger,

               localizedMessage,

               ex));

      }

    //@@end

  }

                            d.      Method complete()

In this method you complete the execution of the callable object. For example, you can set the result state and the output parameters.

Finally, you must call the execution context method processingComplete() to indicate that the callable object has been executed.

Technical exception is used to process the errors that might occur.

Example

public void complete( )

  {

    //@@begin complete()

      try {

         IGPStructure output = executionContext.getOutputStructure();

 

         //Set the values of the output parameters

        

         IGPStructure user = output.addStructure("User");           

         user.setAttributeValue("firstName", wdContext.currentContextElement().getFirstName());

         user.setAttributeValue("lastName", wdContext.currentContextElement().getLastName());

         user.setAttributeValue("phone", wdContext.currentContextElement().getPhone());

         user.setAttributeValue( "fax", wdContext.currentContextElement().getFax());

        

         executionContext.setResultState("Success");

         executionContext.processingComplete();

 

      } catch (GPInvocationException e) {

         String localizedMessage = textAccessor.getText("ERROR_SETTING_PARAMETERS");

         wdThis.wdFireEventTechnicalException(

            new GPTechnicalCallableObjectException(

               logger,

               localizedMessage,

               e));

      } catch (GPEngineException e) {

         String localizedMessage = textAccessor.getText("INTERNAL_ERROR");

         wdThis.wdFireEventTechnicalException(

            new GPTechnicalCallableObjectException(

               logger,

               localizedMessage,

               e));

      }

    //@@end

  }

                            e.      Implement localization

The technical name of the callable object, as well as the technical names of its parameters and result states, is also available as keys that you can add to the message pool.

You can define additional keys to localize error messages, as well as result state descriptions, as shown in the coding examples above.

You add message text for the message keys manually in the message pool.

For more information, see Message Editor.

To access the texts from the message pool, you need an instance of GPWebDynproResourceAccessor.

Example

public void wdDoInit()

  {

    //@@begin wdDoInit()

    IWDTextAccessor textAccessor = wdComponentAPI.getTextAccessor();

    GPWebDynproResourceAccessor resourceAccessor = new GPWebDynproResourceAccessor(textAccessor);

    //@@end

  }

3. Create the User Interface

...

       1.      Expand the node CUserDetails, select Views, open the context menu and choose Create View.

       2.      Specify a name for the view – for example, VuserDetails. Choose Finish.

       3.      Open view VUserDetails, and make the following modifications:

                            a.      In the Properties tab page, choose Add to define a new required controller.

Select CUserDetails – com.sap.examples.wdpco.

                            b.      In the Context tab page, define attributes for the output parameters that you want to display. Map them to the relevant attributes in the interface controller context.

                            c.      In the Actions tab page, define a new action Complete. Choose to use the default event handler.

                            d.      In the implementation of the view, navigate to method onActionComplete(). Implement it to call method complete() of the component controller, as shown in the example below:

Example

public void onActionComplete(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )

  {

    //@@begin onActionComplete(ServerEvent)

    wdThis.wdGetCUserDetailsController().complete();

    //@@end

  }

                            e.      In the Layout tab page, create the view design.

Define an input field for each attribute that you want to display, and map its value to the relevant context attribute.

Create a button, and assign value Complete to its onAction property.

       4.      In your project tree, expand the Windows node, and select the CUserDetails window.

       5.      From its context menu, choose Open Window Editor.

       6.      Choose Embed Existing View, click in the window area and select the VUserDetails view.

       7.      Confirm by choosing Finish.

4. Build and Deploy the Component

...

       1.      Select your Web Dynpro project, and open its context menu.

       2.      Choose Development Component Build and confirm your choice.

Check for errors and warnings in view Infrastructure Console view.

       3.      In the context menu of the project, choose Deploy.

If you have not created an archive for your DC, you will be prompted to do so. Select Make consistent first (Create archive) and choose OK.

       4.      Check if deployment is successful in Deploy View Console.

Result

You have implemented and deployed a Web Dynpro component that can be executed within a GP process.

Your next step is to expose the component as a callable object in GP design time.

More information: Creating an Instance of the Callable Object

End of Content Area