Entering content frame

Procedure documentation Implementing the Controller of the Form View

Use

The implementation of the Form view controller is divided up into the following procedures:

·        First of all, additional methods are declared for the input check and for the initialization of value attributes.

·        The input check for the input fields Name, Birthday, and E-Mail Address is started in the event handler for the Save action using the appropriate method calls.

·        The user should have the option of resetting all input fields by selecting Clear Errors, even if they contain incorrect entries.

·        Navigation to the EMailEditor view should only be possible when the user has entered an e-mail address.

The IWDMessageManager interface is used in different controller methods to display messages.

Procedure

Before you can actually start implementing the controller, you must declare four additional methods in the Methods tab of the Form view.

Method Declarations

...

       1.      Declare the four methods using the following table:

Message Defined in the Message Editor

Method Name

Type of the Return Value

Parameter Name

Parameter Type

checkDateInPast

void

fieldName

string

checkDesired

void

fieldName

string

checkMandatory

void

fieldName

string

initialize

void

 

 

 

Method Implementation

The methods checkDateInPast(), checkDesired(), and checkMandatory() are called by the event handler of the onActionSave() action. They contain the actual input checks.

The error message is stored internally by a source code line of the following type:

messageMgr.reportContextAttributeMessage(

  this.wdContext.currentContextElement(),

  attributeInfo,

  IMessageSimpleErrors.<MessageKey>,

  Object[] arguments,

  true);

 

By calling the report methods of the IWDMessageManagerinterface, it is possible to use the temporary storage of the Web Dynpro runtime environment for multiple messages and display them together in the Web browser after completing a request/response cycle.

If the message text contains arguments, these are passed in an object array (see java.text.MessageFormat).

       2.      Implement the methods checkDateInPast(), checkDesired(), and checkMandatory()using the following source code. Note that you can use the code source block between //@beginsapurl_link_0010_0001_0003 imports and //@endsapurl_link_0010_0002_0003 to import the required Java classes.

 

Form.java

...

//@@begin imports

import java.sql.Date;

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

import com.sap.tc.webdynpro.tutorial.errorbehavior.wdp.IMessageSimpleErrors;

import com.sap.tc.webdynpro.tutorial.errorbehavior.wdp.IPrivateForm;

//@@end

...

/** declared method */

public void checkDesired(java.lang.String fieldName) {

  //@@begin checkDesired()

  IWDMessageManager messageMgr =

    wdComponentAPI.getMessageManager();

  Object attributeValue =

    this.wdContext.currentContextElement().getAttributeValue(fieldName);

  IWDAttributeInfo attributeInfo =

    this.wdContext.getNodeInfo().getAttribute(fieldName);

  if (attributeValue instanceof String) {

    if (((String) attributeValue).length() == 0) {

      if (fieldName.equals("EMailAddress"))

        messageMgr.reportContextAttributeMessage(

          this.wdContext.currentContextElement(),

          attributeInfo,

          IMessageSimpleErrors.DESIRED_E_MAIL,

          null,

          true);

    }

  }

  //@@end

}

 

/** declared method */

public void checkMandatory(java.lang.String fieldName) {

  //@@begin checkMandatory()

  IWDMessageManager messageMgr =

    this.wdComponentAPI.getMessageManager();

  Object attributeValue =

    this.wdContext.currentContextElement().getAttributeValue(fieldName);

  IWDAttributeInfo attributeInfo =

    this.wdContext.getNodeInfo().getAttribute(fieldName);

  if (attributeValue instanceof String) {

    if (((String) attributeValue).length() == 0) {

      String fieldLabel =

          this.wdContext.getNodeInfo().getAttribute(fieldName)

              .getSimpleType().getFieldLabel();

      messageMgr.reportContextAttributeMessage(

        this.wdContext.currentContextElement(),

        attributeInfo,

        IMessageSimpleErrors.MISSING_INPUT,

        new Object[] { fieldLabel },

        true);

    }

  }

  //@@end

}

 

/** declared method */

