package org.trails.page; import org.apache.tapestry.annotations.Bean; import org.apache.tapestry.annotations.InjectObject; import org.apache.tapestry.annotations.Lifecycle; import org.hibernate.validator.InvalidStateException; import org.trails.descriptor.CollectionDescriptor; import org.trails.descriptor.IReferenceSupport; import org.trails.descriptor.ObjectReferenceDescriptor; import org.trails.persistence.HibernatePersistenceService; import org.trails.persistence.PersistenceException; import org.trails.validation.HibernateValidationDelegate; public abstract class HibernateEditPage extends EditPage { @Bean(lifecycle = Lifecycle.REQUEST) public abstract HibernateValidationDelegate getHibernateValidationDelegate(); /** * @todo: remove when the components reuse issue goes away. */ public HibernateValidationDelegate getDelegate() { return getHibernateValidationDelegate(); } @InjectObject("service:trails.hibernate.PersistenceService") public abstract HibernatePersistenceService getHibernatePersistenceService(); /** * @todo: remove when the components reuse issue goes away. */ public HibernatePersistenceService getPersistenceService() { return getHibernatePersistenceService(); } @Override protected boolean save() { if (!getDelegate().getHasErrors()) { try { /** * Do we need to parse for parent? NO, parent pages does not * necessarily mean that the parent has a property of the referenced * page. It could be a link for all we know. * * Use AssociationDescriptor to make sure there is an association. */ if (getAssociationDescriptor() != null && isModelNew()) { IReferenceSupport referenceDescriptor = (IReferenceSupport) getAssociationDescriptor(); if ( referenceDescriptor instanceof CollectionDescriptor ) { setModel(getPersistenceService().saveCollectionElement( getAssociationDescriptor().getAddExpression(), getModel(), getParent())); getPersistenceService().reattach(getParent()); } else if ( referenceDescriptor instanceof ObjectReferenceDescriptor ) { setModel(getPersistenceService().saveAssociation( getAssociationDescriptor().getAddExpression(), getModel(), getParent())); } } else { /** * loose enforcement, dangling edits? */ setModel(getPersistenceService().save(getModel())); } } catch (PersistenceException pe) { getDelegate().record(pe); return false; } catch (InvalidStateException ivex) { getDelegate().record(getClassDescriptor(), ivex); return false; } return true; } return false; } }