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 2015/09/15 08:03:49 UTC

[44/50] [abbrv] isis git commit: ISIS-1194: removing OidGenerator, inlining logic into PersistenceSession

ISIS-1194: removing OidGenerator, inlining logic into PersistenceSession


Project: http://git-wip-us.apache.org/repos/asf/isis/repo
Commit: http://git-wip-us.apache.org/repos/asf/isis/commit/ab78b660
Tree: http://git-wip-us.apache.org/repos/asf/isis/tree/ab78b660
Diff: http://git-wip-us.apache.org/repos/asf/isis/diff/ab78b660

Branch: refs/heads/master
Commit: ab78b660ab954c8999df52ed18d7ab2ed89d24fe
Parents: d627d82
Author: Dan Haywood <da...@haywood-associates.co.uk>
Authored: Mon Sep 14 21:21:17 2015 +0100
Committer: Dan Haywood <da...@haywood-associates.co.uk>
Committed: Mon Sep 14 21:21:17 2015 +0100

----------------------------------------------------------------------
 .../runtime/system/context/IsisContext.java     |   1 -
 .../system/persistence/OidGenerator.java        | 153 -------------------
 .../system/persistence/PersistenceSession.java  |  94 +++++++++---
 3 files changed, 75 insertions(+), 173 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/isis/blob/ab78b660/core/runtime/src/main/java/org/apache/isis/core/runtime/system/context/IsisContext.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/system/context/IsisContext.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/system/context/IsisContext.java
index 2a602c2..0d49e9b 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/system/context/IsisContext.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/system/context/IsisContext.java
@@ -510,7 +510,6 @@ public abstract class IsisContext implements DebuggableWithTitle {
         debugList.add("Transaction Manager", getTransactionManager());
 
         debugList.add("Service injector", getPersistenceSession().getServicesInjector());
-        debugList.add("OID generator", getPersistenceSession().getOidGenerator());
         debugList.add("Services", getPersistenceSession().getServices());
         return debugList.debug();
     }

http://git-wip-us.apache.org/repos/asf/isis/blob/ab78b660/core/runtime/src/main/java/org/apache/isis/core/runtime/system/persistence/OidGenerator.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/system/persistence/OidGenerator.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/system/persistence/OidGenerator.java
deleted file mode 100644
index fb92048..0000000
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/system/persistence/OidGenerator.java
+++ /dev/null
@@ -1,153 +0,0 @@
-/*
- *  Licensed to the Apache Software Foundation (ASF) under one
- *  or more contributor license agreements.  See the NOTICE file
- *  distributed with this work for additional information
- *  regarding copyright ownership.  The ASF licenses this file
- *  to you under the Apache License, Version 2.0 (the
- *  "License"); you may not use this file except in compliance
- *  with the License.  You may obtain a copy of the License at
- *
- *        http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing,
- *  software distributed under the License is distributed on an
- *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- *  KIND, either express or implied.  See the License for the
- *  specific language governing permissions and limitations
- *  under the License.
- */
-
-package org.apache.isis.core.runtime.system.persistence;
-
-import java.util.UUID;
-
-import javax.jdo.PersistenceManager;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import org.apache.isis.core.commons.debug.DebugBuilder;
-import org.apache.isis.core.commons.debug.DebuggableWithTitle;
-import org.apache.isis.core.metamodel.adapter.oid.Oid;
-import org.apache.isis.core.metamodel.adapter.oid.Oid.State;
-import org.apache.isis.core.metamodel.adapter.oid.RootOid;
-import org.apache.isis.core.metamodel.facets.object.viewmodel.ViewModelFacet;
-import org.apache.isis.core.metamodel.spec.ObjectSpecId;
-import org.apache.isis.core.metamodel.spec.ObjectSpecification;
-import org.apache.isis.core.metamodel.spec.SpecificationLoader;
-import org.apache.isis.core.metamodel.spec.SpecificationLoaderSpi;
-import org.apache.isis.objectstore.jdo.datanucleus.persistence.spi.JdoObjectIdSerializer;
-
-public class OidGenerator implements DebuggableWithTitle {
-
-    @SuppressWarnings("unused")
-    private static final Logger LOG = LoggerFactory.getLogger(OidGenerator.class);
-
-    //region > constructor 
-    private final PersistenceSession persistenceSession;
-    private final SpecificationLoaderSpi specificationLoader;
-
-    public OidGenerator(
-            final PersistenceSession persistenceSession,
-            final SpecificationLoaderSpi specificationLoader) {
-        this.persistenceSession = persistenceSession;
-        this.specificationLoader = specificationLoader;
-    }
-    //endregion
-
-    //region > create...Oid (main API)
-    /**
-     * Create a new {@link Oid#isTransient() transient} {@link Oid} for the
-     * supplied pojo, uniquely distinguishable from any other {@link Oid}.
-     */
-    public final RootOid createTransientOrViewModelOid(final Object pojo) {
-        return newIdentifier(pojo, Type.TRANSIENT);
-    }
-
-    /**
-     * Return an equivalent {@link RootOid}, but being persistent.
-     * 
-     * <p>
-     * It is the responsibility of the implementation to determine the new unique identifier.
-     * For example, the generator may simply assign a new value from a sequence, or a GUID;
-     * or, the generator may use the oid to look up the object and inspect the object in order
-     * to obtain an application-defined value.  
-     * 
-     * @param pojo - being persisted
-     */
-    public final RootOid createPersistentOrViewModelOid(Object pojo) {
-        return newIdentifier(pojo, Type.PERSISTENT);
-    }
-
-    //endregion
-
-    //region > helpers: newIdentifier
-
-    enum Type {
-        TRANSIENT,
-        PERSISTENT
-    }
-
-    private RootOid newIdentifier(final Object pojo, final OidGenerator.Type type) {
-        final ObjectSpecification spec = objectSpecFor(pojo);
-        if(spec.isService()) {
-            return newRootId(spec, "1", type);
-        }
-
-        final ViewModelFacet recreatableObjectFacet = spec.getFacet(ViewModelFacet.class);
-        final String identifier =
-                recreatableObjectFacet != null
-                        ? recreatableObjectFacet.memento(pojo)
-                        : newIdentifierFor(pojo, type);
-
-        return newRootId(spec, identifier, type);
-    }
-
-    private String newIdentifierFor(final Object pojo, final Type type) {
-        return type == Type.TRANSIENT
-                ? UUID.randomUUID().toString()
-                : JdoObjectIdSerializer.toOidIdentifier(getJdoPersistenceManager().getObjectId(pojo));
-    }
-
-    private RootOid newRootId(final ObjectSpecification spec, final String identifier, final Type type) {
-        final State state =
-                spec.containsDoOpFacet(ViewModelFacet.class)
-                    ? State.VIEWMODEL
-                    : type == Type.TRANSIENT
-                        ? State.TRANSIENT
-                        : State.PERSISTENT;
-        final ObjectSpecId objectSpecId = spec.getSpecId();
-        return new RootOid(objectSpecId, identifier, state);
-    }
-
-    private ObjectSpecification objectSpecFor(final Object pojo) {
-        final Class<?> pojoClass = pojo.getClass();
-        return getSpecificationLoader().loadSpecification(pojoClass);
-    }
-    //endregion
-
-    //region > dependencies (from constructor)
-    protected SpecificationLoader getSpecificationLoader() {
-        return specificationLoader;
-    }
-
-    protected PersistenceManager getJdoPersistenceManager() {
-        return persistenceSession.getPersistenceManager();
-    }
-    //endregion
-
-    //region > debug
-    @Override
-    public void debugData(final DebugBuilder debug) {
-    }
-
-    @Override
-    public String debugTitle() {
-        return "OidGenerator";
-    }
-
-    //endregion
-
-
-
-}

