| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
package org.apache.tapestry5.internal.services; |
| 16 | |
|
| 17 | |
import static org.apache.tapestry5.ioc.internal.util.CollectionFactory.newList; |
| 18 | |
import static org.apache.tapestry5.ioc.internal.util.Defense.notBlank; |
| 19 | |
import org.apache.tapestry5.services.PersistentFieldChange; |
| 20 | |
import org.apache.tapestry5.services.PersistentFieldStrategy; |
| 21 | |
import org.apache.tapestry5.services.Request; |
| 22 | |
import org.apache.tapestry5.services.Session; |
| 23 | |
|
| 24 | |
import java.util.Collection; |
| 25 | |
import java.util.Collections; |
| 26 | |
import java.util.List; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
public abstract class AbstractSessionPersistentFieldStrategy implements PersistentFieldStrategy |
| 33 | |
{ |
| 34 | |
private final String prefix; |
| 35 | |
|
| 36 | |
private final Request request; |
| 37 | |
|
| 38 | |
protected AbstractSessionPersistentFieldStrategy(String prefix, Request request) |
| 39 | 56 | { |
| 40 | 56 | this.prefix = prefix; |
| 41 | 56 | this.request = request; |
| 42 | 56 | } |
| 43 | |
|
| 44 | |
public final Collection<PersistentFieldChange> gatherFieldChanges(String pageName) |
| 45 | |
{ |
| 46 | 3638 | Session session = request.getSession(false); |
| 47 | |
|
| 48 | 3638 | if (session == null) return Collections.emptyList(); |
| 49 | |
|
| 50 | 3520 | List<PersistentFieldChange> result = newList(); |
| 51 | |
|
| 52 | 3520 | String fullPrefix = prefix + pageName + ":"; |
| 53 | |
|
| 54 | 3520 | for (String name : session.getAttributeNames(fullPrefix)) |
| 55 | |
{ |
| 56 | 1604 | Object persistedValue = session.getAttribute(name); |
| 57 | |
|
| 58 | 1604 | Object applicationValue = persistedValue == null ? null : convertPersistedToApplicationValue( |
| 59 | |
persistedValue); |
| 60 | |
|
| 61 | 1604 | PersistentFieldChange change = buildChange(name, applicationValue); |
| 62 | |
|
| 63 | 1604 | result.add(change); |
| 64 | |
|
| 65 | 1604 | didReadChange(session, name); |
| 66 | 1604 | } |
| 67 | |
|
| 68 | 3520 | return result; |
| 69 | |
} |
| 70 | |
|
| 71 | |
public void discardChanges(String pageName) |
| 72 | |
{ |
| 73 | 8 | Session session = request.getSession(false); |
| 74 | |
|
| 75 | 8 | if (session == null) return; |
| 76 | |
|
| 77 | 6 | String fullPrefix = prefix + pageName + ":"; |
| 78 | |
|
| 79 | 6 | for (String name : session.getAttributeNames(fullPrefix)) |
| 80 | |
{ |
| 81 | 4 | session.setAttribute(name, null); |
| 82 | |
} |
| 83 | 6 | } |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
protected void didReadChange(Session session, String attributeName) |
| 93 | |
{ |
| 94 | 656 | } |
| 95 | |
|
| 96 | |
private PersistentFieldChange buildChange(String name, Object newValue) |
| 97 | |
{ |
| 98 | 1604 | String[] chunks = name.split(":"); |
| 99 | |
|
| 100 | |
|
| 101 | 1604 | String componentId = chunks[2]; |
| 102 | 1604 | String fieldName = chunks[3]; |
| 103 | |
|
| 104 | 1604 | return new PersistentFieldChangeImpl(componentId, fieldName, newValue); |
| 105 | |
} |
| 106 | |
|
| 107 | |
public final void postChange(String pageName, String componentId, String fieldName, |
| 108 | |
Object newValue) |
| 109 | |
{ |
| 110 | 774 | notBlank(pageName, "pageName"); |
| 111 | 774 | notBlank(fieldName, "fieldName"); |
| 112 | |
|
| 113 | 774 | Object persistedValue = newValue == null ? null : convertApplicationValueToPersisted(newValue); |
| 114 | |
|
| 115 | 774 | StringBuilder builder = new StringBuilder(prefix); |
| 116 | 774 | builder.append(pageName); |
| 117 | 774 | builder.append(':'); |
| 118 | |
|
| 119 | 774 | if (componentId != null) builder.append(componentId); |
| 120 | |
|
| 121 | 774 | builder.append(':'); |
| 122 | 774 | builder.append(fieldName); |
| 123 | |
|
| 124 | 774 | Session session = request.getSession(persistedValue != null); |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | 774 | if (session != null) |
| 130 | |
{ |
| 131 | 774 | session.setAttribute(builder.toString(), persistedValue); |
| 132 | |
} |
| 133 | 774 | } |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
protected Object convertApplicationValueToPersisted(Object newValue) |
| 145 | |
{ |
| 146 | 742 | return newValue; |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
protected Object convertPersistedToApplicationValue(Object persistedValue) |
| 158 | |
{ |
| 159 | 916 | return persistedValue; |
| 160 | |
} |
| 161 | |
} |