Skip to Content
Author's profile photo Vijay Kumar Kalluri

Dictionary Types in Web Dynpro Development Components in Web Dynpro for Java

PURPOSE  

Dictionary perspective is one of the many perspectives available in NWDS. We often come across scenarios where we feel custom data types can be re-used. E.g. a String data type of length 20 can be re-used by a number of applications. This can be achieved using dictionary perspective.  

Also, there are scenarios where we want a persistent data store like a table and are left with no option other than using R/3 or external database like oracle or SQL.

In dictionary perspective we can create a table to store persistent data thus avoiding the use of R/3 or external database.


Java Dictionary

  • The SAP Web Application Server provides varied options for all aspects of application development. Different techniques, such as JDBC, are provided also for the database connection for applications.
  • Central storage for neutral platform definition of data types and database objects is very helpful if you are programming extensive SAP applications. SAP provides this storage area in the form of the Java Dictionary.


Steps for consuming Java Dictionary Project in Web Dynpro DC

We will go through the following successive development steps:  

  • Create Development Component (DC) of type ‘Dictionary’
  • Define Dictionary types: Simple Types, Structures & Database Tables
  • Make Dictionary type DC visible to other DCs
  • Build, Create .sda and deploy Dictionary DC on J2EE server
  • Create Web Dynpro DC and Define UI fields
  • Create DC of type Enterprise Application Project
  • Define Database Alias
  • Define Project References
  • Build, deploy Enterprise Application DC
  • Insert Records in Java Dictionary Table
  • Select Records from Java Dictionary Table  


STEPS  

Create Development Component (DC) of type ‘Dictionary’  

Open Java Dictionary Perspective

Open NWDS. Go to Windows->Open Perspective->Other

Select Dictionary. Click on OK. 

1.JPG

Select Development Component.  

Click on Next Local Development->My Components. Click Next.

Provide name as ‘dictionary’, caption as ‘Dictionary DC’and type as ‘Dictionary’

3.JPG

Click Next. Keep the default settings. Click on Finish.  

     Define Dictionary data types: Simple Types, Structures & Database Tables  

Go to Dictionary Explorer->dictionary->Dictionaries->Local Dictionary

->Data Types->Simple Types. Right click on it.

Create Simple Type EMPID  

4.JPG

5.JPG

Click on Finish.

Navigate to the Definition tab of Simple type.

Keep type as String and Maximum length as 10

6.JPG

Create another simple type NAME of type String and Maximum Length 30.

Right click on the Structures node of Data Types.

Create New Structure ‘Employee’.

7.JPG

Provide Package. Click on Finish  

8.JPG

9.JPG

Right click on Database Tables node of Local Dictionary.

Create Database Table TMP_EMPLOYEE

10.JPG

Click on Finish.

Define two table columns EMPID and NAME of respective simple types EMPID and NAME.

11.JPG

Make Dictionary type DC visible to other DCs  

Expand DC Metadata node of the Dictionary DC. Right click on Public Parts

->New Public Part

12.JPG

Give the name pp_dictionary. Click on Next.

13.JPG

In the next screen, select Dictionary Simple Type. Check checkboxes for com/sap/mypackage/EMPID and com/sap/mypackage/NAME.


Then select Dictionary Structure. Check com/sap/mypackage/Employee. Select Dictionary Database Table. Check /TMP_EMPLOYEE.

14.JPG

  

Click on Finish

Build , Create .sda and deploy Dictionary DC on J2EE server

Right Click on Dictionary DC ->Development Component->Build

Create Archive (.sda= software development archive)  

15.JPG

Right Click on Dictionary DC ->Development Component->Deploy  

Create Web Dynpro DC and Define UI fields  

File->New->Development Component Project ‘dictionary_wd’.

16.JPG

Right Click on Web Dypro Components->New. Create a new Web Dynpro component ‘DictionaryUsageComp’ with view name ‘DictionaryUsage’.

Go to dictionary_wd->DC Metadata->DC Definition->Used DCs. Right click on Used DCs->Add Used DC.  

18.JPG

Navigate to My Components->dictionary->pp_dictionary.

Reuse of Dictionary types requires that you select the following dependency types:  

  • Build Time (affected Web Dynpro objects need the Dictionary type for compilation)
  • Run Time (at runtime, the Web Dynpro application dynamically links to the Dictionary data type deployed to the target server)   

19.JPG

Right click on dictionary_wd->Development Component->Build

Now navigate to Web Dynpro Components->DictionaryUsageComp->Views->DictionaryUsage. Select Context tab.

  Define Context node Person with structure binding to Employee Structure defined earlier in Dictionary DC

20.JPG

21.JPG

Click on Next

22.JPG

Click on Finish. Switch to Layout tab. Add following UI elements.

23.JPG

  • Add an onAction event handler onActionCreateRecord() for button ‘Create Record’
  • Add an onAction event handler onActionDisplayRecords() for button ‘Display Records’.  

Create DC of type Enterprise Application Project  

  • Open J2EE DC Perspective
  • File->New->Development Component->J2EE->Enterprise Application with name ‘ear_dc’.

24.JPG

Right click on ear_dc->New->META-INF/data-source-aliases.xml

25.JPG

Provide Alias name as DSSAP

26.JPG

Right Click on ear_dc->Properties->Project References

Check checkboxes for ‘dictionary’ DC and ‘dictionary_wd’ DC. Click on OK.

27.JPG

