001 package net.databinder.components.hib;
002
003 import net.databinder.hib.Databinder;
004
005 import org.apache.wicket.markup.html.form.Form;
006 import org.apache.wicket.model.IModel;
007 import org.hibernate.Session;
008 import org.hibernate.StaleObjectStateException;
009
010 /**
011 * Base class for forms that commit in onSubmit(). This is extended by DataForm, and may be
012 * extended directly by client forms when DataForm is not appropriate. Transactions
013 * are committed only when no errors are displayed.
014 * @author Nathan Hamblen
015 */
016 public class DataFormBase extends Form {
017 private Object factoryKey;
018 public DataFormBase(String id) {
019 super(id);
020 }
021 public DataFormBase(final String id, IModel model)
022 {
023 super(id, model);
024 }
025
026 public Object getFactoryKey() {
027 return factoryKey;
028 }
029
030 public DataFormBase setFactoryKey(Object key) {
031 this.factoryKey = key;
032 return this;
033 }
034
035 protected Session getHibernateSession() {
036 return Databinder.getHibernateSession(factoryKey);
037 }
038
039 /**
040 * Commits transaction if no errors are registered for any form component.
041 */
042 protected void onSubmit() {
043 try {
044 if (!hasError()) {
045 Session session = Databinder.getHibernateSession(factoryKey);
046 session.flush(); // needed for conv. sessions, harmless otherwise
047 session.getTransaction().commit();
048 }
049 } catch (StaleObjectStateException e) {
050 error(getString("version.mismatch", null)); // report error
051 }
052 }
053
054 }