Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Vaadin with Seam

Frederic Nauleau
1 decade ago Feb 02, 2010 2:46pm
Maxim Usoltsev
1 decade ago Feb 12, 2010 1:29pm

Hi !
We have seam (jpa + richfaces) application and we want to redesign some forms using vaadin to make them more usable.
So i started to try to use seam contexts and components with vaadin application.
Here is my experience (experimental):
Vaadin and seam works fine together (for seam vaadin is just another user's servlet)
but:
1. add vaadin servlet path (used in web.xml) to seam filter mapping

2. we need to manage transactions:
// in Application init code
getContext().addTransactionListener(new SeamTransactionListener());
and SeamTransactionListener is (based on seam code):

public class SeamTransactionListener implements TransactionListener {

	private static final long serialVersionUID = 1L;

	public void transactionEnd(Application application, Object transactionData) {
		// will unlock the conversation if any locked
		Manager.instance().unlockConversation();
		System.out.println("Transaction end");

		if (Init.instance().isTransactionManagementEnabled())
			commitOrRollback(); // we commit before destroying contexts, cos the
	}

	public void transactionStart(Application application, Object transactionData) {
		// TODO Auto-generated method stub
		System.out.println("Transaction start");
		if (Init.instance().isTransactionManagementEnabled())
			begin();
		if (application instanceof VApp) 
			((VApp) application).nextTransaction();
	}

	private void commitOrRollback() {
		try {
			if (Transaction.instance().isActive()) {
				try {
					Transaction.instance().commit();
				} catch (IllegalStateException e) {
					System.err
							.println("TX commit failed with illegal state exception. This may be "
									+ "because the tx timed out and was rolled back in the background.");
				}
			} else if (Transaction.instance().isRolledBackOrMarkedRollback()) {
				Transaction.instance().rollback();
			}

		} catch (Exception e) {
			throw new IllegalStateException("Could not commit transaction", e);
		}
	}

	private void begin() {
		try {
			if (!Transaction.instance().isActiveOrMarkedRollback()) {
				// log.debug("beginning transaction ");
				Transaction.instance().begin();
			}
		} catch (Exception e) {
			throw new IllegalStateException("Could not start transaction", e);
		}
	}

}

We use seam's conversations, so when i need to use some conversation-scope components from vaadin event listeners, i call this method to "attach" to some conversation:

public static void restoreConversation(String cid) {
			System.out.println("Restoring conversation " + cid);
			System.out.println("Existsing conversations : "
					+ ConversationEntries.instance().getConversationIds());
			
			if (Conversation.instance().isLongRunning() ) {
				if (Conversation.instance().getId().equals(cid)) {
					System.out.println("already here : " + cid);
					return;
				} else 
					Manager.instance().unlockConversation();
			}
			

			Map<String, Object> params = new HashMap<String, Object>();
			params.put(Manager.instance().getConversationIdParameter(), cid);
			ConversationPropagation.instance().restoreConversationId(params);
			Manager.instance().restoreConversation();
}

So all seam features (entityManager, identity, etc) works fine.
For example, user can login using vaadin form -

...
        private TextField userName;
	private TextField pwd;
.....
 Button b = new Button("Login");
        b.addListener(new Button.ClickListener() {
			
			public void buttonClick(ClickEvent event) {
				login();
			}
		});
....

protected void login() {
		Identity identity = Identity.instance();
		identity.setUsername(userName.getValue().toString());
		identity.setPassword(pwd.getValue().toString());
		
		identity.login();
		
		if (identity.isLoggedIn()) {
			getWindow().showNotification("Welcome, " + identity.getCredentials().getUsername());
			// do some user-specific sfuff here (load menu, show main form, etc)
		} else {
			getWindow().showNotification("Oops !",  Notification.TYPE_ERROR_MESSAGE);
		}
		
	}
...
Last updated on Feb, 12th 2010
Salvo Salvo
1 decade ago May 30, 2011 8:06am
Enrico Falappa
1 decade ago Jun 19, 2011 10:22pm
Enrico Falappa
1 decade ago Jun 22, 2011 5:38pm