Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TAP5-2749: Incorrect behavior of getIfExists in EntityApplicationStatePersistenceStrategy #36

Merged
merged 5 commits into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,16 @@ public <T> T getIfExists(Class<T> ssoClass)

Object sso = session.getAttribute(key);

return sso != null ? (T)sso : null;
return transformPersistedValue(sso);
}
return null;
}

protected <T> T transformPersistedValue(Object value)
{
return (T) value;
}

protected <T> String buildKey(Class<T> ssoClass)
{
return PREFIX + ssoClass.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public EntityApplicationStatePersistenceStrategy(Request request, Session hibern
delegate = new EntityPersistentFieldStrategy(hibernateSession, null);
}

@Override
protected <T> T transformPersistedValue(Object value)
{
return value instanceof SessionRestorable ? (T) delegate.convertPersistedToApplicationValue(value) : (T) value;
}

@Override
@SuppressWarnings("unchecked")
public <T> T get(Class<T> ssoClass, ApplicationStateCreator<T> creator)
Expand All @@ -44,7 +50,7 @@ public <T> T get(Class<T> ssoClass, ApplicationStateCreator<T> creator)

if (persistedValue instanceof SessionRestorable)
{
Object restored = delegate.convertPersistedToApplicationValue(persistedValue);
Object restored = transformPersistedValue(persistedValue);

// Maybe throw an exception instead?
if (restored == null)
Expand Down
1 change: 0 additions & 1 deletion tapestry-jpa/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ task runTestApp6(type:JavaExec) {
args "-d", "src/test/app6", "-p", "8080"
classpath += project.sourceSets.test.runtimeClasspath
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public EntityApplicationStatePersistenceStrategy(final Request request,
this.entityManagerManager = entityManagerManager;
}

@Override
protected <T> T transformPersistedValue(Object value)
{
return value instanceof PersistedEntity ? (T) ((PersistedEntity) value).restore(entityManagerManager) : (T) value;
}

@Override
public <T> T get(final Class<T> ssoClass, final ApplicationStateCreator<T> creator)
{
Expand All @@ -41,7 +47,7 @@ public <T> T get(final Class<T> ssoClass, final ApplicationStateCreator<T> creat
{
final PersistedEntity persisted = (PersistedEntity) persistedValue;

final Object restored = persisted.restore(entityManagerManager);
final Object restored = transformPersistedValue(persisted);

// shall we maybe throw an exception instead?
if (restored == null)
Expand Down
3 changes: 2 additions & 1 deletion tapestry-jpa/src/test/app6/SSOEntity.tml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
<body>
<p>entity name: <span id="name">${user?.firstName}</span></p>
<p>persisted entity class name: <span id="persistedEntityClassName">${persistedEntityClassName}</span></p>
<p>userIfExists: <span id="userIfExists">${userIfExists}</span></p>
<p><t:eventlink event="persistEntity">persist entity</t:eventlink></p>
<p><t:eventlink event="setToNull">set to null</t:eventlink></p>
<p><t:eventlink event="setToTransient">set to transient</t:eventlink></p>
<p><t:eventlink event="delete">delete</t:eventlink></p>
</body>
</html>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.tapestry5.http.services.Request;
import org.apache.tapestry5.http.services.Session;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.ApplicationStateManager;
import org.example.app6.entities.User;
import org.example.app6.services.UserDAO;

Expand All @@ -35,6 +36,14 @@ public class SSOEntity
@Inject
private Request request;

@Inject
ApplicationStateManager manager;

public User getUserIfExists()
{
return manager.getIfExists(User.class);
}

void onPersistEntity()
{
final User user = new User();
Expand Down