| 1 |
0 |
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
package org.trails.record; |
| 16 |
|
|
| 17 |
|
import org.apache.commons.logging.Log; |
| 18 |
|
import org.apache.commons.logging.LogFactory; |
| 19 |
|
import org.apache.hivemind.util.Defense; |
| 20 |
|
import org.apache.tapestry.engine.ServiceEncoding; |
| 21 |
|
import org.apache.tapestry.record.PropertyChange; |
| 22 |
|
import org.apache.tapestry.record.PropertyPersistenceStrategy; |
| 23 |
|
import org.apache.tapestry.record.RecordUtils; |
| 24 |
|
import org.apache.tapestry.record.WebSessionAttributeCallback; |
| 25 |
|
import org.apache.tapestry.web.WebRequest; |
| 26 |
|
import org.apache.tapestry.web.WebSession; |
| 27 |
|
|
| 28 |
|
import java.util.ArrayList; |
| 29 |
|
import java.util.Collection; |
| 30 |
|
import java.util.Collections; |
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
0 |
public abstract class AbstractSessionPropertyPersistenceStrategy implements PropertyPersistenceStrategy |
| 39 |
|
{ |
| 40 |
0 |
private static final Log LOG = LogFactory.getLog(AbstractSessionPropertyPersistenceStrategy.class); |
| 41 |
|
|
| 42 |
|
private String applicationId; |
| 43 |
|
private WebRequest request; |
| 44 |
|
|
| 45 |
|
protected abstract String getStrategyId(); |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
public void store(String pageName, String idPath, String propertyName, Object newValue) |
| 53 |
|
{ |
| 54 |
0 |
Defense.notNull(pageName, "pageName"); |
| 55 |
0 |
Defense.notNull(propertyName, "propertyName"); |
| 56 |
0 |
WebSession session = request.getSession(true); |
| 57 |
0 |
String attributeName = RecordUtils.buildChangeKey(getStrategyId(), applicationId, pageName, idPath, propertyName); |
| 58 |
0 |
session.setAttribute(attributeName, newValue); |
| 59 |
0 |
} |
| 60 |
|
|
| 61 |
|
public Collection getStoredChanges(String pageName) |
| 62 |
|
{ |
| 63 |
0 |
Defense.notNull(pageName, "pageName"); |
| 64 |
|
|
| 65 |
0 |
WebSession session = request.getSession(false); |
| 66 |
|
|
| 67 |
0 |
if (session == null) |
| 68 |
0 |
return Collections.EMPTY_LIST; |
| 69 |
|
|
| 70 |
0 |
final Collection result = new ArrayList(); |
| 71 |
|
|
| 72 |
0 |
WebSessionAttributeCallback callback = new WebSessionAttributeCallback() |
| 73 |
|
{ |
| 74 |
|
public void handleAttribute(WebSession sess, String name) |
| 75 |
|
{ |
| 76 |
0 |
PropertyChange change = RecordUtils.buildChange(name, sess.getAttribute(name)); |
| 77 |
|
|
| 78 |
0 |
result.add(change); |
| 79 |
0 |
} |
| 80 |
|
}; |
| 81 |
|
|
| 82 |
0 |
RecordUtils.iterateOverMatchingAttributes(getStrategyId(), applicationId, pageName, session, callback); |
| 83 |
|
|
| 84 |
0 |
return result; |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
public void discardStoredChanges(String pageName) |
| 88 |
|
{ |
| 89 |
0 |
WebSession session = request.getSession(false); |
| 90 |
|
|
| 91 |
0 |
if (session == null) return; |
| 92 |
|
|
| 93 |
0 |
WebSessionAttributeCallback callback = new WebSessionAttributeCallback() |
| 94 |
|
{ |
| 95 |
|
public void handleAttribute(WebSession sess, String name) |
| 96 |
|
{ |
| 97 |
0 |
sess.setAttribute(name, null); |
| 98 |
0 |
} |
| 99 |
|
}; |
| 100 |
|
|
| 101 |
0 |
RecordUtils.iterateOverMatchingAttributes(getStrategyId(), applicationId, pageName, session, callback); |
| 102 |
0 |
} |
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
public void addParametersForPersistentProperties(ServiceEncoding encoding, |
| 109 |
|
boolean post) |
| 110 |
|
{ |
| 111 |
0 |
} |
| 112 |
|
|
| 113 |
|
public void setApplicationId(String applicationName) |
| 114 |
|
{ |
| 115 |
0 |
this.applicationId = applicationName; |
| 116 |
0 |
} |
| 117 |
|
|
| 118 |
|
public void setRequest(WebRequest request) |
| 119 |
|
{ |
| 120 |
0 |
this.request = request; |
| 121 |
0 |
} |
| 122 |
|
} |