Skip to Content
Author's profile photo Former Member

Model Binding using external model dc in WDJ(Adaptive Webservice Model in WDJ project)

Table of Contents

  1. 1.0       Purpose 3
  2. 2.0       Assumptions 3
  3. 3.0       Step By Step Procedure 3
  4. 3.1       Checking DC’s into Navigator Explorer 3
  5. 3.2       Development to be done in the Web development DC. 10
  6. 3.3       Deploy the WDJ DC on server 14

 

1.0   Purpose

This article will guide you through step by step process in understanding how to consume Adaptive Webservice Model in WDJ project using an external model DC which contains the model. This external model dc can be used asa generic dc for models. hence if same model is required more than one application it can be used without the dependency of the application dc.

We will also see the steps of how to create a model dc and how to use it.

2.0   Assumptions

The following Development components are already created:

1.        Web development component, containing screen to add and view data.

2.        Web development component, to add model

3.        EJB & EAR(Enterprise application) development component,containing the methods.

The dependency of external model DC and Enterprise application DC needs to be added in the wed development DC

3.0   Step By Step Procedure

3.1      Checking DC’s into Navigator Explorer

In the Navigator Explorer, we see that there are 4 DC’s, 2 WDJ (Web Dynpro java i.e., Application and Model ) DC , 1 EJB (Enterprise Java Bean)DC and 1 EAR (Enterprise Application)DC.

3.2      Development to be done in the Web development DC

Step 1:  Create the EJB DC and write the method for creation of record in the database.Add dependency of EJB DC in EAR DC, so that we can deploy our EJB  on the server by deploying the EAR DC.

Code:

package com.irda.br.othrs.sessionbean;

import java.sql.Date;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import javax.ejb.Stateless;

import javax.persistence.EntityManager;

import javax.persistence.PersistenceContext;

import javax.persistence.PersistenceContextType;

import com.irda.br.othrs.entities.BapTrnBrPlmntR;

import com.irda.br.othrs.helper.PlmntRepHelper;

import com.irda.br.othrs.helper.ReturnHelper;

import javax.jws.WebService;

import javax.jws.WebMethod;

import javax.jws.WebParam;

