Skip to Content

This guide provides instructions on how to consume or use SAP BRMS in EJB.

Applies to:


This Document Holds good for all CE 7.3 SPXX. This method can also be used for Older version of CE (CE7.1 SP01 on-wards) with some difference in contend .


Prerequisite:


This Document is a continuation of “How to consume or use SAP BRMS in EJB Part 1.“. Kindly Read this document fist to understand this document.


Summary:

This guide describes step-by-step how to return single value and multiple values from Decision Table.To Explain this we are using an Examples to explain how to read SAP BRMS in EJB.


Example : Multiple List of values Returned.


We will be discussing following points in detail in this document –


  1. How to create & use EJB DTO inside BRMS.
  2. How to call the rules from EJB.
  3. How to read the response received from BRMS.
  4. How to test the rules service.


Special Thanks to Samudra Gupta Samudra Gupta for his guidance to Document the process.


Example : Based on Entered Plant & product code list of  delivery plant with Start Date & End Date is to be defined.


Step 1 : Create an DTO called “DeliveryPlant” as shown below:


import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class DeliveryPlant implements Serializable{
  private static final long serialVersionUID = -623563864537413057L;
  private String entered_Plant ;
  private String entered_ProductCode ;
  private int identifier ;
  private ArrayList<String> deliveryPlantList ;
  private ArrayList<Date> startDateList ;
  private ArrayList<Date> endDateList ;
// Added Code.
  public DeliveryPlant (){
  deliveryPlantList = new ArrayList<String>();
  startDateList = new ArrayList<Date>();
  endDateList = new ArrayList<Date>();
  }
// Generated Code
  public String getEntered_Plant() {
  return entered_Plant;
  }
  public void setEntered_Plant(String enteredPlant) {
  entered_Plant = enteredPlant;
  }
  public String getEntered_ProductCode() {
  return entered_ProductCode;
  }
  public void setEntered_ProductCode(String enteredProductCode) {
  entered_ProductCode = enteredProductCode;
  }
  public int getIdentifier() {
  return identifier;
  }
  public void setIdentifier(int identifier) {
  this.identifier = identifier;
  }
  public ArrayList<String> getDeliveryPlantList() {
  return deliveryPlantList;
  }
  public void setDeliveryPlantList(ArrayList<String> deliveryPlantList) {
  this.deliveryPlantList = deliveryPlantList;
  }
  public ArrayList<Date> getStartDateList() {
  return startDateList;
  }
  public void setStartDateList(ArrayList<Date> startDateList) {
  this.startDateList = startDateList;
  }
  public ArrayList<Date> getEndDateList() {
  return endDateList;
  }
  public void setEndDateList(ArrayList<Date> endDateList) {
  this.endDateList = endDateList;
  }
// Added Code.
  public void addDeliveryPlantList(String deliveryPlant) {
  deliveryPlantList.add(deliveryPlant);
  }
  public void addStartDateList(String startDate) {
  startDateList.add(getDate(startDate));
  }
  public void addEndDateList(String endDate) {
  endDateList.add(getDate(endDate));
  }
  private Date getDate (String ipDate){
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  Date date = null;
  try {
  date = dateFormat.parse(ipDate);
  } catch (ParseException e) {
  }
  return date;
  }
}

Step 2 : Create an DTO called “ResponceDTO” as shown below:


import java.io.Serializable;
import java.util.Date;
public class ResponceDTO implements Serializable{
  private static final long serialVersionUID = 1L;
  private String deliveryPlant ;
  private Date startDate ;
  private Date endDate ;
// Generated Code
  public String getDeliveryPlant() {
  return deliveryPlant;
  }
  public void setDeliveryPlant(String deliveryPlant) {
  this.deliveryPlant = deliveryPlant;
  }
  public Date getStartDate() {
  return startDate;
  }
  public void setStartDate(Date startDate) {
  this.startDate = startDate;
  }
  public Date getEndDate() {
  return endDate;
  }
  public void setEndDate(Date endDate) {
  this.endDate = endDate;
  }
}

Step 3 : Create an DTO called “DeliveryPlantResponce” as shown below:


import java.io.Serializable;
import java.util.ArrayList;
public class DeliveryPlantResponce implements Serializable{
  private static final long serialVersionUID = 1L;
  private String entered_Plant ;
  private String entered_ProductCode ;
  private ArrayList<ResponceDTO> ResponceList ;
// Generated Code
  public String getEntered_Plant() {
  return entered_Plant;
  }
  public void setEntered_Plant(String enteredPlant) {
  entered_Plant = enteredPlant;
  }
  public String getEntered_ProductCode() {
  return entered_ProductCode;
  }
  public void setEntered_ProductCode(String enteredProductCode) {
  entered_ProductCode = enteredProductCode;
  }
  public ArrayList<ResponceDTO> getResponceList() {
  return ResponceList;
  }
  public void setResponceList(ArrayList<ResponceDTO> responceList) {
  ResponceList = responceList;
  }
// Added Code
  public void addResponceList(ResponceDTO responceDto) {
  ResponceList.add(responceDto);
  }
}

