|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.click.control.AbstractControl net.sf.click.control.AbstractContainer net.sf.click.control.Form net.sf.click.extras.cayenne.CayenneForm
Provides Cayenne data aware Form control: <form method='POST'>.
|
DataObject
instances. This form will
automatically apply the given data objects required and max length validation
constraints to the form fields.
The CayenneForm uses the thread local DataContext obtained via
DataContext.getThreadDataContext() for all object for persistence
operations.
saveChanges()
is called.
public class OrganisationEdit extends Page { private CayenneForm form = new CayenneForm("form", Organisation.class); public OrganisationEdit() { form.add(new TextField("name", "Organisation Name:", 35); QuerySelect type = new QuerySelect("type", "Type:"); type.setQueryValueLabel("organisation-types", "VALUE", "LABEL"); form.add(type); form.add(new TextArea("description", "Description:", 35, 2); form.add(new Submit("ok", " OK ", this, "onOkClicked"); form.add(new Submit("cancel", this, "onCancelClicked"); form.setButtonAlign("right"); addControl(form); } public void onGet() { Organisation organisation = (Organisation) getContext().getRequestAttribute("organisation"); if (organisation != null) { form.setDataObject(organisation); } } public boolean onOkClicked() { if (form.isValid()) { if (form.saveChanges()) { Organisation organisation = (Organisation) form.getDataObject(false); String url = getContext().getPagePath(OrganisationViewer.class); setRedirect(url + "?id=" + organisation.getId()); return false; } } return true; } public boolean onCancelClicked() { Organisation organisation = (Organisation) form.getDataObject(false); String url = getContext().getPagePath(OrganisationViewer.class); setRedirect(url + "?id=" + organisation.getId()); return false; } }Note the getDataObject(false) method is used to obtain the DataObject from the Form without applying the field values to the data object. This is very important when dealing with already persistent objects and you dont want to apply any form changes. Alternatively you can save a submitted DataObject using a Service or DAO pattern. For example:
public boolean onOkClicked() { if (form.isValid()) { Organisation organisation = (Organisation) form.getDataObject(); getOrganisationService().save(organisation); String url = getContext().getPagePath(OrganisationViewer.class); setRedirect(url + "?id=" + organisation.getId()); return false; } return true; }Please Note if you are using this pattern with objects already saved, take care to ensure that the form submission is valid before calling
getDataObject()
as this method changes the DataObject's properties
using the submitted form field values.
If you don't commit the changes at this point they will still be present in
the session DataContext
and will be applied in the next
commitChanges() call, which may happen in a subsequent request.
In these exceptional situations the object should be removed from the cache
DataContext using invalidateObjects() method or by reloading the
object from the database.
Alternatively use the DataContextFilter
which will
automatically rollback any uncommitted changes at the end of each request.
Field Summary | |
protected HiddenField |
classField
The data object class name hidden field. |
protected org.apache.cayenne.DataObject |
dataObject
A transient dataObject handle for detecting a committed object. |
protected static String |
FO_CLASS
The form data object classname parameter name. |
protected static String |
FO_ID
The form data 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 data object id hidden field. |
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 | |
CayenneForm()
Create an CayenneForm with no name or dataObjectClass. |
|
CayenneForm(Class dataObjectClass)
Create a new CayenneForm with the given DataObject class. |
|
CayenneForm(String name,
Class dataObjectClass)
Create a new CayenneForm with the given form name and DataObject class. |
Method Summary | |
protected void |
applyMetaData()
Applies the DataObject validation database meta data to the form fields. |
void |
clearValues()
Clear all the form field values setting them to null. |
org.apache.cayenne.access.DataContext |
getDataContext()
Return the thread local DataContext obtained via DataContext.getThreadDataContext(). |
org.apache.cayenne.DataObject |
getDataObject()
Return a DataObject from the form with the form field values set on the object's properties. |
org.apache.cayenne.DataObject |
getDataObject(boolean copyTo)
Return a DataObject from the form, with the form field values set on the object if the copyTo parameter is true. |
Class |
getDataObjectClass()
Return the Class of the form DataObject. |
Object |
getDataObjectPk()
Return the DataObject primary key. |
protected boolean |
isPersistent(org.apache.cayenne.DataObject dataObject)
Return true if the given dataObject is persistent. |
void |
onDestroy()
Clear the cached dataObject and destroy the form fields. |
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 CayenneForm. |
boolean |
saveChanges()
Save the object to the database committing all changes in the DataContext and return true. |
void |
setDataObject(org.apache.cayenne.DataObject dataObject)
Set the given DataObject in the form, copying the object's properties into the form field values. |
void |
setDataObjectClass(Class dataObjectClass)
Set the DataObject class. |
protected void |
setObjEntityFieldConstrains(String relationshipName,
org.apache.cayenne.map.ObjEntity objEntity)
Set the ObjEntity meta data constraints on the form fields. |
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 |
protected static final String FO_CLASS
protected static final String FO_ID
protected HiddenField classField
protected HiddenField oidField
protected boolean metaDataApplied
protected transient org.apache.cayenne.DataObject dataObject
Constructor Detail |
public CayenneForm(String name, Class dataObjectClass)
name
- the form namedataObjectClass
- the DataObject classpublic CayenneForm(Class dataObjectClass)
dataObjectClass
- the DataObject classpublic CayenneForm()
Method Detail |
public void clearValues()
Form.clearValues()
public org.apache.cayenne.access.DataContext getDataContext()
public org.apache.cayenne.DataObject getDataObject(boolean copyTo)
copyTo
- option to copy the form properties to the returned data
object
public org.apache.cayenne.DataObject getDataObject()
public void setDataObject(org.apache.cayenne.DataObject dataObject)
dataObject
- the DataObject to setpublic Class getDataObjectClass()
public void setDataObjectClass(Class dataObjectClass)
dataObjectClass
- the DataObject classpublic Object getDataObjectPk()
public boolean saveChanges()
public boolean onProcess()
Form.onProcess()
public void onDestroy()
Form.onDestroy()
public void render(HtmlStringBuffer buffer)
buffer
- the specified buffer to render the control's output toAbstractContainer.toString()
protected void applyMetaData()
protected boolean isPersistent(org.apache.cayenne.DataObject dataObject)
dataObject
- the DataObject to test
protected void setObjEntityFieldConstrains(String relationshipName, org.apache.cayenne.map.ObjEntity objEntity)
relationshipName
- the object relationship name, null if the
ObjEntity of the CayenneFormobjEntity
- the ObjEntity to lookup meta data from
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |