[jboss-user] [JBoss Seam] - Re: Avoiding the DRY Principle with beans

bfo81 do-not-reply at jboss.com
Thu Jul 13 05:54:09 EDT 2006


Of course, there are many ways to build such a CRUD stuff. But I think one shouldn't persist a new object right after creation, cause then you have an item in your database with empty fields. I mean objects should only be persisted after JSF validation and a click on "Save".

In addition to that, I regard a Delete button on the edit page as useful. And this one should not be shown to the user when he just created a new object. Just for existing ones.

My approach would be like that:

- A page with a list of all existing instances of an entity. Every entity gets an edit link, and at the bottom there's a create button.

- Both buttons lead to the edit page. On the edit page there are the fields belonging to that entity, a save button, a cancel button, and a delete button (not for newly created entities).


So that's what I suggest:

The abstract superclass (it should use generics like iradix suggested):

  | ...
  | 
  | private boolean new;
  | //true: Object has just bean created
  | //false: User is editing an existing object
  | 
  | public setNew(boolean new) {
  |     this.new = new;
  | }
  | 
  | public boolean getNew() {
  |     return new;
  | }
  | 
  | 
  | 
  | public String create() {
  |     setNew(true);
  |     setObject(new T());
  |     return "editPage";
  | }
  | 
  | 
  | public String edit() {
  |     setNew(false);
  |     setObject((T) entityManager.merge(theList.getSelection()));   //how to get the entity from the previous page's list is discussed in another topic - so that's something to implement later ;)
  |     return "editPage";
  | }
  | 
  | 
  | public String delete() {
  |     entityManager.remove(getObject());
  |     return "listPage";
  | }
  | 
  | 
  | public String save() {
  | 
  |     if(getNew())
  |         entityManager.persist(getObject());
  |     else
  |         entityManager.merge(getObject());
  | 
  |     return "listPage";
  | }
  | 
  | 


The concrete bean class:

  | ...
  | 
  | @In(required=false) @Out  //Don't create here, this is done in the edit() or create() method ;)
  | private Employee employee;
  | 
  | public Employee getObject() {
  |     return employee;
  | }
  | 
  | public void setObject(Employee employee) {
  |     this.employee = employee;
  | }
  | ...
  | 


The list page:

  | ...
  | <h:form>
  |     <h:dataTable....>
  | 
  |         ...
  | 
  |         <h:column>
  |             <h:commandLink value="Edit" action="#{employeeBean.edit}" />
  |         </h:column>
  | 
  |     </h:dataTable>
  | 
  | <h:commandLink value="Create new employee" action="#{employeeBean.create}" />
  | 
  | </h:form>
  | 


The edit page:

  | ...
  | <h:form>
  | 
  | 
  |     <inputText value="#{employee.someproperty...}" />
  |     ...
  | 
  |     <h:commandLink value="Save" action="#{employeeBean.save}" />
  |     <h:commandLink value="Delete" action="#{employeeBean.delete}" rendered="#{not employeeBean.new}" />
  |     <h:commandLink value="Cancel" action="#{employeeBean.cancel}" />
  | 
  | </h:form>
  | ...
  | 


Note: This is just a draft and I wasn't able to test it here. Let me know if you like it or if you would do it an other way. Feel free to add your comments and don't be polite ;).


My TODOs (I'm thankful for any hint):

- How to put an entity that's been selected on the list page into the editor bean in a convenient way (without violating the "Don't Repeat Yourself" rule ;))

- cancel method (I'm not sure if return "pageList" is sufficient cause I'm not sure if the entityManager does some stuff behind my back ;))

- How to refresh the list when returning to it. I encounter problems here as the list page always needs an explicit refresh (F5 key) before it shows updated and new entries.

- Safety question: "Oh my god, do you really wanna delete this?" ;)


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

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



More information about the jboss-user mailing list