Step 4 : Expose the “DeliveryPlant” Class file in Public Parts as shown before in Previous Document.


Step 5 : Build the test/ejb/module .


Optional : Add “test/ejb/module” in “Dependencies” of “test/rules”. Only “Design Time” is required (Need to be done if not done before).


Step 6 : Go to the rules project and add the dto class.


  1. Open “Project Resource”. Click on the “Aliases” tab. Click on Add.
  2. On Click of “Add” the below pop Up opens up. Select the “Class” Option. /wp-content/uploads/2014/01/1_357187.png
  3. On Selecting Class another pop up opens up which looks as shown below. Open the Package select the “DeliveryPlant” . Click on the arrow to add in the Selected Class. It will appear as shown below. Finally click on Finish./wp-content/uploads/2014/01/1_357187.png
  4. On Clicking of “Finish” the Plant gets added in the “Alias Name” List. Expand it and rename “DeliveryPlant.getEntered_Plant” with “Entered_Plant” , “DeliveryPlant.getEntered_ProductCode” with “Entered_ProductCode” & “DeliveryPlant.getIdentifier” with “Identifier“. /wp-content/uploads/2014/01/1_357187.png

Step 7 : Now Right Click on “Rules Modeling” & Select “New Ruleset”. /wp-content/uploads/2014/01/1_357187.png

Setp 8 : On Selecting “New Ruleset” a pop up opens up as shown below. Enter the name to the rule set like “DeliveryPlantsRuleset”./wp-content/uploads/2014/01/1_357187.png

Step 9 :  On Clicking on “OK” We get a new Rule Set created as shown below: /wp-content/uploads/2014/01/1_357187.png

Step 10 : Creation of Rules & Decision Table.


  1. To create a “Decision Table” we have 2 process to create new “Decision Table”/wp-content/uploads/2014/01/1_357187.png
    1. Right Click on “Decision Table” and select “New Decision Tables…”.
    2. Open “DeliveryPlantRuleset”. Click on “Decision Table” Tab. Click on “New” button.
  2. On selecting either of the method we get the below pop up. Enter the “Decision Table Name” & “Comments”. Click on “Next”./wp-content/uploads/2014/01/1_357187.png
  3. On Click of “Next” The pop up changes and looks as shown below. Select the “Entered_Plant” , “Entered_ProductCode” & “Identifier” from the Available Condition and Add them in the selected Condition. Click on “Next”./wp-content/uploads/2014/01/1_357187.png
  4. On Click of “Next” The pop up changes and looks as shown below. Select the “Plant.setDeliveryPlantList({String})” from the Available Action and Add them in the selected Action. (In our scenario we do not have any other Action). Click on “Finish”./wp-content/uploads/2014/01/1_357187.png
  5. On Click Of Finish the “Decision Table” is created and looks as shown below. Add the values in build time or you can add values directly in Rules Manager later on. To know how to click here. (Note : This Upload and download of Decision Table is not available in older version)./wp-content/uploads/2014/01/1_357187.png
  6. Expand the “Documentation and Properties”. Make “Return multiple matches = true” & “Rows are mutually exclusive = false”./wp-content/uploads/2014/01/1_357187.png
  7. To create a “Rule” we have 2 process to create new “Rule”/wp-content/uploads/2014/01/1_357187.png
    1. Right Click on “Rules” and select “New Rule…”.
    2. Open “DeliveryPlantRuleset”. Click on “Rules” Tab. Click on “New” button.
  8. On selecting either of the method we get the below pop up. Enter the “Rules Name”. Click on “OK”./wp-content/uploads/2014/01/1_357187.png
  9. On Click Of “OK” the following is visible. In Our example we only have one rule and one decision table so we set a default condition and a default action as shown in the below./wp-content/uploads/2014/01/1_357187.png


Step 11 : Save it build and deploy “test/ejb/app” & “test/rules” in sequence.


