Adding the Implementation for the Web Service Connection

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 turn to the implementation of methods in the view- and component controller.

Controller of the Component EmailWSComponent

The component-controller implements the access to the used email Web-Service model.  The context of the controller contains model-nodes and model-attributes, which are bound to the imported Web-Service model. Alongside the initialization method wdDoInit(), you implement a public method named sendEmail() to allow another controller to call the email Web-Service.

Controller of the EmailFormView view:

In the implementation of the view-controller, you have to implement the event handler onActionSendEmail(), which , at pressing the pushbutton to send an email message (Action SendMail), is called and displayed on the user interface.

Prerequisites

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

·        You have created the SendEmailaction for the EmailFormView view.

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

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

Procedure

Implementing the Method  wdDoInit() of the Component Controller

At runtime, the context model node WebServiceEmailthat is bound to the model must initially be filled with an instance of the appropriate model adapter class (Request_SendEmailPortType_sendEmail). After that, this model object can be used in the public method sendEmail()to execute the Web-Service call.

                            a.      Select the node Web Dynpro а  Web Dynpor Components аEmailWSComponent аComponent Controller and then click on the tab page Implementation.

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

                            b.      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_SendEmailPortType_sendEmail req =

    new Request_SendEmailPortType_sendEmail();

 

  // 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 avail of the Code Assist functions provided by the SAP NetWeaver Developer Studio by selecting the keyboard combination CTRL+SPACE.

                            c.      You can add the missing importstatement automatically by choosing Source ®Organize Imports from the context menu or by selecting the keyboard combination STRG+SHIFT+O:

...//@@begin imports

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

          .model.Request_SendEmailPortType_sendEmail;

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

//@@end

 

Implementing the public method sendEmail()in the Component Controller

The actual Web-Service call for sending the email message is implemented in the public method sendEmail()in the component controller. Thus, the communication with the email Web-Service in the component controller is encapsulated.

The connection between the view- and component context was already defined beforehand via the definition of the context-mapping. Thereby, the view-context points at the context data stored in the component-context. As the component controller is used for context-mapping, its IPublic API (contains public methods) is also accessible in the view-controller.

In the Component Controller define the new public method sendEmail().

                            d.      In the Component Controller switch to the Methods tab page.

                            e.      Choose New to add a new public controller method.

                              f.      In the first dialog step, select method type Method.

                            g.      Enter the name sendEmailfor the method, thereby keeping the type of the return value, void.

                            h.      Close this declaration step by choosing Finish.

Use the Implementation tab page to implement the newly defined method.

                              i.      In the public method sendEmail(), add the following source code: You find more detailed background information about the methods execute()and invalidate()in section Calling the execute method.

//@@begin javadoc:onActionSendMail(ServerEvent)

/** Declared validating event handler. */

//@@end

public void sendEmail() {

  //@@begin onActionSendEmail(ServerEvent)

  IWDMessageManager msgMgr = wdComponentAPI.getMessageManager();

  

  try {

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

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

    wdContext.nodeResponse().invalidate();

         

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

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

     

    if (result.intValue() == 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

} 

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 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 IWDMessageManagerinterface is imported into the component controller.

...

//@@begin imports

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

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

  .Request_SendEmailPortType_sendEmail;

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

//@@end

...

 

Implementing the Action Event Handler onActionSendEmail()

After you implemented the component controller, you only have to call the public method sendEmail() of the component controller in the action event handler onActionSendEmail() of the view controller to trigger the email Web-Service call.

...

                            a.      After selecting the node Web Dynpro а  Web Dynpor Components аEmailWSComponent аViews аEmailFormView, change to the tab page Implementation to display the implementation of the view controller.

                            b.      In the onActionSendEmail() method, enter 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)

  wdThis.wdGetEmailWSComponentController().sendEmail();

  //@@end

} 

 

Result

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

 

Next Step:

Building, Deploying, and Running the Project