001    package net.databinder.components.ao;
002    
003    import java.sql.SQLException;
004    
005    import net.databinder.ao.Databinder;
006    import net.java.ao.EntityManager;
007    import net.java.ao.Transaction;
008    
009    import org.apache.wicket.ResourceReference;
010    import org.apache.wicket.WicketRuntimeException;
011    import org.apache.wicket.markup.html.form.ImageButton;
012    
013    /** Button that provides a submit transaction similar to TransactionalForm. */
014    public abstract class TransactionalImageButton extends ImageButton {
015            public TransactionalImageButton(String id, ResourceReference resourceReference) {
016                    super(id, resourceReference);
017            }
018            
019            /** Called when the form is submitted, do not override if you want transactional behavior. */
020            public void onSubmit() {
021                    try {
022                            new Transaction<Object>(Databinder.getEntityManager()) {
023                                    @Override
024                                    protected Object run() throws SQLException {
025                                            inSubmitTransaction(Databinder.getEntityManager());
026                                            return null;
027                                    }
028                            }.execute();
029                    } catch (SQLException e) {
030                            throw new WicketRuntimeException(e);
031                    }
032                    afterSubmit();
033            }
034            
035            /** 
036             * Called inside onSubmit's database transaction.
037             * @param entityManager associated with button, provided for convenience
038             */
039            protected abstract void inSubmitTransaction(EntityManager entityManager) throws SQLException;
040            /** Called after onSubmit's database transaction. */
041            protected void afterSubmit() { };
042    }