/**
   * This method will use the DocumentService to create a new document. The documentTypeName is
   * gathered by using MaintenanceDocumentDictionaryService which uses Account class to get the
   * document type name.
   *
   * @param AccountCreationStatusDTO
   * @return document returns a new document for the account document type or null if there is an
   *     exception thrown.
   */
  public Document createCGAccountMaintenanceDocument(
      AccountCreationStatusDTO accountCreationStatus) {

    boolean internalUserSession = false;
    try {
      if (GlobalVariables.getUserSession() == null) {
        internalUserSession = true;
        GlobalVariables.setUserSession(new UserSession(KFSConstants.SYSTEM_USER));
        GlobalVariables.clear();
      }
      Document document =
          getDocumentService()
              .getNewDocument(
                  SpringContext.getBean(MaintenanceDocumentDictionaryService.class)
                      .getDocumentTypeName(Account.class));
      return document;

    } catch (Exception e) {
      accountCreationStatus.setErrorMessages(
          GlobalVariablesExtractHelper.extractGlobalVariableErrors());
      accountCreationStatus.setStatus(KcConstants.KcWebService.STATUS_KC_FAILURE);
      return null;
    } finally {
      // if a user session was established for this call, clear it our
      if (internalUserSession) {
        GlobalVariables.clear();
        GlobalVariables.setUserSession(null);
      }
    }
  }
  /** @see org.kuali.rice.krad.service.DocumentService#documentExists(java.lang.String) */
  @Override
  public boolean documentExists(String documentHeaderId) {
    // validate parameters
    if (StringUtils.isBlank(documentHeaderId)) {
      throw new IllegalArgumentException("invalid (blank) documentHeaderId");
    }

    boolean internalUserSession = false;
    try {
      // KFSMI-2543 - allowed method to run without a user session so it can be used
      // by workflow processes
      if (GlobalVariables.getUserSession() == null) {
        internalUserSession = true;
        GlobalVariables.setUserSession(new UserSession(KRADConstants.SYSTEM_USER));
        GlobalVariables.clear();
      }

      // look for workflowDocumentHeader, since that supposedly won't break the transaction
      if (getWorkflowDocumentService().workflowDocumentExists(documentHeaderId)) {
        // look for docHeaderId, since that fails without breaking the transaction
        return documentHeaderService.getDocumentHeaderById(documentHeaderId) != null;
      }

      return false;
    } finally {
      // if a user session was established for this call, clear it our
      if (internalUserSession) {
        GlobalVariables.clear();
        GlobalVariables.setUserSession(null);
      }
    }
  }
  /**
   * The default implementation - this retrieves all documents by a list of documentHeader for a
   * given class.
   *
   * @see
   *     org.kuali.rice.krad.service.DocumentService#getDocumentsByListOfDocumentHeaderIds(java.lang.Class,
   *     java.util.List)
   */
  @Override
  public List<Document> getDocumentsByListOfDocumentHeaderIds(
      Class<? extends Document> documentClass, List<String> documentHeaderIds)
      throws WorkflowException {
    // validate documentHeaderIdList and contents
    if (documentHeaderIds == null) {
      throw new IllegalArgumentException("invalid (null) documentHeaderId list");
    }
    int index = 0;
    for (String documentHeaderId : documentHeaderIds) {
      if (StringUtils.isBlank(documentHeaderId)) {
        throw new IllegalArgumentException(
            "invalid (blank) documentHeaderId at list index " + index);
      }
      index++;
    }

    boolean internalUserSession = false;
    try {
      // KFSMI-2543 - allowed method to run without a user session so it can be used
      // by workflow processes
      if (GlobalVariables.getUserSession() == null) {
        internalUserSession = true;
        GlobalVariables.setUserSession(new UserSession(KRADConstants.SYSTEM_USER));
        GlobalVariables.clear();
      }

      // retrieve all documents that match the document header ids
      List<? extends Document> rawDocuments =
          getLegacyDataAdapter().findByDocumentHeaderIds(documentClass, documentHeaderIds);

      // post-process them
      List<Document> documents = new ArrayList<Document>();
      for (Document document : rawDocuments) {
        WorkflowDocument workflowDocument =
            getWorkflowDocumentService()
                .loadWorkflowDocument(
                    document.getDocumentNumber(), GlobalVariables.getUserSession().getPerson());

        document = postProcessDocument(document.getDocumentNumber(), workflowDocument, document);
        documents.add(document);
      }
      return documents;
    } finally {
      // if a user session was established for this call, clear it our
      if (internalUserSession) {
        GlobalVariables.clear();
        GlobalVariables.setUserSession(null);
      }
    }
  }
 @Deprecated
 public static void clear() {
   GlobalVariables.clear();
   messageLists.set(new MessageList());
   auditErrorMaps.set(new HashMap<String, AuditCluster>());
   kualiForms.set(null);
 }
 @Override
 public String getReceivingDeliveryCampusCode(PurchaseOrderDocument po) {
   if (GlobalVariables.getUserSession() == null) {
     GlobalVariables.setUserSession(new UserSession(KRADConstants.SYSTEM_USER));
     GlobalVariables.clear();
   }
   return super.getReceivingDeliveryCampusCode(po);
 }
  /**
   * This is temporary until workflow 2.0 and reads from a table to get documents whose status has
   * changed to A (approved - no outstanding approval actions requested)
   *
   * @param documentHeaderId
   * @return Document
   * @throws WorkflowException
   */
  @Override
  public Document getByDocumentHeaderId(String documentHeaderId) throws WorkflowException {
    if (documentHeaderId == null) {
      throw new IllegalArgumentException("invalid (null) documentHeaderId");
    }
    boolean internalUserSession = false;
    try {
      // KFSMI-2543 - allowed method to run without a user session so it can be used
      // by workflow processes
      if (GlobalVariables.getUserSession() == null) {
        internalUserSession = true;
        GlobalVariables.setUserSession(new UserSession(KRADConstants.SYSTEM_USER));
        GlobalVariables.clear();
      }

      WorkflowDocument workflowDocument = null;

      if (LOG.isDebugEnabled()) {
        LOG.debug("Retrieving doc id: " + documentHeaderId + " from workflow service.");
      }
      workflowDocument =
          getWorkflowDocumentService()
              .loadWorkflowDocument(documentHeaderId, GlobalVariables.getUserSession().getPerson());
      UserSessionUtils.addWorkflowDocument(GlobalVariables.getUserSession(), workflowDocument);

      Class<? extends Document> documentClass =
          getDocumentClassByTypeName(workflowDocument.getDocumentTypeName());

      // retrieve the Document
      Document document =
          getLegacyDataAdapter().findByDocumentHeaderId(documentClass, documentHeaderId);
      return postProcessDocument(documentHeaderId, workflowDocument, document);
    } finally {
      // if a user session was established for this call, clear it out
      if (internalUserSession) {
        GlobalVariables.clear();
        GlobalVariables.setUserSession(null);
      }
    }
  }