/**
   * resolves the expression in template using in datapath
   *
   * @param input
   * @return
   */
  private String resolveExpressions(String input) {
    try {
      String fragment = input;
      String expression = input;
      String result = "";

      for (int start = fragment.indexOf("#{"); start > -1; start = fragment.indexOf("#{")) {
        result += fragment.substring(0, start);
        fragment = fragment.substring(start + 2);

        int end = fragment.indexOf("}");

        expression = fragment.substring(0, end);

        try {
          result += getCurrentProcessInstanceDataValue(expression);
        } catch (Exception e) {
          trace.warn("Could not resolve expression for expression:" + expression);
          result +=
              "<span style=\"background-color: rgb(255, 255, 153);\">#{" + expression + "}</span>";
        }

        fragment = fragment.substring(end + 1);
      }

      result += fragment;

      return result;
    } catch (Exception e) {
      ExceptionHandler.handleException(e);
    }

    return input;
  }
  /**
   * 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);
    }
  }
  /** @return */
  public String selectAction() {
    try {
      select();
    } catch (Exception e) {
      ExceptionHandler.handleException(e);
    }

    return null;
  }
  /** Action handler method to remove attachments */
  public void removeAttachment() {
    try {

      FacesContext context = FacesContext.getCurrentInstance();
      String documentKey =
          (String) context.getExternalContext().getRequestParameterMap().get("documentKey");

      for (Attachment attachment1 : attachments) {
        if (attachment1.getDocumentKey().equals(documentKey)) {
          attachments.remove(attachment1);
          break;
        }
      }
    } catch (Exception exception) {
      ExceptionHandler.handleException(exception);
    }
  }