@WebService(name = “Othrs”, portName = “OthrsBeanPort”, serviceName = “OthrsService”, targetNamespace = “http://irda.com/br/othrs/sessionbean/“)

@Stateless

public class OthrsBean implements OthrsBeanLocal

{

            @PersistenceContext(unitName = “OthrsPUName”, type = PersistenceContextType.TRANSACTION)

            protected EntityManager em;    

            private static final SimpleDateFormat formatDate = new SimpleDateFormat(“yyyy-MM-dd”);     

            private static final Calendar currYearCal = Calendar.getInstance();

            public OthrsBean()

            {

            }

        

            @WebMethod(operationName = “createPlmntRep”, exclude = false)

            public ReturnHelper createPlmntRep(@WebParam(name = “p_PlmntRepHelper”) PlmntRepHelper p_PlmntRepHelper)

            {

                        BapTrnBrPlmntR BapTrnBrPlmntR;                                     //entity object declared

                        ReturnHelper retHelper = new ReturnHelper();                                //returnHelper class object initialized

                    

                        try

                        {

                                    if (p_PlmntRepHelper != null)                                //to check wether helper object is set to null

                                    {

                                                BapTrnBrPlmntR = new BapTrnBrPlmntR();                          //entity object initialized

                                                BapTrnBrPlmntR.set_creatBy_(p_PlmntRepHelper.get_creatBy_());

                                                BapTrnBrPlmntR.set_creatDt_(new java.sql.Timestamp(System.currentTimeMillis()));

                                                BapTrnBrPlmntR.set_modBy_(p_PlmntRepHelper.get_modBy_());                                        

                                                BapTrnBrPlmntR.set_modDt_(new java.sql.Timestamp(System.currentTimeMillis()));                                            

                                                BapTrnBrPlmntR.set_refPlmnt_(p_PlmntRepHelper.get_refPlmnt_());

                                                if(p_PlmntRepHelper.get_replyDt_() != null)

                                                            BapTrnBrPlmntR.set_replyDt_((Date.valueOf(formatDate.format(p_PlmntRepHelper.get_replyDt_()))));

                                                BapTrnBrPlmntR.setSubj(p_PlmntRepHelper.getSubj());                                         

                                                BapTrnBrPlmntR.set_sumRply_(p_PlmntRepHelper.get_sumRply_());

                                                if(p_PlmntRepHelper.getSubmn_Dt() != null)

                                                            BapTrnBrPlmntR.setSubmn_Dt((Date.valueOf(formatDate.format(p_PlmntRepHelper.getSubmn_Dt()))));

                                                BapTrnBrPlmntR.setQuarter_submn(p_PlmntRepHelper.getQuarter_submn());

                                                BapTrnBrPlmntR.setYear_submn(p_PlmntRepHelper.getYear_submn());                                           

                                                BapTrnBrPlmntR.setQno(p_PlmntRepHelper.getQuestion_Number());                                  

                                            

                                                em.persist(BapTrnBrPlmntR);

                                                BapTrnBrPlmntR.setPlmntdisc_URN(“BR-“+BapTrnBrPlmntR.getPlmnt_rep_Id()+”-“+currYearCal.get(Calendar.YEAR));

                                                retHelper.setPlmnt_rep_Id(BapTrnBrPlmntR.getPlmnt_rep_Id());

                                                retHelper.setPlmntdisc_URN(BapTrnBrPlmntR.getPlmntdisc_URN());

                                                em.merge(BapTrnBrPlmntR);                                        

                                                retHelper.setReturnMsg(“Data inserted for URN=”+BapTrnBrPlmntR.getPlmntdisc_URN());                      //success msg aftr it satisfies the if condition

                                                retHelper.setReturnErr(“rep id :”+retHelper.getPlmnt_rep_Id());

                                    }//end of if condition

                                    else//else for if (to check wether helper object is set to null)

                                    {

                                                retHelper.setReturnErr(“Data cannot be saved”);                  //error msg aftr it fails if condition

                                    }//end of else

                        }

                        catch (Exception e)

                        {

                                    e.printStackTrace();

                        }                   

                        return retHelper;//returns the sucsess msg or error mssg depending on the condition satisfied

            }

}

We need to create webservice so that we can create model.After webservice creation, deploy the Ear DC on the server.

Step 2: Create the WDJ model DC which will contain the model for our webservice. Create model in the DC. There are two ways to create model as follows:

  1. 1.Using the service registry.

           We need to register our webservice using the service registry, and If don’t register our webservice we can simply add our webservice link and proceed with the model creation.

  1. 2.Using the wsdl(webservice description language).

           We need to create the wsdl file of our webservice and save it on our local system. during the time of model creation we cangive the path of our wsdl file.

Steps to create wsdl file

We need to find our bean on the wsnavigator and expand the tray and click on the URL highlighted in the image below

Once we click on the highlighted link it opens up in the browser and appears as an xml file. we need to delete the characters of the link after “wsdl” and press enter. Another xml file opens up and that is our wsdl file which we need to save on our system with an appropriate name.

After we save the file it appears like this on the system.

Steps to create model in our model DC.

Since we are creating model using the wsdl file we need to select “Remote location option” and if we are creating model using service registry option to be selected is “Service Registry”.

on click of next we need to browse the path for our wsdl file and click on next and then finish.

After finish we can see the model created.

Step 3: Add the model in “Used model” so that we can add the methods.If the model is not added in “Used model” we cannot add the methods.

Now we add the methods.

we can select any of the method from the list and say next.

After next select the nodes and attributes under the method name node. These attributes and nodes are nothing but our input parameters and output results (e.g., helper classes and variables).

On click of next we can see our method name and we can say finish. After which we can see the method in the method list

Of component controller.

Now we can see the two extra methods added along with the execute method which we will call to trigger the method call from EJB.

These methods have the importance as follows:

  1. 1. createAndMapModelInstance method is added to create an instance of the model which is created using the webservice
  2. 2. initRequest method is added to initialize the parameters of our webservice.

Execute method is to pass the parameters to the methods.

Step 4: Make the methods as parameterized

We will make the methods as parameterized so that we don’t have to initialize or pass the input variables in the model DC.

we will add two parameters to the init and execute method which will of Request node(i.e., parent node of the added method ) type and Model(i.e.,model created by us) type .

At the time of applying template initRequest method is called on wdDoinit which we need to remove. As that method needs to be called in the execute method and execute method will be called at the place we want to trigger our webservice to perform any action.

Code:

/*************** init method ********************/

public void initRequest_CreatePlmntRep( com.sap.demo.wd.modelbin.wd.models.othersmodel.Request_CreatePlmntRep ReqParam, com.sap.demo.wd.modelbin.wd.models.othersmodel.OthersModel OthrsModel )  {

    //@@begin initRequest_CreatePlmntRep()

    //$$begin Service Controller(838448408)

{

        com.sap.demo.wd.modelbin.wd.models.othersmodel.Request_CreatePlmntRep request_CreatePlmntRep = wdContext.nodeRequest_CreatePlmntRep().createAndAddRequest_CreatePlmntRepElement().modelObject();

        com.sap.demo.wd.modelbin.wd.models.othersmodel.CreatePlmntRep createPlmntRep = new com.sap.demo.wd.modelbin.wd.models.othersmodel.CreatePlmntRep(OthrsModel);

        com.sap.demo.wd.modelbin.wd.models.othersmodel.PlmntRepHelper p_PlmntRepHelper = new com.sap.demo.wd.modelbin.wd.models.othersmodel.PlmntRepHelper(OthrsModel);

    

        createPlmntRep.setP_PlmntRepHelper(ReqParam.getCreatePlmntRep().getP_PlmntRepHelper());       

        request_CreatePlmntRep.setCreatePlmntRep(ReqParam.getCreatePlmntRep());              

        wdContext.nodeRequest_CreatePlmntRep().bind(request_CreatePlmntRep);

}

    //$$end

    //@@end

  }

/*************** createAndMapModel Instance method *************/

public com.sap.demo.wd.modelbin.wd.models.othersmodel.OthersModel createAndMapModelInstance( java.lang.String

modelInstanceKey ) 

{

    //@@begin createAndMapModelInstance()

    //$$begin Service Controller(773151174)

    com.sap.demo.wd.modelbin.wd.models.othersmodel.OthersModel othersModelModel = (com.sap.demo.wd.modelbin.wd.models.othersmodel.OthersModel)wdComponentAPI.getModelInstanceMap().

getDefaultInstance(modelInstanceKey);

    if (othersModelModel == null){

     othersModelModel = new com.sap.demo.wd.modelbin.wd.models.othersmodel.OthersModel();

     wdComponentAPI.getModelInstanceMap().putDefaultInstance(modelInstanceKey,othersModelModel);

    }

    return othersModelModel;

    //$$end

    //@@end

  }

/*********** Execute method **********/

public void m_executeCreatePlmntRep( com.sap.demo.wd.modelbin.wd.models.othersmodel.Request_CreatePlmntRep ReqParam, com.sap.demo.wd.modelbin.wd.models.othersmodel.OthersModel OthrsModel )  {

    //@@begin m_executeCreatePlmntRep()

    //$$begin Service Controller(-164965031)

    try

    {      

            initRequest_CreatePlmntRep(ReqParam, OthrsModel);          

    wdContext.currentRequest_CreatePlmntRepElement().modelObject().execute();

    } catch(Exception e){

    wdComponentAPI.getMessageManager().reportException( e.getMessage());

    }

    //$$end

    //@@end

  }

Step 5: Add the methods and nodes to the interface controller.

We need to add the context nodes as well as the methods(all 3)in the Interface controller. if we don’t add it in theinterface controller we cannot access our methods and nodes.

Step 6: Add the changes to public part  by right click on component controller.

Step 7: Deploy the model DC.

Step 8: Add the dependency(only build and runtime) of our model DC in our application DC and use the methods we want to use.

Step 9: Create used component of the model DC

Select the component controller name of the model DC and say OK

After we click OK we can see the Component created under “Used component” section. And also we can see the nodes which we exposed before adding it to the public part.

Step 10: Create the method in our application DC and initialize all the variables and pass the input parameters to execute the method.

Code:

/*************** Method call *********************/

public void m_createUser( )  {

    //@@begin m_createUser()

              int size = 0;

              OthersModel testFacelessModel = getModelInstance();

              Request_CreatePlmntRep request_CreatePlmntRep = wdContext.nodeRequest_CreatePlmntRep().createAndAddRequest_CreatePlmntRepElement().modelObject();

              CreatePlmntRep createPlmntRep = new CreatePlmntRep(testFacelessModel);

              PlmntRepHelper p_PlmntRepHelper = new PlmntRepHelper(testFacelessModel);

              p_PlmntRepHelper.set_CreatBy_(userid);

              p_PlmntRepHelper.set_ModBy_(userid);

              if (wdContext.currentCTX_VN_Reply_ParliamentElement().getCTX_VA_Reference_Parliament() != null)

              {

                          p_PlmntRepHelper.set_RefPlmnt_(wdContext.currentCTX_VN_Reply_ParliamentElement().getCTX_VA_Reference_Parliament().

trim());

              }

              else

              {

                          p_PlmntRepHelper.set_RefPlmnt_(wdContext.currentCTX_VN_Reply_ParliamentElement().getCTX_VA_Reference_Parliament());

              }

              if (wdContext.currentCTX_VN_Reply_ParliamentElement().getCTX_VA_SummeryOfReply() != null)

              {

                          p_PlmntRepHelper.set_SumRply_(wdContext.currentCTX_VN_Reply_ParliamentElement().getCTX_VA_SummeryOfReply().trim());

              }

              else

              {

                          p_PlmntRepHelper.set_SumRply_(wdContext.currentCTX_VN_Reply_ParliamentElement().getCTX_VA_SummeryOfReply());

              }

             if(wdContext.currentCTX_VN_Reply_ParliamentElement().getCTX_VA_SubjectMatter() != null)

              {                                

                          p_PlmntRepHelper.setSubj(wdContext.currentCTX_VN_Reply_ParliamentElement().getCTX_VA_SubjectMatter().trim());

              }

              else

              {

                          p_PlmntRepHelper.setSubj(wdContext.currentCTX_VN_Reply_ParliamentElement().getCTX_VA_SubjectMatter());

              }

              p_PlmntRepHelper.setQuestion_Number(wdContext.currentCTX_VN_Reply_ParliamentElement().getCTX_VA_QuestionNumber());

              p_PlmntRepHelper.setQuarter_Submn(Integer.parseInt(wdContext.currentCTX_VN_FillParam_Rep_ParElement().

getCTX_VA_Quarter()));

              String year = wdContext.currentCTX_VN_FillParam_Rep_ParElement().getCTX_VA_Year();

              int yrofsub = Integer.parseInt(year);

              p_PlmntRepHelper.setYear_Submn(yrofsub);

              p_PlmntRepHelper.set_ReplyDt_(new java.sql.Timestamp(wdContext.currentCTX_VN_Reply_ParliamentElement().getCTX_VA_DateOfReply().getTime()));

              p_PlmntRepHelper.setSubmn_Dt(new java.sql.Timestamp(System.currentTimeMillis()));

              p_PlmntRepHelper.setPlmntRep_Id(0); 

              createPlmntRep.setP_PlmntRepHelper(p_PlmntRepHelper);

              request_CreatePlmntRep.setCreatePlmntRep(createPlmntRep);

              wdThis.wdGetOthrsInstInterface().m_executeCreatePlmntRep(request_CreatePlmntRep, testFacelessModel);

              size = wdThis.wdGetContext().nodeReturn().size();            

              if(size >0)

              {

                          wdContext.currentCTX_VN_Reply_ParliamentElement().setCTX_VA_RefNo(wdContext.currentReturnElement().getPlmntdisc_URN());

                          wdContext.currentCTX_VN_returnsElement().setCTX_VA_urn(wdContext.currentReturnElement().getPlmntdisc_URN());

                          String RefNO=wdContext.currentCTX_VN_Reply_ParliamentElement().getCTX_VA_RefNo();                                            

              }

    //@@end

  }

/************ Code to be written globally ***************/

IUser currentUser =WDClientUser.getLoggedInClientUser().getSAPUser();         

  String userid =currentUser.getUniqueName();

  //creating a model instance

  private OthersModel getModelInstance()

  {

              com.sap.demo.wd.modelbin.wd.models.othersmodel.OthersModel othersModel = wdThis.wdGetOthrsInstInterface()

              .createAndMapModelInstance(“com.sap.demo.wd.modelbin.wd.models.othersmodel.OthersModel”);

              return othersModel;

  }

3.3      Deploy the WDJ DC(Application DC) on server

Output:

Additional Information:

The external model DC enables us to:

·         Create reusable model

·         Decrease the dependency

·         Makes the DC lightweight by using same model in different application DC rather than having all the application in the same DC.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.