Wednesday, February 24, 2010

Smart Way of Doing Null Checks for JAXBElements

If want to Avoid using if(X!=null){a = x.toString}else{"NULL"}; many times in code, this blog is usefull to you.
I got a situation like : To track the data passed to a webservice, i need to create a large string which has values of the number of inputs passed to a webmethod.As all the input elements are JAXB elements and most of the input elements are optional, I must have to do a null check to each and every element before converting it to string.For this i created a small method as follows:
private String JAXBNullHandler(JAXBElement obj) { if (obj != null) { return String.valueOf(obj.getValue()); } else { return "NULL"; } }Wherever i need the string value of an element i used the method call as follows:
JAXBNullHandler(amount); where amount is a JAXBElement.
This has reduced a considerable amount of code and the code is legible.

Monday, February 8, 2010

Remember this before using Spring Context Loading

This week i faced a strange issue with spring context loading.
My aim is to persist all types of errors occuring in webservices. Hence i have created an helper class and injected the helper class in webservice Impl class as follows:private SaveExceptionHelper saveExceptionHelper;

String[] paths = { "/spring/service.xml"};
ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);
saveExceptionService = (SaveExceptionServiceImpl) ctx.getBean("saveExceptionService");

Also in service.xml i have put tag for SaveExceptionServiceImpl.


Upto this I didnt get any problem in build and deployment.
Later i have used a DAO which is having persitance method of hibernate.
and injected in service.xml under SaveExceptionServiceImpl.

I got an exception like

" abc.def.User class not found"

Where "User.class"" is created in some other project folder and imported in SaveExceptionServiceImpl.


Later i fixed it by using an interface to the helper class as saveExceptionService = (SaveExceptionService) ctx.getBean("saveExceptionService");
And i removed the import of ServiceImpl class.
Moral of the story is:
  1. The Importance of using Interfaces in Spring context initialization.
  2. The helper classes which has implemenation should not use resources from other projects. Incase, it is using other resources, we need an interface for that class and inject the interface in webservice class. Impl class in context loader xml(service.xml).

Thursday, January 21, 2010

Error Occurs in saving List of beans and Displaying in table

I got a task as follows:
  1. A jsp page with a like of Item Details populated in a table.
  2. User will modify any of the data in any of the column.
  3. After submitting the jsp the updated details needs to be saved in database
The scenario seems to be simple. But the problem is: If we iterate the list in a property of the form,
we are able to display in screen. But the List is set to null if we submit the same form again.
The list iteration is like below:

I have searched in net for the solutions. But I couldn't find any thing. Hence I have to go for a hard solution as below:
  1. Once after the list is displayed in Jsp, in the Onclick of Submit button, I have called a javascript:function.
  2. The list of data displayed is retrieved using the javascript by browsing through the tags inside the jsp.
  3. The retrieved data is converted into a string buffer with "," appended for each column values and ";" appended for each rows.
  4. After creating the string buffer, It is set to a variable in a form( say hiddedDataList).
  5. Finally, the variable with the string is set as hidden variable using html:hidden.
  6. We can again tokenize the string from the variable and create a list and save in the same list variable of form.
The sample code is as follow:

Monday, January 18, 2010

Persisting a BLOB in hibernate mapping

I have faced an error in saving a
record in a table
"ORA-01465: invalid hex number" While using BLOB in hibernate:

I have to convert a data object to a BLOB and save it in a data base.
I was facing an error called "ORA-01465: invalid hex number " whenever i tried to save the data row with the blob data in one of the column.

Error created code piece.:


try {
ByteArrayOutputStream ba = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(ba);
os.writeObject(object);
os.flush();
try {
message = oracle.sql.BLOB.createTemporary(null, false, oracle.sql.BLOB.DURATION_SESSION);
message.setBytes(1, ba.toByteArray());
System.out.println("This is the Blob message " + message);
} catch (SQLException e) {
LOG.error("Error while converting Byte array to BLOB ", e);
}



os.close();
} catch (IOException ioe) {
// ioe.printStackTrace();

}
}

Later I found in one of the site that there is a class called BlobImpl in Hibernate. I used following code to resolve the issue.
Solution:
try {
ByteArrayOutputStream ba = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(ba);
os.writeObject(object);
os.flush();
// message = oracle.sql.BLOB.empty_lob();
// message.setBytes(1, ba.toByteArray());
BlobImpl blobImpl = new BlobImpl( ba.toByteArray() );
message = blobImpl;
// System.out.println("This is the Blob message " + message);



os.close();
} catch (IOException ioe) {
// ioe.printStackTrace();

}