Thursday, January 5, 2012

Sons of DHTMLX and Spring Series

Episode 1: Home
After a long Christmas break, traveling, reading, playing with kids and eating my favorite  Roberto's burritos (carne asada with the works: pico di gallo, guacamole, cheese and sour cream), it is time to return to the blog and give my best wishes of a happy New Year to  all readers.  

In the early days of the DHTMLX Java Tag library some developers of a large financial company asked me to give code examples with the Spring Framework. I have also received requests from blog readers to provide examples of using DHTMLX and Spring, so I am taking the opportunity to write a series (see also episode 2 , episode 3 and episode 4) of posts on the subject. 
I have dug up my old examples, updated them to run with Spring 3.1, DHTMLX 3.0 and Google App Engine and packaged in a simple web application. Part of the DHTMLX Spring Adapter code presented comes from an open source library that will be included in the upcoming release (sometime in 2012) of the DHTMLX Java Tag Library.


DHTMLX Server Side
As extension of the JavaScript component library DHTMLX delivers a free open source Java package (dhtmlxConnector) for server side integration.  The connector provides  all the functionality needed to integrate the major database servers. Below is a Java code sample to connect a dhtmlxGrid to a database table: 
  1. /**
  2.  * DHTMLX Grid Connector Example
  3.  */
  4. public class BasicConnector extends ConnectorServlet {
  5.    @Override
  6.    protected void configure() {
  7.       Connection conn= ( new DataBaseConnection()).getConnection();
  8.      
  9.       GridConnector c = new GridConnector(conn);
  10.       c.dynamic_loading(100);
  11.       c.render_table("table", "id", "field1,field2");
  12.    }
  13. }
With the dhtmlxConnector you can have a working dhtmlxGrid in minutes if you have direct access to the database; however, the design of the connector does not allow you to get the same result in a environment where the persistence layer is based on JDO/JPA.  

Spring MVC
In order to get the advantage of the many features that the framework offers, Spring developers adopt a well defined architecture. A typical basic Spring Web Application consists of a  Model, Controller, Service and Repository.
Book.java
  1. /**
  2.  * Model
  3.  */
  4. public class Book {
  5.     private Long id;
  6.     private String author;
  7.     private String title;
  8.     private Integer sales;
  9.     private Integer price;
  10. }
Controller.java
  1. /**
  2.  * Controller
  3.  */
  4. @Controller
  5. public class BookController {
  6.     @Autowired
  7.     private BookService bookService;
  8.     @RequestMapping(value = "/books", method = RequestMethod.GET)
  9.     public void getBooks() {
  10.         List<Book> books = bookService.getBooks();
  11.         ...
  12.     }
  13. }
BookService.java
  1. /**
  2.  * Service
  3.  */
  4. public interface BookService  {
  5.     List<Book> getBooks();
  6.     Book getBook(String id);
  7.     Book store(Book book);
  8. }
BookDao.java
  1. /**
  2.  * Repository
  3.  */
  4. public interface BookDao {
  5.     Book find(String id);
  6.     List<Book> findByTitle(String title);
  7.     Book store(Book book);
  8.     List<Book> findAll();
  9. }

Request Mapping
Spring provides a powerful and flexible Java annotation for request mapping. There are many schools on the request mapping design. The table below highlight the URL mapping of the example application presented in the series:

URL
GET
PUT
POST
DELETE
/books
List books
NA
Create/Update book
NA
/books/id
Get book (id)
NA
NA
NA

DHTMLX Spring Adapter
The sample application of the series illustrates an approach how to integrate a Grid and a Form component with the Spring Framework. Below is the code example for the Controller class using the DHTMLX Spring Adapter.
BookController.java
  1. /**
  2.  * DHTMLX Spring Adapter Example
  3.  */
  4. @Controller
  5. public class BookController {
  6.   @Autowired
  7.   private BookService bookService;
  8.   @RequestMapping(value = "/books", method = RequestMethod.GET)
  9.   public GridAdapter getBooks(@RequestParam("c") String c) {
  10.     GridAdapter adapter = new GridAdapter(c,Book.class);
  11.     adapter.setData( bookService.getBooks() );
  12.     return adapter;
  13.   }
  14.   @RequestMapping(value = "/books/{id}", method = RequestMethod.GET)
  15.   public FormAdapter getBook(@PathVariable("id") final String id) {
  16.     Book book = bookService.getBook(id);
  17.     FormAdapter adapter = new FormAdapter( book );
  18.     return adapter;
  19.   }
  20.   @RequestMapping(value = "/book", method = RequestMethod.POST)
  21.   public FormAdapter storeBook(@Valid @ModelAttribute Book book, ...) {
  22.     FormAdapter adapter = new FormAdapter(book,binding);
  23.     if( adapter.hasValidData() ) {
  24.       bookService.store(book);
  25.     }
  26.     return adapter;
  27.   }
  28. }
Looks familiar, huh?
The series will cover many aspects of the integration of Spring and DHTMLX trying to get the best of both worlds, including client and server site validation, MVC architecture, and URL mapping best practice.

