Skip to Content

In this blog, I want to explain you how you can display dynamic table content and table layout from database table with Web Dynpro Java. First,  we’ll read the table names and fill into the combobox; then, we’ll show the table content of the selected table name from combobox.  Lets start by creating Web Dynpro Java project:

Create a webdynpro java project and create component named “Tabviewcomp”.

In TabViewComp, create the following nodes:

  • Empty node “TableContent” for table content
  • “TableLayout” node with attributes: DataLenght, FieldName,  FieldType
  • “Tables” node with attributes: Description , TableName

The result should look like in the following figure:

Now, let’s read the table names from the database and add into node “Tables”  .

We can read the table names from portal database using the code below:

try {
        java.sql.ResultSet rstab = dbconn.getMetaData().getTables(null, JNDI_NAME, "%", new String[] {"TABLE"});
          while(rstab.next()){
              wdContext.nodeTables().createAndAddTablesElement();
              wdContext.nodeTables().moveLast();
              wdContext.currentTablesElement().setTableName(rstab.getString("TABLE_NAME"));
          }
          wdContext.nodeTables().setLeadSelection(-1);
      } catch (Exception e) {
        wdControllerAPI.getMessageManager().reportException("An error occured while reading control tables.");
        wdControllerAPI.getMessageManager().reportException(e);
      }

In the component view, add “ DropDownByIndex element” and bind as texts  node attribute “Tables.TableName”,  and add “Table element” and bind datasource “TableContent” node ,  and finally add “TableColumnGroup” to the table.  The result should look like in the following figure:

Create an action “ReadTableContent” for the on select action of DropDownByIndex. In this action read table fields and content  from database as selected value. To get table field names and context create a method “readTableContent” in component controller with parameter “java.lang.String tableName “  like below:

publicvoid readTableContent( java.lang.String tableName ) {
    //@@begin readTableContent()
      // Initialize context data
        wdContext.nodeTableContent().invalidate();
        wdContext.nodeTableLayout().invalidate();
        // Remove all attributes from TableLayout node
        // which are representing old selected tables fields
        Iterator<? extends IWDAttributeInfo> it = wdContext.nodeTableContent().getNodeInfo().iterateAttributes();
        while(it.hasNext()){
              IWDAttributeInfo att = it.next();
              att.remove();
        }
        try {
              stmt = dbconn.prepareStatement("select * from " + tableName);
              java.sql.ResultSet rs = stmt.executeQuery();
              java.sql.ResultSetMetaData md = rs.getMetaData();
              for(int i = 1; i <= md.getColumnCount(); i++) {
              int j = wdContext.nodeTableLayout().createAndAddTableLayoutElement().index();
              String fname = md.getColumnName(i);
              String dtype = md.getColumnTypeName(i);
              wdContext.nodeTableLayout().getTableLayoutElementAt(j).setFieldName(fname);
              wdContext.nodeTableLayout().getTableLayoutElementAt(j).setFieldType(dtype);
              wdContext.nodeTableLayout().getTableLayoutElementAt(j).setDataLength("10");
              wdContext.nodeTableContent().getNodeInfo().addAttribute(fname, wdThis.getContextType(dtype));
        }
          while(rs.next()){
              int j = wdContext.nodeTableContent().createAndAddTableContentElement().index();
              for(int i = 0; i < wdContext.nodeTableLayout().size(); i++) {
                    String attributeName = wdContext.nodeTableLayout().getTableLayoutElementAt(i).getFieldName();
                    if(wdContext.nodeTableLayout().getTableLayoutElementAt(i).getFieldType().equals("INTEGER")) {
                          wdContext.nodeTableContent().getTableContentElementAt(j).setAttributeValue(attributeName, rs.getInt(attributeName));
                    } else if(wdContext.nodeTableLayout().getTableLayoutElementAt(i).getFieldType().equals("SMALLINT")) {
                          wdContext.nodeTableContent().getTableContentElementAt(j).setAttributeValue(attributeName, rs.getInt(attributeName));
                    } else if(wdContext.nodeTableLayout().getTableLayoutElementAt(i).getFieldType().equals("BIGINT")) {
                          wdContext.nodeTableContent().getTableContentElementAt(j).setAttributeValue(attributeName, rs.getBigDecimal(attributeName).toString());
                    }else {
                          wdContext.nodeTableContent().getTableContentElementAt(j).setAttributeValue(attributeName, rs.getString(attributeName));
                    }
              }
          }
      } catch (Exception e) {
        wdControllerAPI.getMessageManager().reportException("An error occured while reading control tables.");
        wdControllerAPI.getMessageManager().reportException(e);
      }
    //@@end
  }

In ”ReadTableContent” action, remove all attributes from TableContent node and call method “readTableContent” from component controller using the following code:

publicvoid onActionReadTableContent(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
 {
    //@@begin onActionReadTableContent(ServerEvent)
        if(wdContext.nodeTables().getLeadSelection() >= 0) {
              // Initialize context data
              wdContext.nodeTableContent().invalidate();
              wdContext.nodeTableLayout().invalidate();
              // Remove all attributes from TableLayout node
              // which are representing old selected tables fields
              Iterator<? extends IWDAttributeInfo> it = wdContext.nodeTableContent().getNodeInfo().iterateAttributes();
              while(it.hasNext()){
                    IWDAttributeInfo att = it.next();
                    att.remove();
              }
              String selectedVal = wdContext.nodeTables().getTablesElementAt(wdContext.nodeTables().getLeadSelection()).getTableName();
              wdThis.wdGetTabViewCompController().readTableContent(selectedVal);
        } else {
              // Initialize context data
              wdContext.nodeTableContent().invalidate();
              wdContext.nodeTableLayout().invalidate();
              // Remove all attributes from TableLayout node
              // which are representing old selected tables fields
              Iterator<? extends IWDAttributeInfo> it = wdContext.nodeTableContent().getNodeInfo().iterateAttributes();
              while(it.hasNext()){
                    IWDAttributeInfo att = it.next();
                    att.remove();
              }
        }
    //@@end
  }

Then create Web Dynpro Application and run it. The result should look like:

To report this post you need to login first.

4 Comments

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

    1. Ömer Haziroglu Post author

      In the Jeff Gebo’s project you can only display the database tables created using Dictionary Perspective. In this blog I’ve tried to extend this project for all database tables and share the source code with the community. I’m also planning to extend this project to add table maintenance functionality.

      (0) 
  1. Sidharthan Durai

    Ömer,

    I have created a context node(contentNode) at design time and bound that node to a table in view.

    During run-time i create elements in the node(contentNode) but those element values are not reflected in the table


    My code in wdDoModifyView is


    wdContext.nodeContent().invalidate();

       wdContext.nodeContent().getNodeInfo().addAttribute(“S_NO”, “java:java.lang.String”);

       wdContext.nodeContent().getNodeInfo().addAttribute(“CONTENT”, “java:java.lang.String”);

       for(int i = 1; i <= 5; i++)

       {

      int j = wdContext.nodeContent().createAndAddContentElement().index();

      wdContext.nodeContent().getContentElementAt(j).setAttributeValue(“S_NO”, i+””);

      wdContext.nodeContent().getContentElementAt(j).setAttributeValue(“CONTENT”, i+””);

     

       }

    // to check whether elements created in node

       for(int i = 0; i < 5; i++)

       {

      IWDNodeElement el = wdContext.nodeContent().getContentElementAt(i);

      wdThis.wdGetDuiCompController().wdGetAPI().getMessageManager().reportException(el.getAttributeValue(“S_NO”)+” “+el.getAttributeValue(“CONTENT”));

      }

    Got the element values of elements reported as exception.

    Kindly help me.

    (0) 

Leave a Reply