| 1 |
|
package org.trails.engine.state; |
| 2 |
|
|
| 3 |
|
import org.apache.tapestry.engine.state.StateObjectFactory; |
| 4 |
|
import org.apache.tapestry.engine.state.StateObjectPersistenceManager; |
| 5 |
|
import org.apache.tapestry.services.DataSqueezer; |
| 6 |
|
import org.apache.tapestry.web.WebRequest; |
| 7 |
|
import org.trails.record.IClientAsoPropertyPersistenceStrategy; |
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
40 |
public class RequestScopeManager implements StateObjectPersistenceManager |
| 16 |
|
{ |
| 17 |
|
|
| 18 |
|
private WebRequest request; |
| 19 |
|
private DataSqueezer dataSqueezer; |
| 20 |
|
private IClientAsoPropertyPersistenceStrategy clientAsoPropertyPersistenceStrategy; |
| 21 |
20 |
public boolean stateful = true; |
| 22 |
|
|
| 23 |
|
private static final String PREFIX = "aso:"; |
| 24 |
|
|
| 25 |
|
private String buildKey(String objectName) |
| 26 |
|
{ |
| 27 |
20 |
return PREFIX + objectName; |
| 28 |
|
} |
| 29 |
|
|
| 30 |
|
public boolean exists(String objectName) |
| 31 |
|
{ |
| 32 |
8 |
String key = buildKey(objectName); |
| 33 |
8 |
return request.getAttribute(key) != null || request.getParameterValue(key) != null; |
| 34 |
|
} |
| 35 |
|
|
| 36 |
|
public Object get(String objectName, StateObjectFactory factory) |
| 37 |
|
{ |
| 38 |
8 |
String key = buildKey(objectName); |
| 39 |
|
|
| 40 |
8 |
if (request.getAttribute(key) != null) |
| 41 |
|
{ |
| 42 |
4 |
return request.getAttribute(key); |
| 43 |
|
} |
| 44 |
|
|
| 45 |
4 |
Object result = unsqueeze(key); |
| 46 |
|
|
| 47 |
4 |
if (result == null) |
| 48 |
|
{ |
| 49 |
4 |
result = factory.createStateObject(); |
| 50 |
4 |
storeWithKey(key, result); |
| 51 |
|
} |
| 52 |
|
|
| 53 |
4 |
return result; |
| 54 |
|
} |
| 55 |
|
|
| 56 |
|
private synchronized Object unsqueeze(String key) |
| 57 |
|
{ |
| 58 |
4 |
if (request.getAttribute(key) != null) |
| 59 |
|
{ |
| 60 |
0 |
return request.getAttribute(key); |
| 61 |
|
} |
| 62 |
|
|
| 63 |
4 |
if (request.getParameterValue(key) != null) |
| 64 |
|
{ |
| 65 |
0 |
storeWithKey(key, dataSqueezer.unsqueeze(request.getParameterValue(key))); |
| 66 |
|
} |
| 67 |
|
|
| 68 |
4 |
return request.getAttribute(key); |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
public void store(String objectName, Object stateObject) |
| 72 |
|
{ |
| 73 |
4 |
String key = buildKey(objectName); |
| 74 |
4 |
storeWithKey(key, stateObject); |
| 75 |
4 |
} |
| 76 |
|
|
| 77 |
|
private void storeWithKey(String key, Object stateObject) |
| 78 |
|
{ |
| 79 |
8 |
request.setAttribute(key, stateObject); |
| 80 |
8 |
if (isStateful()) |
| 81 |
|
{ |
| 82 |
0 |
clientAsoPropertyPersistenceStrategy.addPropertyName(key); |
| 83 |
|
} |
| 84 |
8 |
} |
| 85 |
|
|
| 86 |
|
public boolean isStateful() |
| 87 |
|
{ |
| 88 |
8 |
return stateful && clientAsoPropertyPersistenceStrategy != null; |
| 89 |
|
} |
| 90 |
|
|
| 91 |
|
public void setRequest(WebRequest request) |
| 92 |
|
{ |
| 93 |
20 |
this.request = request; |
| 94 |
20 |
} |
| 95 |
|
|
| 96 |
|
public void setDataSqueezer(DataSqueezer dataSqueezer) |
| 97 |
|
{ |
| 98 |
0 |
this.dataSqueezer = dataSqueezer; |
| 99 |
0 |
} |
| 100 |
|
|
| 101 |
|
public void setClientAsoPropertyPersistenceStrategy(IClientAsoPropertyPersistenceStrategy clientAsoPropertyPersistenceStrategy) |
| 102 |
|
{ |
| 103 |
0 |
this.clientAsoPropertyPersistenceStrategy = clientAsoPropertyPersistenceStrategy; |
| 104 |
0 |
} |
| 105 |
|
|
| 106 |
|
public void setStateful(boolean stateful) |
| 107 |
|
{ |
| 108 |
4 |
this.stateful = stateful; |
| 109 |
4 |
} |
| 110 |
|
} |