@Override
  public TaskRunResult run(Task task) {

    OperationResult result =
        task.getResult().createSubresult(WaitForTasksTaskHandler.class.getName() + ".run");
    result.recordInProgress();

    LOGGER.info("WaitForTasksTaskHandler run starting; in task " + task.getName());
    try {
      // todo resolve this brutal hack
      taskManagerImpl.pauseTask(task, TaskWaitingReason.OTHER, result);
      task.startWaitingForTasksImmediate(result);
    } catch (SchemaException e) {
      throw new SystemException(
          "Couldn't mark task as waiting for prerequisite tasks",
          e); // should not occur; will be handled by task runner
    } catch (ObjectNotFoundException e) {
      throw new SystemException(
          "Couldn't mark task as waiting for prerequisite tasks",
          e); // should not occur; will be handled by task runner
    }
    LOGGER.info("WaitForTasksTaskHandler run finishing; in task " + task.getName());

    result.computeStatus();

    TaskRunResult runResult = new TaskRunResult();
    runResult.setOperationResult(result);
    runResult.setProgress(task.getProgress()); // not to overwrite task's progress
    runResult.setRunResultStatus(TaskRunResultStatus.FINISHED);
    return runResult;
  }
  @Override
  public TaskRunResult run(Task task) {

    OperationResult result = task.getResult().createSubresult(DOT_CLASS + "run");
    TaskRunResult runResult = new TaskRunResult();

    LensContextType contextType = task.getModelOperationContext();
    if (contextType == null) {
      LOGGER.trace("No model context found, skipping the model operation execution.");
      if (result.isUnknown()) {
        result.computeStatus();
      }
      runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.FINISHED);
    } else {
      LensContext context = null;
      try {
        context =
            LensContext.fromLensContextType(contextType, prismContext, provisioningService, result);
      } catch (SchemaException e) {
        throw new SystemException(
            "Cannot recover model context from task " + task + " due to schema exception", e);
      } catch (ObjectNotFoundException e) {
        throw new SystemException("Cannot recover model context from task " + task, e);
      } catch (CommunicationException e) {
        throw new SystemException(
            "Cannot recover model context from task " + task, e); // todo wait and retry
      } catch (ConfigurationException e) {
        throw new SystemException("Cannot recover model context from task " + task, e);
      }

      if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Context to be executed = {}", context.debugDump());
      }

      try {
        // here we brutally remove all the projection contexts -- because if we are continuing after
        // rejection of a role/resource assignment
        // that resulted in such projection contexts, we DO NOT want them to appear in the context
        // any more
        context.rot();
        Iterator<LensProjectionContext> projectionIterator =
            context.getProjectionContextsIterator();
        while (projectionIterator.hasNext()) {
          LensProjectionContext projectionContext = projectionIterator.next();
          if (projectionContext.getPrimaryDelta() != null
              && !projectionContext.getPrimaryDelta().isEmpty()) {
            continue; // don't remove client requested actions!
          }
          if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(
                "Removing projection context {}", projectionContext.getHumanReadableName());
          }
          projectionIterator.remove();
        }
        if (task.getChannel() == null) {
          task.setChannel(context.getChannel());
        }
        clockwork.run(context, task, result);

        task.setModelOperationContext(context.toLensContextType());
        task.savePendingModifications(result);

        if (result.isUnknown()) {
          result.computeStatus();
        }
        runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.FINISHED);
      } catch (Exception e) { // too many various exceptions; will be fixed with java7 :)
        String message = "An exception occurred within model operation, in task " + task;
        LoggingUtils.logException(LOGGER, message, e);
        result.recordPartialError(message, e);
        // TODO: here we do not know whether the error is temporary or permanent (in the future we
        // could discriminate on the basis of particular exception caught)
        runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.TEMPORARY_ERROR);
      }
    }

    task.getResult().recomputeStatus();
    runResult.setOperationResult(task.getResult());
    return runResult;
  }