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.
Select Development Component.
Click on Next Local Development->My Components. Click Next.
Provide name as ‘dictionary’, caption as ‘Dictionary DC’and type as ‘Dictionary’
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
Click on Finish.
Navigate to the Definition tab of Simple type.
Keep type as String and Maximum length as 10.
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’.
Provide Package. Click on Finish
Right click on Database Tables node of Local Dictionary.
Create Database Table TMP_EMPLOYEE
Click on Finish.
Define two table columns EMPID and NAME of respective simple types EMPID and NAME.
Make Dictionary type DC visible to other DCs
Expand DC Metadata node of the Dictionary DC. Right click on Public Parts
->New Public Part
Give the name pp_dictionary. Click on Next.
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.
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)
Right Click on Dictionary DC ->Development Component->Deploy
Create Web Dynpro DC and Define UI fields
File->New->Development Component Project ‘dictionary_wd’.
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.
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)
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
Click on Next
Click on Finish. Switch to Layout tab. Add following UI elements.
- 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’.
Right click on ear_dc->New->META-INF/data-source-aliases.xml
Provide Alias name as DSSAP
Right Click on ear_dc->Properties->Project References
Check checkboxes for ‘dictionary’ DC and ‘dictionary_wd’ DC. Click on OK.
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
Click on Application Resource
Select JDBC Data Source Alias
Click on Create
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());
- ps.executeUpdate();
- 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”);
- wdComponentAPI.getMessageManager().reportSuccess(“!–Displaying Record–!”);
int indexofEmpID = rs.findColumn(“EMPID”);
int indexofName = rs.findColumn(“NAME”);
- 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){
- wdContext.nodePerson().removeElement(wdContext.nodePerson().currentPersonElement());
- 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
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
Nice document Vijay......Expecting a more blogs from you.. 🙂