You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2022/07/31 13:56:09 UTC

[isis] branch ISIS-3102 updated: ISIS-3002: reinstates special case of encoding some ObjectIds using UUID, also Long and Integer

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

danhaywood pushed a commit to branch ISIS-3102
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/ISIS-3102 by this push:
     new 97a4812c15 ISIS-3002: reinstates special case of encoding some ObjectIds using UUID, also Long and Integer
97a4812c15 is described below

commit 97a4812c15873361905d4ca5d102a071366db11e
Author: Dan Haywood <da...@haywood-associates.co.uk>
AuthorDate: Sun Jul 31 14:55:45 2022 +0100

    ISIS-3002: reinstates special case of encoding some ObjectIds using UUID, also Long and Integer
---
 .../integtest/CommandLog_IntegTestAbstract.java    |  7 +++-
 .../facets/entity/IdStringifierForObjectId.java    | 37 +++++++++++++++++++--
 .../entity/IdStringifierForObjectIdentity.java     | 38 ++++++++++++++++++++--
 3 files changed, 77 insertions(+), 5 deletions(-)

diff --git a/extensions/core/commandlog/applib/src/test/java/org/apache/isis/extensions/commandlog/applib/integtest/CommandLog_IntegTestAbstract.java b/extensions/core/commandlog/applib/src/test/java/org/apache/isis/extensions/commandlog/applib/integtest/CommandLog_IntegTestAbstract.java
index f52b1991b6..ec2727d0cf 100644
--- a/extensions/core/commandlog/applib/src/test/java/org/apache/isis/extensions/commandlog/applib/integtest/CommandLog_IntegTestAbstract.java
+++ b/extensions/core/commandlog/applib/src/test/java/org/apache/isis/extensions/commandlog/applib/integtest/CommandLog_IntegTestAbstract.java
@@ -252,7 +252,12 @@ public abstract class CommandLog_IntegTestAbstract extends IsisIntegrationTestAb
         assertThat(cleBookmarkIfAny).isPresent();
         Bookmark cleBookmark = cleBookmarkIfAny.get();
         String identifier = cleBookmark.getIdentifier();
-        UUID.fromString(identifier); // should not fail, ie check the format is as we expect
+        if (isisBeanTypeRegistry.determineCurrentPersistenceStack().isJdo()) {
+            assertThat(identifier).startsWith("u_");
+            UUID.fromString(identifier.substring("u_".length())); // should not fail, ie check the format is as we expect
+        } else {
+            UUID.fromString(identifier); // should not fail, ie check the format is as we expect
+        }
 
         // when we start a new session and lookup from the bookmark
         interactionService.nextInteraction();
diff --git a/persistence/jdo/datanucleus/src/main/java/org/apache/isis/persistence/jdo/datanucleus/metamodel/facets/entity/IdStringifierForObjectId.java b/persistence/jdo/datanucleus/src/main/java/org/apache/isis/persistence/jdo/datanucleus/metamodel/facets/entity/IdStringifierForObjectId.java
index 3ebcc66d8b..4897c6099a 100644
--- a/persistence/jdo/datanucleus/src/main/java/org/apache/isis/persistence/jdo/datanucleus/metamodel/facets/entity/IdStringifierForObjectId.java
+++ b/persistence/jdo/datanucleus/src/main/java/org/apache/isis/persistence/jdo/datanucleus/metamodel/facets/entity/IdStringifierForObjectId.java
@@ -18,6 +18,8 @@
  */
 package org.apache.isis.persistence.jdo.datanucleus.metamodel.facets.entity;
 
+import java.util.UUID;
+
 import javax.annotation.Priority;
 
 import org.datanucleus.identity.ObjectId;
@@ -29,11 +31,23 @@ import org.apache.isis.applib.services.bookmark.IdStringifier;
 import lombok.Builder;
 import lombok.NonNull;
 
