Additional Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Likes
2,484

     &nbspWeb Dynpro is the SAP NetWeaver programming model for user interfaces and provides support when developing the Web representation of business applications.



WebDynpro makes designing user interfaces simpler and neat by providing a excellent framework with lots of standard UI elements built in. It has its own advantages and disadvantages. Every concept, programming model, technology takes time to mature. It grows and enhances itself with time. Until their time comes, we have to tweak them now and then to suit our requirements exactly. The sooner we realize it, the better we understand, accept and embrace it.





WebDynpro is no exception to this. UI elements in WebDynpro are a bit restrictive. So we have to go out and tweak them until it matures! “Why should I tweak this when I have other programming models and languages to depend on.” Is a question that’s sure to come up in this context? I guess it depends a lot on your comfort level with a specific technology.. More a matter of flavor, your requirements and the freedom – restrictions you need in designing your applications, considering that there are languages, software engineering concepts listed for every letter in English alphabets. !




This weblog is for table UI element which is good in its own way, but is one UI element perhaps next to Input Field which is expected to do a lot more by developers than what it’s currently capable off. !



Quite a lot of times I come across WebDynpro forum, where people want the table to be available in excel format or a printable format! Why can’t we do it ourselves instead of waiting for the creator’s to do it! Isn’t this how the “coffee bean” language became more powerful than its creator ever expected it to become?


Ok! Here we go! Given below is the table utility class which can be used on the go without worrying much about the implementation. Just set in the needed parameters and this will do the work for you!





Features:



1. Automatic total calculation for particular columns in a table



2. Automatic download to Excel



3. Print Table



4. Generate Business Graphics for Table Data






Using the Table Utility



The utility file which needs be created is “TableUtilities.java”. Create a file with name “TableUtilities.java” and paste the code provided at the end of this weblog. Save the file and paste in some folder in src/packages.









Utility Class Description




The utility class has a constructor which comes in two forms.



1. TableUtilities (IWDNode tablenode)



2. TableUtilities (IWDNode tablenode,IWDView view,IWDTransparentContainer tc)




The second constructor is primarily used when you need the UI element also to be created for displaying the total of a column.




Note: Text view will be created inside the container passed as a argument and the total displayed in the text view.






Table Node structure














Configuration and Usage





Creating a variable of type of table utility








Creating a value attribute say ‘tableutility” , go to properties of the attribute and click on type.

Select Java Native Type and search for TableUtilities. Select the same and save the metadata.








Total Calculation






To calculate total





wdContext.currentContextElement().getTableutility().CalculateSum(nodename,attribute name);





here Inside wdDoModifyView





if(firstTime)
{




IWDTextView tv = (IWDTextView)view.getElement("total");




wdContext.currentContextElement().setTableutility(new TableUtilities(wdContext.nodeDepartments()));




//Returns attribute info of a dynamically created
attribute which can be bound to the UI element neeed.




tv.bindText(wdContext.currentContextElement().getTableutility().CalculateSum("Departments","NoOfPeople"));




}




Now the total will be calculated and displayed in the text view we have mentioned. Total will be calculated dynamically and hence every time a change occurs in the values the corresponding changes will be reflected in the total.








!https://weblogs.sdn.sap.com/weblogs/images/39842/totalcalc.JPG|height=183|alt=image|width=326|src=ht...!









Creating an Excel File






For downloading a table to excel 2003,





Map m = wdContext.currentContextElement().getTableutility().DownloadToExcel(columninfos);




Column infos is a arraylist of column names.






ArrayList al = new ArrayList();



al.add(IPrivateSDNUtilityView.IDepartmentsElement.DEPARTMENTNAME);



al.add(IPrivateSDNUtilityView.IDepartmentsElement.NO_OF_PEOPLE);





This returns a map which has the following


“data” ,”url” , “error”.






“data” returns the binary data which can be mapped to a filedownload ui element.




“url” gives a url from which you can access the excel file.
“error” returns error if any.






Thus we can create a link to Url element(“url”) or a filedownload element (“data”)and map it to this value to get the excel file directly.






Ex :






wdContext.currentContextElement().setExcelfile((String)wdContext.currentContextElement().getTableutility().DownloadToExcel(al).get("url"));









This idea has been with help from Bertram Ganz's article Exporting Context Data into Excel






A few changes have been added. Instead of Map, ArrayList has been used as the order in which map elements are stored is random. This might result in change in the order of columns in the excel table.








Printing a Table







Print Table renders the WebDynpro table data in HTML and provides us an option to print its contents.




For obtaining a table in printable format




public Map printTable(ArrayList columnInfos,String tablesummary,String tableheading)




Pass an arraylist with the name of the column attributes used i.e the id of the attributes with the table heading and summary if any.







ArrayList al = new ArrayList();



al.add(IPrivateSDNUtilityView.IDepartmentsElement.DEPARTMENTNAME);




al.add(IPrivateSDNUtilityView.IDepartmentsElement.NO_OF_PEOPLE);







This also returns a map which has the following





“data” ,”url” , “error”.





“data” returns the binary data which can be mapped to a filedownload ui element.




“url” gives a url from which you can access the excel file.




“error” returns error if any.







wdContext.currentContextElement().setPrinttable((String)wdContext.currentContextElement().getTableutility().printTable(al,"Summary","SDN Print Demo Table").get("url"));

















Business Graphics







Business Graphics for the relevant table data can be created using this utility.




public void CreateGraphics(ArrayList columns,IWDView view,IWDTransparentContainer tc,String categoryattribute,WDBusinessGraphicsType bgtype,WDBusinessGraphicsDimension dim,String IGSUrl) throws Exception




Pass an arraylist with the name of the column attributes for which the graph has to be generated.






ArrayList al = new ArrayList();



