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 2016/04/12 09:08:33 UTC

[7/8] isis git commit: ISIS-1372: just moved BacgroundService#asActionInvocationMemento over to new CommandMementoService#asActionInvocationMemento.

ISIS-1372: just moved BacgroundService#asActionInvocationMemento over to new CommandMementoService#asActionInvocationMemento.


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

Branch: refs/heads/ISIS-1291
Commit: 9b35ceb9d1e1272fcbf864e2ea7906f824f0308f
Parents: b1b3dc5
Author: Dan Haywood <da...@haywood-associates.co.uk>
Authored: Tue Apr 12 00:54:32 2016 +0100
Committer: Dan Haywood <da...@haywood-associates.co.uk>
Committed: Tue Apr 12 00:54:32 2016 +0100

----------------------------------------------------------------------
 .../guides/_rgsvc_application-layer-spi.adoc    |  12 ++
 .../_rgsvc_spi_CommandMementoService.adoc       |  60 +++++++
 .../services/command/CommandMementoService.java |  45 +++++
 ...onInvocationFacetForDomainEventAbstract.java |   9 +-
 .../background/BackgroundServiceDefault.java    |  46 +-----
 .../command/CommandMementoServiceDefault.java   | 163 +++++++++++++++++++
 6 files changed, 285 insertions(+), 50 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/isis/blob/9b35ceb9/adocs/documentation/src/main/asciidoc/guides/_rgsvc_application-layer-spi.adoc
----------------------------------------------------------------------
diff --git a/adocs/documentation/src/main/asciidoc/guides/_rgsvc_application-layer-spi.adoc b/adocs/documentation/src/main/asciidoc/guides/_rgsvc_application-layer-spi.adoc
index 395a6f6..3fc6e30 100644
--- a/adocs/documentation/src/main/asciidoc/guides/_rgsvc_application-layer-spi.adoc
+++ b/adocs/documentation/src/main/asciidoc/guides/_rgsvc_application-layer-spi.adoc
@@ -34,6 +34,17 @@ The table below summarizes the application layer SPIs defined by Apache Isis.  I
 `JdoRepository`
 
 
+|xref:rgsvc.adoc#_rgsvc_api_CommandContext[`o.a.i.applib.` +
+`services.command` +
+`CommandMementoService`]
+|(`1.13.0-SNAPSHOT`) Creates memento of current action invocation, for use either as a reified command or for implementatins of the xref:rgsvc.adoc#_rgsvc_spi_PublishingService[`PublishingService`].
+|`CommandMemento-` +
+`ServiceDefault` +
+``o.a.i.core`` +
+``isis-core-runtim``
+|
+
+
 |xref:rgsvc.adoc#_rgsvc_spi_CommandService[`o.a.i.applib.` +
 `services.command.spi` +
 `CommandService`]
@@ -73,5 +84,6 @@ Key:
 
 
 include::_rgsvc_spi_BackgroundCommandService.adoc[leveloffset=+1]
+include::_rgsvc_api_CommandMementoService.adoc[leveloffset=+1]
 include::_rgsvc_spi_CommandService.adoc[leveloffset=+1]
 include::_rgsvc_api_HomePageProviderService.adoc[leveloffset=+1]

