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