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.PageRenderLinkSource;
011 import org.tynamo.builder.BuilderDirector;
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 Add {
019
020 @Inject
021 private BuilderDirector builderDirector;
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 @Property
039 private TynamoClassDescriptor classDescriptor;
040
041 @Property
042 private BeanModel beanModel;
043
044 @Property
045 private Object bean;
046
047
048 protected void onActivate(Class clazz) throws Exception {
049
050 assert clazz != null; //@todo throw a proper exception
051
052 this.bean = builderDirector.createNewInstance(clazz);
053 this.classDescriptor = descriptorService.getClassDescriptor(clazz);
054 this.beanModel = beanModelSource.createEditModel(clazz, messages);
055 }
056
057 protected void cleanupRender() {
058 bean = null;
059 classDescriptor = null;
060 beanModel = null;
061 }
062
063 protected Object[] onPassivate() {
064 return new Object[]{classDescriptor.getBeanType()};
065 }
066
067 @Log
068 protected Object onSuccess() {
069 persitenceService.save(bean);
070 return pageRenderLinkSource.createPageRenderLinkWithContext("Show", classDescriptor.getBeanType(), bean);
071 }
072
073 public Link onActionFromCancel() {
074 return pageRenderLinkSource.createPageRenderLinkWithContext("List", classDescriptor.getBeanType());
075 }
076
077 public String getTitle() {
078 return messages.format(Utils.ADD_NEW_MESSAGE, DisplayNameUtils.getDisplayName(classDescriptor, messages));
079 }
080
081 public String getListAllLinkMessage() {
082 return messages
083 .format(Utils.LISTALL_LINK_MESSAGE, DisplayNameUtils.getPluralDisplayName(classDescriptor, messages));
084 }
085
086 }