/**
   * Create document for sent mail information
   *
   * @param processAttachmentsFolder
   * @param attachmentInfo
   */
  private void createDocumentForMail(Folder processAttachmentsFolder, String attachmentInfo) {
    String docName =
        RepositoryUtility.createDocumentName(
            processAttachmentsFolder,
            DocumentMgmtUtility.stripOffSpecialCharacters(mailSubject),
            0);

    DocumentInfo docInfo = DmsUtils.createDocumentInfo(docName);
    docInfo.setContentType(MimeTypesHelper.HTML.getType());
    docInfo.setOwner(ContextPortalServices.getUser().getAccount());

    // set correspondence data
    PrintDocumentAnnotations annotations = new PrintDocumentAnnotationsImpl();
    annotations.setTemplateType(DocumentTemplate.CORRESPONDENCE_TEMPLATE);
    annotations.setRecipients(getToMailAddress());
    annotations.setBlindCarbonCopyRecipients(bccMailAddress);
    annotations.setCarbonCopyRecipients(ccMailAddress);
    annotations.setFaxNumber(getFaxMailAddress());
    annotations.setSendDate(Calendar.getInstance().getTime());
    annotations.setAttachments(attachmentInfo);
    annotations.setSender(mailSender);
    annotations.setSubject(getMailSubject());
    annotations.setFaxEnabled(false);
    annotations.setEmailEnabled(false);

    docInfo.setDocumentAnnotations(annotations);

    Document mailDocument =
        DocumentMgmtUtility.getDocumentManagementService()
            .createDocument(
                processAttachmentsFolder.getId(), docInfo, editor.getContent().getBytes(), null);

    DMSHelper.addAndSaveProcessAttachment(processInstance, mailDocument);
  }
  /**
   * Action handler - gets invoked on send mail click button
   *
   * @param actionEvent
   * @throws Exception
   */
  public void sendMessage(ActionEvent actionEvent) throws Exception {
    try {
      Map<RecipientType, String[]> recipientDetails = new HashMap<RecipientType, String[]>();
      List<String> errorMsg = validateMailAddresses(recipientDetails);

      if (errorMsg.size() == 0) {
        String mailContent = editor.getContent();
        // Replace with <br/>, to maintain multiline msg entered on TextArea
        mailContent = mailContent.replaceAll("(\r\n|\n)", "<br />");
        // Send mail
        boolean sendMailSuccess =
            mailService.sendMail(
                recipientDetails, mailSender, mailSubject, mailContent, attachments);

        if (sendMailSuccess) {
          // Save document
          saveEmailDocument();
          PortalApplication.getInstance().closeFocusView();
        }
      } else {
        StringBuilder sb = new StringBuilder();
        for (String msg : errorMsg) {
          sb.append(msg);
          sb.append(",");
          sb.append("\n");
        }
        MessageDialog.addErrorMessage(sb.toString());
      }
    } catch (Exception e) {
      ExceptionHandler.handleException(e);
    }
  }
 /**
  * @param printerDialogPopup
  * @return
  */
 private PdfResource getPdfResource(PrinterDialogPopup printerDialogPopup) {
   FacesContext fc = FacesContext.getCurrentInstance();
   ExternalContext ec = fc.getExternalContext();
   String name = "" + System.currentTimeMillis() + ".pdf";
   PdfResource resource =
       new PdfResource(
           ec,
           name,
           editor.getContent(),
           getAttachments(),
           printerDialogPopup.getPrintingPreferences(),
           pdfConverterHelper);
   return resource;
 }
  /**
   * @param document
   * @param addAtTop
   * @return
   */
  private boolean addTemplate(Document document, boolean addAtTop) {
    if (isDocumentTemplate(document)) {
      String tempContent =
          new String(
              DocumentMgmtUtility.getDocumentManagementService()
                  .retrieveDocumentContent(document.getId()));

      tempContent = resolveExpressions(tempContent);

      editor.addContent(
          tempContent,
          addAtTop ? DocumentEditingPolicy.ADD_AT_TOP : DocumentEditingPolicy.ADD_AT_BOTTOM);

      return true;
    } else {
      MessageDialog.addErrorMessage(
          this.getMessages().getString("invalid.template.message")
              + " "
              + document.getContentType());
      return false;
    }
  }