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