Skip to Content

Introduction


WebDynpro (both Java and ABAP edition) provides great options for formatting majority of data types. However, certain applications have own types (or if you like domains in RDBMS world) and demand even more – phone numbers, SSN, credit card numbers etc. WebDynpro does not support these capabilities out of the box, but you may roll your own solution using calculated attributes.


Scenario


We have Phone Number data type from backend. Originally it is a field of type long. But we want to display it in user-friendly manner, something like (123)456-7890 for value 1234567890.


Solution


You have already an attribute in context node, say Phone (phone number with extension code) of type long.

    1. Create calculated attribute

PhoneFormatted of type string:

    1. create sub-node with cardinality 1..1 and selection 1..1 as direct child of aforementioned node;

create string attribute PhoneFormatted here of type string;

    1. select calculated in properties and WD generates getter / setter methods for attributes in controller.
    1. Add the following block to the custom coding area of controller:


//@@begin others
private MessageFormat fmtPhone = new MessageFormat
(
  “‘(‘{0,number,000}’)'{1,number,000}’-‘{2,number,0000}”
);
final private static Pattern PHONE = Pattern.compile
(
  “^
((
d)
)(
d
)-(
d)$”
);
//@@end


    1. Complete code for calculated attribute getter:


//@@begin javadoc:getPhoneFormatted(IPrivateRTFViewerCV.IContextElement)
/**

  1. Declared getter method for attribute PhoneFormatted of node <SubNodeName>

  1. @param element the element requested for the value

  1. @return the calculated value for attribute PhoneFormatted

*/
//@@end
public java.lang.String get


//@@begin javadoc:setPhoneFormatted(IPrivateRTFViewerCV.IContextElement, java.lang.String)
/**

  1. Declared setter method for attribute PhoneFormatted of node <SubNodeName>

  1. @param element the element to change the value

  1. @param value the new value for attribute PhoneFormatted

*/
//@@end
public void set


To report this post you need to login first.

2 Comments

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

    1. Valery Silaev Post author
      Maksim,

      Not actually the first 😉

      First one was obvious and used intuitively by many of developers: it is simple read-only calculated attribute that converts Boolean to WDVisibility, or negate Boolean, or convert some “state” (numeric value) to WDTextViewSemanticColor etc.

      Oh, you mean “mutator” part…

      Then first one was described in this tutorial: https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/webas/webdynpro/tutorial%20on%20yet%20another%20evs%20valuehelp%20-%20displaytexts%20for%20keys%20-%2036.htm

      So this is actually 2-nd application 😉

      VS

      (0) 

Leave a Reply