Bijection Issue using Factory and Event Observer approch
gewuerzgurke May 14, 2009 11:41 AMI've figured out strange behaviour (maybe a bug) using the event/observer pattern with DataModel and Factory.
I know the difference ideas behind @Factory and @Observer.
Using Seam 2.1.1. GA and Jboss 4.2.3 GA.
Componenent customerListAction raises an event when a customer from it's list is deleted
@Name("customerListAction")
@Scope(ScopeType.CONVERSATION)
@AutoCreate
public class CustomerListAction {
@Logger
Log log;
@DataModel
List<Customer> allCustomers;
@DataModelSelection("allCustomers")
@Out(required = false)
Customer selectedCustomer;
@Factory(value = "allCustomers")
public void findAllCustomers() {
allCustomers = customerList.getResultList();
}
@Observer( create=false, value = {
"org.domain.testseam.session.customer.CustomerListAction.delete",
"org.domain.testseam.session.customer.CustomerEditAction.save" })
public void onModelChange() {
log.debug("model changed");
Contexts.getConversationContext().set("allCustomers", null);
}
@RaiseEvent("org.domain.testseam.session.customer.CustomerListAction.delete")
public void delete() {
if (selectedCustomer != null) {
customerHome.setInstance(selectedCustomer);
customerHome.remove();
selectedCustomer = null;
}
}
}
Have a look on the onModelChange() method that observes the event. The following code doesn't work:
@Observer( create=false, value = {
"org.domain.testseam.session.customer.CustomerListAction.delete",
"org.domain.testseam.session.customer.CustomerEditAction.save" })
public void onModelChange() {
log.debug("model changed");
allCustomers = null
}
It seems that allCustomers is not the same object, maybe a problem with the scopes. In which context will an
observer method be invoked?
The xhtml file looks like this:
<rich:simpleTogglePanel opened="true">
<a:region>
<f:facet name="header">Kundenliste</f:facet>
<h:form id="customerSelectForm">
<rich:dataTable var="_customer" value="#{allCustomers}">
<f:facet name="header">
<rich:columnGroup>
<rich:column>
Id
</rich:column>
<rich:column>
Name
</rich:column>
<rich:column>
Aktion
</rich:column>
</rich:columnGroup>
</f:facet>
<rich:column>
#{_customer.id}
</rich:column>
<rich:column>
#{_customer.name}
</rich:column>
<rich:column>
<a:commandButton action="#{customerListAction.select}" value="Auswählen" reRender="customerSelectForm"/>
<a:commandButton action="#{customerListAction.delete}" value="Entfernen" reRender="customerSelectForm"/>
</rich:column>
</rich:dataTable>
</h:form>
</a:region>
</rich:simpleTogglePanel>
Using Contexts.getConversationContext().set("allCustomers", null); looks ugly, any ideas?