Monday, February 6, 2012

Integrating with PayPal

PayPal is one of the largest online payment processors in the world.

It serves as a digital wallet which allows to purchase online in a secured manner. The PayPal System stores the User details and provides the flexibility to purchase by just using the PayPal Account Id .

To use PayPal,the user should create a PayPal account and provide information about the modes which they would like to use for payment like Credit Card ,Bank account etc. The preference order can be configured for the Payment Options. If the user has insufficient fund in their preferred option then the next available option would be used to complete the transaction.

The PayPal services can be integrated with the web applications by using either HTML or using API integration. PayPal SDK is another option to integrate with API.

Sandbox is an isolated test environment provided by PayPal to test its functionality. This is claimed to be an identical copy of PayPal Live Site and it is recommended verify all the PayPal calls in the Sandbox before deploying in the Live environment.

Its easy to get lost in the vast documentation of PayPal ,I feel it could have been organized better. The developer community and forums are better options to seek to sort out issues.

Friday, July 22, 2011

Incorrect Order of SQL execution within a Spring Hibernate Transaction

Recently I faced a strange issue when trying to insert proper data into table ,it was halting with Unique key Constraint Exception.In a transaction I had to read a record and update the Unique Key fields and in the next step insert a record .The record inserted should have the same Unique Key values as the record read from the table.The sequence was a simple read,update and insert a new record.Was baffled when this sequence continuous threw a Unique Key Constraint.When I removed the UK constraint from the table,the records were updated and inserted as required.

On checking on the hibernate log found that the insert was fired before the update .I tried to disable the Spring transaction and then the order of execution was correct.
Looks like the order of SQl execution is not guaranteed within a transaction.To solve this issue one of the following needs to be done after the update and before the insert
1.call hibernateTemplate.flush() or
2.call hibernateTemplate.refresh() or
3.again read the record from table ,this internally calls the flush.

Friday, April 29, 2011

Upgrading to Spring MVC 2.5

I was trying to get familiarised with Spring MVC 2.5.I've used MVC 2.0 in the past and was counting to levarage on the experience.Gettings hands on I realised that 2.5 is a lot different from 2.0.The following are the major differences based on my understanding

Controllers:
Spring 2.5 has introduced Annotation based Controllers.Apart from the native controllers like AbstractController, SimpleFormController or MultiActionController, the DispatcherServlet can now handle any kind of action type.The controllers need not extend or implement MVC related classes or interface.A simple java class can serve as a controller by just adding the @Controller sterotype.

The annotated controllers are identified by specifying the context:component-scan in application context file.This specifies the package of the any annotated components that should be searched.

The methods that specify the mapping should be annotated with the @RequestMapping contruct .This maps the web request and specifies the type of method.The handler method signature are flexible and within a controller each mapping method can have different signature.The mapping methods allows to return nothing (void), a String representing a view, or just a model.By convention, if method does not explicitly return a view, the view name is defined by converting the mapped URL into a view name.

@ModelAttribute:
The methods marked with @ModelAttribute are invoked before every request.This can be used prepopulate the data required by the form.The functionality is similiar to the FormBackingObject in SimpleFormController.A controller can have any number of ModelAttribute methods.In most cases one method to populate the required data should suffice.


Validation and Binding Errors:
In Spring 2.0,injecting the validator bean would handle the Validation.But in 2.5,the validator needs to be injected and should be invoked manually.The BindingResult can be used to store the request data binding errors .If a method uses multiple objects a separate BindingResult instance can be defined for each object

Property Editor:
The Property editor can be registered using the method annotated with @InitBinder contruct.This is similiar to the initBinder() method .

The sample code has a simple form that has option in English/French to enter the data.The source code can be downloaded here.

Saturday, February 26, 2011

Reading data from Hashmap using JSTL

The requirement was to retrieve date based on the key from a HashMap and to display it in the UI.This is a common scenario when using the foreign key data.Initially seemed quite straightforward to create a HashMap to store the key and Values.In the JSP front ,the data can be retrieved either by
(i) looping the contents ( construct ) to identify the match or
(ii) retrieve the data from HashMap using the key( map["key"]).
I preferred the later approach since it was more performance effective.

Map map = new HashMap();
map.put(1, "Monday");
map.put(2, "Tuesday");
map.put(3, "Wednesday");

This was not rendering the data in the page.But when looping the contents the match was found and displayed in the page.

By modifying the key type in HashMap to String the values were retrieved and displayed in the Page.
Map map = new HashMap();
map.put("1", "Monday");
map.put("2", "Tuesday");
map.put("3", "Wednesday");

My inference from this is that EL does not support the Interger wrapper class for HashMap.But I could not find any documentation to confirm this.

Saturday, January 15, 2011

NullPointerException when creating Spring Bean

If the class name of the Bean is missed (for some reason )in the context file,it gives rise to NullPointerException by the factory.The stack trace was like
Caused by: java.lang.NullPointerException
at java.util.concurrent.ConcurrentHashMap.hash(ConcurrentHashMap.java:157)
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:730)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:186)

Saturday, December 11, 2010

OptimisticLockingFailureException Attempt to update step execution id=1 with wrong version (n), where current version is n+1

My Spring batch application was intended to run in Single thread env and concurrent updates were not in its scope.But one of the Jobs ,kept encountering OptimisticLockingFailureException:Attempt to update step execution id=1 with wrong version (1), where current version is 2
On analyzing the Spring Logs found that this was generated by the MapStepExecutionDao.I am using MapJobRepositoryFactoryBean with ResourcelessTransactionManager which is NOT suppose to persist the data .
After testing with few scenarioes found that the exception was thrown only when the batch operation tries to insert duplicate records to the table with constraints.After removing the duplicate data the batch execution was successful.
In this case OptimisticLockingFailureException was misleading ,a more appropriate DB Exception would have be helpful identify the cause.

Friday, October 22, 2010

Constant Specific methods in Enumeration

Using Java 5 Enums,it is possible to define different behaviours for a method based on the value.This is known as Constant Specific Methods.The methods that requires different behavior to be declared as abstract and it should be overriden in each Enum Value.

public enum Game {
CHESS {
@Override
boolean isIndoor() {
return true;
}
},BADMINTON {
@Override
boolean isIndoor() {
return false;
}
},CHINESE_CHECKER {
@Override
boolean isIndoor() {
return true;
}
};

public abstract boolean isIndoor();
}