Implementing the MealOrderService


We have seen that regardless of how services are exposed to clients, there is an underlying registered service implementation that is available to all of these. Let's use our IDE to begin looking just at the MealOrderService interface itself in the com.example.bundle.business Java package. 

It has several interesting things to note:

  1. It declares a MealOrderService interface. It does not directly extend the framework's com.bmc.arsys.rx.services.common.Service interface, but its implementation must. This is important so other code can use BMC Helix Innovation Studio's ServiceLocator singleton to obtain a handle to an instance of this Service at run-time; it is also required in order to expose methods as Service Actions, as we shall see shortly.

    MealOrderService.java - Snippet
    public interface MealOrderService {

    /**
     * Create a new MealOrder with a single consumer, based on the exact name of an existing dish.
     * @param dish - exact name of a dish
     * @param consumer - login id of an existing Employee
     * @param requestedDate - date representation in ISO-8601 format "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
     * @return MealOrder
     */

    public MealOrder createSingleConsumerOrder(
    @NotBlank String dish,
    @NotBlank String consumer,
    @NotBlank String requestedDate);

    /**
     * This method returns info about a meal order. Includes total cost, price of the dish, delivery charge of the restaurant, and configured tax rate.
     * @param mealOrderId
     * @return MealOrderInfo
     */

    public MealOrder buildMealOrder(@NotBlank String mealOrderId);


    // ...

       /**
     * This action cancels an open order.
     * @param mealOrderId
     * @return true if cancelled successfully
     */

    public abstract boolean cancelMealOrder(@NotBlank String mealOrderId);

    // ...
  2. The GetMealOrder() method returns our domain object called MealOrder. We will take a closer look at its implementation later on; for now, assume it is a kind of "plain old Java object" (or POJO) that can be serialized for outside consumption. 
  3. Looking at the implementation class MealOrderServiceImpl, found in the com.example.bundle.business.impl package,  the methods are often wrappers to functionality that is in other classes. For example, the BMC Helix Innovation Studio SDK standard library RecordService is used to manipulate a record to implement cancelMealOrder().  The RecordInstance is fetched and then updated. Simplifying the code somewhat:

    MealOrderServiceImpl Pseudocode
    RecordService recordService = ServiceLocator.getRecordService();


    // Retrieve the instance object from persistence.
    RecordInstance mealOrderRecordInstance =
    recordService.getRecordInstance(
    MealOrderConstants.MEAL_ORDER_RECORD_DEF_NAME,
    mealOrderId);


    // Set the Status field value in memory.
    mealOrderRecordInstance.setFieldValue(
    MealOrderConstants.MEAL_ORDER_STATUS_FIELD_ID,
    MealOrderConstants.MEAL_ORDER_STATUS_CANCELLED);


    // Persist the updated record instance.
    recordService.updateRecordInstance(mealOrderRecordInstance);

    You can see why the constants in MealOrderConstants have to refer to the correct ids and/or names of definitions in the Lunch Tutorial application in order to function correctly here.

  4. This implementation is registered automatically by the code called at Bundle start up time, in MyApplication.java:

    MyLibrary.java Snippet
    // Register Services
    registerService(new MealOrderServiceImpl());

Challenge

  • Create a new Service Interface and Implementation within this Library called RestaurantService and RestaurantServiceImpl.  Follow the best practices on package structure.
  • Define at least one method - you can give it a very simple dummy method such as callKitchen() for now.
  • Rebuild your Library using.

    projects\meal-program-lib> mvn clean install
  • Don't worry about making it completely functional right now or even accessible from clients - we will have a chance to do this later as we go deeper into the Meal Program Library pre-built code.

What you Learned

  • Services should have both and interface, implementation, and domain objects that are kept in separate Java packages, as a best practice.
  • Framework services such as the RecordService and AssociationService are available to manipulate Records in your implementation.
  • Service implementations need to be registered in the Bundle class (MyApplication.java).


 

Tip: For faster searching, add an asterisk to the end of your partial query. Example: cert*

BMC Helix Innovation Suite 25.4