001 package org.tynamo.hibernate.pages;
002
003 import org.apache.tapestry5.beaneditor.BeanModel;
004 import org.apache.tapestry5.ioc.annotations.Inject;
005 import org.apache.tapestry5.services.Environment;
006 import org.tynamo.descriptor.TynamoClassDescriptor;
007 import org.tynamo.pages.ModelPage;
008 import org.tynamo.services.TynamoBeanContext;
009
010 import java.lang.annotation.Annotation;
011
012 public abstract class HibernateModelPage extends ModelPage
013 {
014
015 @Inject
016 private Environment environment;
017
018 private TynamoClassDescriptor classDescriptor;
019
020 private BeanModel beanModel;
021
022 private Object bean;
023
024 protected void onPrepare()
025 {
026 pushTynamoBeanContext();
027 }
028
029 protected void pushTynamoBeanContext()
030 {
031 TynamoBeanContext context = new TynamoBeanContext()
032 {
033 public Class<?> getBeanClass()
034 {
035 return classDescriptor.getType();
036
037 }
038
039 public <T extends Annotation> T getAnnotation(Class<T> type)
040 {
041 return getBeanClass().getAnnotation(type);
042 }
043
044 public Object getBean()
045 {
046 return bean;
047 }
048 };
049 environment.push(TynamoBeanContext.class, context);
050 }
051
052 protected void activate(Object bean, TynamoClassDescriptor classDescriptor, BeanModel beanModel)
053 {
054 this.bean = bean;
055 this.classDescriptor = classDescriptor;
056 this.beanModel = beanModel;
057 }
058
059 protected void cleanupRender()
060 {
061 bean = null;
062 classDescriptor = null;
063 beanModel = null;
064 }
065
066 final void onActivate(Class clazz, String id) throws Exception
067 {
068 activate(getContextValueEncoder().toValue(clazz, id), getDescriptorService().getClassDescriptor(clazz),
069 createBeanModel(clazz));
070 }
071
072 /**
073 * This tells Tapestry to put type & id into the URL, making it bookmarkable.
074 *
075 * @return
076 */
077 protected Object[] onPassivate()
078 {
079 return new Object[]{getClassDescriptor().getType(), getBean()};
080 }
081
082 public final TynamoClassDescriptor getClassDescriptor()
083 {
084 return classDescriptor;
085 }
086
087 public final BeanModel getBeanModel()
088 {
089 return beanModel;
090 }
091
092 public final Object getBean()
093 {
094 return bean;
095 }
096
097 public final void setBean(Object bean)
098 {
099 this.bean = bean;
100 }
101
102 public boolean isAllowRemove()
103 {
104 return getClassDescriptor().isAllowRemove() && !getClassDescriptor().isChild();
105 }
106
107 }