protected void removeOnlineReviewDocument(
      ProtocolOnlineReviewDocumentBase protocolOnlineReviewDocument,
      ProtocolSubmissionBase submission,
      String annotation) {

    if (protocolOnlineReviewDocument != null) {
      if (LOG.isDebugEnabled()) {
        LOG.debug(
            String.format(
                "Found protocolOnlineReviewDocument %s, removing it.",
                protocolOnlineReviewDocument.getDocumentNumber()));
      }
      cancelOnlineReviewDocument(protocolOnlineReviewDocument, submission, annotation);
      protocolOnlineReviewDocument
          .getProtocolOnlineReview()
          .setProtocolOnlineReviewStatusCode(
              ProtocolOnlineReviewStatus.REMOVED_CANCELLED_STATUS_CD);

      List<CommitteeScheduleMinuteBase> reviewComments =
          protocolOnlineReviewDocument.getProtocolOnlineReview().getCommitteeScheduleMinutes();
      List<CommitteeScheduleMinuteBase> deletedReviewComments =
          new ArrayList<CommitteeScheduleMinuteBase>();
      getReviewerCommentsService().deleteAllReviewComments(reviewComments, deletedReviewComments);
      getReviewerCommentsService().saveReviewComments(reviewComments, deletedReviewComments);

      submission
          .getProtocolReviewers()
          .remove(protocolOnlineReviewDocument.getProtocolOnlineReview().getProtocolReviewer());
      getBusinessObjectService().save(protocolOnlineReviewDocument.getProtocolOnlineReview());
    }
  }
  protected void finalizeOnlineReviewDocument(
      ProtocolOnlineReviewDocumentBase protocolOnlineReviewDocument,
      ProtocolSubmissionBase submission,
      String annotation) {

    try {

      final String principalId =
          identityManagementService
              .getPrincipalByPrincipalName(KRADConstants.SYSTEM_USER)
              .getPrincipalId();
      WorkflowDocument workflowDocument =
          WorkflowDocumentFactory.loadDocument(
              principalId, protocolOnlineReviewDocument.getDocumentNumber());
      ProtocolOnlineReviewBase review = protocolOnlineReviewDocument.getProtocolOnlineReview();
      review.addActionPerformed(
          "Finalize:"
              + workflowDocument.getStatus().getCode()
              + ":"
              + review.getProtocolOnlineReviewStatusCode());

      if (workflowDocument.isEnroute()
          || workflowDocument.isInitiated()
          || workflowDocument.isSaved()) {
        workflowDocument.superUserBlanketApprove(annotation);
      }
    } catch (Exception e) {
      String errorMessage =
          String.format(
              "Workflow exception generated while executing superUserApprove on document %s in finalizeOnlineReviewDocument. Message:%s",
              protocolOnlineReviewDocument.getDocumentNumber(), e.getMessage());
      LOG.error(errorMessage);
      throw new RuntimeException(errorMessage, e);
    }
  }
  protected void cancelOnlineReviewDocument(
      ProtocolOnlineReviewDocumentBase protocolOnlineReviewDocument,
      ProtocolSubmissionBase submission,
      String annotation) {
    try {

      final String principalId =
          identityManagementService
              .getPrincipalByPrincipalName(KRADConstants.SYSTEM_USER)
              .getPrincipalId();
      WorkflowDocument workflowDocument =
          WorkflowDocumentFactory.loadDocument(
              principalId, protocolOnlineReviewDocument.getDocumentNumber());

      if (workflowDocument.isEnroute()
          || workflowDocument.isInitiated()
          || workflowDocument.isSaved()) {
        workflowDocument.superUserCancel(
            String.format(
                "Review Cancelled from assign reviewers action by %s",
                GlobalVariables.getUserSession().getPrincipalId()));
      }
    } catch (Exception e) {
      String errorMessage =
          String.format(
              "Exception generated while executing superUserCancel on document %s in removeOnlineReviewDocument. Message: %s",
              protocolOnlineReviewDocument.getDocumentNumber(), e.getMessage());
      LOG.error(errorMessage);
      throw new RuntimeException(errorMessage, e);
    }
  }
 public void returnProtocolOnlineReviewDocumentToReviewer(
     ProtocolOnlineReviewDocumentBase reviewDocument, String reason, String principalId) {
   kraDocumentRejectionService.reject(
       reviewDocument.getDocumentHeader().getWorkflowDocument(),
       reason,
       principalId,
       (String) null,
       reviewerApproveNodeName);
 }
  @Override
  public void removeOnlineReviewDocument(
      String personId,
      boolean nonEmployeeFlag,
      ProtocolSubmissionBase submission,
      String annotation) {
    ProtocolOnlineReviewDocumentBase protocolOnlineReviewDocument =
        this.getProtocolOnlineReviewDocument(personId, nonEmployeeFlag, submission);

    ProtocolOnlineReviewBase submissionsProtocolOnlineReview = null;
    for (ProtocolOnlineReviewBase rev : submission.getProtocolOnlineReviews()) {
      if (rev.getProtocolOnlineReviewId()
          .equals(
              protocolOnlineReviewDocument.getProtocolOnlineReview().getProtocolOnlineReviewId())) {
        submissionsProtocolOnlineReview = rev;
        break;
      }
    }

    if (submissionsProtocolOnlineReview == null) {
      throw new IllegalStateException(
          "Could not match OnlineReview document being removed to a protocolOnlineReview in the submission.");
    }

    if (LOG.isDebugEnabled()) {
      LOG.debug(
          String.format(
              "Processing request to remove online review for (personId=%s,nonEmployeeFlag=%s) from (protocol=%s,submission=%s)",
              personId,
              nonEmployeeFlag,
              submission.getProtocol().getProtocolNumber(),
              submission.getSubmissionNumber()));
    }

    if (protocolOnlineReviewDocument != null) {
      if (LOG.isDebugEnabled()) {
        LOG.debug(
            String.format(
                "Found protocolOnlineReviewDocument %s, removing it.",
                protocolOnlineReviewDocument.getDocumentNumber()));
      }
      cancelOnlineReviewDocument(protocolOnlineReviewDocument, submission, annotation);
      submissionsProtocolOnlineReview.setProtocolOnlineReviewStatusCode(
          getProtocolOLRRemovedCancelledStatusCodeHook());

      List<CommitteeScheduleMinuteBase> reviewComments =
          protocolOnlineReviewDocument.getProtocolOnlineReview().getCommitteeScheduleMinutes();
      List<CommitteeScheduleMinuteBase> deletedReviewComments =
          new ArrayList<CommitteeScheduleMinuteBase>();
      getReviewerCommentsService().deleteAllReviewComments(reviewComments, deletedReviewComments);
      getReviewerCommentsService().saveReviewComments(reviewComments, deletedReviewComments);

      getBusinessObjectService().save(submissionsProtocolOnlineReview);

    } else {
      LOG.warn(
          String.format(
              "ProtocolBase Online Review document could not be found for (personId=%s,nonEmployeeFlag=%s) from (protocol=%s,submission=%s)",
              personId,
              nonEmployeeFlag,
              submission.getProtocol().getProtocolNumber(),
              submission.getSubmissionNumber()));
    }
  }
  /**
   * Creates a new ProtocolReviewDocument.
   *
   * <p>Handles creating the workflow document, and the underlying ProtocolReview BO linking the
   * protocol, submission, and reviewer.
   *
   * @param protocolSubmission The protocol submission
   * @param protocolReviewerBean The bean that holds
   * @param documentDescription the description for the created document
   * @param documentExplanation the explanation for the created document
   * @param documentOrganizationNumber the organizationNumber for the created document
   * @param principalId The principalId to use when creating the workflow document. Usually this
   *     should be the principal of the user creating the review.
   * @return
   * @throws WorkflowException
   */
  protected ProtocolOnlineReviewDocumentBase createProtocolOnlineReviewDocument(
      ProtocolSubmissionBase protocolSubmission,
      ProtocolReviewer protocolReviewer,
      String documentDescription,
      String documentExplanation,
      String documentOrganizationDocumentNumber,
      Date dateRequested,
      Date dateDue,
      String principalId)
      throws WorkflowException {

    ProtocolOnlineReviewDocumentBase protocolReviewDocument;

    Person person = personService.getPerson(principalId);
    WorkflowDocument workflowDocument =
        workflowDocumentService.createWorkflowDocument(getProtocolOLRDocumentTypeHook(), person);

    DocumentHeader docHeader = new DocumentHeader();
    docHeader.setWorkflowDocument(workflowDocument);
    docHeader.setDocumentNumber(workflowDocument.getDocumentId().toString());
    protocolReviewDocument = getNewProtocolOnlineReviewDocumentInstanceHook();
    protocolReviewDocument.setDocumentNumber(docHeader.getDocumentNumber());
    protocolReviewDocument.setDocumentHeader(docHeader);

    protocolReviewDocument.getProtocolOnlineReview().setProtocol(protocolSubmission.getProtocol());

    protocolReviewDocument
        .getProtocolOnlineReview()
        .setProtocolId(protocolSubmission.getProtocolId());

    protocolReviewDocument.getProtocolOnlineReview().setProtocolSubmission(protocolSubmission);
    protocolReviewDocument
        .getProtocolOnlineReview()
        .setSubmissionIdFk(protocolSubmission.getSubmissionId());
    protocolReviewDocument
        .getProtocolOnlineReview()
        .setProtocolOnlineReviewStatusCode(getProtocolOLRSavedStatusCodeHook());
    protocolReviewDocument
        .getProtocolOnlineReview()
        .setDateRequested(
            dateRequested == null ? new Date((new java.util.Date()).getTime()) : dateRequested);
    protocolReviewDocument.getProtocolOnlineReview().setDateDue(dateDue);

    protocolReviewDocument
        .getProtocolOnlineReview()
        .setProtocolReviewerId(protocolReviewer.getProtocolReviewerId());
    protocolReviewDocument.getProtocolOnlineReview().setProtocolReviewer(protocolReviewer);

    docHeader.setDocumentDescription(documentDescription);
    docHeader.setOrganizationDocumentNumber(documentOrganizationDocumentNumber);
    docHeader.setExplanation(documentExplanation);

    documentService.saveDocument(protocolReviewDocument);
    return protocolReviewDocument;
  }