Skip to content

Commit

Permalink
TAP5-2749: Incorrect behavior of getIfExists in EntityApplicationStat…
Browse files Browse the repository at this point in the history
…ePersistenceStrategy (#36)

* TAP5-2749: fixing incorrect behavior of getIfExists in subclasses of SessionApplicationStatePersistenceStrategy

* TAP5-2749: add integration test

* TAP5-2749: use existing test aplication instead of creating new one

* TAP5-2749: Simplified test.

* TAP5-2749: Removed obsolete runTestApp7 task.

---------

Co-authored-by: Volker Lamp <vlamp@apache.org>
  • Loading branch information
bvfalcon and vjlamp committed Apr 23, 2023
1 parent 9501c0f commit dca4fd0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
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

0 comments on commit dca4fd0

Please sign in to comment.