You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by ah...@apache.org on 2023/06/29 11:06:37 UTC
[causeway] branch 3500-entity.state.cleanup updated: CAUSEWAY-3500: refactors TitleService to swallow exceptions if fails to reattach entities
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch 3500-entity.state.cleanup
in repository https://gitbox.apache.org/repos/asf/causeway.git
The following commit(s) were added to refs/heads/3500-entity.state.cleanup by this push:
new 2b9f7ade56 CAUSEWAY-3500: refactors TitleService to swallow exceptions if fails to reattach entities
2b9f7ade56 is described below
commit 2b9f7ade56782bc783194905f7cccb9a0496de0b
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Jun 29 13:06:32 2023 +0200
CAUSEWAY-3500: refactors TitleService to swallow exceptions if fails to
reattach entities
- only 5 unsafe methods left
---
.../applib/services/repository/EntityState.java | 42 +++++++++------------
.../core/metamodel/object/MmEntityUtils.java | 43 +++++++++++++---------
.../core/metamodel/object/MmVisibilityUtils.java | 2 +-
.../services/title/TitleServiceDefault.java | 25 +++++++++----
.../metamodel/facets/entity/JdoEntityFacet.java | 4 +-
.../applib/fixturescripts/FixtureScripts.java | 16 +++++++-
6 files changed, 80 insertions(+), 52 deletions(-)
diff --git a/api/applib/src/main/java/org/apache/causeway/applib/services/repository/EntityState.java b/api/applib/src/main/java/org/apache/causeway/applib/services/repository/EntityState.java
index bf71076de1..8d8caa8de3 100644
--- a/api/applib/src/main/java/org/apache/causeway/applib/services/repository/EntityState.java
+++ b/api/applib/src/main/java/org/apache/causeway/applib/services/repository/EntityState.java
@@ -107,12 +107,22 @@ public enum EntityState {
public boolean isJdoDeleted() { return this == JDO_DELETED; }
/** @see #DETACHED */
public boolean isDetached() { return this == DETACHED; }
- /** @see #TRANSIENT_OR_REMOVED */
- public boolean isTransientOrRemoved() { return this == TRANSIENT_OR_REMOVED; }
// -- ADVANCED PREDICATES
+ /**
+ * @see #TRANSIENT_OR_REMOVED
+ * @see #JDO_DELETED
+ */
+ public boolean isTransientOrRemoved() {
+ return this == TRANSIENT_OR_REMOVED
+ || isJdoDeleted();
+ }
+ public boolean canDelete() {
+ return isAttached()
+ || isDetached();
+ }
// -- DEPRECATIONS
@@ -123,29 +133,9 @@ public enum EntityState {
public boolean isDeleted() {
return isJdoHollow()
|| isTransientOrRemoved()
- || isJdoDeleted()
|| isDetached();
}
- //TODO[CAUSEWAY-3500] potential misnomer: if detached per def has an OID
- //FIXME[CAUSEWAY-3500] in fact hollow can now be re-attached
- public boolean isDetachedCannotReattach() {
- return (isJdoHollow()
- || isTransientOrRemoved()
- || isJdoDeleted())
- && !isDetached();
- }
-
- /**
- * @apiNote 'removed' is only supported by JDO.
- * @deprecated not supported by JPA
- */
- @Deprecated
- public boolean isAttachedOrRemoved() {
- return isAttached()
- || isJdoDeleted();
- }
-
//FIXME[CAUSEWAY-3500] hollow has OID
public boolean hasOid() {
return isAttached() || isDetached();
@@ -158,12 +148,16 @@ public enum EntityState {
|| isDetached();
}
+ //FIXME[CAUSEWAY-3500] 3 different types of flush checks, which is it though
public boolean canFlush() {
return !unsafe.hasOid();
}
- public boolean canDelete() {
- return unsafe.hasOid();
+ //FIXME[CAUSEWAY-3500] 3 different types of flush checks, which is it though
+ public boolean shouldFlush() {
+ return isPersistable()
+ && !isAttached()
+ && !isJdoDeleted();
}
}
diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/object/MmEntityUtils.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/object/MmEntityUtils.java
index 719ba54a60..e5f7648b2b 100644
--- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/object/MmEntityUtils.java
+++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/object/MmEntityUtils.java
@@ -24,6 +24,7 @@ import java.util.stream.Stream;
import org.springframework.lang.Nullable;
import org.apache.causeway.applib.services.repository.EntityState;
+import org.apache.causeway.commons.functional.Try;
import org.apache.causeway.commons.internal.assertions._Assert;
import org.apache.causeway.commons.internal.exceptions._Exceptions;
import org.apache.causeway.core.config.beans.PersistenceStack;
@@ -61,6 +62,12 @@ public final class MmEntityUtils {
: EntityState.NOT_PERSISTABLE;
}
+ public Try<EntityState> tryGetEntityState(final @Nullable ManagedObject adapter) {
+ return adapter!=null
+ ? Try.call(adapter::getEntityState)
+ : Try.success(EntityState.NOT_PERSISTABLE);
+ }
+
public void persistInCurrentTransaction(final ManagedObject managedObject) {
requiresEntity(managedObject);
val spec = managedObject.getSpecification();
@@ -98,7 +105,7 @@ public final class MmEntityUtils {
|| entity.isBookmarkMemoized()) {
return;
}
- if(canFlush(entity)) {
+ if(getEntityState(entity).unsafe.canFlush()) {
entity.getTransactionService().flushTransaction();
// force reassessment: as a side-effect transitions the transient entity to a bookmarked one
entity.getEntityState();
@@ -106,13 +113,24 @@ public final class MmEntityUtils {
}
/**
- * Side-effect free check for whether given entity is attached.
+ * Side-effect free check for whether given domain object is an entity and is attached.
+ */
+ public boolean isAttachedEntity(final @Nullable ManagedObject domainObject) {
+ return domainObject!=null
+ ? domainObject.getSpecialization().isEntity()
+ && domainObject.isBookmarkMemoized()
+ && domainObject.getEntityState().isAttached()
+ : false;
+ }
+
+ /**
+ * Side-effect free check for whether given domain object is an entity and is not attached.
*/
- public boolean isAttachedEntity(final @Nullable ManagedObject entity) {
- return entity!=null
- ? entity.getSpecialization().isEntity()
- && entity.isBookmarkMemoized()
- && entity.getEntityState().isAttached()
+ public boolean isNonAttachedEntity(final @Nullable ManagedObject domainObject) {
+ return domainObject!=null
+ ? domainObject.getSpecialization().isEntity()
+ && domainObject.isBookmarkMemoized()
+ && !domainObject.getEntityState().isAttached()
: false;
}
@@ -191,17 +209,8 @@ public final class MmEntityUtils {
return getEntityState(adapter).hasOid();
}
- public boolean canFlush(final @Nullable ManagedObject adapter) {
- return getEntityState(adapter).unsafe.canFlush();
- }
-
public boolean canDelete(final @Nullable ManagedObject adapter) {
- return getEntityState(adapter).unsafe.canDelete();
- }
-
- //TODO[CAUSEWAY-3500] potential misnomer: if detached per def has an OID
- public boolean isDetachedCannotReattach(final @Nullable ManagedObject entity) {
- return getEntityState(entity).unsafe.isDetachedCannotReattach();
+ return getEntityState(adapter).canDelete();
}
public boolean isDeleted(final @Nullable ManagedObject entity) {
diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/object/MmVisibilityUtils.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/object/MmVisibilityUtils.java
index e27aaf446a..d82c997e07 100644
--- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/object/MmVisibilityUtils.java
+++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/object/MmVisibilityUtils.java
@@ -99,7 +99,7 @@ public final class MmVisibilityUtils {
}
val spec = adapter.getSpecification();
if(spec.isEntity()) {
- if(MmEntityUtils.isDetachedCannotReattach(adapter)) {
+ if(MmEntityUtils.getEntityState(adapter).isTransientOrRemoved()) {
return false;
}
}
diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/services/title/TitleServiceDefault.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/services/title/TitleServiceDefault.java
index 7bf70b39c2..984701372c 100644
--- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/services/title/TitleServiceDefault.java
+++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/services/title/TitleServiceDefault.java
@@ -26,6 +26,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.apache.causeway.applib.annotation.PriorityPrecedence;
+import org.apache.causeway.applib.services.repository.EntityState;
import org.apache.causeway.applib.services.title.TitleService;
import org.apache.causeway.applib.services.wrapper.WrapperFactory;
import org.apache.causeway.core.metamodel.CausewayModuleCoreMetamodel;
@@ -61,14 +62,24 @@ public class TitleServiceDefault implements TitleService {
return "[UNSPECIFIED]";
}
- if(MmEntityUtils.isDetachedCannotReattach(objectAdapter)) {
- return "[DETACHED]";
- } else {
- return objectAdapter.getSpecification().getTitle(
- TitleRenderRequest.builder()
- .object(objectAdapter)
- .build());
+ // side-effect free check
+ if(MmEntityUtils.isNonAttachedEntity(objectAdapter)) {
+
+ // try to re-attach / silently swallow exceptions
+ val isAttached = MmEntityUtils.tryGetEntityState(objectAdapter)
+ .getValue()
+ .map(EntityState::isAttached)
+ .orElse(false);
+
+ if(!isAttached) {
+ return "[DETACHED]";
+ }
}
+
+ return objectAdapter.getSpecification().getTitle(
+ TitleRenderRequest.builder()
+ .object(objectAdapter)
+ .build());
}
@Override
diff --git a/persistence/jdo/datanucleus/src/main/java/org/apache/causeway/persistence/jdo/datanucleus/metamodel/facets/entity/JdoEntityFacet.java b/persistence/jdo/datanucleus/src/main/java/org/apache/causeway/persistence/jdo/datanucleus/metamodel/facets/entity/JdoEntityFacet.java
index 2c05fec3d4..9392e32bed 100644
--- a/persistence/jdo/datanucleus/src/main/java/org/apache/causeway/persistence/jdo/datanucleus/metamodel/facets/entity/JdoEntityFacet.java
+++ b/persistence/jdo/datanucleus/src/main/java/org/apache/causeway/persistence/jdo/datanucleus/metamodel/facets/entity/JdoEntityFacet.java
@@ -382,8 +382,8 @@ implements EntityFacet {
// guard against misuse
_Assert.assertNullableObjectIsInstanceOf(pojo, entityClass);
- if (!DnEntityStateProvider.entityState(pojo).unsafe.canDelete()) {
- throw _Exceptions.illegalArgument("can only delete an attached entity");
+ if (!DnEntityStateProvider.entityState(pojo).canDelete()) {
+ throw _Exceptions.illegalArgument("can only delete an entity when its state is ATTACHED OR DETACHED");
}
val pm = getPersistenceManager();
diff --git a/testing/fixtures/applib/src/main/java/org/apache/causeway/testing/fixtures/applib/fixturescripts/FixtureScripts.java b/testing/fixtures/applib/src/main/java/org/apache/causeway/testing/fixtures/applib/fixturescripts/FixtureScripts.java
index 41178a061a..ec6186bb7e 100644
--- a/testing/fixtures/applib/src/main/java/org/apache/causeway/testing/fixtures/applib/fixturescripts/FixtureScripts.java
+++ b/testing/fixtures/applib/src/main/java/org/apache/causeway/testing/fixtures/applib/fixturescripts/FixtureScripts.java
@@ -541,7 +541,7 @@ public class FixtureScripts {
}
if (object instanceof ViewModel
- || repositoryService.getEntityState(object).unsafe.isAttachedOrRemoved()) {
+ || repositoryService.getEntityState(object).unsafe.shouldFlush()) {
// continue
} else {
switch(getNonPersistedObjectsStrategy()) {
@@ -554,6 +554,20 @@ public class FixtureScripts {
throw _Exceptions.unmatchedCase(getNonPersistedObjectsStrategy());
}
}
+
+
+// switch(getNonPersistedObjectsStrategy()) {
+// case PERSIST:
+// if (repositoryService.getEntityState(object).unsafe.shouldFlush()) {
+// transactionService.flushTransaction();
+// }
+// break;
+// case IGNORE:
+// return null;
+// default:
+// throw _Exceptions.unmatchedCase(getNonPersistedObjectsStrategy());
+// }
+
final FixtureResult fixtureResult = serviceInjector.injectServicesInto(
new FixtureResult());
fixtureResult.setFixtureScriptClassName(firstTime ? script.getClass().getName() : null);