Build, deploy Enterprise Application DC  

  • Right Click on ear_dc->Development Component->Build
  • Right Click on ear_dc->Development Component->Deploy  

How to Create Data Source (DSSAP) Alias name in Portal

Login into the Portal : http://hostName:50000/nwa   Click on Configuration


28.JPG

Click on Application Resource

29.JPG

Select JDBC Data Source Alias

30.JPG

Click on Create

31.JPG

 

Enter Resource Name and JDBC Data Source and Click on OK and Save


Insert Records in Java Dictionary Table  

In order to insert the records into the java dictionary table TMP_EMPLOYEE

On the click of button ‘Create Record’, following code snippet needs to be added to onActionCreateRecord ().  

Connection con = null;

PreparedStatement ps = null;

try{

    InitialContext ctx = new InitialContext();

    DataSource ds = (DataSource) ctx.lookup(“jdbc/DSSAP”);

    con = ds.getConnection();

    String sql = “Insert Into TMP_EMPLOYEE (EMPID,NAME) values(?,?)”;

    ps = con.prepareStatement(sql);

    ps.setString(1,wdContext.nodePerson().currentPersonElement().getEMPID());

    ps.setString(2,wdContext.nodePerson().currentPersonElement().getNAME());

  1. ps.executeUpdate();
  2. wdComponentAPI.getMessageManager().reportSuccess(“!—Record Inserted—!”);

}catch(NamingException ne){

   wdComponentAPI.getMessageManager().reportSuccess(ne.getMessage());

  }catch(SQLException se){

      wdComponentAPI.getMessageManager().reportSuccess(se.getMessage());

    }   finally{

      try{

      if(ps != null)

      ps.close();

      if(con != null)

      con.close();

      }catch(Exception ne){

            wdComponentAPI.getMessageManager().reportSuccess(ne.getMessage());

      }

    }   

Display Records from Java Dictionary Table  

In order to display the records from dictionary table TMP_EMPLOYEE on th click of button ‘Display Records’, following code snippet needs to be added to onActionDisplayRecords().

Connection con = null;

Statement st = null;

try{

   InitialContext ctx = new InitialContext();

   DataSource ds = (DataSource) ctx.lookup(“jdbc/DSSAP”);

   con = ds.getConnection();

   st = con.createStatement();

ResultSet rs = st.executeQuery(“Select EMPID,NAME from TMP_EMPLOYEE”);

  1. wdComponentAPI.getMessageManager().reportSuccess(“!–Displaying Record–!”);

int indexofEmpID = rs.findColumn(“EMPID”);

int indexofName = rs.findColumn(“NAME”);

  1. wdComponentAPI.getMessageManager().reportSuccess(“ID Index:”+“”+indexofEmpID+“Name Index:”+“”+indexofName);

while(rs.next()){

String str1 = rs.getString(“EMPID”);

            String str2 = rs.getString(“NAME”);

      wdComponentAPI.getMessageManager().reportSuccess(“Emp ID:”+“”+str1+“Name:”+“”+str2);

} }catch(NamingException ne){

      wdComponentAPI.getMessageManager().reportSuccess(ne.getMessage());

      }catch(SQLException se){

      wdComponentAPI.getMessageManager().reportSuccess(se.getMessage());

      }

      finally{

try{

if(st != null)

st.close();

if(con != null)

con.close();

}catch(Exception ne){ wdComponentAPI.getMessageManager().reportSuccess(ne.getMessage());

         }

         }

Delete Records from Java dictionary Table

In order to delete a record from dictionary table TMP_EMPLOYEE on the click of button ‘Delete Record’, following code snippet needs to be added to onActionDeleteRecord().  

    Connection con = null;

PreparedStatement ps = null;

try{

            InitialContext ctx = new InitialContext();

            DataSource ds = (DataSource) ctx.lookup(“jdbc/DSSAP”);

            con = ds.getConnection();

            String sql = “DELETE from TMP_EMPLOYEE WHERE EMPID=?”;

            ps = con.prepareStatement(sql);          

            ps.setString(1,wdContext.nodePerson().getPersonElementAt(wdContext.nodePerson().getLeadSelection()).getEMPID());

          

            int upd = ps.executeUpdate();

            if(upd ==1){

  1. wdContext.nodePerson().removeElement(wdContext.nodePerson().currentPersonElement());
  2. wdComponentAPI.getMessageManager().reportSuccess(“!—-Record Deleted—-!”);

}  

}catch(NamingException ne){

      wdComponentAPI.getMessageManager().reportSuccess(ne.getMessage());

      }catch(SQLException se){

      wdComponentAPI.getMessageManager().reportSuccess(se.getMessage());

      } finally{

      try{

      if(ps != null)

      ps.close();

      if(con != null)

      con.close();

      }catch(Exception ne){

      wdComponentAPI.getMessageManager().reportSuccess(ne.getMessage());

}}

Imports Statements

//@@begin imports

import java.awt.image.RescaleOp;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.sql.DataSource;

import com.sap.mypackage.wdp.IPrivateDictionaryUsage;

import com.sap.tc.webdynpro.progmodel.context.Context;

//@@end

Build, Deploy and run the application 

32.JPG

We can check the Tables in Portal By using this URL:

http://hostName:50000/webdynpro/dispatcher/sap.com/nwrig~tm~wd/TMApp

Thanks a lot !!!

Regards

Vijay Kalluri

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Anil Kumar
      Anil Kumar

      Nice document Vijay......Expecting a more blogs from you.. 🙂