You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sk...@apache.org on 2008/02/26 09:58:15 UTC
svn commit: r631138 - in
/myfaces/orchestra/trunk/core15/src/test/java/org/apache/myfaces/orchestra/conversation:
PersistentBackingBean.java PersistentConversation.java
TestConversationPersistence.java
Author: skitching
Date: Tue Feb 26 00:58:11 2008
New Revision: 631138
URL: http://svn.apache.org/viewvc?rev=631138&view=rev
Log:
Rename bean for easier understanding.
Added:
myfaces/orchestra/trunk/core15/src/test/java/org/apache/myfaces/orchestra/conversation/PersistentBackingBean.java
- copied, changed from r630854, myfaces/orchestra/trunk/core15/src/test/java/org/apache/myfaces/orchestra/conversation/PersistentConversation.java
Removed:
myfaces/orchestra/trunk/core15/src/test/java/org/apache/myfaces/orchestra/conversation/PersistentConversation.java
Modified:
myfaces/orchestra/trunk/core15/src/test/java/org/apache/myfaces/orchestra/conversation/TestConversationPersistence.java
Copied: myfaces/orchestra/trunk/core15/src/test/java/org/apache/myfaces/orchestra/conversation/PersistentBackingBean.java (from r630854, myfaces/orchestra/trunk/core15/src/test/java/org/apache/myfaces/orchestra/conversation/PersistentConversation.java)
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core15/src/test/java/org/apache/myfaces/orchestra/conversation/PersistentBackingBean.java?p2=myfaces/orchestra/trunk/core15/src/test/java/org/apache/myfaces/orchestra/conversation/PersistentBackingBean.java&p1=myfaces/orchestra/trunk/core15/src/test/java/org/apache/myfaces/orchestra/conversation/PersistentConversation.java&r1=630854&r2=631138&rev=631138&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core15/src/test/java/org/apache/myfaces/orchestra/conversation/PersistentConversation.java (original)
+++ myfaces/orchestra/trunk/core15/src/test/java/org/apache/myfaces/orchestra/conversation/PersistentBackingBean.java Tue Feb 26 00:58:11 2008
@@ -25,15 +25,33 @@
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
-public class PersistentConversation implements ConversationBindingListener
+/**
+ * A backing bean for a view (ie a "view controller") that has an associated
+ * persistence context.
+ * <p>
+ * In real applications, the PersistenceContext is more likely to be on a
+ * DAO bean referenced from the backing bean than directly on the backing
+ * bean class. However that doesn't change the principles.
+ */
+public class PersistentBackingBean implements ConversationBindingListener
{
+ /**
+ * Inject a PersistenceContext that is associated with the conversation
+ * that this bean instance is in.
+ */
@PersistenceContext
private EntityManager entityManager;
+ /**
+ * A UserData instance read via the PersistenceContext object associated
+ * with this bean, ie one associated with the conversation that this bean
+ * is in.
+ */
private UserData restartedUser;
+
private UserData createdUser;
- public PersistentConversation()
+ public PersistentBackingBean()
{
}
@@ -50,11 +68,26 @@
return restartedUser;
}
+ /**
+ * Create a UserData object and commit it to the database.
+ * <p>
+ * Because this method is marked with the Transactional annotation, a
+ * commit will be performed when the method returns successfully
+ * (without exception), and a rollback will be performed when the
+ * method throws an exception.
+ */
@Transactional
public UserData createUser()
{
+ // Create an object that is not yet attached to a persistence context.
UserData userData = new UserData();
userData.setUsername("test");
+
+ // Attach the object to the persistence context. Note that nothing
+ // is written to the database at this point; that only happens when
+ // the persistence context is flushed and committed. Due to the
+ // Transactional annotation on this method, that happens when this
+ // method returns.
entityManager.persist(userData);
createdUser = userData;
@@ -67,14 +100,33 @@
return entityManager.find(UserData.class, id);
}
+ /**
+ * Invalidate the conversation in which this bean lives.
+ * <p>
+ * This means that the next attempt to dereference a proxy or
+ * EL expression will cause a new instance of this bean to
+ * be created. A new Conversation instance will be created to
+ * hold that bean, and a new PersistenceContext object will
+ * also be created (and then injected into this instance).
+ */
public void invalidate()
{
Conversation.getCurrentInstance().invalidate();
}
+ /**
+ * Invalidate the conversation in which this bean lives, and
+ * then create a new bean instance (in a new conversation) and
+ * copy some data from the old to the new instance.
+ * <p>
+ * Using invalidateAndRestart allows information to be communicated
+ * between one instance of this bean and its replacement.
+ */
public void invalidateAndRestart()
{
- PersistentConversation conv = (PersistentConversation) ConversationUtils.invalidateAndRestart(Conversation.getCurrentInstance());
+ PersistentBackingBean conv = (PersistentBackingBean)
+ ConversationUtils.invalidateAndRestart(
+ Conversation.getCurrentInstance());
conv.setRestartedUser(createdUser.getId());
}
Modified: myfaces/orchestra/trunk/core15/src/test/java/org/apache/myfaces/orchestra/conversation/TestConversationPersistence.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core15/src/test/java/org/apache/myfaces/orchestra/conversation/TestConversationPersistence.java?rev=631138&r1=631137&r2=631138&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core15/src/test/java/org/apache/myfaces/orchestra/conversation/TestConversationPersistence.java (original)
+++ myfaces/orchestra/trunk/core15/src/test/java/org/apache/myfaces/orchestra/conversation/TestConversationPersistence.java Tue Feb 26 00:58:11 2008
@@ -51,7 +51,7 @@
{
final String CONVERSATION_NAME = "persistentConversation";
- PersistentConversation conv = (PersistentConversation) applicationContext.getBean(CONVERSATION_NAME);
+ PersistentBackingBean conv = (PersistentBackingBean) applicationContext.getBean(CONVERSATION_NAME);
/* create and reread a user */
UserData user = conv.createUser();
@@ -72,7 +72,7 @@
{
final String CONVERSATION_NAME = "persistentConversation";
- PersistentConversation conv = (PersistentConversation) applicationContext.getBean(CONVERSATION_NAME);
+ PersistentBackingBean conv = (PersistentBackingBean) applicationContext.getBean(CONVERSATION_NAME);
UserData user = conv.createUser();