http://git-wip-us.apache.org/repos/asf/isis/blob/9b35ceb9/adocs/documentation/src/main/asciidoc/guides/_rgsvc_spi_CommandMementoService.adoc
----------------------------------------------------------------------
diff --git a/adocs/documentation/src/main/asciidoc/guides/_rgsvc_spi_CommandMementoService.adoc b/adocs/documentation/src/main/asciidoc/guides/_rgsvc_spi_CommandMementoService.adoc
new file mode 100644
index 0000000..17c3d47
--- /dev/null
+++ b/adocs/documentation/src/main/asciidoc/guides/_rgsvc_spi_CommandMementoService.adoc
@@ -0,0 +1,60 @@
+[[_rgsvc_spi_CommandMementoService]]
+= `CommandMementoService` (`1.13.0-SNAPSHOT`)
+:Notice: 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.
+:_basedir: ../
+:_imagesdir: images/
+
+
+The `CommandMementoService` (`1.13.0-SNAPSHOT`) is responsible for creating a memento of a current action invocation,
+to store in the `Command` object (from xref:rgsvc.adoc#_rgsvc_api_CommandContext[`CommandContext`]).
+This memento can be reified for use either in xref:rgsvc.adoc#_rgsvc_spi_BackgroundCommandService[background command]s, or to be published using xref:rgsvc.adoc#_rgsvc_spi_PublishingService[`PublishingService`].
+
+
+
+
+== SPI & Implementation
+
+The SPI of the service is:
+
+[source,java]
+----
+public interface CommandMementoService {
+    @Programmatic
+    ActionInvocationMemento asActionInvocationMemento(
+                        Method m, Object domainObject, Object[] args);
+}
+----
+
+The default implementation is provided by core (`o.a.i.core.runtime.services.command.CommandMementoServiceDefault`).
+
+
+
+
+== Registering the Services
+
+Assuming that the `configuration-and-annotation` services installer is configured (implicit if using the
+`AppManifest` to xref:rgcms.adoc#_rgcms_classes_AppManifest-bootstrapping[bootstrap the app]) then Apache Isis' core
+implementation of `CommandMementoService` is automatically registered (it is annotated with `@DomainService`) so no
+further configuration is required.
+
+To use an alternative implementation, use
+xref:rgant.adoc#_rgant-DomainServiceLayout_menuOrder[`@DomainServiceLayout#menuOrder()`] (as explained
+in the xref:rgsvc.adoc#_rgsvc_intro_overriding-the-services[introduction] to this guide).
+
+
+
+== Related Services
+
+This service is used when invoking an action, the memento being copied onto the `Command`.
+If the action turns out to be published, then the memento is copied down from the `Command`
+ and may be used by the xref:rgsvc.adoc#_rgsvc_spi_PublishingService[`PublishingService`] implementation.
+
+In addition, the service is also called by xref:rgsvc.adoc#_rgsvc_api_BackgroundService[`BackgroundService`] in order to create a memento
+for a command that can then be run in the background at some time in the future.
+
+
+
+
+
+
+

http://git-wip-us.apache.org/repos/asf/isis/blob/9b35ceb9/core/applib/src/main/java/org/apache/isis/applib/services/command/CommandMementoService.java
----------------------------------------------------------------------
diff --git a/core/applib/src/main/java/org/apache/isis/applib/services/command/CommandMementoService.java b/core/applib/src/main/java/org/apache/isis/applib/services/command/CommandMementoService.java
new file mode 100644
index 0000000..57eb8b3
--- /dev/null
+++ b/core/applib/src/main/java/org/apache/isis/applib/services/command/CommandMementoService.java
@@ -0,0 +1,45 @@
+/**
+ *  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.applib.services.command;
+
+import java.lang.reflect.Method;
+
+import org.apache.isis.applib.annotation.Programmatic;
+import org.apache.isis.applib.services.background.ActionInvocationMemento;
+
+/**
+ * Submit actions to be invoked in the background.
+ * 
+ * <p>
+ * Example usage:
+ * <pre>
+ * public void submitInvoices() {
+ *     for(Customer customer: customerRepository.findCustomersToInvoice()) {
+ *         backgroundService.execute(customer).submitInvoice();
+ *     }
+ * }
+ * 
+ * &#64;javax.inject.Inject
+ * private BackgroundService backgroundService;
+ * </pre>
+ */
+public interface CommandMementoService {
+
+    @Programmatic
+    ActionInvocationMemento asActionInvocationMemento(Method m, Object domainObject, Object[] args);
+
+}

http://git-wip-us.apache.org/repos/asf/isis/blob/9b35ceb9/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/actions/action/invocation/ActionInvocationFacetForDomainEventAbstract.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/actions/action/invocation/ActionInvocationFacetForDomainEventAbstract.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/actions/action/invocation/ActionInvocationFacetForDomainEventAbstract.java
index 14bd6aa..790ff8c 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/actions/action/invocation/ActionInvocationFacetForDomainEventAbstract.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/actions/action/invocation/ActionInvocationFacetForDomainEventAbstract.java
@@ -39,10 +39,10 @@ import org.apache.isis.applib.annotation.InvokedOn;
 import org.apache.isis.applib.clock.Clock;
 import org.apache.isis.applib.services.actinvoc.ActionInvocationContext;
 import org.apache.isis.applib.services.background.ActionInvocationMemento;
-import org.apache.isis.applib.services.background.BackgroundService;
 import org.apache.isis.applib.services.bookmark.Bookmark;
 import org.apache.isis.applib.services.command.Command;
 import org.apache.isis.applib.services.command.CommandContext;
+import org.apache.isis.applib.services.command.CommandMementoService;
 import org.apache.isis.applib.services.command.spi.CommandService;
 import org.apache.isis.applib.services.eventbus.AbstractDomainEvent;
 import org.apache.isis.applib.services.eventbus.ActionDomainEvent;
@@ -339,12 +339,11 @@ public abstract class ActionInvocationFacetForDomainEventAbstract
                     if(command.getMemento() == null) {
                         // similarly, guard here to deal with subsequent contributed/mixin actions.
 
-                        // the background service is used here merely as a means to capture an invocation memento
-                        final BackgroundService backgroundService = getServicesInjector().lookupService(BackgroundService.class);
-                        if(backgroundService != null) {
+                        final CommandMementoService commandMementoService = getServicesInjector().lookupService(CommandMementoService.class);
+                        if(commandMementoService != null) {
                             final Object targetObject = unwrap(targetAdapter);
                             final Object[] args = CommandUtil.objectsFor(arguments);
-                            final ActionInvocationMemento aim = backgroundService.asActionInvocationMemento(method, targetObject, args);
+                            final ActionInvocationMemento aim = commandMementoService.asActionInvocationMemento(method, targetObject, args);
 
                             if(aim != null) {
                                 command.setMemento(aim.asMementoString());

http://git-wip-us.apache.org/repos/asf/isis/blob/9b35ceb9/core/runtime/src/main/java/org/apache/isis/core/runtime/services/background/BackgroundServiceDefault.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/background/BackgroundServiceDefault.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/background/BackgroundServiceDefault.java
index f56a697..457dfb9 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/background/BackgroundServiceDefault.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/background/BackgroundServiceDefault.java
@@ -18,9 +18,6 @@ package org.apache.isis.core.runtime.services.background;
 
 import java.lang.reflect.Method;
 import java.util.List;
-import java.util.Map;
-
-import javax.annotation.PostConstruct;
 
 import com.google.common.collect.Lists;
 
@@ -76,13 +73,6 @@ public class BackgroundServiceDefault implements BackgroundService {
         this.mementoService = mementoService.withNoEncoding();
     }
     
-    // //////////////////////////////////////
-
-    
-    @Programmatic
-    @PostConstruct
-    public void init(Map<String,String> props) {
-    }
 
     // //////////////////////////////////////
 
@@ -213,43 +203,10 @@ public class BackgroundServiceDefault implements BackgroundService {
     @Programmatic
     @Override
     public ActionInvocationMemento asActionInvocationMemento(Method method, Object domainObject, Object[] args) {
-        
-        final ObjectSpecificationDefault targetObjSpec = getJavaSpecificationOfOwningClass(method);
-        final ObjectMember member = targetObjSpec.getMember(method);
-        if(member == null) {
-            return null;
-        }
-        if(!(member instanceof ObjectAction)) {
-            return null;
-        }
-
-        final ObjectAction action = (ObjectAction) member;
-        final String actionIdentifier = CommandUtil.actionIdentifierFor(action);
-        
-        final Bookmark domainObjectBookmark = bookmarkService.bookmarkFor(domainObject);
-
-        final List<Class<?>> argTypes = Lists.newArrayList();
-        final List<Object> argObjs = Lists.newArrayList();
-        CommandUtil.buildMementoArgLists(mementoService, bookmarkService, method, args, argTypes, argObjs);
-
-        final ActionInvocationMemento aim = 
-                new ActionInvocationMemento(mementoService, 
-                        actionIdentifier, 
-                        domainObjectBookmark,
-                        argTypes,
-                        argObjs);
-       
-        return aim;
+        throw new RuntimeException("Replaced by CommandMementoService");
     }
 
 
-    /**
-     * Not API
-     */
-    ActionInvocationMemento newActionInvocationMemento(String mementoStr) {
-        return new ActionInvocationMemento(mementoService, mementoStr);
-    }
-
     // //////////////////////////////////////
 
     @javax.inject.Inject
@@ -258,7 +215,6 @@ public class BackgroundServiceDefault implements BackgroundService {
     @javax.inject.Inject
     private BookmarkService bookmarkService;
 
-    @javax.inject.Inject
     private CommandContext commandContext;
 
     /**

http://git-wip-us.apache.org/repos/asf/isis/blob/9b35ceb9/core/runtime/src/main/java/org/apache/isis/core/runtime/services/command/CommandMementoServiceDefault.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/command/CommandMementoServiceDefault.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/command/CommandMementoServiceDefault.java
new file mode 100644
index 0000000..a0ef2bf
--- /dev/null
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/command/CommandMementoServiceDefault.java
@@ -0,0 +1,163 @@
+/**
+ *  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.services.command;
+
+import java.lang.reflect.Method;
+import java.util.List;
+import java.util.Map;
+
+import javax.annotation.PostConstruct;
+
+import com.google.common.collect.Lists;
+
+import org.apache.isis.applib.annotation.DomainService;
+import org.apache.isis.applib.annotation.NatureOfService;
+import org.apache.isis.applib.annotation.Programmatic;
+import org.apache.isis.applib.services.background.ActionInvocationMemento;
+import org.apache.isis.applib.services.background.BackgroundCommandService;
+import org.apache.isis.applib.services.bookmark.Bookmark;
+import org.apache.isis.applib.services.bookmark.BookmarkService;
+import org.apache.isis.applib.services.command.CommandMementoService;
+import org.apache.isis.core.commons.ensure.Ensure;
+import org.apache.isis.core.metamodel.adapter.mgr.AdapterManager;
+import org.apache.isis.core.metamodel.facets.actions.action.invocation.CommandUtil;
+import org.apache.isis.core.metamodel.spec.ObjectSpecification;
+import org.apache.isis.core.metamodel.spec.SpecificationLoaderSpi;
+import org.apache.isis.core.metamodel.spec.feature.ObjectAction;
+import org.apache.isis.core.metamodel.spec.feature.ObjectMember;
+import org.apache.isis.core.metamodel.specloader.specimpl.dflt.ObjectSpecificationDefault;
+import org.apache.isis.core.runtime.services.memento.MementoServiceDefault;
+import org.apache.isis.core.runtime.system.context.IsisContext;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.nullValue;
+
+/**
+ * Depends on an implementation of {@link BackgroundCommandService} to
+ * be configured.
+ */
+@DomainService(
+        nature = NatureOfService.DOMAIN
+)
+public class CommandMementoServiceDefault implements CommandMementoService {
+
+    private final MementoServiceDefault mementoService;
+
+    public CommandMementoServiceDefault() {
+        this(new MementoServiceDefault());
+    }
+
+    CommandMementoServiceDefault(MementoServiceDefault mementoService) {
+        this.mementoService = mementoService.withNoEncoding();
+    }
+    
+    // //////////////////////////////////////
+
+    
+    @Programmatic
+    @PostConstruct
+    public void init(Map<String,String> props) {
+    }
+
+    // //////////////////////////////////////
+
+
+    private ObjectSpecificationDefault getJavaSpecificationOfOwningClass(final Method method) {
+        return getJavaSpecification(method.getDeclaringClass());
+    }
+
+    private ObjectSpecificationDefault getJavaSpecification(final Class<?> cls) {
+        final ObjectSpecification objectSpec = getSpecification(cls);
+        if (!(objectSpec instanceof ObjectSpecificationDefault)) {
+            throw new UnsupportedOperationException(
+                "Only Java is supported "
+                + "(specification is '" + objectSpec.getClass().getCanonicalName() + "')");
+        }
+        return (ObjectSpecificationDefault) objectSpec;
+    }
+
+    private ObjectSpecification getSpecification(final Class<?> type) {
+        return getSpecificationLoader().loadSpecification(type);
+    }
+
+
+    // //////////////////////////////////////
+
+    @Programmatic
+    @Override
+    public ActionInvocationMemento asActionInvocationMemento(Method method, Object domainObject, Object[] args) {
+        
+        final ObjectSpecificationDefault targetObjSpec = getJavaSpecificationOfOwningClass(method);
+        final ObjectMember member = targetObjSpec.getMember(method);
+        if(member == null) {
+            return null;
+        }
+        if(!(member instanceof ObjectAction)) {
+            return null;
+        }
+
+        final ObjectAction action = (ObjectAction) member;
+        final String actionIdentifier = CommandUtil.actionIdentifierFor(action);
+        
+        final Bookmark domainObjectBookmark = bookmarkService.bookmarkFor(domainObject);
+
+        final List<Class<?>> argTypes = Lists.newArrayList();
+        final List<Object> argObjs = Lists.newArrayList();
+        CommandUtil.buildMementoArgLists(mementoService, bookmarkService, method, args, argTypes, argObjs);
+
+        final ActionInvocationMemento aim = 
+                new ActionInvocationMemento(mementoService, 
+                        actionIdentifier, 
+                        domainObjectBookmark,
+                        argTypes,
+                        argObjs);
+       
+        return aim;
+    }
+
+
+    /**
+     * Not API
+     */
+    ActionInvocationMemento newActionInvocationMemento(String mementoStr) {
+        return new ActionInvocationMemento(mementoService, mementoStr);
+    }
+
+    // //////////////////////////////////////
+
+    @javax.inject.Inject
+    private BookmarkService bookmarkService;
+
+
+    private void ensureDependenciesInjected() {
+        Ensure.ensureThatState(this.bookmarkService, is(not(nullValue())), "BookmarkService domain service must be configured");
+    }
+
+    // //////////////////////////////////////
+
+    protected SpecificationLoaderSpi getSpecificationLoader() {
+        return IsisContext.getSpecificationLoader();
+    }
+
+    protected AdapterManager getAdapterManager() {
+        return IsisContext.getPersistenceSession();
+    }
+
+
+
+}