001    package org.tynamo.pages;
002    
003    import org.apache.tapestry5.Link;
004    import org.apache.tapestry5.annotations.Property;
005    import org.apache.tapestry5.beaneditor.BeanModel;
006    import org.apache.tapestry5.ioc.Messages;
007    import org.apache.tapestry5.ioc.annotations.Inject;
008    import org.apache.tapestry5.services.BeanModelSource;
009    import org.apache.tapestry5.services.ContextValueEncoder;
010    import org.apache.tapestry5.services.PageRenderLinkSource;
011    import org.tynamo.descriptor.TynamoClassDescriptor;
012    import org.tynamo.services.DescriptorService;
013    import org.tynamo.services.PersistenceService;
014    import org.tynamo.util.DisplayNameUtils;
015    import org.tynamo.util.Utils;
016    
017    public class Show {
018    
019            @Inject
020            private ContextValueEncoder contextValueEncoder;
021    
022            @Inject
023            private BeanModelSource beanModelSource;
024    
025            @Inject
026            private Messages messages;
027    
028            @Inject
029            private PersistenceService persitenceService;
030    
031            @Inject
032            private DescriptorService descriptorService;
033    
034            @Inject
035            private PageRenderLinkSource pageRenderLinkSource;
036    
037            private TynamoClassDescriptor classDescriptor;
038    
039            @Property
040            private BeanModel beanModel;
041    
042            @Property(read = false)
043            private Object bean;
044    
045            protected void onActivate(Class clazz, String id) {
046    
047                    assert clazz != null; //@todo throw a proper exception
048    
049                    this.bean = contextValueEncoder.toValue(clazz, id);
050                    this.classDescriptor = descriptorService.getClassDescriptor(clazz);
051                    this.beanModel = beanModelSource.createDisplayModel(clazz, messages);
052    
053                    assert bean != null; //@todo throw a proper exception
054            }
055    
056            protected void cleanupRender() {
057                    bean = null;
058                    classDescriptor = null;
059                    beanModel = null;
060            }
061    
062            /**
063             * This tells Tapestry to put type & id into the URL, making it bookmarkable.
064             *
065             * @return
066             */
067            protected Object[] onPassivate() {
068                    return new Object[]{classDescriptor.getBeanType(), bean};
069            }
070    
071            public Object[] getEditPageContext() {
072                    return new Object[]{classDescriptor.getBeanType(), bean};
073            }
074    
075            public String getEditLinkMessage() {
076                    return messages.format(Utils.EDIT_MESSAGE, DisplayNameUtils.getDisplayName(classDescriptor, messages));
077            }
078    
079            public String getTitle() {
080                    return messages.format(Utils.SHOW_MESSAGE, bean.toString(), messages);
081            }
082    
083            public Link onActionFromDelete() {
084                    persitenceService.remove(bean);
085                    return pageRenderLinkSource.createPageRenderLinkWithContext("List", classDescriptor.getBeanType());
086            }
087    
088            public boolean isAllowRemove() {
089                    return classDescriptor.isAllowRemove() && !classDescriptor.isChild();
090            }
091    
092            public String getListAllLinkMessage() {
093                    return messages
094                                    .format(Utils.LISTALL_LINK_MESSAGE, DisplayNameUtils.getPluralDisplayName(classDescriptor, messages));
095            }
096    
097    
098            public TynamoClassDescriptor getClassDescriptor() {
099                    return classDescriptor;
100            }
101    
102            public Object getBean() {
103                    return bean;
104            }
105    }