incubator-isis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From danhayw...@apache.org
Subject svn commit: r1343904 [1/2] - in /incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/isis/ src/main/java/org/apache/isis/runti...
Date Tue, 29 May 2012 18:40:25 GMT
Author: danhaywood
Date: Tue May 29 18:40:23 2012
New Revision: 1343904

URL: http://svn.apache.org/viewvc?rev=1343904&view=rev
Log:
ISIS-48: started work on the OpenJPA object store

Added:
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/NOTICE   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaObjectStore.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaPersistenceMechanismInstaller.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaSimplePersistAlgorithm.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/AbstractJpaObjectCommand.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaCreateObjectCommand.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaDeleteObjectCommand.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaUpdateObjectCommand.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindAllInstancesProcessor.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindByPatternProcessor.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindByTitleProcessor.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindUsingApplibQueryProcessor.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryProcessor.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryProcessorAbstract.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/QueryUtil.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/spi/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/spi/OpenJpaIdentifierGenerator.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/spi/OpenJpaSimplePersistAlgorithm.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/spi/ResolveStateUtil.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/resources/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/site/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/site/apt/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/site/apt/index.apt   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/site/apt/jottings.apt   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/site/site.xml   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/test/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/test/java/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/test/java/org/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/test/java/org/apache/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/test/java/org/apache/isis/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/test/java/org/apache/isis/runtimes/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/test/java/org/apache/isis/runtimes/dflt/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/test/java/org/apache/isis/runtimes/dflt/objectstores/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/test/java/org/apache/isis/runtimes/dflt/objectstores/jpa/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/test/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/test/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaPersistenceMechanismInstallerTest_getName.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/test/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaPersistenceMechanismInstallerTest_openAndClose.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/test/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/fixtures/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/test/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/fixtures/JpaPrimitiveValuedEntity.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/test/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/fixtures/JpaPrimitiveValuedEntityRepository.java   (with props)
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/test/resources/

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/NOTICE
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/NOTICE?rev=1343904&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/NOTICE (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/NOTICE Tue May 29 18:40:23 2012
@@ -0,0 +1,7 @@
+Apache Isis
+Copyright 2010-2011 The Apache Software Foundation
+
+This product includes software developed at
+The Apache Software Foundation (http://www.apache.org/).
+
+

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/NOTICE
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaObjectStore.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaObjectStore.java?rev=1343904&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaObjectStore.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaObjectStore.java Tue May 29 18:40:23 2012
@@ -0,0 +1,746 @@
+package org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa;
+
+import static org.apache.isis.core.commons.ensure.Ensure.ensureThatArg;
+import static org.apache.isis.core.commons.ensure.Ensure.ensureThatContext;
+import static org.apache.isis.core.commons.ensure.Ensure.ensureThatState;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+
+import java.text.MessageFormat;
+import java.util.List;
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Query;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import org.apache.log4j.Logger;
+
+import org.apache.isis.core.commons.config.ConfigurationConstants;
+import org.apache.isis.core.commons.config.IsisConfiguration;
+import org.apache.isis.core.commons.debug.DebugBuilder;
+import org.apache.isis.core.commons.exceptions.NotYetImplementedException;
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.adapter.ObjectAdapterFactory;
+import org.apache.isis.core.metamodel.adapter.ResolveState;
+import org.apache.isis.core.metamodel.adapter.oid.AggregatedOid;
+import org.apache.isis.core.metamodel.adapter.oid.Oid;
+import org.apache.isis.core.metamodel.adapter.oid.RootOid;
+import org.apache.isis.core.metamodel.adapter.oid.TypedOid;
+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.SpecificationLoaderAware;
+import org.apache.isis.core.metamodel.spec.feature.ObjectAssociation;
+import org.apache.isis.extensions.jpa.metamodel.facets.object.namedquery.NamedQuery;
+import org.apache.isis.extensions.jpa.metamodel.util.JpaPropertyUtils;
+import org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.commands.JpaCreateObjectCommand;
+import org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.commands.JpaDeleteObjectCommand;
+import org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.commands.JpaUpdateObjectCommand;
+import org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.queries.PersistenceQueryFindAllInstancesProcessor;
+import org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.queries.PersistenceQueryFindByPatternProcessor;
+import org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.queries.PersistenceQueryFindByTitleProcessor;
+import org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.queries.PersistenceQueryFindUsingApplibQueryProcessor;
+import org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.queries.PersistenceQueryProcessor;
+import org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.queries.QueryUtil;
+import org.apache.isis.runtimes.dflt.runtime.persistence.ObjectNotFoundException;
+import org.apache.isis.runtimes.dflt.runtime.persistence.PersistenceSessionHydratorAware;
+import org.apache.isis.runtimes.dflt.runtime.persistence.UnsupportedFindException;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.ObjectStore;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.transaction.CreateObjectCommand;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.transaction.DestroyObjectCommand;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.transaction.PersistenceCommand;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.transaction.SaveObjectCommand;
+import org.apache.isis.runtimes.dflt.runtime.persistence.query.PersistenceQueryFindAllInstances;
+import org.apache.isis.runtimes.dflt.runtime.persistence.query.PersistenceQueryFindByPattern;
+import org.apache.isis.runtimes.dflt.runtime.persistence.query.PersistenceQueryFindByTitle;
+import org.apache.isis.runtimes.dflt.runtime.persistence.query.PersistenceQueryFindUsingApplibQueryDefault;
+import org.apache.isis.runtimes.dflt.runtime.system.context.IsisContext;
+import org.apache.isis.runtimes.dflt.runtime.system.persistence.AdapterManager;
+import org.apache.isis.runtimes.dflt.runtime.system.persistence.PersistenceQuery;
+import org.apache.isis.runtimes.dflt.runtime.system.persistence.PersistenceSessionHydrator;
+import org.apache.isis.runtimes.dflt.runtime.system.transaction.IsisTransaction;
+import org.apache.isis.runtimes.dflt.runtime.system.transaction.IsisTransactionManager;
+import org.apache.isis.runtimes.dflt.runtime.system.transaction.IsisTransactionManagerAware;
+
+public class OpenJpaObjectStore implements ObjectStore, PersistenceSessionHydratorAware, SpecificationLoaderAware, IsisTransactionManagerAware {
+
+    private static final Logger LOG = Logger.getLogger(OpenJpaObjectStore.class);
+
+    static enum State {
+        NOT_YET_OPEN, OPEN, CLOSED;
+    }
+
+    /**
+     * @see #isFixturesInstalled()
+     */
+    public static final String IS_FIXTURES_INSTALLED_KEY = ConfigurationConstants.ROOT + "persistor.openjpa.install-fixtures";
+    public static final boolean IS_FIXTURES_INSTALLED_DEFAULT = true;
+
+    static enum TransactionMode {
+        /**
+         * Requires transactions to be started explicitly.
+         */
+        UNCHAINED,
+        /**
+         * Transactions are started automatically if not already in progress.
+         */
+        CHAINED;
+    }
+
+    private final IsisConfiguration configuration;
+    private final ObjectAdapterFactory adapterFactory;
+    private final AdapterManager adapterManager;
+    private final EntityManagerFactory entityManagerFactory;
+    private final Map<String, NamedQuery> namedQueryByName;
+    
+    private final Map<ObjectSpecId, RootOid> registeredServices = Maps.newHashMap();
+
+    private SpecificationLoader specificationLoader;
+    private EntityManager entityManager;
+
+    private final Map<Class<?>, PersistenceQueryProcessor<?>> persistenceQueryProcessorByClass = Maps.newHashMap();
+    // private final LoadPostProcessor loadPostProcessor;
+    // private HibernateApplicationComponents hibernateApplicationComponents;
+
+    private State state;
+    private TransactionMode transactionMode;
+
+    private PersistenceSessionHydrator hydrator;
+    private IsisTransactionManager transactionManager;
+
+    public OpenJpaObjectStore(IsisConfiguration configuration, ObjectAdapterFactory adapterFactory, AdapterManager adapterManager, EntityManagerFactory entityManagerFactory, Map<String, NamedQuery> namedQueryByName) {
+        ensureThatArg(configuration, is(notNullValue()));
+        ensureThatArg(adapterFactory, is(notNullValue()));
+        ensureThatArg(adapterManager, is(notNullValue()));
+        ensureThatArg(entityManagerFactory, is(notNullValue()));
+        ensureThatArg(namedQueryByName, is(notNullValue()));
+
+        this.state = State.NOT_YET_OPEN;
+        this.transactionMode = TransactionMode.UNCHAINED;
+
+        this.configuration = configuration;
+        this.adapterFactory = adapterFactory;
+        this.adapterManager = adapterManager;
+        this.entityManagerFactory = entityManagerFactory;
+        this.namedQueryByName = namedQueryByName;
+    }
+
+    @Override
+    public String name() {
+        return "openjpa";
+    }
+
+    // ///////////////////////////////////////////////////////////////////////
+    // open, close
+    // ///////////////////////////////////////////////////////////////////////
+
+    public void open() {
+        ensureNotYetOpen();
+        ensureDependenciesInjected();
+
+        openSession();
+        ensureThatState(entityManager, is(notNullValue()));
+
+        addPersistenceQueryProcessors(entityManager);
+
+        state = State.OPEN;
+    }
+
+    /**
+     * {@inheritDoc}
+     * <p>
+     * Automatically {@link IsisTransactionManager#endTransaction() ends
+     * (commits)} the current (Isis) {@link Transaction}. This in turn
+     * {@link OpenJpaObjectStore#commitJpaTransaction() commits the underlying
+     * JPA transaction}.
+     * <p>
+     * The corresponding OpenJPA {@link EntityManager} is then
+     * {@link EntityManager#close() close}d.
+     */
+    public void close() {
+        ensureOpened();
+        ensureThatState(entityManager, is(notNullValue()));
+
+        final IsisTransaction currentTransaction = getTransactionManager().getTransaction();
+        if (currentTransaction != null && currentTransaction.getState().canCommit()) {
+            getTransactionManager().endTransaction();
+        }
+
+        entityManager.close();
+
+        state = State.CLOSED;
+    }
+
+    private EntityManager openSession() {
+        return entityManager = entityManagerFactory.createEntityManager();
+    }
+
+    private void addPersistenceQueryProcessors(final EntityManager entityManager) {
+        persistenceQueryProcessorByClass.put(PersistenceQueryFindAllInstances.class, new PersistenceQueryFindAllInstancesProcessor(adapterManager, entityManager));
+        persistenceQueryProcessorByClass.put(PersistenceQueryFindByTitle.class, new PersistenceQueryFindByTitleProcessor(adapterManager, entityManager));
+        persistenceQueryProcessorByClass.put(PersistenceQueryFindByPattern.class, new PersistenceQueryFindByPatternProcessor(adapterManager, entityManager));
+        persistenceQueryProcessorByClass.put(PersistenceQueryFindUsingApplibQueryDefault.class, new PersistenceQueryFindUsingApplibQueryProcessor(adapterManager, entityManager));
+    }
+
+    // ///////////////////////////////////////////////////////////////////////
+    // isFixturesInstalled
+    // ///////////////////////////////////////////////////////////////////////
+
+    /**
+     * Implementation looks for the {@link #IS_FIXTURES_INSTALLED_KEY} in the
+     * {@link #getConfiguration() configuration}.
+     * <p>
+     * By default this is not expected to be there, but utilities can add in on
+     * the fly during bootstrapping if required.
+     */
+    public boolean isFixturesInstalled() {
+        return getConfiguration().getBoolean(IS_FIXTURES_INSTALLED_KEY, IS_FIXTURES_INSTALLED_DEFAULT);
+    }
+
+
+    // ///////////////////////////////////////////////////////////////////////
+    // reset
+    // ///////////////////////////////////////////////////////////////////////
+
+    public void reset() {
+        // does nothing.
+    }
+
+    // ///////////////////////////////////////////////////////////////////////
+    // TransactionMode (not API)
+    // ///////////////////////////////////////////////////////////////////////
+
+    public TransactionMode getTransactionMode() {
+        return transactionMode;
+    }
+
+    public void setTransactionMode(final TransactionMode transactionMode) {
+        ensureNotInTransaction();
+        this.transactionMode = transactionMode;
+    }
+
+    // ///////////////////////////////////////////////////////////////////////
+    // Transactions
+    // ///////////////////////////////////////////////////////////////////////
+
+    public void startTransaction() {
+        beginJpaTransaction();
+    }
+
+    public void endTransaction() {
+        commitJpaTransaction();
+    }
+
+    public void abortTransaction() {
+        rollbackJpaTransaction();
+    }
+
+    private void beginJpaTransaction() {
+        final EntityTransaction transaction = getEntityManager().getTransaction();
+        if (transaction.isActive()) {
+            throw new IllegalStateException("Transaction already active");
+        }
+        //getSession().beginTransaction();
+        transaction.begin();
+    }
+
+    private void commitJpaTransaction() {
+        final EntityTransaction transaction = getEntityManager().getTransaction();
+        if (transaction.isActive()) {
+            transaction.commit();
+        }
+    }
+
+    private void rollbackJpaTransaction() {
+        final EntityTransaction transaction = getEntityManager().getTransaction();
+        if (transaction.isActive()) {
+            transaction.rollback();
+        }
+    }
+
+    // ///////////////////////////////////////////////////////////////////////
+    // Command Factory
+    // ///////////////////////////////////////////////////////////////////////
+
+    public CreateObjectCommand createCreateObjectCommand(final ObjectAdapter adapter) {
+        ensureOpened();
+        ensureInSession();
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("create object - creating command for: " + adapter);
+        }
+        if (adapter.isPersistent()) {
+            throw new IllegalArgumentException("Adapter is persistent; adapter: " + adapter);
+        }
+        return new JpaCreateObjectCommand(adapter, getEntityManager());
+    }
+
+    public SaveObjectCommand createSaveObjectCommand(final ObjectAdapter adapter) {
+        ensureOpened();
+        ensureInSession();
+
+        if (!adapter.isPersistent()) {
+            throw new IllegalArgumentException("Adapter is not persistent; adapter: " + adapter);
+        }
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("save object - creating command for: " + adapter);
+        }
+        return new JpaUpdateObjectCommand(adapter, getEntityManager());
+    }
+
+    public DestroyObjectCommand createDestroyObjectCommand(final ObjectAdapter adapter) {
+        ensureOpened();
+        ensureInSession();
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("destroy object - creating command for: " + adapter);
+        }
+        if (!adapter.isPersistent()) {
+            throw new IllegalArgumentException("Adapter is not persistent; adapter: " + adapter);
+        }
+        return new JpaDeleteObjectCommand(adapter, getEntityManager());
+    }
+
+    // ///////////////////////////////////////////////////////////////////////
+    // Execute
+    // ///////////////////////////////////////////////////////////////////////
+
+    public void execute(final List<PersistenceCommand> commands) {
+        ensureOpened();
+        ensureInTransaction();
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("execute " + commands.size() + " commands");
+        }
+
+        if (commands.size() <= 0) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("no commands");
+            }
+            return;
+        }
+
+        executeCommands(commands);
+    }
+
+    private void executeCommands(final List<PersistenceCommand> commands) {
+        try {
+            for (final PersistenceCommand command : commands) {
+                command.execute(null);
+            }
+            getEntityManager().flush();
+        } catch (final RuntimeException e) {
+            LOG.warn("Failure during execution", e);
+            throw e;
+        }
+    }
+
+    // ///////////////////////////////////////////////////////////////////////
+    // getObject, resolveImmediately, resolveField
+    // ///////////////////////////////////////////////////////////////////////
+
+    public ObjectAdapter getObject(final TypedOid oid) {
+        ensureOpened();
+        ensureInTransaction();
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("getObject; oid=" + oid);
+        }
+
+        if(oid instanceof AggregatedOid) {
+            // does it make sense to get these directly?  not sure, so for now have decided to fail fast. 
+            throw new UnsupportedOperationException("Cannot retrieve aggregated objects directly, oid: " + oid.enString());
+        }
+        final RootOid rootOid = (RootOid) oid;
+        Object result = null;
+        try {
+            final Class<?> cls = clsOf(rootOid);
+            final Object idPropValue = idValueOf(rootOid);
+            result = getEntityManager().find(cls, idPropValue);
+        } catch (final RuntimeException e) {
+            throw e;
+        }
+
+        if (result == null) {
+            throw new ObjectNotFoundException(oid);
+        }
+        final ObjectAdapter adapter = hydrator.recreateAdapter(oid, result);
+        
+        //TODO: loadPostProcessor.loaded(adapter);
+        return adapter;
+    }
+
+    /**
+     * Will do nothing if object is already resolved or if object is transient.
+     * <p>
+     * TODO:
+     * The final {@link ResolveState} of the adapter is set using
+     * {@link NakedLoadPostEventListener#onPostLoad(org.hibernate.event.PostLoadEvent)}
+     * Note: this is the same behaviour as MemoryObjectStore, XmlObjectStore
+     * and HibernateObjectStore.
+     * <p>
+     * REVIEW: if the initial state is RESOLVING_PART, then the
+     * {@link ResolveState} is not changed. Is this right?
+     */
+    public void resolveImmediately(final ObjectAdapter adapter) {
+        ensureOpened();
+        ensureInTransaction();
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("resolveImmediately; oid=" + adapter.getOid().enString());
+        }
+
+        if (adapter.getResolveState().isResolved()) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("; already resolved - ignoring");
+            }
+            return;
+        }
+        if (!adapter.isPersistent()) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("; not persistent - ignoring");
+            }
+            return;
+        }
+
+        final Oid oid = adapter.getOid();
+        if (oid instanceof AggregatedOid) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("; aggregated - resolving parent");
+            }
+            final AggregatedOid aggregatedOid = (AggregatedOid) oid;
+            final TypedOid parentOid = aggregatedOid.getParentOid();
+            final ObjectAdapter parentAdapter = this.getObject(parentOid);
+            resolveImmediately(parentAdapter);
+            return;
+        }
+
+        JpaPropertyUtils.setPropertyIdFromOid(adapter, getAdapterFactory());
+        try {
+            final Object domainObject = adapter.getObject();
+
+            getEntityManager().refresh(domainObject);
+        } catch (final RuntimeException e) {
+            throw new ObjectNotFoundException(adapter.getOid(), e);
+        }
+
+        if (adapter.getObject() == null) {
+            throw new ObjectNotFoundException(adapter.getOid());
+        }
+
+        // possibly redundant because also called in the post-load event
+        // listener,
+        // but is required if we were ever to get an eager left-outer-join as
+        // the result of a refresh (sounds possible).
+        
+        //TODO: loadPostProcessor.loaded(adapter);
+    }
+
+    /**
+     * Walking the graph.
+     */
+    public void resolveField(final ObjectAdapter object, final ObjectAssociation association) {
+        ensureOpened();
+        ensureInTransaction();
+
+        final ObjectAdapter referencedCollectionAdapter = association.get(object);
+
+        // if a proxy collection, then force it to initialize.
+        if (association.isOneToManyAssociation()) {
+            ensureThatState(referencedCollectionAdapter, is(notNullValue()));
+
+            final Object referencedCollection = referencedCollectionAdapter.getObject();
+            ensureThatState(referencedCollection, is(notNullValue()));
+
+            // just 'touching' the object is sufficient.
+            referencedCollection.hashCode();
+        }
+
+        if (referencedCollectionAdapter != null) {
+            // this works and seems to be sufficient (is also called from
+            // NakedPostLoadEventListener for direct retrievals rather than
+            // walking the
+            // graph).
+            
+            // TODO: loadPostProcessor.loaded(referencedCollectionAdapter);
+        }
+    }
+
+    // ///////////////////////////////////////////////////////////////////////
+    // getInstances, hasInstances
+    // ///////////////////////////////////////////////////////////////////////
+
+    public List<ObjectAdapter> getInstances(final PersistenceQuery persistenceQuery) {
+        ensureOpened();
+        ensureInTransaction();
+
+        final PersistenceQueryProcessor<? extends PersistenceQuery> processor = persistenceQueryProcessorByClass.get(persistenceQuery.getClass());
+        if (processor == null) {
+            throw new UnsupportedFindException(MessageFormat.format("Unsupported criteria type: {0}", persistenceQuery.getClass().getName()));
+        }
+        return processPersistenceQuery(processor, persistenceQuery);
+    }
+
+    @SuppressWarnings("unchecked")
+    private <Q extends PersistenceQuery> List<ObjectAdapter> processPersistenceQuery(final PersistenceQueryProcessor<Q> persistenceQueryProcessor, final PersistenceQuery persistenceQuery) {
+        return persistenceQueryProcessor.process((Q)persistenceQuery);
+    }
+
+    public boolean hasInstances(final ObjectSpecification specification) {
+        ensureOpened();
+        ensureInTransaction();
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("hasInstances: class=" + specification.getFullIdentifier());
+        }
+
+        if (!specification.persistability().isPersistable()) {
+            LOG.warn("hasInstances: trying to run for non-persistent class " + specification);
+            return false;
+        }
+
+        final Query query = QueryUtil.createQuery(getEntityManager(), "o", "select o.id", specification, null);
+        query.setMaxResults(1);
+        return !query.getResultList().isEmpty();
+    }
+
+    // ///////////////////////////////////////////////////////////////////////
+    // Helpers (loadObjects)
+    // ///////////////////////////////////////////////////////////////////////
+
+    @SuppressWarnings("unused")
+    private List<ObjectAdapter> loadObjects(final ObjectSpecification specification, final List<?> listOfPojs, final AdapterManager adapterManager) {
+        final List<ObjectAdapter> adapters = Lists.newArrayList();
+        int i = 0;
+        for (final Object pojo : listOfPojs) {
+            // REVIEW: cannot just load adapter for object - if Naked Objects
+            // has
+            // already loaded the object
+            // then object won't match it (e.g. if getInstances has been called
+            // and an instance has
+            // been loaded) - so need to use Hibernate session to get an Oid to
+            // do a lookup in that case
+            adapters.add(adapterManager.getAdapterFor(pojo));
+        }
+        return adapters;
+    }
+
+    // ///////////////////////////////////////////////////////////////////////
+    // Services
+    // ///////////////////////////////////////////////////////////////////////
+
+    @Override
+    public void registerService(RootOid rootOid) {
+        ensureOpened();
+        this.registeredServices.put(rootOid.getObjectSpecId(), rootOid);
+    }
+
+    @Override
+    public RootOid getOidForService(ObjectSpecification serviceSpec) {
+        ensureOpened();
+        return this.registeredServices.get(serviceSpec.getSpecId());
+    }
+
+    // ///////////////////////////////////////////////////////////////////////
+    // Helpers: ensure*
+    // ///////////////////////////////////////////////////////////////////////
+
+    private void ensureNotYetOpen() {
+        ensureStateIs(State.NOT_YET_OPEN);
+    }
+
+    private void ensureOpened() {
+        ensureStateIs(State.OPEN);
+    }
+
+    private void ensureInSession() {
+        ensureThatContext(IsisContext.inSession(), is(true));
+    }
+
+    private void ensureNotInTransaction() {
+        ensureInSession();
+        ensureThatContext(IsisContext.inTransaction(), is(false));
+    }
+
+    private void ensureInTransaction() {
+        if (transactionMode == TransactionMode.UNCHAINED) {
+            ensureThatContext(IsisContext.inTransaction(), is(true));
+            ensureInHibernateTransaction();
+        } else {
+            ensureInSession();
+            if (IsisContext.inTransaction()) {
+                ensureInHibernateTransaction();
+            } else {
+                getTransactionManager().startTransaction();
+            }
+        }
+    }
+
+    private void ensureInHibernateTransaction() {
+        ensureThatState(getEntityManager().getTransaction(), is(notNullValue()));
+        ensureThatState(getEntityManager().getTransaction().isActive(), is(true));
+    }
+
+    private void ensureStateIs(final State stateRequired) {
+        if (state == stateRequired) {
+            return;
+        }
+        throw new IllegalStateException("State is: " + state + "; should be: " + stateRequired);
+    }
+
+    // ///////////////////////////////////////////////////////////////////////
+    // Debugging
+    // ///////////////////////////////////////////////////////////////////////
+
+    public void debugData(final DebugBuilder debug) {
+        throw new NotYetImplementedException();
+    }
+
+    public String debugTitle() {
+        throw new NotYetImplementedException();
+    }
+
+    // ///////////////////////////////////////////////////////////////////////
+    // non-API: named queries
+    // ///////////////////////////////////////////////////////////////////////
+
+    public NamedQuery getNamedQuery(String queryName) {
+        return namedQueryByName.get(queryName);
+    }
+
+
+    // ///////////////////////////////////////////////////////////////////////
+    // Helpers
+    // ///////////////////////////////////////////////////////////////////////
+
+    private Class<?> clsOf(final TypedOid oid) {
+        final ObjectSpecification objectSpec = getSpecificationLoader().lookupBySpecId(oid.getObjectSpecId());
+        return objectSpec.getCorrespondingClass();
+    }
+
+    private Object idValueOf(final RootOid oid) {
+        final ObjectSpecification objectSpec = getSpecificationLoader().lookupBySpecId(oid.getObjectSpecId());
+        return JpaPropertyUtils.idValueOf(oid, objectSpec);
+    }
+
+    
+    /**
+     * Intended for internal and test use only.
+     */
+    public EntityManager getEntityManager() {
+        return entityManager;
+    }
+
+    // ///////////////////////////////////////////////////////////////////////
+    // Dependencies (from constructor)
+    // ///////////////////////////////////////////////////////////////////////
+
+    public IsisConfiguration getConfiguration() {
+        return configuration;
+    }
+
+    public ObjectAdapterFactory getAdapterFactory() {
+        return adapterFactory;
+    }
+
+    /**
+     * @see #setAdapterManager(AdapterManager)
+     */
+    public AdapterManager getAdapterManager() {
+        return adapterManager;
+    }
+
+    // ///////////////////////////////////////////////////////////////////////
+    // Dependencies (injected)
+    // ///////////////////////////////////////////////////////////////////////
+
+    private void ensureDependenciesInjected() {
+        ensureThatState(specificationLoader, is(notNullValue()));
+        ensureThatState(adapterManager, is(notNullValue()));
+        ensureThatState(hydrator, is(notNullValue()));
+        //TODO: ensureThatState(hibernateApplicationComponents, is(notNullValue()));
+        ensureThatState(transactionManager, is(notNullValue()));
+    }
+
+    /**
+     * @see #setSpecificationLoader(SpecificationLoader)
+     */
+    public SpecificationLoader getSpecificationLoader() {
+        return specificationLoader;
+    }
+
+    /**
+     * Injected prior to {@link #open()}ing.
+     * <p>
+     * Injected by owning {@link PersistenceSessionObjectStore} (see
+     * {@link PersistenceSessionObjectStore#open()}) by virtue of fact that this
+     * implementation is {@link SpecificationLoaderAware aware} of the
+     * {@link SpecificationLoader}.
+     */
+    public void setSpecificationLoader(final SpecificationLoader specificationLoader) {
+        this.specificationLoader = specificationLoader;
+    }
+
+    /**
+     * @see #setHydrator(PersistenceSessionHydrator)
+     */
+    public PersistenceSessionHydrator getHydrator() {
+        return hydrator;
+    }
+
+    /**
+     * Injected prior to {@link #open()}ing.
+     * <p>
+     * Injected by owning {@link PersistenceSessionObjectStore} (see
+     * {@link PersistenceSessionObjectStore#open()}) by virtue of fact that this
+     * implementation is {@link PersistenceSessionHydratorAware aware} of the
+     * {@link PersistenceSessionHydrator}.
+     */
+    public void setHydrator(final PersistenceSessionHydrator hydrator) {
+        this.hydrator = hydrator;
+    }
+
+    /**
+     * @see #getTransactionManager()
+     */
+    public IsisTransactionManager getTransactionManager() {
+        return transactionManager;
+    }
+
+    /**
+     * Injected prior to {@link #open()}ing.
+     * <p>
+     * Injected by owning {@link PersistenceSessionObjectStore} (see
+     * {@link PersistenceSessionObjectStore#open()}) by virtue of fact that this
+     * implementation is {@link NakedObjectTransactionManagerAware aware} of the
+     * {@link NakedObjectTransactionManager}.
+     */
+    public void setTransactionManager(final IsisTransactionManager transactionManager) {
+        this.transactionManager = transactionManager;
+    }
+
+
+
+//    /**
+//     * @see #setHibernateApplicationComponents(HibernateMetaDataComponents)
+//     */
+//    public HibernateApplicationComponents getHibernateApplicationComponents() {
+//        return hibernateApplicationComponents;
+//    }
+//
+//    /**
+//     * Injected prior to {@link #open()}ing.
+//     * <p>
+//     * Injected by owning {@link JpaPersistenceSession} (see
+//     * {@link JpaPersistenceSession#doOpen()); hard-coded into implementation.
+//     */
+//    public void setHibernateApplicationComponents(final HibernateApplicationComponents hibernateApplicationComponents) {
+//        this.hibernateApplicationComponents = hibernateApplicationComponents;
+//    }
+
+}

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaObjectStore.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaPersistenceMechanismInstaller.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaPersistenceMechanismInstaller.java?rev=1343904&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaPersistenceMechanismInstaller.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaPersistenceMechanismInstaller.java Tue May 29 18:40:23 2012
@@ -0,0 +1,107 @@
+package org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.persistence.EntityManagerFactory;
+
+import com.google.common.collect.Maps;
+
+import org.apache.isis.core.commons.config.IsisConfiguration;
+import org.apache.isis.core.metamodel.adapter.ObjectAdapterFactory;
+import org.apache.isis.core.metamodel.spec.ObjectSpecification;
+import org.apache.isis.core.metamodel.spec.SpecificationLookup;
+import org.apache.isis.extensions.jpa.metamodel.facets.object.entity.JpaEntityFacet;
+import org.apache.isis.extensions.jpa.metamodel.facets.object.namedquery.JpaNamedQueryFacet;
+import org.apache.isis.extensions.jpa.metamodel.facets.object.namedquery.NamedQuery;
+import org.apache.isis.runtimes.dflt.bytecode.identity.objectfactory.ObjectFactoryBasic;
+import org.apache.isis.runtimes.dflt.runtime.installerregistry.installerapi.PersistenceMechanismInstallerAbstract;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.ObjectStore;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.algorithm.PersistAlgorithm;
+import org.apache.isis.runtimes.dflt.runtime.system.context.IsisContext;
+import org.apache.isis.runtimes.dflt.runtime.system.persistence.AdapterManager;
+import org.apache.isis.runtimes.dflt.runtime.system.persistence.ObjectFactory;
+
+public class OpenJpaPersistenceMechanismInstaller extends PersistenceMechanismInstallerAbstract {
+
+    public static final String NAME = "openjpa";
+
+    private EntityManagerFactory entityManagerFactory = null;
+    private Map<String, NamedQuery> namedQueryByName = null;
+
+    public OpenJpaPersistenceMechanismInstaller() {
+        super(NAME);
+    }
+
+    @Override
+    protected ObjectStore createObjectStore(IsisConfiguration configuration, ObjectAdapterFactory adapterFactory, AdapterManager adapterManager) {
+        if(entityManagerFactory == null) {
+            Map<String,String> props = Maps.newHashMap();
+
+            props.put("openjpa.jdbc.SynchronizeMappings", "buildSchema");
+            props.put("openjpa.ConnectionURL", "jdbc:hsqldb:db/test");
+            props.put("openjpa.ConnectionDriverName", "org.hsqldb.jdbcDriver");
+            props.put("openjpa.ConnectionUserName", "sa");
+            props.put("openjpa.ConnectionPassword", "");
+            props.put("openjpa.Log", "DefaultLevel=WARN, Tool=INFO");
+            props.put("openjpa.RuntimeUnenhancedClasses", "supported"); // in production, should always pre-enhance using the maven openjpa plugin
+            
+            final String typeList = entityTypeList();
+            props.put("openjpa.MetaDataFactory", "org.apache.openjpa.persistence.jdbc.PersistenceMappingFactory(types=" + typeList + ")");
+            
+            entityManagerFactory = javax.persistence.Persistence.createEntityManagerFactory(null, props);
+            
+            namedQueryByName = Collections.unmodifiableMap(catalogNamedQueries());
+        }
+        final OpenJpaObjectStore objectStore = new OpenJpaObjectStore(configuration, adapterFactory, adapterManager, entityManagerFactory, namedQueryByName);
+        return objectStore;
+    }
+
+    /**
+     * "org.apache.openjpa.persistence.jdbc.PersistenceMappingFactory(types=org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.fixtures.JpaPrimitiveValuedEntity;)"
+     */
+    private String entityTypeList() {
+        final StringBuilder buf = new StringBuilder();
+        final Collection<ObjectSpecification> allSpecifications = getSpecificationLoader().allSpecifications();
+        for(ObjectSpecification objSpec: allSpecifications) {
+            if(objSpec.containsFacet(JpaEntityFacet.class)) {
+                final String fqcn = objSpec.getFullIdentifier();
+                buf.append(fqcn).append(";");
+            }
+        }
+        final String typeList = buf.toString();
+        return typeList;
+    }
+    
+    private Map<String, NamedQuery> catalogNamedQueries() {
+        final HashMap<String, NamedQuery> namedQueryByName = Maps.newHashMap();
+        for (final ObjectSpecification spec : getSpecificationLoader().allSpecifications()) {
+            final JpaNamedQueryFacet facet = spec.getFacet(JpaNamedQueryFacet.class);
+            if (facet == null) {
+                continue;
+            }
+            for (final NamedQuery namedQuery : facet.getNamedQueries()) {
+                namedQueryByName.put(namedQuery.getName(), namedQuery);
+            }
+        }
+        return namedQueryByName;
+    }
+
+    
+    @Override
+    protected PersistAlgorithm createPersistAlgorithm(IsisConfiguration configuration) {
+        return new OpenJpaSimplePersistAlgorithm();
+    }
+    
+    @Override
+    protected ObjectFactory createObjectFactory(IsisConfiguration configuration) {
+        return new ObjectFactoryBasic();
+    }
+    
+    protected SpecificationLookup getSpecificationLoader() {
+        return IsisContext.getSpecificationLoader();
+    }
+
+}

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaPersistenceMechanismInstaller.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaSimplePersistAlgorithm.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaSimplePersistAlgorithm.java?rev=1343904&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaSimplePersistAlgorithm.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaSimplePersistAlgorithm.java Tue May 29 18:40:23 2012
@@ -0,0 +1,63 @@
+package org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa;
+
+import org.apache.log4j.Logger;
+
+import org.apache.isis.core.commons.lang.ToString;
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.facets.object.callbacks.CallbackUtils;
+import org.apache.isis.core.metamodel.facets.object.callbacks.PersistingCallbackFacet;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.algorithm.PersistAlgorithm;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.algorithm.PersistAlgorithmAbstract;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.algorithm.ToPersistObjectSet;
+
+
+/**
+ * A {@link PersistAlgorithm} which simply saves the object made persistent.
+ */
+public class OpenJpaSimplePersistAlgorithm extends PersistAlgorithmAbstract {
+    private static final Logger LOG = Logger
+            .getLogger(OpenJpaSimplePersistAlgorithm.class);
+
+
+    // ////////////////////////////////////////////////////////////////
+    // name
+    // ////////////////////////////////////////////////////////////////
+
+    public String name() {
+        return "SimplePersistAlgorithm";
+    }
+
+
+    // ////////////////////////////////////////////////////////////////
+    // makePersistent
+    // ////////////////////////////////////////////////////////////////
+
+    /**
+     * @see NakedInsertPostEventListener#onPostInsert(org.hibernate.event.PostInsertEvent)
+     */
+    public void makePersistent(final ObjectAdapter adapter,
+            final ToPersistObjectSet toPersistObjectSet) {
+        if (alreadyPersistedOrNotPersistable(adapter)) {
+            return;
+        }
+        if (LOG.isInfoEnabled()) {
+            LOG.info("persist " + adapter);
+        }
+        // IsisContext.getObjectLoader().madePersistent(object);
+        // Don't do here - allow EventListener to propogate persistent state
+        CallbackUtils.callCallback(adapter, PersistingCallbackFacet.class);
+        toPersistObjectSet.addPersistedObject(adapter);
+    }
+
+
+    // ////////////////////////////////////////////////////////////////
+    // toString
+    // ////////////////////////////////////////////////////////////////
+
+    @Override
+    public String toString() {
+        final ToString toString = new ToString(this);
+        return toString.toString();
+    }
+}
+// Copyright (c) Naked Objects Group Ltd.

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/OpenJpaSimplePersistAlgorithm.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/AbstractJpaObjectCommand.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/AbstractJpaObjectCommand.java?rev=1343904&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/AbstractJpaObjectCommand.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/AbstractJpaObjectCommand.java Tue May 29 18:40:23 2012
@@ -0,0 +1,26 @@
+package org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.commands;
+
+import javax.persistence.EntityManager;
+
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.transaction.PersistenceCommandAbstract;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.transaction.PersistenceCommandContext;
+
+public abstract class AbstractJpaObjectCommand extends PersistenceCommandAbstract {
+    
+    private final EntityManager entityManager;
+
+    AbstractJpaObjectCommand(final ObjectAdapter adapter,
+            final EntityManager entityManager) {
+        super(adapter);
+        this.entityManager = entityManager;
+        
+    }
+
+    protected EntityManager getEntityManager() {
+        return entityManager;
+    }
+    
+    public abstract void execute(final PersistenceCommandContext context);
+
+}
\ No newline at end of file

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/AbstractJpaObjectCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaCreateObjectCommand.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaCreateObjectCommand.java?rev=1343904&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaCreateObjectCommand.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaCreateObjectCommand.java Tue May 29 18:40:23 2012
@@ -0,0 +1,37 @@
+package org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.commands;
+
+import javax.persistence.EntityManager;
+
+import org.apache.log4j.Logger;
+
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.transaction.CreateObjectCommand;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.transaction.PersistenceCommandContext;
+
+public class JpaCreateObjectCommand extends AbstractJpaObjectCommand implements CreateObjectCommand {
+
+    private static final Logger LOG = Logger
+            .getLogger(JpaCreateObjectCommand.class);
+
+    public JpaCreateObjectCommand(ObjectAdapter adapter, EntityManager entityManager) {
+        super(adapter, entityManager);
+    }
+
+
+    @Override
+    public void execute(final PersistenceCommandContext context) {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("create object - executing command for: " + onAdapter());
+        }
+        final ObjectAdapter adapter = onAdapter();
+        final Object domainObject = adapter.getObject();
+
+        getEntityManager().persist(domainObject);
+    }
+
+    @Override
+    public String toString() {
+        return "CreateObjectCommand [adapter=" + onAdapter() + "]";
+    }
+
+}

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaCreateObjectCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaDeleteObjectCommand.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaDeleteObjectCommand.java?rev=1343904&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaDeleteObjectCommand.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaDeleteObjectCommand.java Tue May 29 18:40:23 2012
@@ -0,0 +1,32 @@
+package org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.commands;
+
+import javax.persistence.EntityManager;
+
+import org.apache.log4j.Logger;
+
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.transaction.DestroyObjectCommand;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.transaction.PersistenceCommandContext;
+
+public class JpaDeleteObjectCommand extends AbstractJpaObjectCommand implements DestroyObjectCommand {
+
+    private static final Logger LOG = Logger.getLogger(JpaDeleteObjectCommand.class);
+
+    public JpaDeleteObjectCommand(ObjectAdapter adapter, EntityManager entityManager) {
+        super(adapter, entityManager);
+    }
+
+    @Override
+    public void execute(final PersistenceCommandContext context) {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("destroy object - executing command for " + onAdapter());
+        }
+        getEntityManager().remove(onAdapter().getObject());
+    }
+
+    @Override
+    public String toString() {
+        return "DestroyObjectCommand [adapter=" + onAdapter() + "]";
+    }
+
+}

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaDeleteObjectCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaUpdateObjectCommand.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaUpdateObjectCommand.java?rev=1343904&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaUpdateObjectCommand.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaUpdateObjectCommand.java Tue May 29 18:40:23 2012
@@ -0,0 +1,31 @@
+package org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.commands;
+
+import javax.persistence.EntityManager;
+
+import org.apache.log4j.Logger;
+
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.transaction.PersistenceCommandContext;
+import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.transaction.SaveObjectCommand;
+
+public class JpaUpdateObjectCommand extends AbstractJpaObjectCommand implements SaveObjectCommand {
+    private static final Logger LOG = Logger
+            .getLogger(JpaDeleteObjectCommand.class);
+
+    public JpaUpdateObjectCommand(ObjectAdapter adapter, EntityManager entityManager) {
+        super(adapter, entityManager);
+    }
+
+    @Override
+    public void execute(final PersistenceCommandContext context) {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("save object - executing command for: " + onAdapter());
+        }
+        getEntityManager().merge(onAdapter().getObject());
+    }
+
+    @Override
+    public String toString() {
+        return "SaveObjectCommand [adapter=" + onAdapter() + "]";
+    }
+}

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/commands/JpaUpdateObjectCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindAllInstancesProcessor.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindAllInstancesProcessor.java?rev=1343904&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindAllInstancesProcessor.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindAllInstancesProcessor.java Tue May 29 18:40:23 2012
@@ -0,0 +1,34 @@
+package org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.queries;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.apache.log4j.Logger;
+
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.spec.ObjectSpecification;
+import org.apache.isis.runtimes.dflt.runtime.persistence.query.PersistenceQueryFindAllInstances;
+import org.apache.isis.runtimes.dflt.runtime.system.persistence.AdapterManager;
+
+public class PersistenceQueryFindAllInstancesProcessor extends PersistenceQueryProcessorAbstract<PersistenceQueryFindAllInstances> {
+
+    private static final Logger LOG = Logger.getLogger(PersistenceQueryFindAllInstancesProcessor.class);
+
+    public PersistenceQueryFindAllInstancesProcessor(final AdapterManager adapterManager, final EntityManager entityManager) {
+        super(adapterManager, entityManager);
+    }
+
+    public List<ObjectAdapter> process(final PersistenceQueryFindAllInstances persistenceQuery) {
+
+        final ObjectSpecification specification = persistenceQuery.getSpecification();
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("getInstances: class=" + specification.getFullIdentifier());
+        }
+        final Query query = QueryUtil.createQuery(getEntityManager(), "o", null, specification, null);
+        return loadAdapters(specification, query.getResultList());
+    }
+}
+
+// Copyright (c) Naked Objects Group Ltd.

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindAllInstancesProcessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindByPatternProcessor.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindByPatternProcessor.java?rev=1343904&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindByPatternProcessor.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindByPatternProcessor.java Tue May 29 18:40:23 2012
@@ -0,0 +1,43 @@
+package org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.queries;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+
+import org.apache.isis.core.commons.exceptions.NotYetImplementedException;
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.runtimes.dflt.runtime.persistence.query.PersistenceQueryFindByPattern;
+import org.apache.isis.runtimes.dflt.runtime.system.persistence.AdapterManager;
+
+public class PersistenceQueryFindByPatternProcessor extends
+        PersistenceQueryProcessorAbstract<PersistenceQueryFindByPattern> {
+
+    public PersistenceQueryFindByPatternProcessor(
+            final AdapterManager objectManager, final EntityManager entityManager) {
+        super(objectManager, entityManager);
+    }
+
+    public List<ObjectAdapter> process(
+            final PersistenceQueryFindByPattern persistenceQuery) {
+
+        
+        // TODO: can support using an OpenJPA feature...
+        // see http://stackoverflow.com/questions/2880209/jpa-findbyexample
+
+        
+//        final Object pojoPattern = persistenceQuery.getPattern().getObject();
+//        
+//        final CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
+//        
+//        
+//        final CriteriaQuery<Object> criteriaQuery = criteriaBuilder.createQuery();
+//        
+//        final Query query = getEntityManager().createQuery(criteriaQuery);
+//        final List<?> results = query.getResultList();
+//        return loadAdapters(persistenceQuery.getSpecification(), results);
+        
+        throw new NotYetImplementedException();
+    }
+}
+
+// Copyright (c) Naked Objects Group Ltd.

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindByPatternProcessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindByTitleProcessor.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindByTitleProcessor.java?rev=1343904&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindByTitleProcessor.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindByTitleProcessor.java Tue May 29 18:40:23 2012
@@ -0,0 +1,47 @@
+package org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.queries;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.TypedQuery;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Path;
+import javax.persistence.criteria.Root;
+import javax.persistence.metamodel.EntityType;
+import javax.persistence.metamodel.SingularAttribute;
+
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.spec.ObjectSpecification;
+import org.apache.isis.runtimes.dflt.runtime.persistence.query.PersistenceQueryFindByTitle;
+import org.apache.isis.runtimes.dflt.runtime.system.persistence.AdapterManager;
+
+public class PersistenceQueryFindByTitleProcessor extends PersistenceQueryProcessorAbstract<PersistenceQueryFindByTitle> {
+
+    public PersistenceQueryFindByTitleProcessor(final AdapterManager adapterManager, final EntityManager entityManager) {
+        super(adapterManager, entityManager);
+    }
+
+    public List<ObjectAdapter> process(final PersistenceQueryFindByTitle persistenceQuery) {
+        final ObjectSpecification objectSpec = persistenceQuery.getSpecification();
+        final Class<?> correspondingClass = objectSpec.getCorrespondingClass();
+        return process(persistenceQuery, correspondingClass);
+    }
+
+    private <Z> List<ObjectAdapter> process(final PersistenceQueryFindByTitle persistenceQuery, Class<Z> correspondingClass) {
+        final CriteriaQuery<Z> criteriaQuery = getEntityManager().getCriteriaBuilder().createQuery(correspondingClass);
+
+        final ObjectSpecification objectSpec = persistenceQuery.getSpecification();
+        final Root<Z> from = criteriaQuery.from(correspondingClass);
+        
+        final EntityType<Z> model = from.getModel();
+        final SingularAttribute<? super Z, String> titleAttribute = model.getSingularAttribute("title", String.class);
+        final Path<String> titlePath = from.get(titleAttribute);
+        titlePath.equals(persistenceQuery.getTitle());
+        
+        final TypedQuery<Z> query = getEntityManager().createQuery(criteriaQuery);
+        final List<Z> pojos = query.getResultList();
+        return loadAdapters(objectSpec, pojos);
+    }
+}
+
+// Copyright (c) Naked Objects Group Ltd.

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindByTitleProcessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindUsingApplibQueryProcessor.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindUsingApplibQueryProcessor.java?rev=1343904&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindUsingApplibQueryProcessor.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindUsingApplibQueryProcessor.java Tue May 29 18:40:23 2012
@@ -0,0 +1,88 @@
+package org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.queries;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import com.google.common.collect.Maps;
+
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.services.container.query.QueryCardinality;
+import org.apache.isis.core.metamodel.spec.ObjectAdapterUtils;
+import org.apache.isis.extensions.jpa.metamodel.facets.object.namedquery.NamedQuery;
+import org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.OpenJpaObjectStore;
+import org.apache.isis.runtimes.dflt.runtime.persistence.query.PersistenceQueryFindUsingApplibQueryDefault;
+import org.apache.isis.runtimes.dflt.runtime.system.context.IsisContext;
+import org.apache.isis.runtimes.dflt.runtime.system.persistence.AdapterManager;
+import org.apache.isis.runtimes.dflt.runtime.system.persistence.PersistenceSession;
+
+public class PersistenceQueryFindUsingApplibQueryProcessor extends PersistenceQueryProcessorAbstract<PersistenceQueryFindUsingApplibQueryDefault> {
+    
+    public PersistenceQueryFindUsingApplibQueryProcessor(final AdapterManager objectManager, final EntityManager entityManager) {
+        super(objectManager, entityManager);
+    }
+
+    public List<ObjectAdapter> process(final PersistenceQueryFindUsingApplibQueryDefault persistenceQuery) {
+        final String queryName = persistenceQuery.getQueryName();
+        final Map<String, Object> map = unwrap(persistenceQuery.getArgumentsAdaptersByParameterName());
+        final QueryCardinality cardinality = persistenceQuery.getCardinality();
+        final List<?> results = getResults(queryName, map, cardinality);
+        return loadAdapters(persistenceQuery.getSpecification(), results);
+    }
+
+    public List<?> getResults(final String queryName, final Map<String, Object> argumentsByParameterName, final QueryCardinality cardinality) {
+        final NamedQuery namedQuery = getNamedQuery(queryName);
+
+        final EntityManager entityManager = getJpaObjectStore().getEntityManager();
+        final Query emQuery = entityManager.createNamedQuery(namedQuery.getName());
+        
+        for (final Map.Entry<String, Object> argumentByParameterName : argumentsByParameterName.entrySet()) {
+            final String parameterName = argumentByParameterName.getKey();
+            final Object argument = argumentByParameterName.getValue();
+            emQuery.setParameter(parameterName, argument);
+        }
+
+        if (cardinality == QueryCardinality.MULTIPLE) {
+            return emQuery.getResultList();
+        }
+        if (cardinality == QueryCardinality.SINGLE) {
+            final Object result = emQuery.getSingleResult();
+            return result == null ? Collections.EMPTY_LIST : Arrays.asList(result);
+        }
+        // should never get here
+        return Collections.EMPTY_LIST;
+    }
+
+    private NamedQuery getNamedQuery(final String queryName) {
+        return getJpaObjectStore().getNamedQuery(queryName);
+    }
+
+    private static Map<String, Object> unwrap(final Map<String, ObjectAdapter> argumentAdaptersByParameterName) {
+        final Map<String, Object> argumentsByParameterName = Maps.newHashMap();
+        for (final String parameterName : argumentAdaptersByParameterName.keySet()) {
+            final ObjectAdapter argumentAdapter = argumentAdaptersByParameterName.get(parameterName);
+            final Object argument = ObjectAdapterUtils.unwrapObject(argumentAdapter);
+            argumentsByParameterName.put(parameterName, argument);
+        }
+        return argumentsByParameterName;
+    }
+
+    // /////////////////////////////////////////////////////////////
+    // Dependencies (from context)
+    // /////////////////////////////////////////////////////////////
+
+    private OpenJpaObjectStore getJpaObjectStore() {
+        return (OpenJpaObjectStore) getPersistenceSession().getObjectStore();
+    }
+
+    private PersistenceSession getPersistenceSession() {
+        return IsisContext.getPersistenceSession();
+    }
+
+}
+
+// Copyright (c) Naked Objects Group Ltd.

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryFindUsingApplibQueryProcessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryProcessor.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryProcessor.java?rev=1343904&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryProcessor.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryProcessor.java Tue May 29 18:40:23 2012
@@ -0,0 +1,12 @@
+package org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.queries;
+
+import java.util.List;
+
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.runtimes.dflt.runtime.system.persistence.PersistenceQuery;
+
+public interface PersistenceQueryProcessor<T extends PersistenceQuery> {
+	List<ObjectAdapter> process(T query);
+}
+
+// Copyright (c) Naked Objects Group Ltd.

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryProcessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryProcessorAbstract.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryProcessorAbstract.java?rev=1343904&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryProcessorAbstract.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryProcessorAbstract.java Tue May 29 18:40:23 2012
@@ -0,0 +1,68 @@
+package org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.queries;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.metamodel.ManagedType;
+import javax.persistence.metamodel.Metamodel;
+
+import com.google.common.collect.Lists;
+
+import org.apache.isis.core.commons.factory.InstanceUtil;
+import org.apache.isis.core.commons.lang.CastUtils;
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.spec.ObjectSpecification;
+import org.apache.isis.runtimes.dflt.runtime.system.persistence.AdapterManager;
+import org.apache.isis.runtimes.dflt.runtime.system.persistence.PersistenceQuery;
+
+public abstract class PersistenceQueryProcessorAbstract<T extends PersistenceQuery>
+        implements PersistenceQueryProcessor<T> {
+
+    private final AdapterManager adapterManager;
+    private final EntityManager entityManager;
+
+    protected PersistenceQueryProcessorAbstract(
+            final AdapterManager objectManager , final EntityManager entityManager) {
+        this.adapterManager = objectManager;
+        this.entityManager = entityManager;
+    }
+
+    protected EntityManagerFactory getEntityManagerFactory() {
+        return getEntityManager().getEntityManagerFactory();
+    }
+
+    protected EntityManager getEntityManager() {
+        return entityManager;
+    }
+    
+    protected <X> ManagedType<X> getClassMetadata(final String classFullName) {
+        final ManagedType<X> managedType = CastUtils.cast(getMetaModel().managedType(InstanceUtil.loadClass(classFullName)));
+        return managedType;
+    }
+
+    protected Metamodel getMetaModel() {
+        return getEntityManagerFactory().getMetamodel();
+    }
+
+    protected List<ObjectAdapter> loadAdapters(
+            final ObjectSpecification specification, final List<?> pojos) {
+        final List<ObjectAdapter> adapters = Lists.newArrayList();
+        for (final Object pojo : pojos) {
+            // REVIEW: cannot just load adapter for object - if Isis
+            // has already loaded the object then object won't match it 
+            // (e.g. if getInstances has been called and an instance has
+            // been loaded) - so need to use JPA session to get an Oid to
+            // do a lookup in that case
+            adapters.add( adapterManager.getAdapterFor(pojo) );
+        }
+        return adapters;
+    }
+
+    protected AdapterManager getAdapterManager() {
+        return adapterManager;
+    }
+
+}
+
+// Copyright (c) Naked Objects Group Ltd.

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/PersistenceQueryProcessorAbstract.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/QueryUtil.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/QueryUtil.java?rev=1343904&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/QueryUtil.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/QueryUtil.java Tue May 29 18:40:23 2012
@@ -0,0 +1,69 @@
+package org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.queries;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.apache.log4j.Logger;
+
+import org.apache.isis.core.metamodel.spec.ObjectSpecification;
+
+public final class QueryUtil {
+
+    private static final Logger LOG = Logger.getLogger(QueryUtil.class);
+
+    private QueryUtil() {}
+
+    public static Query createQuery(
+            final EntityManager entityManager,
+            final String alias,
+            final String select,
+            final ObjectSpecification specification,
+            final String whereClause) {
+
+        final StringBuilder buf = new StringBuilder(128);
+        appendSelect(buf, select, alias);
+        appendFrom(buf, specification, alias);
+        appendWhere(buf, whereClause);
+
+        final String queryString = buf.toString();
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("creating query: " + queryString);
+        }
+
+        return entityManager.createQuery(queryString);
+    }
+
+    private static StringBuilder appendSelect(
+            final StringBuilder buf,
+            final String select, 
+            String alias) {
+        if (select != null) {
+            buf.append(select);
+        } else {
+            buf.append("select ").append(alias);
+        }
+        buf.append(" ");
+        return buf;
+    }
+
+    private static void appendWhere(StringBuilder buf, String whereClause) {
+        if(whereClause == null) {
+            return;
+        }
+        buf.append(" where ").append(whereClause);
+    }
+
+
+    private static StringBuilder appendFrom(
+            final StringBuilder buf,
+            final ObjectSpecification specification,
+            final String alias) {
+        return buf.append("from ")
+                .append(specification.getFullIdentifier())
+                .append(" as ")
+                .append(alias);
+    }
+}
+
+
+// Copyright (c) Naked Objects Group Ltd.

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/queries/QueryUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/spi/OpenJpaIdentifierGenerator.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/spi/OpenJpaIdentifierGenerator.java?rev=1343904&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/spi/OpenJpaIdentifierGenerator.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/spi/OpenJpaIdentifierGenerator.java Tue May 29 18:40:23 2012
@@ -0,0 +1,81 @@
+package org.apache.isis.runtimes.dflt.objectstores.jpa.openjpa.spi;
+
+import java.util.UUID;
+
+import org.apache.log4j.Logger;
+
+import org.apache.isis.core.commons.debug.DebugBuilder;
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.adapter.map.AdapterMap;
+import org.apache.isis.core.metamodel.adapter.map.AdapterMapAware;
+import org.apache.isis.core.metamodel.adapter.oid.RootOid;
+import org.apache.isis.core.metamodel.facets.accessor.PropertyOrCollectionAccessorFacet;
+import org.apache.isis.core.metamodel.spec.ObjectSpecId;
+import org.apache.isis.core.metamodel.spec.feature.OneToOneAssociation;
+import org.apache.isis.extensions.jpa.metamodel.util.JpaPropertyUtils;
+import org.apache.isis.runtimes.dflt.runtime.system.persistence.IdentifierGenerator;
+
+public class OpenJpaIdentifierGenerator implements IdentifierGenerator, AdapterMapAware {
+
+    @SuppressWarnings("unused")
+    private static final Logger LOG = Logger.getLogger(OpenJpaIdentifierGenerator.class);
+    private AdapterMap adapterMap;
+
+
+    // //////////////////////////////////////////////////////////////
+    // main api
+    // //////////////////////////////////////////////////////////////
+
+    @Override
+    public String createTransientIdentifierFor(ObjectSpecId objectSpecId, Object pojo) {
+        return UUID.randomUUID().toString();
+    }
+
+
+    @Override
+    public String createAggregateLocalId(ObjectSpecId objectSpecId, Object pojo, ObjectAdapter parentAdapter) {
+        return UUID.randomUUID().toString();
+    }
+
+
+    @Override
+    public String createPersistentIdentifierFor(ObjectSpecId objectSpecId, Object pojo, RootOid transientRootOid) {
+        final ObjectAdapter adapter = adapterMap.adapterFor(pojo);
+        final OneToOneAssociation idPropertyFor = JpaPropertyUtils.getIdPropertyFor(adapter.getSpecification());
+        if (idPropertyFor == null) {
+            throw new IllegalStateException("cannot find id property for pojo");
+        }
+        final PropertyOrCollectionAccessorFacet facet = idPropertyFor.getFacet(PropertyOrCollectionAccessorFacet.class);
+        final Object propertyValue = facet.getProperty(adapter);
+        return propertyValue.toString();
+    }
+
+
+    // //////////////////////////////////////////////////////////////
+    // Debugging
+    // //////////////////////////////////////////////////////////////
+
+
+    public String debugTitle() {
+        return "OpenJpa Identifier Generator";
+    }
+
+    
+    @Override
+    public void debugData(DebugBuilder debug) {
+        
+    }
+
+
+    // //////////////////////////////////////////////////////////////
+    // Dependencies (injected by setter)
+    // //////////////////////////////////////////////////////////////
+
+    @Override
+    public void setAdapterMap(AdapterMap adapterMap) {
+        this.adapterMap = adapterMap;
+    }
+
+
+}
+// Copyright (c) Naked Objects Group Ltd.

Propchange: incubator/isis/trunk/framework/runtimes/dflt/objectstores/jpa/jpa-openjpa/src/main/java/org/apache/isis/runtimes/dflt/objectstores/jpa/openjpa/spi/OpenJpaIdentifierGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message