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.CollectionDescriptor;
013    import org.tynamo.descriptor.TynamoClassDescriptor;
014    import org.tynamo.descriptor.TynamoPropertyDescriptor;
015    import org.tynamo.services.DescriptorService;
016    import org.tynamo.services.PersistenceService;
017    import org.tynamo.util.DisplayNameUtils;
018    import org.tynamo.util.Utils;
019    
020    /**
021     * Edit Composition Page
022     */
023    public class EditC {
024    
025            @Inject
026            private ContextValueEncoder contextValueEncoder;
027    
028            @Inject
029            private BeanModelSource beanModelSource;
030    
031            @Inject
032            private Messages messages;
033    
034            @Inject
035            private PersistenceService persitenceService;
036    
037            @Inject
038            private DescriptorService descriptorService;
039    
040            @Inject
041            private PageRenderLinkSource pageRenderLinkSource;
042    
043            @Property(write = false)
044            private CollectionDescriptor collectionDescriptor;
045    
046            @Property
047            private Object bean;
048    
049            @Property(write = false)
050            private Object parentBean;
051    
052            @Property(write = false)
053            private TynamoClassDescriptor classDescriptor;
054    
055            @Property
056            private BeanModel beanModel;
057    
058    
059            final void onActivate(Class clazz, String parentId, String property, String id) throws Exception {
060    
061                    assert clazz != null; //@todo throw a proper exception
062    
063                    TynamoPropertyDescriptor propertyDescriptor = descriptorService.getClassDescriptor(clazz).getPropertyDescriptor(property);
064    
065                    assert propertyDescriptor != null; //@todo throw a proper exception
066    
067                    this.collectionDescriptor = ((CollectionDescriptor) propertyDescriptor);
068    
069                    this.classDescriptor = descriptorService.getClassDescriptor(collectionDescriptor.getElementType());
070                    this.beanModel = beanModelSource.createEditModel(classDescriptor.getBeanType(), messages);
071    
072                    this.parentBean = contextValueEncoder.toValue(clazz, parentId);
073                    assert parentBean != null; //@todo throw a proper exception
074    
075                    this.bean = contextValueEncoder.toValue(classDescriptor.getBeanType(), id);
076                    assert bean != null; //@todo throw a proper exception
077    
078            }
079    
080            /**
081             * This tells Tapestry to put type & id into the URL, making it bookmarkable.
082             *
083             * @return
084             */
085            protected Object[] onPassivate() {
086                    return new Object[]{collectionDescriptor.getBeanType(), parentBean, collectionDescriptor.getName(), bean};
087            }
088    
089            protected void cleanupRender() {
090                    bean = null;
091                    classDescriptor = null;
092                    beanModel = null;
093                    parentBean = null;
094                    collectionDescriptor = null;
095            }
096    
097            @Log
098            protected Object onSuccess() {
099                    persitenceService.save(bean);
100                    return back();
101            }
102    
103            public Link onActionFromCancel() {
104                    return back();
105            }
106    
107            public String getTitle() {
108                    return messages.format(Utils.EDIT_MESSAGE, DisplayNameUtils.getDisplayName(classDescriptor, messages));
109            }
110    
111            public Link back() {
112                    return pageRenderLinkSource.createPageRenderLinkWithContext("Edit", collectionDescriptor.getBeanType(), parentBean);
113            }
114    
115            public String getListAllLinkMessage() {
116                    return messages.format(Utils.LISTALL_LINK_MESSAGE, DisplayNameUtils.getPluralDisplayName(classDescriptor, messages));
117            }
118    
119    }