In the upcoming episodes I will go deep in the code, illustrating  the design of the DHTMLX Spring Adapter. Below is the list of  Sons of DHTMLX and SPRING  episodes: 

Episode 1: Home
Introduction to the DHTMLX Spring Adapater (this post)

Episode 2: Caregiver
Take care of your objects with client and server side validation  (released 01/11/12)

Episode 3: Turning and Turning
Spring MVC annotation driven configuration (released 01/18/12)

Episode 4: Fruit for the Crows
Full Source code delivery (released 02/16/12)


Episode 5: Call of Duty 
Get involved and contribute to the open source development (released 02/22/2012)

Stay Tuned!

Monday, November 7, 2011

Moving Paypal API based Web Store in Java to the Google App Engine

A member of the development team nicknamed Zeo, living in south California sent me an email asking help to solve an issue. In his spare time he was migrating a web store application in Java/Spring 3.1 to the Google App Engine. One of the requirements was to integrate  Express Checkout API to streamline the checkout process for buyers of digital goods. PayPal X delivers an open source java toolkit for Google App Engine available on Google Code named paypalx-gae-toolkit, which provides support for the Adaptive Payments APIs.  Unfortunatly  using the Adaptive Payments API, PayPal redirect page is not designed for mobile, so it creates a bad user experience.  Googling the internet Zeo found an entry in  the x.commerce forum which talk about this issue.
Currently PayPal don't have a mobile version of Adaptive Checkout, we only provide a full web flow or the embedded checkout flow (lightbox). At the moment the only mobile web solution PayPal offers is Mobile Express Checkout. One alternative is to have the customers sign up to a preApproval, once they set that up you can charge their PayPal account without the need for a redirect to PayPal for each payment. The main issue again with  this is that there is no mobile friendly PreApproval flow.
In addition, after reviewing the source code developed by Zeo I realized that most of the application use the Paypal NVP HTTP API to interface the Express Checkout  and to adopt  the Paypalx GAE toolkit would require major changes to the application and would provide limited support for mobile devices.
The first issue Zeo faced, was an exception raised during the application start up.The problem was related to the xerces.jar included with the Paypal SDK. A solution was to remove the dependency changing the original code :


APICallerBase.java
  1. protected static void setupHandler(final String message, DefaultHandler handler)
  2.             throws SAXException, IOException {
  3.    ClassLoader cl = APICallerBase.class.getClassLoader();
  4.    URL url = cl.getResource(message);
  5.    SAXParserFactory factory = SAXParserFactory.newInstance();
  6.    try {
  7.        SAXParser saxParser = factory.newSAXParser();
  8.        XMLReader xr = saxParser.getXMLReader();
  9.        xr.setContentHandler(handler);
  10.        xr.parse(new InputSource(url.openStream()));
  11.    } catch (ParserConfigurationException e) {
  12.        e.printStackTrace();
  13.    }
  14. }

EndpointsReader.java
  1. public void startElement(String uri, String xname, String name, Attributes atts) {
  2.     /* Do not change the original code */
  3. }
  4. public void endElement(String uri, String xname, String name) {
  5.     /* Do not change the original code */
  6. }

Basically it replaces the xerces parser with the SAX Parser included with the Java SDK, which removes the dependency from the library delivered with the Paypal SDK.  One less library that needs to be  included in the application WEB-INF/lib.
The second issue related to the  Google app engine  “Black List” , in other words, Java APIs that did not work on GAE. In this case the issue was related to the Apache Http Client dependency, because of the NVPClientSocket class. .
Also in this case, a solution was to remove the dependency from the Apache Http Library (what a shame) and use the Google App Engine's URL fetch service.
Below is the code:

NVPAPICaller.java
  1. public final String call(String payload) throws PayPalException {
  2.    String nvpResponse = "";
  3.    URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
  4.    try {
  5.       HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST);
  6.       StringBuffer payloadData = new         StringBuffer(payload).append("&").append(header);
  7.    request.setPayload( payloadData.toString().getBytes() );
  8.    HTTPResponse response = fetcher.fetch(request);
  9.    byte[] content = response.getContent();
  10.    // 200, 404, 500, etc
  11.    int responseCode = response.getResponseCode();
  12.    if ( responseCode == HttpURLConnection.HTTP_OK) {
  13.        nvpResponse = new String(content);
  14.    } else {
  15.        throw new FatalException("HTTP Error code " + responseCode + " received, transaction not submitted");
  16.    }
  17.    } catch (IOException e) {
  18.        e.printStackTrace();
  19.    }
  20.    return nvpResponse;
  21. }

Another change, was to remove the NVPSSLSocketFactory.java and all the references to the org.apache.commons.httpclient.*  in the code. 
I packed the changes in a new paypal-base-gae.jar and sent to Zeo to test on the web store: it worked. I explained to Zeo this solution is a workaround, and does not pretend to be "the solution" to run the Paypal API on GAE. He promised me next time I will be in South California we will grab a burger at one of my favorite places Hodads.


Cheers !