http://git-wip-us.apache.org/repos/asf/isis/blob/ab78b660/core/runtime/src/main/java/org/apache/isis/core/runtime/system/persistence/PersistenceSession.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/system/persistence/PersistenceSession.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/system/persistence/PersistenceSession.java
index d2455a8..6c65f3d 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/system/persistence/PersistenceSession.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/system/persistence/PersistenceSession.java
@@ -24,6 +24,7 @@ import java.text.MessageFormat;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.UUID;
 
 import javax.jdo.FetchGroup;
 import javax.jdo.FetchPlan;
@@ -154,7 +155,6 @@ public class PersistenceSession implements TransactionalResource, SessionScopedC
     //region > constructor, fields, finalize()
 
     private final PersistenceSessionFactory persistenceSessionFactory;
-    private final OidGenerator oidGenerator;
 
     private final PersistenceQueryFactory persistenceQueryFactory;
     private final IsisConfiguration configuration;
@@ -208,8 +208,6 @@ public class PersistenceSession implements TransactionalResource, SessionScopedC
 
         this.oidMarshaller = new OidMarshaller();
 
-        this.oidGenerator = new OidGenerator(this, specificationLoader);
-
 
         this.persistenceQueryFactory = new PersistenceQueryFactory(this, getSpecificationLoader());
         this.transactionManager = new IsisTransactionManager(this, servicesInjector);
@@ -831,7 +829,7 @@ public class PersistenceSession implements TransactionalResource, SessionScopedC
         if (persistenceManager.getObjectId(pojo) == null) {
             return null;
         }
-        final RootOid oid = oidGenerator.createPersistentOrViewModelOid(pojo);
+        final RootOid oid = createPersistentOrViewModelOid(pojo);
         final ObjectAdapter adapter = mapRecreatedPojo(oid, pojo);
         return adapter;
     }
@@ -1163,7 +1161,6 @@ public class PersistenceSession implements TransactionalResource, SessionScopedC
         debug.appendln();
 
         debug.appendTitle("OID Generator");
-        oidGenerator.debugData(debug);
         debug.appendln();
 
         debug.appendTitle("Services");