Step 12 : Calling the Rule from “test/ejb/module”.


  1. Create a Java Class Called “EngineInvoker” (Optional – If not created before).
  2. Create a “Session Bean” in “test/ejb/module” with the name “DeliveryPlantsRules”.
  3. Copy past the code in Appendix 1 in the “Session Bean” in “test/ejb/module” with the name “DeliveryPlantRules”.
  4. The Code in line 30 in Appendix 1 is displayed below. there are 3 inputs to the method/wp-content/uploads/2014/01/1_357187.png
    1. The first one is the “Application Name” which can be found inside “test/rules” dc’s “Project Resources” as shown below:/wp-content/uploads/2014/01/1_357187.png
    2. The Second one is “Ruleset Name” which can be found inside “test/rules” dc’s “DeliveryPlantRuleset” as shown below:/wp-content/uploads/2014/01/1_357187.png
    3. The third and last one is the dto sent inside a list.
  5. Right Click on the “Session Bean” “DeliveryPlantsRules” and Create Web service


Step 13 : Build the dc’s “test/ejb/module” & “test/ejb/app”. Deploy the dc “test/ejb/app“.


Step 14 : Go to http://<Server>:<port>/wsnavigator and test the service be entering the “entered_Plant” & “entered_ProductCode” you will get back the “DeliveryPlantResponce” DTO with the entered values along with “ResponceDTO” List.


Appendix 1 : DeliveryPlantsRules.java Class file code.



import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import util.EngineInvoker;
import com.sap.tc.logging.Location;
import dto.DeliveryPlant;
import dto.DeliveryPlantResponce;
import dto.ResponceDTO;
public class DeliveryPlantsRules implements DeliveryPlantsRulesLocal {
static Location logger = Location.getLocation(DeliveryPlantsRules.class);
  @SuppressWarnings("unchecked")
  public DeliveryPlantResponce getDeliveringPlantForProductsRules(DeliveryPlant plant){
  logger.debugT("start:getRullesData");
  List<Serializable> ilist = new ArrayList<Serializable>();
  plant.setIdentifier(Integer.MAX_VALUE);
  ilist.add(plant);
  Object object = new Object();
  if (ilist.size() != 0) {
  List output = EngineInvoker.invokeRuleset("demo.sap.com~test~rules", "DeliveryPlantsRuleset", ilist);
  if (output.size() != 0) {
  object = output.get(0);
  }
  } else {
  object = "Input has not been set.";
  }
  logger.debugT("end:getRullesData");
  if (object instanceof DeliveryPlant) {
  plant = (DeliveryPlant) object;
  DeliveryPlantResponce deliveryPlantResponce = new DeliveryPlantResponce();
  ResponceDTO responceDTO = null ;
  deliveryPlantResponce.setEntered_Plant(plant.getEntered_Plant());
  deliveryPlantResponce.setEntered_ProductCode(plant.getEntered_ProductCode());
  for (int i = 0; i < plant.getDeliveryPlantList().size(); i++) {
  responceDTO = new ResponceDTO();
  responceDTO.setDeliveryPlant(plant.getDeliveryPlantList().get(i));
  responceDTO.setStartDate(plant.getStartDateList().get(i));
  responceDTO.setEndDate(plant.getEndDateList().get(i));
  deliveryPlantResponce.addResponceList(responceDTO);
  }
  return deliveryPlantResponce ;
  }
  return null;
  }
}
To report this post you need to login first.

18 Comments

You must be Logged on to comment or reply to a post.

  1. Samudra Gupta

    Hi Piyas,

    First ever document in order to get multiple actions on the basic of any condition in BRM.

    Very much helpful for BRM developers.

    Well work done!!

    Regards,

    Samudra

    (0) 
    1. Piyas Kumar Das Post author

      Hi Samudra,

      Thanks for the Appreciation and the motivation you gave me during my learning years.

      This document is a small tribute to you

      Regards

      Piyas

      (0) 
  2. Antony FERMINUS

    Hi Kumar,

    Thanks for the article. I got working for the part 1. But in part 2 i have a problem of understanding the step 4.

    It means do i need the plant class still?

    do i have to add the Plant.setDeliveryPlantList{String} to the selected action.

    But the screenshot shows only the 3 from DeliveryPlant.

    Could you please let me know what am i missing here.

    I get the error below.brmError.PNG

    Thanks in advance.

    Regards,

    Antony.

    (0) 
    1. Piyas Kumar Das Post author

      Hi Antony,

      Good day to you.

      By the look of it I feel you have missed something in the code or may be i have missed some step.

      It will be great if you could send me the .sca file of these 3 DC’s. One more thing I would like to know what server version and JDK version you are currently using to test the application.

      If you want to send me the SCA file you can share it with me on my email “piyas.das@gmail.com“.

      Thanks & Regards

      Piyas

      (0) 
              1. Antony FERMINUS

                Hi Piyas,

                I made it.

                In the deliveryPlantRespnse.java i add the code bellow for the initilization of the ArrayList.

                private ArrayList<ResponceDTO> ResponceList = new ArrayList<ResponceDTO>();

                It wors fine now.

                I got the result.result.PNG

                Regards,

                Antony.

                (0) 

Leave a Reply