Announcement: You can find the guides for Commerce 7.5 and later on the new Elastic Path Documentation site. This Developer Center contains the guides for Commerce 6.13.0 through 7.4.1.Visit new site

This version of Elastic Path Commerce is no longer supported or maintained. To upgrade to the latest version, contact your Elastic Path representative.

How to configure a new object for conversion by DWR

How to configure a new object for conversion by DWR

When a remote method returns a Java object, DWR needs to know how to convert that Java object into a Javascript object that can be used in the browser. Similarly, when the browser sends a Javascript object as a parameter to a remote method call, DWR needs to know how to convert the Javascript object into a Java object.

There are two steps to configure a new object for conversion by DWR.

Step 1 - Declare the object in dwr.xml

The following snippet from dwr.xml shows how to declare a converter for the new object.

<convert converter="epBean" match="com.elasticpath.domain.Customer">
       <param name="include" value="uidPk, userId, email, firstName,
        lastName, creationDate, lastEditDate, dateOfBirth,
        encryptedPassword, addresses, orders"/commerce-legacy/>
</convert>

This specifies that Java objects of type com.elasticpath.domain.Customer will be converted by a converter with the bean id, "epBean." epBean is the bean id given to EpBeanConverter.java. Therefore, EpBeanConverter will be responsible for converting Customer objects. The parameter block declares which members of the Customer object will be available on the Javascript object.

Note: Important

DWR does not use reflection to read private fields, the getter methods are used to retrieve the values of members declared in the "<param name="include" ..." block. In the above example, getEmail() will be called on the Java object and the return value will be stored in the "email" field of the Javascript object. Only the results of calling the specified methods will be available on the Javascript object - no "code" will be converted and made available in Javascript.

Step 2 - Create or update a converter

The "converter" attribute of the <convert> block above specifies the bean name of the class that will actually perform the conversion. In most cases, the existing EpBeanConverter class can be used to convert your new class.

Using EpBeanConverter

When using EpBeanConverter to convert a new class, the converter needs to know how to get an instance of the new class during inbound conversion. If objects of the new class are typically retrieved from a singleton Service, add a new map entry in EpBeanConverter.java as shown below.

BEAN_SERVICE_MAPPING.put(Order.class,
  ELASTICPATH.getBean(ContextIdNames.ORDER_SERVICE));

The above example tells the converter that Order instances can be retrieved by calling getObject() on the order service.

If your bean does not have an identity of its own and any new instance will suffice, add a map entry to EpBeanConverter.java as shown below.

BEAN_CONTEXTID_MAPPING.put(OrderAddress.class,
  ContextIdNames.ORDER_ADDRESS);

In this case, the map entry tells the converter that instances of OrderAddress can be retrived by calling getBean() with the given bean name from the ContextIdNames constants.

Using a custom converter

In some cases, you may wish to perform special processing logic upon inbound or outbound conversion. For example, you may wish to use a simplified string representation of a Locale or Currency on the browser side. In this case, you can implement DWR's Converter interface and specify your custom converter as the converter to use in the <converter> block in dwr.xml (Your converter will need to be configured elsewhere as a Spring bean with an id that can be specified in the <converter> block).