} else {
command.setTargetClass(CommandUtil.targetClassNameFor(targetAdapter));
command.setTargetAction(CommandUtil.targetActionNameFor(owningAction));
command.setArguments(CommandUtil.argDescriptionFor(owningAction, arguments));
final Bookmark targetBookmark = CommandUtil.bookmarkFor(targetAdapter);
command.setTarget(targetBookmark);
}
command.setMemberIdentifier(CommandUtil.actionIdentifierFor(owningAction));
// 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 Object targetObject = unwrap(targetAdapter);
final Object[] args = CommandUtil.objectsFor(arguments);
ActionInvocationMemento aim = backgroundService.asActionInvocationMemento(method, targetObject, args);
if(aim != null) {
command.setMemento(aim.asMementoString());
} else {
throw new IsisException(
"Unable to build memento for action " +
owningAction.getIdentifier().toClassAndNameIdentityString());
}
}
// copy over the command execution 'context' (if available)
final CommandFacet commandFacet = getFacetHolder().getFacet(CommandFacet.class);
if(commandFacet != null && !commandFacet.isDisabled()) {
command.setExecuteIn(commandFacet.executeIn());
command.setPersistence(commandFacet.persistence());
} else {
// if no facet, assume do want to execute right now, but only persist (eventually) if hinted.
command.setExecuteIn(org.apache.isis.applib.annotation.Command.ExecuteIn.FOREGROUND);
command.setPersistence(org.apache.isis.applib.annotation.Command.Persistence.IF_HINTED);
}
}
if( command != null &&
command.getExecutor() == Command.Executor.USER &&
command.getExecuteIn() == org.apache.isis.applib.annotation.Command.ExecuteIn.BACKGROUND) {
// persist command so can be this command can be in the 'background'
final CommandService commandService = getServicesInjector().lookupService(CommandService.class);
if(commandService.persistIfPossible(command)) {
// force persistence, then return the command itself.
final ObjectAdapter resultAdapter = getAdapterManager().adapterFor(command);
return InvocationResult.forActionThatReturned(resultAdapter);
} else {
throw new IsisException(
"Unable to schedule action '"
+ owningAction.getIdentifier().toClassAndNameIdentityString() + "' to run in background: "
+ "CommandService does not support persistent commands " );
}
} else {
// otherwise, go ahead and execute action in the 'foreground'
if(command != null) {
command.setStartedAt(Clock.getTimeAsJavaSqlTimestamp());
}
Object result = method.invoke(targetPojo, executionParameters);
if (LOG.isDebugEnabled()) {
LOG.debug(" action result " + result);
}
if (result == null) {
if(targetAdapter.getSpecification().isViewModelCloneable(targetAdapter)) {
// if this was a void method on a ViewModel.Cloneable, then (to save boilerplate in the domain)
// automatically do the clone and return the clone instead.
final ViewModel.Cloneable cloneable = (ViewModel.Cloneable) targetAdapter.getObject();
final Object clone = cloneable.clone();
final ObjectAdapter clonedAdapter = getAdapterManager().adapterFor(clone);
return InvocationResult.forActionThatReturned(clonedAdapter);
}
return InvocationResult.forActionThatReturned(null);
}
ObjectAdapter resultAdapter = getAdapterManager().adapterFor(result);
if(resultAdapter.getSpecification().isViewModelCloneable(resultAdapter)) {
// if the object returned is a ViewModel.Cloneable, then
// (to save boilerplate in the domain) automatically do the clone.
final ViewModel.Cloneable cloneable = (ViewModel.Cloneable) result;
result = cloneable.clone();
resultAdapter = getAdapterManager().adapterFor(result);
}
// copy over TypeOfFacet if required
final TypeOfFacet typeOfFacet = getFacetHolder().getFacet(TypeOfFacet.class);
resultAdapter.setElementSpecificationProvider(ElementSpecificationProviderFromTypeOfFacet.createFrom(typeOfFacet));
if(command != null) {
if(!resultAdapter.getSpecification().containsDoOpFacet(ViewModelFacet.class)) {
final Bookmark bookmark = CommandUtil.bookmarkFor(resultAdapter);
command.setResult(bookmark);
}
}
final PublishedActionFacet publishedActionFacet = getIdentified().getFacet(PublishedActionFacet.class);