/**
   * Resumes a workflow instance associated with this capture, if one exists.
   *
   * @param recordingId the recording id, which is assumed to correspond to the scheduled event id
   * @param state the new state for this recording
   */
  protected void updateWorkflow(String recordingId, String state) {
    if (!RecordingState.CAPTURING.equals(state)
        && !RecordingState.UPLOADING.equals(state)
        && !state.endsWith("_error")) {
      logger.debug("Recording state updated to {}.  Not updating an associated workflow.", state);
      return;
    }

    WorkflowInstance workflowToUpdate = null;
    try {
      workflowToUpdate = workflowService.getWorkflowById(Long.parseLong(recordingId));
    } catch (NumberFormatException e) {
      logger.info("Recording id '{}' is not a long, assuming an unscheduled capture", recordingId);
      return;
    } catch (WorkflowDatabaseException e) {
      logger.warn("Unable to update workflow for recording {}: {}", recordingId, e);
      return;
    } catch (NotFoundException e) {
      logger.warn("Unable to find a workflow with id='{}'", recordingId);
      return;
    } catch (UnauthorizedException e) {
      logger.warn("Can not update workflow: {}", e.getMessage());
    }

    // Does the workflow exist?
    if (workflowToUpdate == null) {
      logger.warn("The workflow '{}' cannot be updated because it does not exist", recordingId);
      return;
    }

    WorkflowState wfState = workflowToUpdate.getState();
    switch (workflowToUpdate.getState()) {
      case FAILED:
      case FAILING:
      case STOPPED:
      case SUCCEEDED:
        logger.debug(
            "The workflow '{}' should not be updated because it is {}",
            recordingId,
            wfState.toString().toLowerCase());
        return;
      default:
        break;
    }

    try {
      if (state.endsWith("_error")) {
        workflowToUpdate
            .getCurrentOperation()
            .setState(WorkflowOperationInstance.OperationState.FAILED);
        workflowToUpdate.setState(WorkflowState.FAILED);
        workflowService.update(workflowToUpdate);
        logger.info(
            "Recording status changed to '{}', failing workflow '{}'",
            state,
            workflowToUpdate.getId());
      } else {
        workflowService.resume(workflowToUpdate.getId());
        logger.info(
            "Recording status changed to '{}', resuming workflow '{}'",
            state,
            workflowToUpdate.getId());
      }
    } catch (Exception e) {
      logger.warn("Unable to update workflow {}: {}", workflowToUpdate.getId(), e);
    }
  }
    @Override
    public void run() {
      logger.info("Adding sample series...");
      String[] organizationIds =
          new String[] {DefaultOrganization.DEFAULT_ORGANIZATION_ID, TENANT1};

      for (int i = 1; i <= NUM_SERIES; i++) {
        String seriesId = SERIES_PREFIX + i;
        DublinCoreCatalog dc = DublinCoreCatalogImpl.newInstance();
        AccessControlList acl = new AccessControlList();

        // Add read permissions for viewing the series content in engage
        acl.getEntries()
            .add(new AccessControlEntry(SERIES_PREFIX + i + "_" + STUDENT_PREFIX, READ, true));
        acl.getEntries()
            .add(new AccessControlEntry(SERIES_PREFIX + i + "_" + INSTRUCTOR_PREFIX, READ, true));
        acl.getEntries()
            .add(new AccessControlEntry(SERIES_PREFIX + i + "_" + ADMIN_PREFIX, READ, true));

        // Add contribute permissions for adding recordings to these series
        acl.getEntries()
            .add(
                new AccessControlEntry(
                    SERIES_PREFIX + i + "_" + INSTRUCTOR_PREFIX, CONTRIBUTE, true));
        acl.getEntries()
            .add(new AccessControlEntry(SERIES_PREFIX + i + "_" + ADMIN_PREFIX, CONTRIBUTE, true));

        // Add write permissions for the instructors and admins to make changes to the series
        // themselves
        acl.getEntries()
            .add(new AccessControlEntry(SERIES_PREFIX + i + "_" + INSTRUCTOR_PREFIX, WRITE, true));
        acl.getEntries()
            .add(new AccessControlEntry(SERIES_PREFIX + i + "_" + ADMIN_PREFIX, WRITE, true));

        try {
          dc.set(DublinCore.PROPERTY_IDENTIFIER, seriesId);
          dc.set(DublinCore.PROPERTY_TITLE, "Series #" + i);
          dc.set(DublinCore.PROPERTY_CREATOR, "Creator #" + i);
          dc.set(DublinCore.PROPERTY_CONTRIBUTOR, "Contributor #" + i);

          for (String orgId : organizationIds) {
            Organization org = organizationDirectoryService.getOrganization(orgId);
            try {
              securityService.setUser(
                  new User(
                      "userandseriesloader",
                      orgId,
                      new String[] {SecurityConstants.GLOBAL_ADMIN_ROLE}));
              securityService.setOrganization(org);

              try {
                // Test if the serie already exist, it does not overwrite it.
                if (seriesService.getSeries(seriesId) != null) continue;
              } catch (NotFoundException e) {
                // If the series does not exist, we create it.
                seriesService.updateSeries(dc);
                seriesService.updateAccessControl(seriesId, acl);
              }
            } catch (UnauthorizedException e) {
              logger.warn(e.getMessage());
            } catch (SeriesException e) {
              logger.warn("Unable to create series {}", dc);
            } catch (NotFoundException e) {
              logger.warn("Unable to find series {}", dc);
            } finally {
              securityService.setOrganization(null);
              securityService.setUser(null);
            }
          }
          logger.debug("Added series {}", dc);
        } catch (NotFoundException e) {
          logger.warn("Unable to find organization {}", e.getMessage());
        }
      }

      load(
          STUDENT_PREFIX,
          20,
          new String[] {USER_ROLE},
          DefaultOrganization.DEFAULT_ORGANIZATION_ID);
      load(STUDENT_PREFIX, 20, new String[] {USER_ROLE}, TENANT1);

      load(
          INSTRUCTOR_PREFIX,
          2,
          new String[] {USER_ROLE, INSTRUCTOR_ROLE},
          DefaultOrganization.DEFAULT_ORGANIZATION_ID);
      load(INSTRUCTOR_PREFIX, 2, new String[] {USER_ROLE, INSTRUCTOR_ROLE}, TENANT1);

      load(
          ADMIN_PREFIX,
          1,
          new String[] {USER_ROLE, COURSE_ADMIN_ROLE},
          DefaultOrganization.DEFAULT_ORGANIZATION_ID);
      load(ADMIN_PREFIX, 1, new String[] {USER_ROLE, COURSE_ADMIN_ROLE}, TENANT1);

      loadLdapUser(DefaultOrganization.DEFAULT_ORGANIZATION_ID);
      loadLdapUser(TENANT1);

      logger.info("Finished loading sample series and users");
    }