001 package net.databinder.components.ao;
002
003 import java.sql.SQLException;
004
005 import net.databinder.ao.Databinder;
006 import net.databinder.models.ao.EntityModel;
007 import net.java.ao.EntityManager;
008 import net.java.ao.RawEntity;
009
010 import org.apache.wicket.model.CompoundPropertyModel;
011
012 /** Form to be used with a single object, wraps in a compound property model. */
013 @SuppressWarnings("unchecked")
014 public class DataForm extends TransactionalForm {
015
016 public DataForm(String id, Class entityType) {
017 super(id, new CompoundPropertyModel(new EntityModel(entityType)));
018 }
019
020 public DataForm(String id, EntityModel entityModel) {
021 super(id, new CompoundPropertyModel(entityModel));
022 }
023
024 /** Default implementation saves object if bound, otherwise creates new object using model's fieldMap. */
025 @Override
026 protected void inSubmitTransaction(EntityManager entityManager) throws SQLException {
027 if (getEntityModel().isBound())
028 ((RawEntity)getModelObject()).save();
029 else
030 setModelObject(entityManager.create(getEntityModel().getEntityType(), getEntityModel().getFieldMap()));
031 }
032
033 public EntityModel getEntityModel() {
034 return (EntityModel) ((CompoundPropertyModel) getModel()).getChainedModel();
035 }
036
037 /** Button to delete this form's model object. */
038 public class DeleteButton extends TransactionalButton {
039 public DeleteButton(String id) {
040 super(id);
041 setDefaultFormProcessing(false);
042 }
043 @Override
044 protected void inSubmitTransaction(EntityManager entityManager) throws SQLException {
045 Databinder.getEntityManager().delete((RawEntity)DataForm.this.getModelObject());
046 }
047 @Override
048 protected void afterSubmit() {
049 getEntityModel().unbind();
050 DataForm.this.modelChanged();
051 }
052 @Override
053 public boolean isEnabled() {
054 return getEntityModel().isBound();
055 }
056 }
057
058 }