use of com.cognifide.apm.api.actions.Context in project APM by Cognifide.
the class DestroyUser method execute.
@Override
public ActionResult execute(Context context) throws ActionExecutionException {
ActionResult actionResult;
try {
User user = context.getAuthorizableManager().getUser(userId);
// local context is used here to not override current authorizable in given context
Context localContext = context.newContext();
localContext.setCurrentAuthorizable(user);
Action removeFromGroups = new RemoveParents(getGroups(user));
ActionResult purgeResult = purge.execute(localContext);
ActionResult removeFromGroupsResult = removeFromGroups.execute(localContext);
ActionResult removeResult = remove.execute(localContext);
actionResult = purgeResult.merge(removeFromGroupsResult, removeResult);
actionResult.setAuthorizable(context.getCurrentAuthorizable().getID() + " (ignored)");
} catch (RepositoryException | ActionExecutionException e) {
actionResult = context.createActionResult();
actionResult.logError(MessagingUtils.createMessage(e));
}
return actionResult;
}
use of com.cognifide.apm.api.actions.Context in project APM by Cognifide.
the class ScriptManagerImpl method execute.
private Progress execute(Script script, final ExecutionMode mode, Map<String, String> customDefinitions, ResourceResolver resolver) throws ExecutionException, RepositoryException {
if (script == null) {
throw new ExecutionException("Script is not specified");
}
if (mode == null) {
throw new ExecutionException("Execution mode is not specified");
}
final String path = script.getPath();
LOG.info(String.format("Script execution started: %s [%s]", path, mode));
final Progress progress = new ProgressImpl(resolver.getUserID());
final ActionExecutor actionExecutor = createExecutor(mode, resolver);
final Context context = actionExecutor.getContext();
final SessionSavingPolicy savingPolicy = context.getSavingPolicy();
eventManager.trigger(new ScriptLaunchedEvent(script, mode));
ScriptRunner scriptRunner = new ScriptRunner(scriptFinder, resolver, mode == ExecutionMode.VALIDATION, (executionContext, commandName, arguments) -> {
try {
context.setCurrentAuthorizable(executionContext.getAuthorizable());
ActionDescriptor descriptor = actionFactory.evaluate(commandName, arguments);
ActionResult result = actionExecutor.execute(descriptor);
executionContext.setAuthorizable(context.getCurrentAuthorizableIfExists());
progress.addEntry(descriptor, result);
if ((Status.ERROR != result.getStatus()) || (ExecutionMode.DRY_RUN == mode)) {
savingPolicy.save(context.getSession(), SessionSavingMode.EVERY_ACTION);
}
return result.getStatus();
} catch (RepositoryException | ActionCreationException e) {
LOG.error("Error while processing command: {}", commandName, e);
progress.addEntry(Status.ERROR, e.getMessage(), commandName);
return Status.ERROR;
}
});
try {
Map<String, String> definitions = new HashMap<>();
definitions.putAll(getPredefinedDefinitions());
definitions.putAll(customDefinitions);
scriptRunner.execute(script, progress, definitions);
} catch (RuntimeException e) {
progress.addEntry(Status.ERROR, e.getMessage());
}
if (progress.isSuccess()) {
savingPolicy.save(context.getSession(), SessionSavingMode.SINGLE);
}
return progress;
}
Aggregations