[jboss-user] [JBoss Seam] - Re: EntityHome for nested entity CRUD?

kingcu do-not-reply at jboss.com
Fri May 25 12:08:18 EDT 2007


Thank you both for the help. Now, I got it to work by adding another @Role to the BusinessHome. But, I had to create a second page for creating the child (the page is almost identical to the page creating the parent, except for the different name used for the Home component), which is a little less than ideal: originally, I though I could do this with just one page and now I have to maintain two almost identical pages.

One other thing is that I have my BusinessHome with two different names, but both in CONVERSATION scope and seem to work fine, which seems to be against what is stated in the reference doc (section 3.2.9):
anonymous wrote : The @Role annotation lets us define an additional named role for a component, with a different scope Can someone please clarify?

Anyway, I'll post my code below and hopefully it will be helpful for others who may face the same issue. Please do let me know if there is any possibility of improvement. Thanks.

BusinessHome.java
@Name("businessHome")
  | @Scope(CONVERSATION)
  | @Role(name="childBusinessHome", scope=CONVERSATION)
  | public class BusinessHome extends EntityHome<Business> {
  | 	
  | 	private Long parentBusinessId;
  | 	
  | 	@Logger
  | 	Log log;
  | 
  | 	public Long getParentBusinessId() {
  | 		return parentBusinessId;
  | 	}
  | 
  | 	public void setParentBusinessId(Long parentBusinessId) {
  | 		this.parentBusinessId = parentBusinessId;
  | 	}
  | 
  | 	public void setBusinessId(Long id) {
  | 		setId(id);
  | 	}
  | 
  | 	public Long getBusinessId() {
  | 		return (Long) getId();
  | 	}
  | 
  | 	@Override
  | 	protected Business createInstance() {
  | 		Business business = new Business();
  | 		return business;
  | 	}
  | 
  | 	public void wire() {
  | 		if (parentBusinessId != null) {
  | 			EntityManager em = getEntityManager();
  | 			Business parentBusiness = em.find(Business.class, parentBusinessId);
  | 			getInstance().setParentBusiness(parentBusiness);
  | 		}
  | 	}
  | 
  | 	public boolean isWired() {
  | 		return true;
  | 	}
  | 
  | 	public Business getDefinedInstance() {
  | 		return isIdDefined() ? getInstance() : null;
  | 	}
  | 
  | }
  | 

BusinessEdit.xhtml
<ui:define name="body">
  |     
  |     <h:messages globalOnly="true" styleClass="message" id="globalMessages"/>
  | 
  |     <h:form id="business" styleClass="edit">
  |     
  |         <rich:panel>
  |             <f:facet name="header">Edit Business</f:facet>
  | 
  |             <s:decorate id="nameDecoration" template="layout/edit.xhtml">
  |                 <ui:define name="label">name</ui:define>
  |                 <h:inputTextarea id="name"
  |                                cols="80"
  |                                rows="3"
  |                               value="#{businessHome.instance.name}"/>
  |             </s:decorate>
  | 
  |             <s:decorate id="descriptionDecoration" template="layout/edit.xhtml">
  |                 <ui:define name="label">description</ui:define>
  |                 <h:inputTextarea id="description"
  |                                cols="80"
  |                                rows="3"
  |                               value="#{businessHome.instance.description}"/>
  |             </s:decorate>
  |         
  |             <div style="clear:both">
  |                 <span class="required">*</span> 
  |                 required fields
  |             </div>
  |         </rich:panel>
  |                 
  |         <div class="actionButtons">
  | 
  |             <h:commandButton id="save" 
  |                           value="Save" 
  |                          action="#{businessHome.persist}"
  |                        disabled="#{!businessHome.wired}"
  |                        rendered="#{!businessHome.managed}"/>  
  |                           			  
  |             <h:commandButton id="update" 
  |                           value="Update" 
  |                          action="#{businessHome.update}"
  |                        rendered="#{businessHome.managed}"/>
  |                         			  
  |             <h:commandButton id="delete" 
  |                           value="Delete" 
  |                          action="#{businessHome.remove}"
  |                        rendered="#{businessHome.managed}"/>
  |                     
  |             <s:button id="done" 
  |                    value="Done"
  |              propagation="end"
  |                     view="/BusinessSearch.xhtml"
  |                 rendered="#{businessHome.managed}"/>
  |                 
  |             <s:button id="cancel" 
  |                    value="Cancel"
  |              propagation="end"
  |                     view="/BusinessSearch.xhtml"
  |                 rendered="#{!businessHome.managed}"/>
  | 
  |         </div>
  |         
  |         <s:div styleClass="actionButtons">
  |             <s:button view="/ChildBusinessEdit.xhtml"
  |                         id="createChild" 
  |                      value="Create Child Business">
  |                 <f:param name="parentBusinessId" value="#{businessHome.instance.id}"/>
  |             </s:button>
  |         </s:div>
  |         
  |     </h:form>
  | 
  | </ui:define>
  | 