+/**
+ * Implementation for application-defined primary keys.
+ *
+ * <p>
+ *     For the most part this relies on the JDO spec (5.4.3), but with special case handling if the primary key is
+ *     of type int, long or UUID: rather than encode the fully qualified classname, instead uses a simpler prefix.
+ * </p>
+ */
 @Component
 @Priority(PriorityPrecedence.LATE)
 @Builder
 public class IdStringifierForObjectId extends IdStringifier.Abstract<ObjectId> {
 
+    private static final String PREFIX_UUID = "u_";
+    private static final String PREFIX_LONG = "l_";
+    private static final String PREFIX_INT = "i_";
+
     public IdStringifierForObjectId() {
         super(ObjectId.class);
     }
@@ -41,14 +55,33 @@ public class IdStringifierForObjectId extends IdStringifier.Abstract<ObjectId> {
     @Override
     public String enstring(final @NonNull ObjectId value) {
         Object keyAsObject = value.getKeyAsObject();
-        // rely on JDO spec (5.4.3)
-        return value.toString();
+        if (keyAsObject instanceof Long) {
+            return PREFIX_LONG + keyAsObject;
+        }
+        if (keyAsObject instanceof Integer) {
+            return PREFIX_INT + keyAsObject;
+        }
+        if (keyAsObject instanceof  UUID) {
+            return PREFIX_UUID + keyAsObject;
+        }
+        // fall through to JDO spec (5.4.3)
+        return keyAsObject.toString();
     }
 
     @Override
     public ObjectId destring(
             final @NonNull String stringified,
             final Class<?> targetEntityClassIfAny) {
+        if (stringified.startsWith(PREFIX_LONG)) {
+            return new ObjectId(targetEntityClassIfAny, Long.parseLong(stringified.substring(PREFIX_LONG.length())));
+        }
+        if (stringified.startsWith(PREFIX_INT)) {
+            return new ObjectId(targetEntityClassIfAny, Integer.parseInt(stringified.substring(PREFIX_INT.length())));
+        }
+        if (stringified.startsWith(PREFIX_UUID)) {
+            return new ObjectId(targetEntityClassIfAny, UUID.fromString(stringified.substring(PREFIX_UUID.length())));
+        }
+        // fall through to JDO spec (5.4.3)
         return new ObjectId(targetEntityClassIfAny, stringified);
     }
 }
diff --git a/persistence/jdo/datanucleus/src/main/java/org/apache/isis/persistence/jdo/datanucleus/metamodel/facets/entity/IdStringifierForObjectIdentity.java b/persistence/jdo/datanucleus/src/main/java/org/apache/isis/persistence/jdo/datanucleus/metamodel/facets/entity/IdStringifierForObjectIdentity.java
index fbe8b47ecf..c32c0b6a88 100644
--- a/persistence/jdo/datanucleus/src/main/java/org/apache/isis/persistence/jdo/datanucleus/metamodel/facets/entity/IdStringifierForObjectIdentity.java
+++ b/persistence/jdo/datanucleus/src/main/java/org/apache/isis/persistence/jdo/datanucleus/metamodel/facets/entity/IdStringifierForObjectIdentity.java
@@ -18,9 +18,12 @@
  */
 package org.apache.isis.persistence.jdo.datanucleus.metamodel.facets.entity;
 
+import java.util.UUID;
+
 import javax.annotation.Priority;
 import javax.jdo.identity.ObjectIdentity;
 
+import org.datanucleus.identity.ObjectId;
 import org.springframework.stereotype.Component;
 
 import org.apache.isis.applib.annotation.PriorityPrecedence;
@@ -29,11 +32,23 @@ import org.apache.isis.applib.services.bookmark.IdStringifier;
 import lombok.Builder;
 import lombok.NonNull;
 
+/**
+ * Implementation for application-defined primary keys.
+ *
+ * <p>
+ *     For the most part this relies on the JDO spec (5.4.3), but with special case handling if the primary key is
+ *     of type int, long or UUID: rather than encode the fully qualified classname, instead uses a simpler prefix.
+ * </p>
+ */
 @Component
 @Priority(PriorityPrecedence.LATE)
 @Builder
 public class IdStringifierForObjectIdentity extends IdStringifier.Abstract<ObjectIdentity> {
 
+    private static final String PREFIX_UUID = "u_";
+    private static final String PREFIX_LONG = "l_";
+    private static final String PREFIX_INT = "i_";
+
     public IdStringifierForObjectIdentity() {
         super(ObjectIdentity.class);
     }
@@ -41,14 +56,33 @@ public class IdStringifierForObjectIdentity extends IdStringifier.Abstract<Objec
     @Override
     public String enstring(final @NonNull ObjectIdentity value) {
         Object keyAsObject = value.getKeyAsObject();
-        // rely on JDO spec (5.4.3)
-        return value.toString();
+        if (keyAsObject instanceof Long) {
+            return PREFIX_LONG + keyAsObject;
+        }
+        if (keyAsObject instanceof Integer) {
+            return PREFIX_INT + keyAsObject;
+        }
+        if (keyAsObject instanceof UUID) {
+            return PREFIX_UUID + keyAsObject;
+        }
+        // fall through to JDO spec (5.4.3)
+        return keyAsObject.toString();
     }
 
     @Override
     public ObjectIdentity destring(
             final @NonNull String stringified,
             final Class<?> targetEntityClassIfAny) {
+        if (stringified.startsWith(PREFIX_LONG)) {
+            return new ObjectIdentity(targetEntityClassIfAny, Long.parseLong(stringified.substring(PREFIX_LONG.length())));
+        }
+        if (stringified.startsWith(PREFIX_INT)) {
+            return new ObjectIdentity(targetEntityClassIfAny, Integer.parseInt(stringified.substring(PREFIX_INT.length())));
+        }
+        if (stringified.startsWith(PREFIX_UUID)) {
+            return new ObjectIdentity(targetEntityClassIfAny, UUID.fromString(stringified.substring(PREFIX_UUID.length())));
+        }
+        // fall through to JDO spec (5.4.3)
         return new ObjectIdentity(targetEntityClassIfAny, stringified);
     }
 }