private static Job getJob(CommandBase<?> command, VdcActionType actionType) {
   VdcActionParametersBase params = command.getParameters();
   Job job;
   // if Job is external, we had already created the Job by AddExternalJobCommand, so just get it
   // from DB
   if (params.getJobId() != null) {
     job = DbFacade.getInstance().getJobDao().get((Guid) params.getJobId());
   } else {
     job = createJob(actionType, command);
     JobRepositoryFactory.getJobRepository().saveJob(job);
   }
   return job;
 }
  private void endCommandOperations() {
    for (VdcActionParametersBase p : getParameters().getImagesParameters()) {
      Guid diskId = ((AddImageFromScratchParameters) p).getDiskInfo().getId();
      if (p.getTaskGroupSuccess()) {

        StorageDomainOvfInfo storageDomainOvfInfoDb = getStorageDomainOvfInfoDao().get(diskId);

        if (storageDomainOvfInfoDb == null
            || storageDomainOvfInfoDb.getStatus() != StorageDomainOvfInfoStatus.DISABLED) {
          continue;
        }

        getBackend()
            .endAction(
                p.getCommandType(),
                p,
                getContext().clone().withoutCompensationContext().withoutExecutionContext());
        storageDomainOvfInfoDb.setStatus(StorageDomainOvfInfoStatus.OUTDATED);
        getStorageDomainOvfInfoDao().update(storageDomainOvfInfoDb);
      } else {
        getBackend()
            .endAction(
                p.getCommandType(),
                p,
                getContext()
                    .clone()
                    .withoutCompensationContext()
                    .withoutExecutionContext()
                    .withoutLock());
        addCustomValue("DiskId", diskId.toString());
        auditLogDirector.log(this, AuditLogType.CREATE_OVF_STORE_FOR_STORAGE_DOMAIN_FAILED);
      }
    }

    // if we'd have the possibility to know whether we failed because of failure to acquire locks as
    // there's an
    // update in progress, we could
    // try again (avoid setSucceeded(true) in that scenario).
    VdcReturnValueBase returnValue =
        runInternalActionWithTasksContext(
            VdcActionType.ProcessOvfUpdateForStorageDomain,
            createProcessOvfUpdateForDomainParams(),
            null);
    getReturnValue().getInternalVdsmTaskIdList().addAll(returnValue.getInternalVdsmTaskIdList());

    setSucceeded(true);
  }
 /**
  * Evaluates if a given correlation-ID as part of the parameters is set correctly. If the
  * correlation-ID is null or empty, a valid correlation-ID will be set. If the correlation-ID
  * exceeds its permitted length, an error return value will be created and returned.
  *
  * @param parameters The parameters input of the command
  * @return A {@code null} object emphasis correlation-ID is valid or {@code VdcReturnValueBase}
  *     contains the correlation-ID violation message
  */
 public static VdcReturnValueBase evaluateCorrelationId(VdcActionParametersBase parameters) {
   VdcReturnValueBase returnValue = null;
   String correlationId = parameters.getCorrelationId();
   if (StringUtils.isEmpty(correlationId)) {
     correlationId = ThreadLocalParamsContainer.getCorrelationId();
     if (StringUtils.isEmpty(correlationId)) {
       correlationId = LoggedUtils.getObjectId(parameters);
     }
     parameters.setCorrelationId(correlationId);
   } else {
     List<String> messages = ValidationUtils.validateInputs(validationGroups, parameters);
     if (!messages.isEmpty()) {
       VdcReturnValueBase returnErrorValue = new VdcReturnValueBase();
       returnErrorValue.setCanDoAction(false);
       returnErrorValue.getCanDoActionMessages().addAll(messages);
       return returnErrorValue;
     }
   }
   return returnValue;
 }
  @Override
  @Before
  public void setUp() throws Exception {
    super.setUp();

    dao = prepareDAO(dbFacade.getAsyncTaskDAO());
    params = new VdcActionParametersBase();
    params.setSessionId("ASESSIONID");
    params.setTransactionScopeOption(TransactionScopeOption.RequiresNew);

    // create some test data
    newAsyncTask = new async_tasks();
    newAsyncTask.settask_id(Guid.NewGuid());
    newAsyncTask.setaction_type(VdcActionType.AddDisk);
    newAsyncTask.setstatus(AsyncTaskStatusEnum.running);
    newAsyncTask.setresult(AsyncTaskResultEnum.success);
    newAsyncTask.setaction_parameters(params);
    newAsyncTask.setCommandId(Guid.NewGuid());

    existingAsyncTask = dao.get(new Guid("340fd52b-3400-4cdd-8d3f-C9d03704b0aa"));
  }
 /**
  * Check all the failing resources retrieved from the dao.
  *
  * @param dao the dao to get the list of failing resources from
  * @param actionType autorecovery action
  * @param paramsCallback a closure to create the parameters for the autorecovery action
  * @param filter a filter to select the recoverable resources
  * @param logMsg a user-readable name for the failing resource type
  */
 <T extends BusinessEntity<Guid>> void check(
     final AutoRecoverDAO<T> dao,
     final VdcActionType actionType,
     final DoWithClosure<T, VdcActionParametersBase> paramsCallback,
     final FilterClosure<T> filter,
     final String logMsg) {
   if (!shouldPerformRecoveryOnType(logMsg)) {
     log.info("Autorecovering " + logMsg + " is disabled, skipping");
     return;
   }
   log.debugFormat("Checking autorecoverable {0}", logMsg);
   final List<T> fails = filter.filter(dao.listFailedAutorecoverables());
   if (fails.size() > 0) {
     final BackendInternal backend = getBackend();
     log.info("Autorecovering " + fails.size() + " " + logMsg);
     for (final T fail : fails) {
       log.info("Autorecovering " + logMsg + " id: " + fail.getId() + getHostName(fail));
       final VdcActionParametersBase actionParams = paramsCallback.doWith(fail);
       actionParams.setShouldBeLogged(true);
       backend.runInternalAction(actionType, actionParams);
     }
   }
   log.debugFormat("Checking autorecoverable {0} done", logMsg);
 }