You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2021/03/17 16:27:07 UTC

[isis] branch master updated: ISIS-2579: fix jpa-entity-facet to also detect deleted/removed entity state

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new 09c9410  ISIS-2579: fix jpa-entity-facet to also detect deleted/removed entity state
09c9410 is described below

commit 09c94106f45cc81e1c9bea1d3826901a74632f9d
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Mar 17 17:26:42 2021 +0100

    ISIS-2579: fix jpa-entity-facet to also detect deleted/removed entity
    state
---
 .../isis/applib/services/repository/EntityState.java |  2 +-
 .../isis/core/metamodel/spec/ManagedObjects.java     |  4 ++--
 .../integration/JpaEntityInjectionPointResolver.java |  5 ++---
 .../integration/metamodel/JpaEntityFacetFactory.java | 15 ++++++++++-----
 .../viewer/wicket/ui/panels/FormExecutorDefault.java | 20 +++++++++++---------
 5 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/api/applib/src/main/java/org/apache/isis/applib/services/repository/EntityState.java b/api/applib/src/main/java/org/apache/isis/applib/services/repository/EntityState.java
index 2464e3a..8beccd0 100644
--- a/api/applib/src/main/java/org/apache/isis/applib/services/repository/EntityState.java
+++ b/api/applib/src/main/java/org/apache/isis/applib/services/repository/EntityState.java
@@ -46,7 +46,7 @@ public enum EntityState {
     PERSISTABLE_DETACHED,
     /**
      * Object with this state is an entity that has been removed from the
-     * database.  Objects in this state may no longer be interacted with.
+     * database. Objects in this state may no longer be interacted with.
      */
     PERSISTABLE_DESTROYED,
     ;
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/spec/ManagedObjects.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/spec/ManagedObjects.java
index 8a4d366..c019ab8 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/spec/ManagedObjects.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/spec/ManagedObjects.java
@@ -410,13 +410,13 @@ public final class ManagedObjects {
         
         @NonNull
         public static EntityState getEntityState(@Nullable ManagedObject adapter) {
-            if(adapter==null) {
+            if(isNullOrUnspecifiedOrEmpty(adapter)) {
                 return EntityState.NOT_PERSISTABLE;
             }
             val spec = adapter.getSpecification();
             val pojo = adapter.getPojo();
             
-            if(spec==null || pojo==null || !spec.isEntity()) {
+            if(!spec.isEntity()) {
                 return EntityState.NOT_PERSISTABLE;
             }
 
diff --git a/persistence/jpa/applib/src/main/java/org/apache/isis/persistence/jpa/applib/integration/JpaEntityInjectionPointResolver.java b/persistence/jpa/applib/src/main/java/org/apache/isis/persistence/jpa/applib/integration/JpaEntityInjectionPointResolver.java
index 6908348..b6d5085 100644
--- a/persistence/jpa/applib/src/main/java/org/apache/isis/persistence/jpa/applib/integration/JpaEntityInjectionPointResolver.java
+++ b/persistence/jpa/applib/src/main/java/org/apache/isis/persistence/jpa/applib/integration/JpaEntityInjectionPointResolver.java
@@ -60,13 +60,12 @@ public class JpaEntityInjectionPointResolver {
     @PostUpdate
     @PostRemove
     private void afterAnyUpdate(Object entityPojo) {
-        log.debug("afterAnyUpdate: {}" + entityPojo);
-        //serviceInjector.injectServicesInto(entityPojo);
+        log.debug("afterAnyUpdate: {}", entityPojo);
     }
 
     @PostLoad
     private void afterLoad(Object entityPojo) {
-        log.debug("afterLoad: {}" + entityPojo);
+        log.debug("afterLoad: {}", entityPojo);
         serviceInjector.injectServicesInto(entityPojo);
     }
 
diff --git a/persistence/jpa/integration/src/main/java/org/apache/isis/persistence/jpa/integration/metamodel/JpaEntityFacetFactory.java b/persistence/jpa/integration/src/main/java/org/apache/isis/persistence/jpa/integration/metamodel/JpaEntityFacetFactory.java
index c0ba41b..ad1d205 100644
--- a/persistence/jpa/integration/src/main/java/org/apache/isis/persistence/jpa/integration/metamodel/JpaEntityFacetFactory.java
+++ b/persistence/jpa/integration/src/main/java/org/apache/isis/persistence/jpa/integration/metamodel/JpaEntityFacetFactory.java
@@ -294,11 +294,16 @@ public class JpaEntityFacetFactory extends FacetFactoryAbstract {
                 return EntityState.PERSISTABLE_ATTACHED;
             }
 
-            //TODO[2033] how to determine whether deleted? (even relevant?)
-//            if(isDeleted) {
-//                return EntityState.PERSISTABLE_DESTROYED;
-//            }
-            return EntityState.PERSISTABLE_DETACHED;
+            val primaryKey = getPersistenceUnitUtil(entityManager).getIdentifier(pojo);
+            if(primaryKey == null) {
+                return EntityState.PERSISTABLE_DETACHED;
+            }
+            
+            //XXX this find operation is potentially expensive, 
+            // compared to JDO, which does not require this extra step 
+            return entityManager.find(entityClass, primaryKey)==null
+                    ? EntityState.PERSISTABLE_DESTROYED
+                    : EntityState.PERSISTABLE_DETACHED;
         }
 
         @Override
diff --git a/viewers/wicket/ui/src/main/java/org/apache/isis/viewer/wicket/ui/panels/FormExecutorDefault.java b/viewers/wicket/ui/src/main/java/org/apache/isis/viewer/wicket/ui/panels/FormExecutorDefault.java
index 0db6c5e..6648cb3 100644
--- a/viewers/wicket/ui/src/main/java/org/apache/isis/viewer/wicket/ui/panels/FormExecutorDefault.java
+++ b/viewers/wicket/ui/src/main/java/org/apache/isis/viewer/wicket/ui/panels/FormExecutorDefault.java
@@ -134,13 +134,18 @@ implements FormExecutor {
             // (The DB exception might actually be thrown by the flush() that follows.
             //
             val resultAdapter = obtainResultAdapter();
+            
             // flush any queued changes; any concurrency or violation exceptions will actually be thrown here
             if(commonContext.getInteractionTracker().isInInteractionSession()) {
                 commonContext.getTransactionService().flushTransaction();
 
-                // update target, since version updated
-                targetAdapter = targetEntityModel.getManagedObject();
-                if(!EntityUtil.isDestroyed(targetAdapter)) {
+                if(EntityUtil.isDestroyed(targetAdapter)) {
+                    // if this was an entity delete action
+                    // then we don't re-fetch / re-create the targetAdapter  
+                    targetAdapter = ManagedObject.empty(targetAdapter.getSpecification());
+                } else {
+                    // update target, since version updated
+                    targetAdapter = targetEntityModel.getManagedObject();
                     targetEntityModel.resetPropertyModels();
                 }
             }
@@ -148,12 +153,9 @@ implements FormExecutor {
             // hook to close prompt etc.
             onExecuteAndProcessResults(targetIfAny);
 
-            final M model = this.model;
-            RedirectFacet redirectFacet = null;
-            if(model instanceof ActionModel) {
-                final ActionModel actionModel = (ActionModel) model;
-                redirectFacet = actionModel.getMetaModel().getFacet(RedirectFacet.class);
-            }
+            val redirectFacet =  model instanceof ActionModel
+                ? ((ActionModel) model).getMetaModel().getFacet(RedirectFacet.class)
+                : null;
 
             if (shouldRedirect(targetAdapter, resultAdapter, redirectFacet)
                     || hasBlobsOrClobs(page)