org.apache.click.extras.hibernate
Class HibernateForm

java.lang.Object
  extended by org.apache.click.control.AbstractControl
      extended by org.apache.click.control.AbstractContainer
          extended by org.apache.click.control.Form
              extended by org.apache.click.extras.hibernate.HibernateForm
All Implemented Interfaces:
Serializable, Control, Container, Stateful

public class HibernateForm
extends Form

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

 
Hibernate is an Object Relational Mapping (ORM) framework. The HibernateForm 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 HibernateForm 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;
    }
 } 

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  Session session
          The Hibernate session.
protected  SessionFactory sessionFactory
          The Hibernate session factory.
 
Fields inherited from class org.apache.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, formSubmission, javaScriptValidation, labelAlign, labelsPosition, labelStyle, method, MULTIPART_FORM_DATA, POSITION_BOTTOM, POSITION_LEFT, POSITION_MIDDLE, POSITION_TOP, readonly, SUBMIT_CHECK, validate
 
Fields inherited from class org.apache.click.control.AbstractContainer
controlMap, controls
 
Fields inherited from class org.apache.click.control.AbstractControl
actionListener, attributes, behaviors, headElements, listener, listenerMethod, messages, name, parent, styles
 
Fields inherited from interface org.apache.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.
 Session getSession()
          Return the form Hibernate Session.
 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(Session session)
          Set the user's Hibernate Session.
 void setSessionFactory(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 org.apache.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, getHeadElements, getJavaScriptValidation, getLabelAlign, getLabelsPosition, getLabelStyle, getMethod, getState, getTag, getValidate, hasPostError, insert, isDisabled, isFormSubmission, isJavaScriptValidation, isReadonly, isValid, onDestroy, onSubmitCheck, onSubmitCheck, onSubmitCheck, performSubmitCheck, remove, removeField, removeFields, removeState, renderButtons, renderControls, renderErrors, renderFields, renderFocusJavaScript, renderHeader, renderTagEnd, renderValidationJavaScript, replace, restoreState, saveState, setActionURL, setButtonAlign, setButtonStyle, setColumns, setDefaultFieldSize, setDisabled, setEnctype, setError, setErrorsAlign, setErrorsPosition, setErrorsStyle, setFieldStyle, setJavaScriptValidation, setLabelAlign, setLabelsPosition, setLabelStyle, setListener, setMethod, setName, setReadonly, setState, setValidate, startTag, validate, validateFileUpload
 
Methods inherited from class org.apache.click.control.AbstractContainer
contains, getControl, getControlMap, getControls, hasControls, onInit, onRender, renderChildren, renderContent, renderTagEnd, toString
 
Methods inherited from class org.apache.click.control.AbstractControl
addBehavior, addStyleClass, appendAttributes, dispatchActionEvent, getActionListener, getAttribute, getAttributes, getBehaviors, getContext, getHtmlImports, getId, getMessage, getMessage, getMessages, getName, getPage, getParent, getStyle, getStyles, hasAttribute, hasAttributes, hasBehaviors, hasStyles, isAjaxTarget, onDeploy, removeBehavior, 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 org.apache.click.Control
getBehaviors, getContext, getId, getMessages, getName, getParent, hasBehaviors, isAjaxTarget, 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 Session session
The Hibernate session.


sessionFactory

protected 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 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(Session session)
Set the user's Hibernate Session.

Parameters:
session - the user's Hibernate session

getSessionFactory

public 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(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 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:
HibernateException - if a persistence error occurred

onProcess

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

Specified by:
onProcess in interface Control
Overrides:
onProcess in class Form
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.

Specified by:
render in interface Control
Overrides:
render in class Form
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