} 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(ExecuteIn.FOREGROUND);
command.setPersistence(Persistence.IF_HINTED);
}
}
if( command != null &&
command.getExecutor() == Executor.USER &&
command.getExecuteIn() == 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());
}
final Object result = method.invoke(object, executionParameters);
if (LOG.isDebugEnabled()) {
LOG.debug(" action result " + result);
}
if (result == null) {
return InvocationResult.forActionThatReturned(null);
}
final ObjectAdapter 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);