Entering content frame

Procedure documentation Adding the Implementation for the Web Service Connection Locate the document in its SAP Library structure

Prerequisites

·         You have imported a model that is based on the e-mail Web service of a third party provider.

·         You have created the SendEmail action for the EmailFormView view.

·         You have constructed the view context for the EmailForm view and mapped it to the component controller context.

·         The structure of your project WebDynpro_EmailWS is currently displayed in the Web Dynpro Explorer.

 

Procedure

After you have executed all the declarative development steps, such as model binding, context mapping, and data binding, and before you determine the view layout, you now need to add individual lines of Java code in the source code of the view controller.

The data binding between UI elements and the view context, which itself is bound to the model, is based - at runtime - on the fact that there is a reference in a UI element property to the context attribute of an actual node element. In this current example, a node element of the same type as the appropriate, generated model class must be stored in the model node WebServiceEmail. That is, this kind of model node element must be bound to the model node WebServiceEmail. This binding takes place in the wdDoInit() method.

The actual Web service call must be implemented in the action event handler onActionSendEmail(), based on the data entered by the user that is stored in the context. In addition, the results returned by the Web service are to be stored in the context.

Implementing the Generic Event Handler wdDoInit() of the View Controller

At runtime, the context model node WebServiceEmail that is bound to the model must initially be filled with an instance of the appropriate model adapter class.  This model object, in turn, passes its data to the suitable Java proxy, which then communicates with the actual Web service.

       1.      In the View Designer, click on the Implementation tab for the EmailFormView view.

       2.      After the generation routines have been run once again, the updated source code of the view controller implementation is displayed.

       3.      Now add the following Java code into the User Coding Area  provided.

//@@begin javadoc:wdDoInit()

/** Hook method called to initialize controller. */

//@@end

public void wdDoInit()

{

  //@@begin wdDoInit()

  // create a new instance of the Web Service ModelClass

  Request_IEmailService_sendMail req = new Request_IEmailService_sendMail();

 

  // bind new instance of the Web Service ModelClass to the

  // independent Model Node ¢WebServiceEmail¢

  wdContext.nodeWebServiceEmail().bind(req);

  //@@end

} 

To enter the source code, you can use the Code Assist functions provided by the SAP NetWeaver Developer Studio by selecting the keyboard combination CTRL+SPACE.

This graphic is explained in the accompanying text

       4.      You can add the missing import statement by choosing Source ® Organize Imports from the context menu:

...//@@begin imports

import com.sap.tc.webdynpro.tutorial.emailws

          .model.proxies.Request_IEmailService_sendMail;

import com.sap.tc.webdynpro.tutorials.emailws.wdp.IPrivateEmailFormView;

//@@end

 

Implementing the action event handler onActionSendEmail

The actual Web service is now called using the execute() method of the model object currently stored in the context model node. This already contains the reservation data entered by the user (through data binding and context mapping). The data stored in the component controller context is a copy of the data stored in the model, that is, the one does not directly reference the other. Therefore, the view context bound through context mapping also does not yet contain the returned results of the Web service call executed previously and stored in the model.

As an application developer, you therefore need to explicitly invalidate the model node response (this is contained in the context as an inner node underneath the node WebServiceEmail). The response data most recently stored in the model is then transmitted to the corresponding context node element.  

The returned result (in the example application this is just a single integer value) is then displayed in an appropriate message text in the message bar of the Web Dynpro application.

...

       1.      In the onActionSendEmail() method, add the following source code:

//@@begin javadoc:onActionSendMail(ServerEvent)

/** Declared validating event handler. */

//@@end

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

{

  //@@begin onActionSendEmail(ServerEvent)

  IWDMessageManager msgMgr=

    wdThis.wdGetAPI().getComponent().getMessageManager();

  

  try {

    // call Email Web Service and update dependent model node ¢Response¢

    wdContext.currentWebServiceEmailElement().modelObject().execute();

    wdContext.nodeResponse().invalidate();

         

    int result = wdContext.currentResponseElement().getResult(); 

    String msg = "Email Web Service returned " + Integer.toString(result);

     

    if (result == 0) {

      msgMgr.reportSuccess("Your email was successfully sent ("

        + msg + ")!");   

    } else {

      msgMgr.reportWarning("Your email was not successfully sent ("

        + msg + ")!");   

    }

  } catch(Exception ex) {

    msgMgr.reportException(ex.getLocalizedMessage(),true); 

  } 

  //@@end

} 

This graphic is explained in the accompanying text

To use the generic UI service provided by the interface IWDMessageManager for displaying message texts in the user interface, you must insert an appropriate import line in the view controller implementation. To do this, choose the entry Source  ®  Organize Imports  from the context menu of the source code editor (Implementation tab).

After the import statements have been adjusted, the IWDMessageManager interface is imported into the view controller.

...

//@@begin imports

import com.sap.tc.webdynpro.progmodel.api.IWDMessageManager;

import com.sap.tc.webdynpro.tutorials.emailws.model.proxies

              .Request_IEmailService_sendMail;

import com.sap.tc.webdynpro.tutorials.emailws.wdp.IPrivateEmailForm;

//@@end

} 

...

 

Result

The Developer Studio updates and compiles the Java classes belonging to your project. (Note: Compilation only occurs if you are using the default Workbench settings.) After you have done this, no more error messages should appear in your Tasks view.

 

Next step:

Building, Deploying, and Running Your Application

 

Leaving content frame