вторник, 16 декабря 2008 г.

  Just a nice image of my fiancee feeding ostriches ...
Posted by Picasa

суббота, 1 ноября 2008 г.

Seam 2.1.0GA and JBoss 5.0.0CR2

As seam and jboss are developed by the same team, I would expect them to be compatible. But in reality they are not =)

So if you download the versions I mentioned in the subject and try to generate a default project by seam-gen (including db access) then there wil be a surprise for you. Deploying this project to jboss won't actually work. When it wants to connect to the database it will suddenly go:
 EntityManagerFactory not found in JNDI 


Googling that string was quite painful, but after couple of hours I finally got a result!
Someone commented out something from jboss codebase and seam doesn't know about it =)

So if you're looking for a solution, you should be using another way of binding your entity manager, not jndi. To do that in j2ee enviroment (which i suppose you would have running jboss) you need to create a small helper class that will translate the entitymanager from jboss world to seam world:

@Stateless
@Name("entityManagerFactory")
public class EntityManagerFactoryHackBean implements EntityManagerFactoryHackLocal {

@PersistenceUnit(name="main")
EntityManagerFactory factory;

@Unwrap
public EntityManagerFactory getEntityMangagerFactory()
{
return factory;
}

}

see, having both stateless and name annotations it serves like a bridge between two worlds.

then you have to use it in your components.xml instead of jndi name:
<persistence:managed-persistence-context name="entityManager"
entity-manager-factory="#{entityManagerFactory}"
scope="conversation" auto-create="true" />

Thanks for this thread on jboss forum

There is another thread which has a solution without ugly hacks, but it doesn't work in j2ee env and a small amendment that should make it work even in j2ee env. Btw this second thread tells us that this should be fixed in a consequent release of jboss.

суббота, 4 октября 2008 г.

Display a message when GWT app is loading

Quick search through the internet revealed a way to achieve the subject:
developerlife and google groups

If you're to lazy to read those, here is an extremely short tutorial:

1. Place this div on your html (tip: you can generate your own loading gif here):
<div id="initialLoading">
<img src="images/ajax-loader.gif" alt="loading ..." />
</div>


2. In your app's entry point add this line to remove the div when the app is actually loaded:
DOM.removeChild(RootPanel.getBodyElement(), DOM.getElementById("initialLoading"));


that's all

воскресенье, 28 сентября 2008 г.

Adding custom styles to odd/even rows in a GWT table

For the sake of better UI we decided to implement a table like gmail one that has differnt colors for even/odd rows. This seems to be quite a simple task, but I had to try several approaches before I managed to implement it.

1. Use custom RowFormatter 
RowFormatter interface defines a getElement method which is supposed to be called for every row. I believed that if you override it you can attach special style to a row object based on it's number.
Wrong guess =)
The specific table implementation - FixedWidthGrid - I was using from the gwt incubator project is using some custom/native logic inside prepareCell method so it doesn't call the getElement method =(

2. Directly override prepareCell method - doesn't seem correct, cuz would be called much more often then you need it

3. Finally I got the idea that I'm not able to find any OO way of introducing this functionality and went with a brute force approach: implement a method that applies some special formatting to all the rows and call it every time the table content is updated. 

Now I faced a new problem: there is no explicit "onContentUpdate" callback method I could use.
The only way my application now allows the content to be updated is by sorting it, so I implemented an appropriate callback. Here is the final source code packed into a class:
    public static class StripedTable extends FixedWidthGrid {
private static final String EVEN_STYLE = "even_row";
private static final String ODD_STYLE = "odd_row";

public StripedTable() {
super();
addSortableColumnsListener(new SortableColumnsListener() {
public void onColumnSorted(TableModel.ColumnSortList sortList) {
applyStripes();
}
});
}

public StripedTable(int rows, int columns) {
this();
resize(rows, columns);
}

private void applyStripes() {
for (int i = 0; i < getRowCount(); i++) {
this.getRowFormatter().setStyleName(i, getRowStyle(i));
}
}

private static String getRowStyle(int row) {
return row % 2 == 0 ? EVEN_STYLE : ODD_STYLE;
}
}

hibernate4gwt and processing all class fields

Today I was stuck working on my project which employed both hibernate and GWT technologies and thus also needed to use hibernate4gwt lib.

This is a great library that makes it possible to integrate two other great framewroks into one project. But as usual it has some caveats =)

It worked great for my model classes passing them from server side to JS client side. With one exception. All model object's ids were dropped. At first I thought this might have to do with @Id or @GeneratedValue annotations ... but that even sounds silly - why would you expect a cloning lib to drop a plain simple Long value because it has an @Id annotation? =)

Half an hour and 15Mb source code downloads later I found out the real cause. The library that hibernate4gwt uses for actual cloning of hibernate-enchanced objects to POJO is called beanlib (specifically beanlib-hibernate) and deep inside, in a method called net.sf.beanlib.provider.BeanPopulator#populate it uses all defined setters on a given class to populate it. Apparently I haven't defined a setter for id field - which caused the beanutil lib to drop it when converting to POJO.

So the moral of this story is: always create both getters and setters for all you fields on a class that you expect to pass to some 3rd party bean handling libs =)