| 1 |
|
package org.trails.engine; |
| 2 |
|
|
| 3 |
|
import ognl.Ognl; |
| 4 |
|
import ognl.OgnlException; |
| 5 |
|
import org.apache.commons.lang.StringUtils; |
| 6 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
| 7 |
|
import org.apache.hivemind.util.Defense; |
| 8 |
|
import org.apache.hivemind.util.PropertyUtils; |
| 9 |
|
import org.apache.tapestry.IPage; |
| 10 |
|
import org.apache.tapestry.IRequestCycle; |
| 11 |
|
import org.apache.tapestry.PageNotFoundException; |
| 12 |
|
import org.apache.tapestry.engine.EngineMessages; |
| 13 |
|
import org.apache.tapestry.engine.IEngineService; |
| 14 |
|
import org.apache.tapestry.engine.ILink; |
| 15 |
|
import org.apache.tapestry.services.DataSqueezer; |
| 16 |
|
import org.apache.tapestry.services.LinkFactory; |
| 17 |
|
import org.apache.tapestry.services.ResponseRenderer; |
| 18 |
|
import org.trails.Trails; |
| 19 |
|
import org.trails.builder.BuilderDirector; |
| 20 |
|
import org.trails.exception.TrailsRuntimeException; |
| 21 |
|
import org.trails.descriptor.CollectionDescriptor; |
| 22 |
|
import org.trails.descriptor.IClassDescriptor; |
| 23 |
|
import org.trails.page.*; |
| 24 |
|
import org.trails.services.ServiceConstants; |
| 25 |
|
|
| 26 |
|
import java.io.IOException; |
| 27 |
|
import java.util.HashMap; |
| 28 |
|
import java.util.Map; |
| 29 |
|
|
| 30 |
0 |
public class TrailsPagesService implements IEngineService |
| 31 |
|
{ |
| 32 |
|
|
| 33 |
|
private ResponseRenderer responseRenderer; |
| 34 |
|
private LinkFactory linkFactory; |
| 35 |
|
private DataSqueezer dataSqueezer; |
| 36 |
|
private BuilderDirector builderDirector; |
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
public PageResolver pageResolver; |
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
public ILink getLink(boolean post, Object parameter) |
| 49 |
|
{ |
| 50 |
0 |
Defense.isAssignable(parameter, TrailsPagesServiceParameter.class, "parameter"); |
| 51 |
|
|
| 52 |
0 |
TrailsPagesServiceParameter tpsp = (TrailsPagesServiceParameter) parameter; |
| 53 |
|
|
| 54 |
0 |
Map parameters = new HashMap(); |
| 55 |
|
|
| 56 |
0 |
parameters.put(ServiceConstants.PAGE_TYPE, tpsp.getPageType().name().toLowerCase()); |
| 57 |
0 |
parameters.put(ServiceConstants.CLASS_DESCRIPTOR, dataSqueezer.squeeze(tpsp.getClassDescriptor())); |
| 58 |
|
|
| 59 |
0 |
putIfNotNull(parameters, ServiceConstants.MODEL, tpsp.getModel()); |
| 60 |
0 |
putIfNotNull(parameters, ServiceConstants.ASSOC_DESCRIPTOR, tpsp.getAssociationDescriptor()); |
| 61 |
0 |
putIfNotNull(parameters, ServiceConstants.PAERNT_MODEL, tpsp.getParent()); |
| 62 |
|
|
| 63 |
0 |
return linkFactory.constructLink(this, post, parameters, true); |
| 64 |
|
} |
| 65 |
|
|
| 66 |
|
private void putIfNotNull(Map map, final String key, Object value) |
| 67 |
|
{ |
| 68 |
0 |
if (value != null) |
| 69 |
|
{ |
| 70 |
0 |
map.put(key, dataSqueezer.squeeze(value)); |
| 71 |
|
} |
| 72 |
0 |
} |
| 73 |
|
|
| 74 |
|
private Object unsqueezeIfNotNull(IRequestCycle cycle, final String key) |
| 75 |
|
{ |
| 76 |
|
|
| 77 |
0 |
String value = cycle.getParameter(key); |
| 78 |
0 |
if (value != null) |
| 79 |
|
{ |
| 80 |
0 |
return dataSqueezer.unsqueeze(value); |
| 81 |
|
} |
| 82 |
|
|
| 83 |
0 |
return null; |
| 84 |
|
} |
| 85 |
|
|
| 86 |
|
public void service(IRequestCycle cycle) throws IOException |
| 87 |
|
{ |
| 88 |
0 |
PageType pageType = null; |
| 89 |
|
try |
| 90 |
|
{ |
| 91 |
0 |
pageType = PageType.valueOf(cycle.getParameter(ServiceConstants.PAGE_TYPE).toUpperCase()); |
| 92 |
0 |
} catch (IllegalArgumentException e) |
| 93 |
|
{ |
| 94 |
0 |
throw e; |
| 95 |
|
} |
| 96 |
|
|
| 97 |
0 |
IClassDescriptor classDescriptor = |
| 98 |
0 |
(IClassDescriptor) dataSqueezer.unsqueeze(cycle.getParameter(ServiceConstants.CLASS_DESCRIPTOR)); |
| 99 |
0 |
Object model = unsqueezeIfNotNull(cycle, ServiceConstants.MODEL); |
| 100 |
0 |
CollectionDescriptor collectionDescriptor = |
| 101 |
0 |
(CollectionDescriptor) unsqueezeIfNotNull(cycle, ServiceConstants.ASSOC_DESCRIPTOR); |
| 102 |
0 |
Object parent = unsqueezeIfNotNull(cycle, ServiceConstants.PAERNT_MODEL); |
| 103 |
|
|
| 104 |
0 |
activateTrailsPage(cycle, pageType, classDescriptor, model, parent, collectionDescriptor); |
| 105 |
|
|
| 106 |
0 |
responseRenderer.renderResponse(cycle); |
| 107 |
0 |
} |
| 108 |
|
|
| 109 |
|
private void activateTrailsPage(IRequestCycle cycle, PageType pageType, IClassDescriptor classDescriptor, Object model, Object parent, CollectionDescriptor collectionDescriptor) |
| 110 |
|
{ |
| 111 |
|
|
| 112 |
0 |
IPage rawPage = null; |
| 113 |
0 |
IActivatableTrailsPage page = null; |
| 114 |
|
|
| 115 |
|
try |
| 116 |
|
{ |
| 117 |
|
|
| 118 |
0 |
rawPage = pageResolver.resolvePage(cycle, classDescriptor.getType(), pageType); |
| 119 |
|
|
| 120 |
0 |
} catch (PageNotFoundException ae) |
| 121 |
|
{ |
| 122 |
0 |
if (PageType.NEW.equals(pageType)) |
| 123 |
|
{ |
| 124 |
0 |
rawPage = pageResolver.resolvePage(cycle, classDescriptor.getType(), PageType.EDIT); |
| 125 |
|
try |
| 126 |
|
{ |
| 127 |
0 |
model = builderDirector.createNewInstance(classDescriptor.getType()); |
| 128 |
0 |
} catch (Exception ex) |
| 129 |
|
{ |
| 130 |
0 |
throw new TrailsRuntimeException(ex, classDescriptor.getType()); |
| 131 |
|
} |
| 132 |
|
} else |
| 133 |
|
{ |
| 134 |
0 |
throw ae; |
| 135 |
|
} |
| 136 |
|
} |
| 137 |
|
|
| 138 |
|
|
| 139 |
|
try |
| 140 |
|
{ |
| 141 |
0 |
page = (IActivatableTrailsPage) rawPage; |
| 142 |
0 |
} catch (ClassCastException ex) |
| 143 |
|
{ |
| 144 |
0 |
throw new ApplicationRuntimeException( |
| 145 |
0 |
EngineMessages.pageNotCompatible(rawPage, IActivatableTrailsPage.class), rawPage, null, ex); |
| 146 |
|
} |
| 147 |
|
|
| 148 |
0 |
page.setClassDescriptor(classDescriptor); |
| 149 |
|
|
| 150 |
0 |
if (page instanceof ModelPage) |
| 151 |
|
{ |
| 152 |
0 |
((ModelPage) page).setModel(model); |
| 153 |
|
|
| 154 |
0 |
if (PageType.NEW.equals(pageType)) |
| 155 |
|
{ |
| 156 |
0 |
((ModelPage) page).setModelNew(true); |
| 157 |
0 |
if ((collectionDescriptor != null) && (parent != null) && |
| 158 |
0 |
(!StringUtils.isEmpty(collectionDescriptor.getInverseProperty()))) |
| 159 |
|
{ |
| 160 |
0 |
PropertyUtils.write(model, collectionDescriptor.getInverseProperty(), parent); |
| 161 |
|
} |
| 162 |
|
} else |
| 163 |
|
{ |
| 164 |
0 |
((ModelPage) page).setModelNew(false); |
| 165 |
|
} |
| 166 |
|
|
| 167 |
0 |
if (page instanceof EditPage) |
| 168 |
|
{ |
| 169 |
0 |
((EditPage) page).setParent(parent); |
| 170 |
0 |
((EditPage) page).setAssociationDescriptor(collectionDescriptor); |
| 171 |
|
} |
| 172 |
|
} |
| 173 |
|
|
| 174 |
0 |
Object[] parameters = linkFactory.extractListenerParameters(cycle); |
| 175 |
0 |
cycle.setListenerParameters(parameters); |
| 176 |
0 |
cycle.activate(page); |
| 177 |
|
|
| 178 |
0 |
page.activateTrailsPage(parameters, cycle); |
| 179 |
|
|
| 180 |
0 |
} |
| 181 |
|
|
| 182 |
|
public String getName() |
| 183 |
|
{ |
| 184 |
0 |
return Trails.TRAILS_PAGES_SERVICE; |
| 185 |
|
} |
| 186 |
|
|
| 187 |
|
public void setResponseRenderer(ResponseRenderer responseRenderer) |
| 188 |
|
{ |
| 189 |
0 |
this.responseRenderer = responseRenderer; |
| 190 |
0 |
} |
| 191 |
|
|
| 192 |
|
public void setLinkFactory(LinkFactory linkFactory) |
| 193 |
|
{ |
| 194 |
0 |
this.linkFactory = linkFactory; |
| 195 |
0 |
} |
| 196 |
|
|
| 197 |
|
public void setDataSqueezer(DataSqueezer dataSqueezer) |
| 198 |
|
{ |
| 199 |
0 |
this.dataSqueezer = dataSqueezer; |
| 200 |
0 |
} |
| 201 |
|
|
| 202 |
|
public void setPageResolver(PageResolver pageResolver) |
| 203 |
|
{ |
| 204 |
0 |
this.pageResolver = pageResolver; |
| 205 |
0 |
} |
| 206 |
|
|
| 207 |
|
public void setBuilderDirector(BuilderDirector builderDirector) |
| 208 |
|
{ |
| 209 |
0 |
this.builderDirector = builderDirector; |
| 210 |
0 |
} |
| 211 |
|
|
| 212 |
|
} |
| 213 |
|
|