al.add(IPrivateSDNUtilityView.IDepartmentsElement.NO_OF_PEOPLE);




wdContext.currentContextElement().getTableutility().CreateGraphics(al,view,tc,IPrivateSDNUtilityView.IDepartmentsElement.CATEGORY,WDBusinessGraphicsType.COLUMNS,WDBusinessGraphicsDimension.PSEUDO_THREE,null);







In case default values are to be used, graphics type, graphics dimension, IGSUrl can be set to null.










A point to note is that we can make use of the same array list used in Excel for all these utilities to perform the required function.




Code for Table Utility






/*

  • Created on May 1, 2006

*

  • To change the template for this generated file go to

  • Window>Preferences>Java>Code Generation>Code and Comments

  • @Author  : Bharathwaj R

  • @Company : Satyam Computer Services Ltd.,

*

*/

package bh;

import java.io.UnsupportedEncodingException;

import java.sql.Array;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.StringTokenizer;

import com.sap.tc.webdynpro.clientserver.uielib.graphics.api.IWDBusinessGraphics;

import com.sap.tc.webdynpro.clientserver.uielib.graphics.api.IWDCategory;

import com.sap.tc.webdynpro.clientserver.uielib.graphics.api.IWDSimpleSeries;

import com.sap.tc.webdynpro.clientserver.uielib.graphics.api.WDBusinessGraphicsDimension;

import com.sap.tc.webdynpro.clientserver.uielib.graphics.api.WDBusinessGraphicsType;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDLabel;

import com.sap.tc.webdynpro.progmodel.api.IWDAttributeInfo;

import com.sap.tc.webdynpro.progmodel.api.IWDCalculatedAttributeAccessor;

import com.sap.tc.webdynpro.progmodel.api.IWDNode;

import com.sap.tc.webdynpro.progmodel.api.IWDNodeInfo;

import com.sap.tc.webdynpro.progmodel.api.IWDView;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTable;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTransparentContainer;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDInputField;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDLabel;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTab;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTransparentContainer;

import com.sap.tc.webdynpro.progmodel.api.IWDAttributeInfo;

import com.sap.tc.webdynpro.progmodel.api.IWDCalculatedAttributeAccessor;

import com.sap.tc.webdynpro.progmodel.api.IWDNode;

import com.sap.tc.webdynpro.progmodel.api.IWDNodeElement;

import com.sap.tc.webdynpro.progmodel.api.IWDNodeInfo;

import com.sap.tc.webdynpro.progmodel.repository.enum.WDComponentLifecycleEnumInfo;

import com.sap.tc.webdynpro.services.sal.url.api.IWDCachedWebResource;

import com.sap.tc.webdynpro.services.sal.url.api.WDURLException;

import com.sap.tc.webdynpro.services.sal.url.api.WDWebResource;

import com.sap.tc.webdynpro.services.sal.url.api.WDWebResourceType;

/**

  • @author  Bharathwaj

  • @version 1.0

  • To change the template for this generated type comment go to

  • Window>Preferences>Java>Code Generation>Code and Comments

*/

public final class TableUtilities {

     private IWDNode wdTableNode = null;

     private IWDNodeInfo cninfo = null;

     private IWDAttributeInfo atrinfo = null;

     public TableUtilities(IWDNode tablenode)

     {

          wdTableNode = tablenode;

          cninfo = wdTableNode.getNodeInfo().addChild("UTotal",null,true,true,false,true,false,true,null,null,null);

          atrinfo = cninfo.addAttribute("GrandTotalAttr","ddic:com.sap.dictionary.string");

     }

     public TableUtilities(IWDNode tablenode,IWDView view,IWDTransparentContainer tc)

     {

          wdTableNode = tablenode;

          cninfo = wdTableNode.getNodeInfo().addChild("UTotal",null,true,true,false,true,false,true,null,null,null);

          atrinfo = cninfo.addAttribute("GrandTotalAttr","ddic:com.sap.dictionary.string");

          IWDLabel l1 = (IWDLabel)view.createElement(IWDLabel.class,null);

          l1.setText("Grand Total");

          IWDInputField l2 = (IWDInputField)view.createElement(IWDInputField.class,null);

          l2.bindValue(atrinfo);

          tc.addChild(l1);

          tc.addChild(l2);

     }

     public IWDAttributeInfo CalculateSum( java.lang.String NodeName, java.lang.String AttributeName )

     {

       //@@begin CalculateSum()

       final String NodeName1 = NodeName;

       final String AttributeName1 = AttributeName;

 

       atrinfo.setCalculatedAttributeAccessor(new IWDCalculatedAttributeAccessor() {

       public Object get(IWDNodeElement element, IWDAttributeInfo info) {

            // TODO Auto-generated method stub

          float sum =0;

          int size = wdTableNode.size();

          for(int i=0;i<size;i++){

                      sum = Float.parseFloat(""wdTableNode.getElementAt(i).getAttributeValue(AttributeName1));

          }

          return new String(Float.toString(sum));

       }

     

       public void set(

            IWDNodeElement element,

            IWDAttributeInfo info,

            Object value) {

               // TODO -- Blank Implementation

       }

  });

      return atrinfo;

       //@@end

     }

     

     public Map DownloadToExcel(ArrayList columnInfos1)

     {

          byte[] b = null;

          String linktoFile = null;

          StringBuffer err = new StringBuffer();

          StringBuffer xml_file = new StringBuffer();

               

          int noofelem = wdTableNode.size();

               

          ArrayList columnInfos = trimHeaderTexts(columnInfos1);

          String nodename = wdTableNode.getNodeInfo().getName().trim();

          String _nodename = nodename.substring(0, 1).toUpperCase()+nodename.substring(1).toLowerCase();

          xml_file.append("

84 Comments