/**
  * This method initializes an act instance and persists it.
  *
  * @param contract
  * @param scope
  * @return
  */
 private Act createAct(Contract contract, ScopeEnum scope, String clientIp) {
   Date beginDate = new Date();
   Act act = actFactory.createAct(beginDate, contract);
   act.setStatus(ActStatus.RUNNING);
   act.setScope(scopeMap.get(scope));
   act.setClientIp(clientIp);
   actDataService.saveOrUpdate(act);
   return act;
 }
 /**
  * @param act
  * @return
  */
 private String getProjectNameFromAct(Act act) {
   StringBuilder projectName = new StringBuilder();
   projectName.append(act.getContract().getLabel());
   if (act.getScope().getCode().equals(ScopeEnum.SCENARIO)) {
     projectName.append(" - ");
     // the name of the scenario is persisted on engine's side as the URL
     // of the parent WebResource
     projectName.append(act.getAudit().getSubject().getURL());
   }
   return projectName.toString();
 }
 /**
  * @param act
  * @return
  */
 private String buildResultUrl(Act act) {
   StringBuilder strb = new StringBuilder();
   strb.append(webappUrl);
   strb.append(pageResultUrlSuffix);
   strb.append(act.getAudit().getId());
   return strb.toString();
 }
 protected void onActTerminated(Act act, Audit audit) {
   this.audit = audit;
   Date endDate = new Date();
   act.setEndDate(endDate);
   if (audit.getStatus().equals(AuditStatus.COMPLETED)) {
     act.setStatus(ActStatus.COMPLETED);
   } else {
     act.setStatus(ActStatus.ERROR);
   }
   act = actDataService.saveOrUpdate(act);
   if (exception != null) {
     sendKrashAuditEmail(act, locale, exception);
     actDataService.delete(act.getId());
     auditDataService.delete(audit.getId());
     this.audit = null;
   }
 }
 /**
  * @param act
  * @param bundle
  * @return
  */
 private void sendFailureMessageOnActTerminated(
     Act act, ResourceBundle bundle, String emailFrom, Set<String> emailTo, String projectName) {
   String emailSubject =
       bundle.getString(ERROR_SUBJECT_KEY).replaceAll(PROJECT_NAME_TO_REPLACE, projectName);
   String messageContent;
   if (act.getScope().getCode().equals(ScopeEnum.DOMAIN)) {
     messageContent = bundle.getString(SITE_ERROR_MSG_CONTENT_KEY);
   } else {
     messageContent = bundle.getString(PAGE_ERROR_MSG_CONTENT_KEY);
   }
   messageContent =
       messageContent.replaceAll(PROJECT_URL_TO_REPLACE, buildContractUrl(act.getContract()));
   messageContent = messageContent.replaceAll(URL_TO_REPLACE, buildResultUrl(act));
   messageContent = messageContent.replaceAll(PROJECT_NAME_TO_REPLACE, projectName);
   LOGGER.info("failure email sent to " + emailTo + " on audit n° " + act.getAudit().getId());
   sendEmail(emailFrom, emailTo, emailSubject, messageContent);
 }
 /**
  * Send an email when an audit terminates
  *
  * @param act
  * @param locale
  * @param exception
  */
 private void sendAuditResultEmail(Act act, Locale locale) {
   String emailTo = act.getContract().getUser().getEmail1();
   if (this.emailSentToUserExclusionList.contains(emailTo)) {
     LOGGER.info("Email not set cause user " + emailTo + " belongs to " + "exlusion list");
     return;
   }
   ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME, locale);
   String emailFrom = bundle.getString(RECIPIENT_KEY);
   Set<String> emailToSet = new HashSet<>();
   emailToSet.add(emailTo);
   if (act.getStatus().equals(ActStatus.COMPLETED)) {
     sendSuccessfulMessageOnActTerminated(
         act, bundle, emailFrom, emailToSet, getProjectNameFromAct(act));
   } else if (act.getStatus().equals(ActStatus.ERROR)) {
     sendFailureMessageOnActTerminated(
         act, bundle, emailFrom, emailToSet, getProjectNameFromAct(act));
   }
 }
 public AuditThread(
     AuditService auditService, Act act, Set<Parameter> parameterSet, Locale locale) {
   if (parameterSet != null) {
     this.parameterSet.addAll(parameterSet);
   }
   this.auditService = auditService;
   this.currentAct = act;
   this.locale = locale;
   startDate = act.getBeginDate();
 }
  /**
   * @param act
   * @param bundle
   * @param emailFrom
   * @param emailTo
   * @param projectName
   */
  private void sendSuccessfulMessageOnActTerminated(
      Act act, ResourceBundle bundle, String emailFrom, Set<String> emailTo, String projectName) {

    String emailSubject =
        bundle.getString(SUCCESS_SUBJECT_KEY).replaceAll(PROJECT_NAME_TO_REPLACE, projectName);

    String messageContent = bundle.getString(SUCCESS_MSG_CONTENT_KEY);
    messageContent = messageContent.replaceAll(URL_TO_REPLACE, buildResultUrl(act));
    messageContent = messageContent.replaceAll(PROJECT_NAME_TO_REPLACE, projectName);

    LOGGER.info("success email sent to " + emailTo + " on audit n° " + act.getAudit().getId());
    sendEmail(emailFrom, emailTo, emailSubject, messageContent);
  }
  /**
   * @param act
   * @param locale
   * @param exception
   */
  private void sendKrashAuditEmail(Act act, Locale locale, Exception exception) {
    ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME, locale);
    String emailFrom = bundle.getString(RECIPIENT_KEY);
    String projectName = getProjectNameFromAct(act);

    if (isAllowedToSendKrashReport && CollectionUtils.isNotEmpty(krashReportMailList)) {
      Set<String> emailToSet = new HashSet<>();
      emailToSet.addAll(krashReportMailList);
      String host = "";
      try {
        host = InetAddress.getLocalHost().getHostName();
      } catch (UnknownHostException uhe) {
      }

      String msgSubject = bundle.getString(KRASH_ADMIN_SUBJECT_KEY);
      msgSubject = StringUtils.replace(msgSubject, PROJECT_NAME_TO_REPLACE, projectName);
      msgSubject = StringUtils.replace(msgSubject, HOST_TO_REPLACE, host);

      String msgContent = bundle.getString(KRASH_ADMIN_MSG_CONTENT_KEY);
      msgContent = StringUtils.replace(msgContent, PROJECT_NAME_TO_REPLACE, projectName);
      msgContent =
          StringUtils.replace(
              msgContent, USER_EMAIL_TO_REPLACE, act.getContract().getUser().getEmail1());
      msgContent = StringUtils.replace(msgContent, HOST_TO_REPLACE, host);
      msgContent =
          StringUtils.replace(
              msgContent, EXCEPTION_TO_REPLACE, ExceptionUtils.getStackTrace(exception));
      if (act.getAudit().getSubject() != null) {
        msgContent =
            StringUtils.replace(
                msgContent, AUDIT_URL_TO_REPLACE, act.getAudit().getSubject().getURL());
      }
      LOGGER.info(
          "krash email sent to " + krashReportMailList + " on audit n° " + act.getAudit().getId());
      sendEmail(emailFrom, emailToSet, msgSubject, msgContent);
    }

    String emailTo = act.getContract().getUser().getEmail1();
    if (this.emailSentToUserExclusionList.contains(emailTo)) {
      LOGGER.info("Email not set cause user " + emailTo + " belongs to " + "exlusion list");
      return;
    }
    Set<String> emailToSet = new HashSet<>();
    emailToSet.add(emailTo);
    String msgSubject = bundle.getString(KRASH_SUBJECT_KEY);
    msgSubject = StringUtils.replace(msgSubject, PROJECT_NAME_TO_REPLACE, projectName);
    String msgContent = bundle.getString(KRASH_MSG_CONTENT_KEY);
    msgContent = StringUtils.replace(msgContent, PROJECT_NAME_TO_REPLACE, projectName);
    msgContent =
        StringUtils.replace(
            msgContent, PROJECT_URL_TO_REPLACE, buildContractUrl(act.getContract()));
    LOGGER.info("krash email sent to [" + emailTo + "]" + " on audit n° " + act.getAudit().getId());
    sendEmail(emailFrom, emailToSet, msgSubject, msgContent);
  }
 @Override
 public String toString() {
   return act.toString();
 }