private String buildAlarmTitle(ThresholdAlarmMeta meta) {
    String type = meta.getType();

    if (type.equalsIgnoreCase(AlertInfo.EXCEPTION)) {
      return CatString.EXCEPTION + "[ " + String.valueOf(meta.getDomain()) + " ]";
    } else if (type.equalsIgnoreCase(AlertInfo.SERVICE)) {
      return CatString.SERVICE + "[ " + String.valueOf(meta.getDomain()) + " ]";
    }

    return "Default";
  }
  private AlertInfo buildAlertInfo(
      ThresholdAlarmMeta meta, String title, String content, String ruleType, int alertType) {
    AlertInfo info = new AlertInfo();

    info.setContent(content);
    info.setTitle(title);
    info.setRuleId(meta.getRuleId());
    info.setDate(meta.getDate());
    info.setRuleType(ruleType);
    info.setAlertType(alertType);
    return info;
  }
  private String buildEmailAlarmContent(ThresholdAlarmMeta meta) {
    Map<Object, Object> root = new HashMap<Object, Object>();
    StringWriter sw = new StringWriter(5000);

    root.put("rule", buildRuleMeta(meta.getDuration()));
    root.put("count", meta.getRealCount());
    root.put("domain", meta.getDomain());
    root.put("date", meta.getDate());
    root.put("url", buildProblemUrl(meta.getBaseShowUrl(), meta.getDomain(), meta.getDate()));

    try {
      String type = meta.getType();

      if (type.equalsIgnoreCase(AlertInfo.EXCEPTION)) {
        Template t = m_configuration.getTemplate("exceptionAlarm.ftl");

        t.process(root, sw);
      } else if (type.equalsIgnoreCase(AlertInfo.SERVICE)) {
        Template t = m_configuration.getTemplate("serviceAlarm.ftl");

        t.process(root, sw);
      }
    } catch (Exception e) {
      Cat.logError(e);
    }
    return sw.toString();
  }
  @Override
  public void onEvent(Event event) {
    ThresholdAlertEvent alertEvent = (ThresholdAlertEvent) event;
    ThresholdAlarmMeta metaInfo = alertEvent.getAlarmMeta();
    String title = buildAlarmTitle(metaInfo);
    String content = buildEmailAlarmContent(metaInfo);
    String alertType = metaInfo.getDuration().getAlarm().toLowerCase();
    String ruleType = metaInfo.getType();

    if (alertType != null && alertType.length() > 0) {
      String[] types = alertType.split(",");

      for (String type : types) {
        if (type.equalsIgnoreCase(AlertInfo.EMAIL)) {
          List<String> emailAddress = m_ruleManager.queryUserMailsByRuleId(metaInfo.getRuleId());
          AlertInfo info = buildAlertInfo(metaInfo, title, content, ruleType, AlertInfo.EMAIL_TYPE);

          info.setMails(emailAddress);
          m_alertManager.addAlarmInfo(info);
        }
        if (type.equalsIgnoreCase(AlertInfo.SMS)) {
          List<String> emailAddress = m_ruleManager.queryUserMailsByRuleId(metaInfo.getRuleId());
          AlertInfo info =
              buildAlertInfo(metaInfo, title + "[SMS]", content, ruleType, AlertInfo.EMAIL_TYPE);

          info.setMails(emailAddress);
          m_alertManager.addAlarmInfo(info);

          List<String> phoneAddress = m_ruleManager.queryUserPhonesByRuleId(metaInfo.getRuleId());
          AlertInfo smsInfo =
              buildAlertInfo(metaInfo, title, content, ruleType, AlertInfo.SMS_TYPE);

          smsInfo.setPhones(phoneAddress);
          m_alertManager.addAlarmInfo(smsInfo);
        }
      }
    }
  }