private static final long serialVersionUID = 1L;
@Override
public void onClick() {
final ObjectAction objectAction = actionMemento.getAction();
final ConcurrencyChecking concurrencyChecking =
ConcurrencyChecking.concurrencyCheckingFor(objectAction.getSemantics());
try {
final List<ObjectAdapterMemento> toggleMementosList = model.getToggleMementosList();
final Iterable<ObjectAdapter> toggledAdapters =
Iterables.transform(toggleMementosList, ObjectAdapterMemento.Functions.fromMemento(concurrencyChecking));
final List<Object> domainObjects = Lists.newArrayList(Iterables.transform(toggledAdapters, ObjectAdapter.Functions.getObject()));
final Bulk.InteractionContext bulkInteractionContext = Bulk.InteractionContext.current.get();
if (bulkInteractionContext != null) {
bulkInteractionContext.setInvokedAs(InvokedAs.BULK);
bulkInteractionContext.setDomainObjects(domainObjects);
}
final CommandContext commandContext = getServicesInjector().lookupService(CommandContext.class);
final Command command;
if (commandContext != null) {
command = commandContext.getCommand();
command.setExecutor(Executor.USER);
}
ObjectAdapter lastReturnedAdapter = null;
int i=0;
for(final ObjectAdapter adapter : toggledAdapters) {
int numParameters = objectAction.getParameterCount();
if(numParameters != 0) {
return;
}
if (bulkInteractionContext != null) {
bulkInteractionContext.setIndex(i++);
}
lastReturnedAdapter = objectAction.executeWithRuleChecking(adapter, new ObjectAdapter[]{}, getAuthenticationSession(), ActionModel.WHERE_FOR_ACTION_INVOCATION);
}
model.clearToggleMementosList();
toggleboxColumn.clearToggles();
final ActionModel actionModelHint = model.getActionModelHint();
if(actionModelHint != null && actionModelHint.getActionMemento().getAction().getSemantics().isIdempotentInNature()) {
ObjectAdapter resultAdapter = actionModelHint.getObject();
model.setObjectList(resultAdapter);
} else {
model.setObject(persistentAdaptersWithin(model.getObject()));
}
if(lastReturnedAdapter != null) {
final ActionResultResponse resultResponse =
ActionResultResponseType.determineAndInterpretResult(actionModelHint, null, lastReturnedAdapter);
resultResponse.getHandlingStrategy().handleResults(this, resultResponse);
}
} catch(final ConcurrencyException ex) {
recover();
// display a warning to the user so that they know that the action wasn't performed
getMessageBroker().addWarning(ex.getMessage());
return;
} catch(final RuntimeException ex) {
final RecoverableException appEx = ActionModel.getApplicationExceptionIfAny(ex);
if (appEx != null) {
recover();
getMessageBroker().setApplicationError(appEx.getMessage());
// there's no need to abort the transaction, it will have already been done
// (in IsisTransactionManager#executeWithinTransaction(...)).
return;
}
throw ex;
}
}
private void recover() {
// resync with the objectstore
final List<ObjectAdapterMemento> toggleMementosList = Lists.newArrayList(model.getToggleMementosList());
for (ObjectAdapterMemento oam : toggleMementosList) {
// just requesting the adapter will sync the OAM's version with the objectstore
oam.getObjectAdapter(ConcurrencyChecking.NO_CHECK);
}
// discard any adapters that might have been deleted
model.setObject(persistentAdaptersWithin(model.getObject()));
// attempt to preserve the toggled adapters
final List<ObjectAdapter> adapters = model.getObject();
model.clearToggleMementosList();
for (ObjectAdapterMemento oam : toggleMementosList) {
final ObjectAdapter objectAdapter = oam.getObjectAdapter(ConcurrencyChecking.NO_CHECK);
if(adapters.contains(objectAdapter)) {
// in case it has been deleted...
model.toggleSelectionOn(objectAdapter);
}
}
}
private List<ObjectAdapter> persistentAdaptersWithin(List<ObjectAdapter> adapters) {
return Lists.newArrayList(Iterables.filter(adapters, new Predicate<ObjectAdapter>() {
@Override
public boolean apply(ObjectAdapter input) {
return !input.isTransient() && !input.isDestroyed();
}
}));
}
};
link.add(new JGrowlBehaviour());
final boolean explorationOrPrototype = CssMenuItem.isExplorationOrPrototype(objectAction);
final String actionIdentifier = CssMenuItem.actionIdentifierFor(objectAction);
final String cssClass = CssMenuItem.cssClassFor(objectAction);
final String cssClassFa = CssMenuItem.cssClassFaFor(objectAction);
return new LinkAndLabel(link, objectAction.getName(), null, false, explorationOrPrototype, actionIdentifier, cssClass, cssClassFa);
}