BusinessEdit.page.xml
<page no-conversation-view-id="/BusinessSearch.xhtml">
  |    
  |    <begin-conversation join="true"/>
  |    
  |    <action execute="#{businessHome.wire}"/>
  |    
  |    <param name="businessId" value="#{businessHome.businessId}"/>
  | 
  |    <navigation from-action="#{businessHome.persist}">
  |        <end-conversation/>
  |        <redirect view-id="/BusinessSearch.xhtml"/>
  |    </navigation>
  |    
  |    <navigation from-action="#{businessHome.update}">
  |        <end-conversation/>
  |        <redirect view-id="/BusinessSearch.xhtml"/>
  |    </navigation>
  |    
  |    <navigation from-action="#{businessHome.remove}">
  |        <end-conversation/>
  |        <redirect view-id="/BusinessSearch.xhtml"/>
  |    </navigation>
  |    
  | </page>

ChildBusinessEdit.xhtml
<ui:define name="body">
  |     
  |     <h:messages globalOnly="true" styleClass="message" id="globalMessages"/>
  | 
  |     <h:form id="business" styleClass="edit">
  |     
  |         <rich:panel>
  |             <f:facet name="header">Edit Business</f:facet>
  | 
  |             <s:decorate id="nameDecoration" template="layout/edit.xhtml">
  |                 <ui:define name="label">name</ui:define>
  |                 <h:inputTextarea id="name"
  |                                cols="80"
  |                                rows="3"
  |                               value="#{childBusinessHome.instance.name}"/>
  |             </s:decorate>
  | 
  |             <s:decorate id="descriptionDecoration" template="layout/edit.xhtml">
  |                 <ui:define name="label">description</ui:define>
  |                 <h:inputTextarea id="description"
  |                                cols="80"
  |                                rows="3"
  |                               value="#{childBusinessHome.instance.description}"/>
  |             </s:decorate>
  |         
  |             <div style="clear:both">
  |                 <span class="required">*</span> 
  |                 required fields
  |             </div>
  |         </rich:panel>
  |                 
  |         <div class="actionButtons">
  | 
  |             <h:commandButton id="save" 
  |                           value="Save" 
  |                          action="#{childBusinessHome.persist}">
  |                 <f:param name="businessId" value="#{childBusinessHome.instance.id}"/>
  |             </h:commandButton>
  |                           			  
  |             <s:button id="cancel" 
  |                    value="Cancel"
  |              propagation="end"
  |                     view="/BusinessEdit.xhtml"/>
  | 
  |         </div>
  |         
  |     </h:form>
  | 
  | </ui:define>
  | 

ChildBusinessEdit.page.xml
<page no-conversation-view-id="/BusinessEdit.xhtml">
  |    
  |    <begin-conversation join="true"/>
  |    
  |    <action execute="#{childBusinessHome.wire}"/>
  |    
  |    <param name="parentBusinessId" value="#{childBusinessHome.parentBusinessId}"/>
  | 
  |    <navigation from-action="#{childBusinessHome.persist}">
  |        <end-conversation/>
  |        <redirect view-id="/BusinessEdit.xhtml"/>
  |    </navigation>
  |    
  |    <navigation from-action="#{childBusinessHome.update}">
  |        <end-conversation/>
  |        <redirect view-id="/BusinessEdit.xhtml"/>
  |    </navigation>
  |    
  |    <navigation from-action="#{childBusinessHome.remove}">
  |        <end-conversation/>
  |        <redirect view-id="/BusinessEdit.xhtml"/>
  |    </navigation>
  |    
  | </page>

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4048728#4048728

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4048728



More information about the jboss-user mailing list