/**
   * Handles versioning for AMPP documents. Major version will be created if document of particular
   * type already exists in Article Delivery Folder.
   *
   * @param idfSession
   * @param idfAttachmentDocObj
   * @param strArticleFolderPath
   */
  private void setAttachmentToArticleForAMPP(
      IDfSession idfSession,
      IDfDocument idfAttachmentDocObj,
      String strArticleFolderPath,
      String strStageFolderName,
      String strDeliveryFolderName) {
    DfLogger.debug(this, utilObj.getSpaces() + "Inside method processAttachments.", null, null);
    Boolean isCheckedOut = false;
    IDfDocument docObj = null;
    String strObjectName = "";
    boolean isLogObject = false;
    String strObjectType = "";
    String strItemType = "";
    String strLogCategory = "";
    String strLogType = "";

    StringBuilder strFolderPah = new StringBuilder(strArticleFolderPath + "/" + strStageFolderName);

    try {
      strObjectName = idfAttachmentDocObj.getObjectName();

      if (strObjectName.toLowerCase().contains(IConstants.AMPP_FILE_SUFFIX_ARTICLE_XML)) {

        strItemType = IConstants.AMPP_WORKITEM_VALUE_XML;
        strObjectType = IConstants.TYPE_WORKITEM_OBJECT;
        strFolderPah.append(IConstants.PATH_AMPP_COPY_EDITING);
      } else if (strObjectName.toLowerCase().contains(IConstants.FILE_SUFFIX_ARTICLE_PDF)) {

        strItemType = IConstants.AMPP_WORKITEM_VALUE_ONLINE_PDF;
        strObjectType = IConstants.TYPE_WORKITEM_OBJECT;
        strFolderPah.append(IConstants.PATH_AMPP_COPY_EDITING);
      } else if (strObjectName
          .toLowerCase()
          .contains(IConstants.AMPP_FILE_SUFFIX_VALIDATION_LOGL)) {

        strObjectType = IConstants.TYPE_LOG_OBJECT;
        strLogCategory = IConstants.ATTR_LOG_CATEGORY_AMPP_CC;
        isLogObject = true;
        strFolderPah.append(IConstants.PATH_COPY_EDITING_LOG_FOLDER);
      } else if (strObjectName.toLowerCase().contains(IConstants.AMPP_FILE_PREFX_ERROR_LOG)
          && strObjectName.endsWith(".xml")) {

        strObjectType = IConstants.TYPE_LOG_OBJECT;
        strLogCategory = IConstants.ATTR_LOG_CATEGORY_AMPP_MESSAGES;
        strLogType = IConstants.LOG_TYPE_FAILURE;
        isLogObject = true;
        strFolderPah.append(
            IConstants.PATH_COPY_EDITING_FOLDER + "/" + IConstants.FOLDER_NAME_LOGS);
      } else if (strObjectName.toLowerCase().contains(IConstants.BREEZE_FILE_SUFFIX_ERROR_LOG)
          && strObjectName.endsWith(".txt")) {

        strObjectType = IConstants.TYPE_LOG_OBJECT;
        strLogCategory = IConstants.ATTR_LOG_CATEGORY_BREEZE;
        isLogObject = true;
        strFolderPah.append(
            IConstants.PATH_COPY_EDITING_FOLDER + "/" + IConstants.FOLDER_NAME_LOGS);
      } else if (strObjectName.toLowerCase().contains(IConstants.BREEZE_FILE_SUFFIX_SUCCESS_LOG)
          && strObjectName.endsWith(".txt")) {

        strObjectType = IConstants.TYPE_LOG_OBJECT;
        strLogCategory = IConstants.ATTR_LOG_CATEGORY_BREEZE;
        isLogObject = true;
        strFolderPah.append(
            IConstants.PATH_COPY_EDITING_FOLDER + "/" + IConstants.FOLDER_NAME_LOGS);
      } else if (strObjectName.toLowerCase().contains(IConstants.AMPP_FILE_SUFFIX_STYLED_DOC)) {

        for (String strExtensions : IConstants.AMPP_FILE_EXT_STYLED_DOC) {
          if (strObjectName.contains("." + strExtensions)) {
            strItemType = IConstants.AMPP_WORKITEM_VALUE_DOC;
            strObjectType = IConstants.TYPE_WORKITEM_OBJECT;
            strFolderPah.append(IConstants.PATH_COPY_EDITING_FOLDER);
            break;
          }
        }
      } else {
        idfAttachmentDocObj.destroy();
        throw new RuntimeException(
            "Unexpected file received : " + strObjectName + " | Destroyed Object.. ");
      }

      DfLogger.debug(this, "setAttachmentToArticle folder path : " + strFolderPah, null, null);

      if (!isLogObject) {
        docObj =
            (IDfDocument)
                idfSession.getObjectByQualification(
                    strObjectType
                        + " where Folder('"
                        + strFolderPah
                        + "') "
                        + " and item_type='"
                        + strItemType
                        + "'");
      } else {
        docObj =
            (IDfDocument)
                idfSession.getObjectByQualification(
                    strObjectType
                        + " where Folder('"
                        + strFolderPah
                        + "') "
                        + " and "
                        + IConstants.ATTR_LOG_CATEGORY
                        + " = '"
                        + strLogCategory
                        + "'");
      }

      idfAttachmentDocObj.getFile(null);

      if (docObj != null) {

        DfLogger.debug(
            this,
            utilObj.getSpaces()
                + "Existing version found.Creating new version inside Delivery Folder",
            null,
            null);
        // [TODO] see if checking of "if checked out" can be added in
        // executeCheckoutOperation
        if (docObj.isCheckedOut()) {
          docObj.cancelCheckout();
        }
        isCheckedOut = utilObj.executeCheckoutOperation(docObj);

        if (isCheckedOut) {
          utilObj.executeCheckinOperation(idfSession, docObj, idfAttachmentDocObj);
        }
      } else {
        DfLogger.debug(
            this,
            utilObj.getSpaces() + "New document processing. Linking to article folder",
            null,
            null);

        if (isLogObject == true) {
          DfLogger.debug(this, "Object Name of log file : " + strObjectName, null, null);
          IDfQuery qry = new DfQuery();
          String strObjId = idfAttachmentDocObj.getObjectId().getId();
          qry.setDQL(
              "change "
                  + IConstants.TYPE_WORKITEM_OBJECT
                  + " objects to dm_document where r_object_id='"
                  + strObjId
                  + "'");
          DfLogger.debug(this, qry.getDQL(), null, null);

          IDfCollection idfColl1 = qry.execute(idfSession, IDfQuery.DF_EXEC_QUERY);
          if (null != idfColl1 && IDfCollection.DF_CLOSED_STATE != idfColl1.getState()) {
            idfColl1.close();
          }

          qry.setDQL(
              "change dm_document objects to "
                  + IConstants.TYPE_LOG_OBJECT
                  + " where r_object_id='"
                  + strObjId
                  + "'");
          DfLogger.debug(this, qry.getDQL(), null, null);

          IDfCollection idfColl2 = qry.execute(idfSession, IDfQuery.DF_EXEC_QUERY);
          if (null != idfColl2 && IDfCollection.DF_CLOSED_STATE != idfColl2.getState()) {
            idfColl2.close();
          }
          idfAttachmentDocObj.fetch(null);
          idfAttachmentDocObj.setString(IConstants.ATTR_LOG_TYPE, strLogType);
          idfAttachmentDocObj.setString(IConstants.ATTR_LOG_CATEGORY, strLogCategory);
          idfAttachmentDocObj.setObjectName(strObjectName);
          idfAttachmentDocObj.save();
        }

        IDfACL folderAcl = idfSession.getFolderByPath(strFolderPah.toString()).getACL();
        DfLogger.debug(this, folderAcl.getObjectName(), null, null);

        idfAttachmentDocObj.setACL(folderAcl);
        idfAttachmentDocObj.unlink(idfAttachmentDocObj.getFolderId(0).toString());
        idfAttachmentDocObj.link(strFolderPah.toString());
        setWorkitemMetadata(idfAttachmentDocObj);
      }
      DfLogger.debug(
          this,
          utilObj.getSpaces() + "Method processAttachments completed successfully.",
          null,
          null);
    } catch (DfException e) {
      DfLogger.error(this, "Error occured in processing attachment: " + e.getMessage(), null, e);
    }
  }