public void checkDateInPast(java.lang.String fieldName) {

  //@@begin checkDateInPast()

  IWDMessageManager msgMgr =

    this.wdComponentAPI.getMessageManager();

  Date theDate = (Date)

    this.wdContext.currentContextElement().getAttributeValue(fieldName);

  IWDAttributeInfo attributeInfo =

    this.wdContext.getNodeInfo().getAttribute(fieldName);

  

  if (theDate.after(new Date(System.currentTimeMillis()))) {

    String fieldLabel =

      this.wdContext.getNodeInfo().getAttribute(fieldName)

          .getSimpleType().getFieldLabel();

    msgMgr.reportContextAttributeMessage(

      this.wdContext.currentContextElement(),

      attributeInfo,

      IMessageSimpleErrors.DATE_IS_IN_FUTURE,

      new Object[] { fieldLabel, theDate },

      true);

  }

  //@@end

}

...

Initialization

The previously declared initialize() method is used to initialize the value attributes in the controller context and the data type information field label that belongs to the EMailAddress value attribute. The method is called from the wdDoInit() method and from the non-validating action event handler onActionClear() directly after instantiating the view controller.

Form.java

...

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

public void wdDoInit() {

  //@@begin wdDoInit()

  this.initialize();

  //@@end

}

...

/** declared method */

public void initialize() {

  //@@begin initialize()

  wdContext.currentContextElement().setName("");

  wdContext.currentContextElement().setBirthday(new Date(0));

  wdContext.currentContextElement().setEMailAddress("");

  wdContext.getNodeInfo().getAttribute("EMailAddress")

           .getModifiableSimpleType().setFieldLabel("E-Mail Address");

  //@@end

}

...

 

Event Handling

Three different input checks are run in the onActionsSave() event handler using the previously implemented methods checkDateInPast(), checkDesired(), and checkMandatory(). Any error messages that may be contained in the event handler are first stored internally and displayed by calling the interface method raisePendingException()of the IWDMessageManager interface and the subsequent source code is not executed.

IWDMessageManager.java

...

/**

 * This method checks if any exceptions were reported to the exception manager and

 * are still stored in the exceptions manager. In case there is at least one

 * exception still stored, this method does not return, but raises some framework

 * internal exception instead in order to return to the framework error handler.

 */

public void raisePendingException();

...

The raisePendingException() method triggers an internal framework exception error if at least one error was reported to the message manager. Only in this case does the method not return to the caller (in this example: the view controller), and - as intended - the following source code line is not executed.

wdThis.wdGetAPI().getComponent().getMessageManager().reportSuccess(

  "The sample form data was successfully saved!");

 

 

       3.      Now implement the method onActionSave(IWDCustomEvent wdEvent):

Form.java

...

/** declared validating event handler */

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

{

  //@@begin onActionSave(ServerEvent)

  this.checkMandatory("Name");

  this.checkDateInPast("Birthday");

  this.checkDesired("EMailAddress");

  wdComponentAPI.getMessageManager().raisePendingException();

  wdComponentAPI.getMessageManager().reportSuccess(

    "The sample form data was successfully saved!");

  //@@end

}

...

 

Navigation to the input of an e-mail message in the EMailEditor view is only started in the Web Dynpro runtime environment if no error is reported in the checkMandatory() method.

       4.      The implementation of the onActionSendEMail(IWDCustomEvent wdEvent) method contains two lines of source code:

Form.java

...

/** declared validating event handler */

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

{

  //@@begin onActionSendEMail(ServerEvent)

  this.checkMandatory("EMailAddress");

  wdThis.wdFirePlugSendEMailOut(wdContext.currentContextElement().getEMailAddress());

  //@@end

}

...

 

In the last step, the event handler is implemented for the non-validating Clear action. Such an event handler is called before individual user entries are validated and stored in the controller context. This allows you to initialize the context again despite incorrect user input.

       5.      Add the following source code to the Form view controller:

Form.java

...

//@@begin javadoc:onActionClear(ServerEvent)

/** declared non validating event handler */

//@@end

public void onActionClear(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent

                          wdEvent )

{

  //@@begin onActionClear(ServerEvent)

  // Re-initialize this view´s context

  this.initialize();

  //@@end

}

...

 

This graphic is explained in the accompanying text  The next step is the Structure linkimplementation of the EMailEditor view controller.  

 

Leaving content frame