Package com.veeva.vault.sdk.api.page


package com.veeva.vault.sdk.api.page
This package provides interfaces to send data when loading a page or responding to events after the page has loaded.

Overview

A custom implementation PageController can be used to send data back to the client upon page load and respond to events sent from the client.

To send data back to the client on page load, the custom PageController implementation must define the PageController.onLoad(com.veeva.vault.sdk.api.page.PageLoadContext) method.

Example: Sending data in the form of a JSON object back to the client upon page load

The following example shows a sample implementation of PageController with an onLoad method that returns a JsonObject to the client once a page has been loaded.

 @ExecuteAs(ExecuteAsUser.REQUEST_OWNER)
 @PageControllerInfo
 public class HelloWorldController implements PageController {
      @Override
      public PageLoadResponse onLoad(PageLoadContext context) {
          JsonService jsonService = ServiceLocator.locate(JsonService.class);

          JsonObject jsonObject = jsonService.newJsonObjectBuilder()
              .setValue("userId", RequestContext.get().getInitiatingUserId())
              .build();

          return context.newLoadResponseBuilder()
              .withData(jsonObject)
              .build();
      }
 }
 

Example: Sending data as a UserDefinedModel back to the client

The following example shows a sample implementation of PageController with an onLoad method that returns a UserDefinedModel to the client once a page has been loaded. This is achieved by specifying a type of UserDefinedModel along with an instance of the object to return.

 // com.veeva.vault.custom.models.HelloUserResponse.java
 @UserDefinedModelInfo
 public interface HelloUserResponse extends UserDefinedModel {
     @UserDefinedProperty
     String getUserId();
     void setUserId(String userId);

     @UserDefinedProperty
     String getMessage();
     void setMessage(String message);
 }

 // com.veeva.vault.custom.pages.HelloWorldController.java
 @ExecuteAs(ExecuteAsUser.REQUEST_OWNER)
 @PageControllerInfo
 public class HelloWorldController implements PageController {
      @Override
      public PageLoadResponse onLoad(PageLoadContext context) {
          UserDefinedModelService userDefinedModelService = ServiceLocator.locate(UserDefinedModelService.class);

          HelloUserResponse helloUserResponse = userDefinedModelService.newUserDefinedModel(HelloUserResponse.class);
          helloUserResponse.setUserId(RequestContext.get().getInitiatingUserId());
          helloUserResponse.setMessage("Welcome!");

          return context.newLoadResponseBuilder()
              .withData(helloUserResponse, HelloUserResponse.class)
              .build();
      }
 }
 

A custom implementation of PageController can also respond to events sent from the client by defining its PageController.onEvent(com.veeva.vault.sdk.api.page.PageEventContext) method. Like the onLoad method, a definition for onEvent may also return either a JSON object or UserDefinedModel.

Example: Responding to events sent from the client

The following example shows a sample implementation of PageController with an onEvent method that returns a JSON object when the client sends pageEventA and returns a UserDefinedModel when the client sends pageEventB.

 @ExecuteAs(ExecuteAsUser.REQUEST_OWNER)
 @PageControllerInfo
 public class HelloWorldController implements PageController {
      @Override
      public PageEventResponse onEvent(PageEventContext context) {
          String event = context.getEventName()
          if ("pageEventA".equals(event)) {
              JsonService jsonService = ServiceLocator.locate(JsonService.class);

              JsonObject jsonObject = jsonService.newJsonObjectBuilder()
                  .setValue("userId", RequestContext.get().getInitiatingUserId())
                  .build();

              return context.newEventResponseBuilder()
                  .withData(jsonObject)
                  .build();
          } else if ("pageEventB".equals(event)) {
              UserDefinedModelService userDefinedModelService = ServiceLocator.locate(UserDefinedModelService.class);

              HelloUserResponse helloUserResponse = userDefinedModelService.newUserDefinedModel(HelloUserResponse.class);
              helloUserResponse.setUserId(RequestContext.get().getInitiatingUserId());
              helloUserResponse.setMessage("Thank you!");

              return context.newLoadResponseBuilder()
                  .withData(helloUserResponse, HelloUserResponse.class)
                  .build();
          } else {
              // If the event sent from the client is not one of pageEventA or pageEventB, return an error response
              // with a custom title and user-facing message.
              return context.newEventErrorResponseBuilder()
                  .withTitle("Event not found")
                  .withUserMessage("Error occurred while processing page event")
                  .build();
          }
      }
 }