@@ -1208,16 +1205,6 @@ public class PersistenceSession implements TransactionalResource, SessionScopedC
     }
 
     /**
-     * The configured {@link OidGenerator}.
-     * 
-     * <p>
-     * Injected in constructor.
-     */
-    public final OidGenerator getOidGenerator() {
-        return oidGenerator;
-    }
-
-    /**
      * The configured {@link ServicesInjectorSpi}.
      */
     public ServicesInjectorSpi getServicesInjector() {
@@ -1683,7 +1670,7 @@ public class PersistenceSession implements TransactionalResource, SessionScopedC
             persistedRootOid = hintRootOid;
         } else {
             // normal flow - delegate to OidGenerator to obtain a persistent root oid
-            persistedRootOid = oidGenerator.createPersistentOrViewModelOid(adapter.getObject());
+            persistedRootOid = createPersistentOrViewModelOid(adapter.getObject());
         }
 
         // associate root adapter with the new Oid, and remap
@@ -1834,7 +1821,7 @@ public class PersistenceSession implements TransactionalResource, SessionScopedC
      * object.
      */
     private ObjectAdapter createTransientOrViewModelRootAdapter(final Object pojo) {
-        final RootOid rootOid = oidGenerator.createTransientOrViewModelOid(pojo);
+        final RootOid rootOid = createTransientOrViewModelOid(pojo);
         return createRootAdapter(pojo, rootOid);
     }
 
@@ -1994,7 +1981,7 @@ public class PersistenceSession implements TransactionalResource, SessionScopedC
                 }
             }
         } else {
-            originalOid = oidGenerator.createPersistentOrViewModelOid(pojo);
+            originalOid = createPersistentOrViewModelOid(pojo);
 
             // it appears to be possible that there is already an adapter for this Oid,
             // ie from ObjectStore#resolveImmediately()
@@ -2010,6 +1997,75 @@ public class PersistenceSession implements TransactionalResource, SessionScopedC
         adapter.setVersion(datastoreVersion);
     }
 
+
+    //region > create...Oid (main API)
+    /**
+     * Create a new {@link Oid#isTransient() transient} {@link Oid} for the
+     * supplied pojo, uniquely distinguishable from any other {@link Oid}.
+     */
+    public final RootOid createTransientOrViewModelOid(final Object pojo) {
+        return newIdentifier(pojo, Type.TRANSIENT);
+    }
+
+    /**
+     * Return an equivalent {@link RootOid}, but being persistent.
+     *
+     * <p>
+     * It is the responsibility of the implementation to determine the new unique identifier.
+     * For example, the generator may simply assign a new value from a sequence, or a GUID;
+     * or, the generator may use the oid to look up the object and inspect the object in order
+     * to obtain an application-defined value.
+     *
+     * @param pojo - being persisted
+     */
+    public final RootOid createPersistentOrViewModelOid(Object pojo) {
+        return newIdentifier(pojo, Type.PERSISTENT);
+    }
+
+    enum Type {
+        TRANSIENT,
+        PERSISTENT
+    }
+
+    private RootOid newIdentifier(final Object pojo, final Type type) {
+        final ObjectSpecification spec = objectSpecFor(pojo);
+        if(spec.isService()) {
+            return newRootId(spec, "1", type);
+        }
+
+        final ViewModelFacet recreatableObjectFacet = spec.getFacet(ViewModelFacet.class);
+        final String identifier =
+                recreatableObjectFacet != null
+                        ? recreatableObjectFacet.memento(pojo)
+                        : newIdentifierFor(pojo, type);
+
+        return newRootId(spec, identifier, type);
+    }
+
+    private String newIdentifierFor(final Object pojo, final Type type) {
+        return type == Type.TRANSIENT
+                ? UUID.randomUUID().toString()
+                : JdoObjectIdSerializer.toOidIdentifier(getPersistenceManager().getObjectId(pojo));
+    }
+
+    private RootOid newRootId(final ObjectSpecification spec, final String identifier, final Type type) {
+        final Oid.State state =
+                spec.containsDoOpFacet(ViewModelFacet.class)
+                        ? Oid.State.VIEWMODEL
+                        : type == Type.TRANSIENT
+                        ? Oid.State.TRANSIENT
+                        : Oid.State.PERSISTENT;
+        final ObjectSpecId objectSpecId = spec.getSpecId();
+        return new RootOid(objectSpecId, identifier, state);
+    }
+
+    private ObjectSpecification objectSpecFor(final Object pojo) {
+        final Class<?> pojoClass = pojo.getClass();
+        return getSpecificationLoader().loadSpecification(pojoClass);
+    }
+    //endregion
+
+
     /**
      * Called either when an entity is initially persisted, or when an entity is updated; fires the appropriate
      * lifecycle callback.
@@ -2055,7 +2111,7 @@ public class PersistenceSession implements TransactionalResource, SessionScopedC
 
         if (rootOid.isTransient()) {
             // persisting
-            final RootOid persistentOid = oidGenerator.createPersistentOrViewModelOid(pojo);
+            final RootOid persistentOid = createPersistentOrViewModelOid(pojo);
 
             remapAsPersistent(adapter, persistentOid);