net.sf.click.extras.hibernate
Class HibernateForm

java.lang.Object
  extended bynet.sf.click.control.AbstractControl
      extended bynet.sf.click.control.AbstractContainer
          extended bynet.sf.click.control.Form
              extended bynet.sf.click.extras.hibernate.HibernateForm
All Implemented Interfaces:
Container, Control, Serializable

public class HibernateForm
extends Form

Provides Hibernate data aware Form control:   <form method='POST'>.

 
Hibernate is an Object Relational Mapping (ORM) framework. The HibernatteForm supports creating (inserting) and saving (updating) POJO instances. This form will automatically apply the given objects property required validation constraints to the form fields.

The HibernatteForm uses the thread local Session obtained via SessionContext.getSession() for all object for persistence operations. To use an alternative Session source override set the forms getSession() method.

The example below provides a User data object creation and editing page. To edit an existing user object, the object is passed to the page as a request parameter. Otherwise a new user object will be created when saveChanges() is called.

 public class UserEdit extends Page {

   private HibernateForm form = new HibernateForm("form", User.class);

    public UserEdit() {
        form.add(new TextField("firstName");
        form.add(new TextField("middleNames");
        form.add(new TextField("FamilyName");

        form.add(new Submit("ok", "   OK   ", this, "onOkClicked");
        form.add(new Submit("cancel", this, "onCancelClicked");

        form.setButtonAlign("right");
        form.setLabelRequiredPrefix("<b>");
        form.setLabelRequiredSuffix("</b>");
        addControl(form);
    }

    public void setUser(User user) {
        form.setValueObject(user);
    }

    public boolean onOkClicked() {
        if (form.isValid()) {
           if (form.saveChanges()) {
               setRedirect("user-list.htm");
           }
        }
        return true;
    }

    public boolean onCancelClicked() {
        setRedirect("user-list.htm");
        return false;
    }
 } 

Author:
Malcolm Edgar
See Also:
SessionContext, SessionFilter, Serialized Form

Field Summary
protected  HiddenField classField
          The value object class name hidden field.
protected static String FO_CLASS
          The form value object classname parameter name.
protected static String FO_ID
          The form value object id parameter name.
protected  boolean metaDataApplied
          The flag specifying that object validation meta data has been applied to the form fields.
protected  HiddenField oidField
          The value object identifier hidden field.
protected  org.hibernate.Session session
          The Hibernate session.
protected  org.hibernate.SessionFactory sessionFactory
          The Hibernate session factory.
 
Fields inherited from class net.sf.click.control.Form
actionURL, ALIGN_CENTER, ALIGN_LEFT, ALIGN_RIGHT, buttonAlign, buttonList, buttonStyle, columns, defaultFieldSize, disabled, enctype, error, errorsAlign, errorsPosition, errorsStyle, fieldList, fieldStyle, fieldWidths, FOCUS_JAVASCRIPT, FORM_NAME, HTML_IMPORTS, javaScriptValidation, labelAlign, labelsPosition, labelStyle, method, MULTIPART_FORM_DATA, POSITION_BOTTOM, POSITION_LEFT, POSITION_MIDDLE, POSITION_TOP, readonly, SUBMIT_CHECK, validate
 
Fields inherited from class net.sf.click.control.AbstractContainer
controlMap, controls
 
Fields inherited from class net.sf.click.control.AbstractControl
actionListener, attributes, listener, listenerMethod, messages, name, parent, styles
 
Fields inherited from interface net.sf.click.Control
CONTROL_MESSAGES
 
Constructor Summary
HibernateForm(String name, Class valueClass)
          Create a new HibernateForm with the given form name and value object class.
 
Method Summary
protected  void applyMetaData()
          Applies the ClassMetadata validation database meta data to the form fields.
protected  String getClassname(Class aClass)
          Return the original classname for the given class removing any CGLib proxy information.
 org.hibernate.Session getSession()
          Return the form Hibernate Session.
 org.hibernate.SessionFactory getSessionFactory()
          Return the application Hibernate SessionFactory.
 Object getValueObject()
          Return a Hibernate value object from the form with the form field values copied into the object's properties.
 boolean onProcess()
          This method applies the object meta data to the form fields and then invokes the super.onProcess() method.
 void render(HtmlStringBuffer buffer)
          Render the HTML representation of the HibernateForm.
 boolean saveChanges()
          Save or update the object to the database and return true.
 void setSession(org.hibernate.Session session)
          Set the user's Hibernate Session.
 void setSessionFactory(org.hibernate.SessionFactory sessionFactory)
          Set the form Hibernate SessionFactory.
 void setValueObject(Object valueObject)
          Set the given Hibernate value object in the form, copying the object's properties into the form field values.
 
Methods inherited from class net.sf.click.control.Form
add, add, add, add, clearErrors, clearValues, copyFrom, copyFrom, copyTo, copyTo, endTag, getActionURL, getButtonAlign, getButtonList, getButtonStyle, getColumns, getControlSizeEst, getDefaultFieldSize, getEnctype, getError, getErrorFields, getErrorsAlign, getErrorsPosition, getErrorsStyle, getField, getFieldList, getFields, getFieldStyle, getFieldValue, getFieldWidths, getFormSizeEst, getHtmlImports, getJavaScriptValidation, getLabelAlign, getLabelsPosition, getLabelStyle, getMethod, getTag, getValidate, hasPostError, insert, isDisabled, isFormSubmission, isReadonly, isValid, onDestroy, onSubmitCheck, onSubmitCheck, onSubmitCheck, performSubmitCheck, remove, removeField, removeFields, renderButtons, renderControls, renderErrors, renderFields, renderFocusJavaScript, renderHeader, renderTagEnd, renderValidationJavaScript, setActionURL, setButtonAlign, setButtonStyle, setColumns, setDefaultFieldSize, setDisabled, setEnctype, setError, setErrorsAlign, setErrorsPosition, setErrorsStyle, setFieldStyle, setJavaScriptValidation, setLabelAlign, setLabelsPosition, setLabelStyle, setListener, setMethod, setName, setReadonly, setValidate, startTag, validate
 
Methods inherited from class net.sf.click.control.AbstractContainer
contains, getControl, getControlMap, getControls, hasControls, onInit, onRender, renderChildren, renderContent, renderTagEnd, toString
 
Methods inherited from class net.sf.click.control.AbstractControl
addStyleClass, appendAttributes, getActionListener, getAttribute, getAttributes, getContext, getId, getMessage, getMessage, getMessage, getMessages, getName, getPage, getParent, getStyle, getStyles, hasAttribute, hasAttributes, hasStyles, onDeploy, registerActionEvent, removeStyleClass, renderTagBegin, setActionListener, setAttribute, setId, setParent, setStyle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface net.sf.click.Control
getContext, getId, getMessages, getName, getParent, onDeploy, setParent
 

Field Detail

FO_CLASS

protected static final String FO_CLASS
The form value object classname parameter name.

See Also:
Constant Field Values

FO_ID

protected static final String FO_ID
The form value object id parameter name.

See Also:
Constant Field Values

classField

protected HiddenField classField
The value object class name hidden field.


oidField

protected HiddenField oidField
The value object identifier hidden field.


session

protected org.hibernate.Session session
The Hibernate session.


sessionFactory

protected org.hibernate.SessionFactory sessionFactory
The Hibernate session factory.


metaDataApplied

protected boolean metaDataApplied
The flag specifying that object validation meta data has been applied to the form fields.

Constructor Detail

HibernateForm

public HibernateForm(String name,
                     Class valueClass)
Create a new HibernateForm with the given form name and value object class.

Parameters:
name - the form name
valueClass - the value object class
Method Detail

getSession

public org.hibernate.Session getSession()
Return the form Hibernate Session. If form session is not defined this method will obtain a session from the SessionContext.

Applications using alternative Hibernate Session sources should set the form's session using the setSession(Session) method.

Returns:
the form Hibernate session

setSession

public void setSession(org.hibernate.Session session)
Set the user's Hibernate Session.

Parameters:
session - the user's Hibernate session

getSessionFactory

public org.hibernate.SessionFactory getSessionFactory()
Return the application Hibernate SessionFactory. If session factory is not defined this method will obtain the session factory from the SessionContext.

Applications using an alternative Hibernate SessionFactory sources should set the form's session factory using the setSessionFactory(SessionFactory) method.

Returns:
the user's Hibernate session

setSessionFactory

public void setSessionFactory(org.hibernate.SessionFactory sessionFactory)
Set the form Hibernate SessionFactory.

Parameters:
sessionFactory - the Hibernate SessionFactory

getValueObject

public Object getValueObject()
Return a Hibernate value object from the form with the form field values copied into the object's properties.

Returns:
the Hibernate object from the form with the form field values applied to the object properties.

setValueObject

public void setValueObject(Object valueObject)
Set the given Hibernate value object in the form, copying the object's properties into the form field values.

Parameters:
valueObject - the Hibernate value object to set

saveChanges

public boolean saveChanges()
                    throws org.hibernate.HibernateException
Save or update the object to the database and return true. If a HibernateException occurs the Transaction will be rolled back the exception will be raised.

If no object is added to the form using setValueObject() then this method will:

If an existing persistent object is added to the form using setValueObject() then this method will:

Returns:
true if the object was saved or false otherwise
Throws:
org.hibernate.HibernateException - if a persistence error occured

onProcess

public boolean onProcess()
This method applies the object meta data to the form fields and then invokes the super.onProcess() method.

Returns:
true to continue Page event processing or false otherwise
See Also:
Form.onProcess()

render

public void render(HtmlStringBuffer buffer)
Render the HTML representation of the HibernateForm.

This method applies the object meta data to the form fields and then invokes the super.toString() method.

Parameters:
buffer - the specified buffer to render the control's output to
See Also:
AbstractContainer.toString()

applyMetaData

protected void applyMetaData()
Applies the ClassMetadata validation database meta data to the form fields.

The field validation attributes include:


getClassname

protected String getClassname(Class aClass)
Return the original classname for the given class removing any CGLib proxy information.

Parameters:
aClass - the class to obtain the original name from
Returns:
the original classname for the given class