| 1 |
|
package org.trails.component; |
| 2 |
|
|
| 3 |
|
import ognl.Ognl; |
| 4 |
|
import ognl.OgnlException; |
| 5 |
|
|
| 6 |
|
import org.apache.commons.logging.Log; |
| 7 |
|
import org.apache.commons.logging.LogFactory; |
| 8 |
|
import org.apache.tapestry.IPage; |
| 9 |
|
import org.apache.tapestry.IRequestCycle; |
| 10 |
|
import org.apache.tapestry.annotations.Bean; |
| 11 |
|
import org.apache.tapestry.annotations.ComponentClass; |
| 12 |
|
import org.apache.tapestry.annotations.InjectObject; |
| 13 |
|
import org.apache.tapestry.annotations.InjectState; |
| 14 |
|
import org.apache.tapestry.annotations.Lifecycle; |
| 15 |
|
import org.apache.tapestry.annotations.Parameter; |
| 16 |
|
import org.apache.tapestry.callback.ICallback; |
| 17 |
|
import org.trails.TrailsRuntimeException; |
| 18 |
|
import org.trails.callback.AssociationCallback; |
| 19 |
|
import org.trails.callback.CallbackStack; |
| 20 |
|
import org.trails.callback.EditCallback; |
| 21 |
|
import org.trails.descriptor.BlockFinder; |
| 22 |
|
import org.trails.descriptor.DescriptorService; |
| 23 |
|
import org.trails.descriptor.IClassDescriptor; |
| 24 |
|
import org.trails.descriptor.ObjectReferenceDescriptor; |
| 25 |
|
import org.trails.descriptor.OwningObjectReferenceDescriptor; |
| 26 |
|
import org.trails.page.EditPage; |
| 27 |
|
import org.trails.page.PageResolver; |
| 28 |
|
import org.trails.page.TrailsPage.PageType; |
| 29 |
|
import org.trails.persistence.PersistenceException; |
| 30 |
|
import org.trails.persistence.PersistenceService; |
| 31 |
|
import org.trails.validation.TrailsValidationDelegate; |
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
@ComponentClass(allowBody = true, allowInformalParameters = true) |
| 39 |
0 |
public abstract class AssociationMgt extends TrailsComponent |
| 40 |
|
{ |
| 41 |
0 |
protected static final Log LOG = LogFactory.getLog(AssociationMgt.class); |
| 42 |
|
|
| 43 |
|
@Bean(lifecycle = Lifecycle.REQUEST) |
| 44 |
|
public abstract TrailsValidationDelegate getDelegate(); |
| 45 |
|
|
| 46 |
|
public abstract String getCreateExpression(); |
| 47 |
|
|
| 48 |
|
public abstract void setCreateExpression(String CreateExpression); |
| 49 |
|
|
| 50 |
|
@Parameter(required = true, cache = true) |
| 51 |
|
public abstract ObjectReferenceDescriptor getDescriptor(); |
| 52 |
|
|
| 53 |
|
public abstract void setDescriptor(ObjectReferenceDescriptor descriptor); |
| 54 |
|
|
| 55 |
|
@Parameter(required = true, cache = true) |
| 56 |
|
public abstract Object getOwner(); |
| 57 |
|
|
| 58 |
|
public abstract void setOwner(Object owner); |
| 59 |
|
|
| 60 |
|
@Parameter(required = true, cache = true) |
| 61 |
|
public abstract Object getAssociation(); |
| 62 |
|
|
| 63 |
|
public abstract void setAssociation(Object association); |
| 64 |
|
|
| 65 |
|
@Parameter(required = true, cache = true) |
| 66 |
|
public abstract Object getValue(); |
| 67 |
|
|
| 68 |
|
public abstract void setValue(Object value); |
| 69 |
|
|
| 70 |
|
@Parameter(required = true, cache = true) |
| 71 |
|
public abstract Object getModel(); |
| 72 |
|
|
| 73 |
|
public abstract void setModel(Object bytes); |
| 74 |
|
|
| 75 |
|
@InjectState("callbackStack") |
| 76 |
|
public abstract CallbackStack getCallbackStack(); |
| 77 |
|
|
| 78 |
|
public abstract void setCallbackStack(CallbackStack stack); |
| 79 |
|
|
| 80 |
|
@InjectObject("service:trails.core.PageResolver") |
| 81 |
|
public abstract PageResolver getPageResolver(); |
| 82 |
|
|
| 83 |
|
@InjectObject("spring:persistenceService") |
| 84 |
|
public abstract PersistenceService getPersistenceService(); |
| 85 |
|
|
| 86 |
|
@InjectObject("spring:editorService") |
| 87 |
|
public abstract BlockFinder getBlockFinder(); |
| 88 |
|
|
| 89 |
|
@InjectObject("spring:descriptorService") |
| 90 |
|
public abstract DescriptorService getDescriptorService(); |
| 91 |
|
|
| 92 |
|
public IClassDescriptor getClassDescriptor() |
| 93 |
|
{ |
| 94 |
0 |
return getDescriptorService().getClassDescriptor(getDescriptor().getPropertyType()); |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
public AssociationMgt() |
| 98 |
|
{ |
| 99 |
0 |
super(); |
| 100 |
0 |
} |
| 101 |
|
|
| 102 |
|
public String getOwnerTypeName() |
| 103 |
|
{ |
| 104 |
0 |
return getOwner().getClass().getName(); |
| 105 |
|
} |
| 106 |
|
|
| 107 |
|
public Class getOwnerType() |
| 108 |
|
{ |
| 109 |
0 |
return getOwner().getClass(); |
| 110 |
|
} |
| 111 |
|
|
| 112 |
|
public String getAssociationTypeName() |
| 113 |
|
{ |
| 114 |
0 |
return getDescriptor().getPropertyType().getCanonicalName(); |
| 115 |
|
} |
| 116 |
|
|
| 117 |
|
public Class getAssociationType() |
| 118 |
|
{ |
| 119 |
0 |
return getDescriptor().getPropertyType().getClass(); |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
AssociationCallback buildCallback() |
| 123 |
|
{ |
| 124 |
0 |
AssociationCallback callback = new AssociationCallback(getPage().getRequestCycle().getPage().getPageName(), |
| 125 |
0 |
getModel(), getDescriptor()); |
| 126 |
0 |
return callback; |
| 127 |
|
} |
| 128 |
|
|
| 129 |
|
public void addNew(IRequestCycle cycle) |
| 130 |
|
{ |
| 131 |
0 |
getCallbackStack().push(buildCallback()); |
| 132 |
|
|
| 133 |
0 |
String currentEditPageName = getPage().getRequestCycle().getPage().getPageName(); |
| 134 |
0 |
EditPage ownerEditPage = (EditPage) getPageResolver().resolvePage(cycle, getDescriptor().getClass(), |
| 135 |
0 |
PageType.Edit); |
| 136 |
|
|
| 137 |
|
try |
| 138 |
|
{ |
| 139 |
0 |
Object newModel = buildNewMemberInstance(); |
| 140 |
0 |
EditCallback nextPage = new EditCallback(ownerEditPage.getPageName(), newModel); |
| 141 |
|
|
| 142 |
0 |
((EditPage) cycle.getPage(currentEditPageName)).setNextPage(nextPage); |
| 143 |
0 |
nextPage.performCallback(cycle); |
| 144 |
0 |
} catch (Exception ex) |
| 145 |
|
{ |
| 146 |
0 |
throw new TrailsRuntimeException(ex, getDescriptor().getClass().getClass()); |
| 147 |
|
} |
| 148 |
0 |
} |
| 149 |
|
|
| 150 |
|
public void remove(IRequestCycle cycle) |
| 151 |
|
{ |
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
0 |
EditPage editPage = (EditPage) getPageResolver().resolvePage(cycle, getDescriptor().getClass(), PageType.Edit); |
| 156 |
|
|
| 157 |
0 |
AssociationCallback callback = buildCallback(); |
| 158 |
|
|
| 159 |
0 |
callback.remove(getPersistenceService(), getAssociation()); |
| 160 |
0 |
cycle.activate(editPage); |
| 161 |
|
|
| 162 |
0 |
EditCallback nextPage = new EditCallback(editPage.getPageName(), getModel()); |
| 163 |
|
|
| 164 |
0 |
String currentEditPageName = getPage().getRequestCycle().getPage().getPageName(); |
| 165 |
0 |
((EditPage) cycle.getPage(currentEditPageName)).setNextPage(nextPage); |
| 166 |
0 |
nextPage.performCallback(cycle); |
| 167 |
0 |
} |
| 168 |
|
|
| 169 |
|
public IPage edit(Object member) |
| 170 |
|
{ |
| 171 |
0 |
AssociationCallback callback = new AssociationCallback(getPage().getRequestCycle().getPage().getPageName(), |
| 172 |
0 |
getModel(), getDescriptor()); |
| 173 |
0 |
getCallbackStack().push(callback); |
| 174 |
0 |
EditPage editPage = (EditPage) getPageResolver().resolvePage(getPage().getRequestCycle(), |
| 175 |
0 |
Utils.checkForCGLIB(member.getClass()), PageType.Edit); |
| 176 |
|
|
| 177 |
0 |
editPage.setModel(member); |
| 178 |
0 |
return editPage; |
| 179 |
|
} |
| 180 |
|
|
| 181 |
|
protected Object buildNewMemberInstance() throws InstantiationException, IllegalAccessException |
| 182 |
|
{ |
| 183 |
|
Object associationModel; |
| 184 |
0 |
if (getCreateExpression() == null) |
| 185 |
|
{ |
| 186 |
0 |
associationModel = getDescriptor().getPropertyType().newInstance(); |
| 187 |
|
} else |
| 188 |
|
{ |
| 189 |
|
try |
| 190 |
|
{ |
| 191 |
0 |
associationModel = Ognl.getValue(getCreateExpression(), getOwner()); |
| 192 |
0 |
} catch (OgnlException oe) |
| 193 |
|
{ |
| 194 |
0 |
oe.printStackTrace(); |
| 195 |
0 |
return null; |
| 196 |
|
} |
| 197 |
|
} |
| 198 |
|
|
| 199 |
0 |
if (getOwningObjectReferenceDescriptor() != null |
| 200 |
0 |
&& getOwningObjectReferenceDescriptor().getInverseProperty() != null) |
| 201 |
|
{ |
| 202 |
|
try |
| 203 |
|
{ |
| 204 |
0 |
Ognl.setValue(getOwningObjectReferenceDescriptor().getInverseProperty(), associationModel, getOwner()); |
| 205 |
0 |
} catch (OgnlException e) |
| 206 |
|
{ |
| 207 |
0 |
LOG.error(e.getMessage()); |
| 208 |
|
} |
| 209 |
|
} |
| 210 |
|
|
| 211 |
0 |
return associationModel; |
| 212 |
|
} |
| 213 |
|
|
| 214 |
|
public OwningObjectReferenceDescriptor getOwningObjectReferenceDescriptor() |
| 215 |
|
{ |
| 216 |
0 |
return getDescriptor().getExtension(OwningObjectReferenceDescriptor.class); |
| 217 |
|
} |
| 218 |
|
} |