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