Пример #1
0
 /**
  * Get Excel Report
  *
  * @param rule
  * @param sql
  * @param trxName
  * @param attachments
  * @return summary message to be added into mail content
  * @throws Exception
  */
 private String getExcelReport(
     MAlertRule rule, String sql, String trxName, Collection<File> attachments) throws Exception {
   ArrayList<ArrayList<Object>> data = getData(sql, trxName);
   if (data.size() <= 1) return null;
   // File
   File file = rule.createReportFile("xls");
   //
   ArrayExcelExporter exporter = new ArrayExcelExporter(getCtx(), data);
   exporter.export(file, null, false);
   attachments.add(file);
   String msg = rule.getName() + " (@SeeAttachment@ " + file.getName() + ")" + Env.NL;
   return Msg.parseTranslation(Env.getCtx(), msg);
 }
Пример #2
0
  /**
   * Process Alert
   *
   * @param alert alert
   * @return true if processed
   */
  private boolean processAlert(MAlert alert) {
    if (!alert.isValid()) return false;
    log.info("" + alert);

    StringBuffer message = new StringBuffer(alert.getAlertMessage()).append(Env.NL);
    //
    boolean valid = true;
    boolean processed = false;
    ArrayList<File> attachments = new ArrayList<File>();
    MAlertRule[] rules = alert.getRules(false);
    for (int i = 0; i < rules.length; i++) {
      if (i > 0) message.append(Env.NL);
      String trxName = null; // 	assume r/o

      MAlertRule rule = rules[i];
      if (!rule.isValid()) continue;
      log.fine("" + rule);

      //	Pre
      String sql = rule.getPreProcessing();
      if (sql != null && sql.length() > 0) {
        int no = DB.executeUpdate(sql, false, trxName);
        if (no == -1) {
          ValueNamePair error = CLogger.retrieveError();
          rule.setErrorMsg("Pre=" + error.getName());
          m_errors.append("Pre=" + error.getName());
          rule.setIsValid(false);
          rule.save();
          valid = false;
          break;
        }
      } //	Pre

      //	The processing
      sql = rule.getSql(true);
      try {
        String text = null;
        if (MSysConfig.getBooleanValue(
            "ALERT_SEND_ATTACHMENT_AS_XLS", true, Env.getAD_Client_ID(getCtx())))
          text = getExcelReport(rule, sql, trxName, attachments);
        else text = getPlainTextReport(rule, sql, trxName, attachments);
        if (text != null && text.length() > 0) {
          message.append(text);
          processed = true;
        }
      } catch (Exception e) {
        rule.setErrorMsg("Select=" + e.getLocalizedMessage());
        m_errors.append("Select=" + e.getLocalizedMessage());
        rule.setIsValid(false);
        rule.save();
        valid = false;
        break;
      }

      //	Post
      sql = rule.getPostProcessing();
      if (sql != null && sql.length() > 0) {
        int no = DB.executeUpdate(sql, false, trxName);
        if (no == -1) {
          ValueNamePair error = CLogger.retrieveError();
          rule.setErrorMsg("Post=" + error.getName());
          m_errors.append("Post=" + error.getName());
          rule.setIsValid(false);
          rule.save();
          valid = false;
          break;
        }
      } //	Post

      /** Trx */
      if (trxName != null) {
        Trx trx = Trx.get(trxName, false);
        if (trx != null) {
          trx.commit();
          trx.close();
        }
      }
    } //	 for all rules

    //	Update header if error
    if (!valid) {
      alert.setIsValid(false);
      alert.save();
      return false;
    }

    //	Nothing to report
    if (!processed) {
      m_summary.append(alert.getName()).append("=No Result - ");
      return true;
    }

    //
    // Report footer - Date Generated
    DateFormat df = DisplayType.getDateFormat(DisplayType.DateTime);
    message.append("\n\n");
    message
        .append(Msg.translate(getCtx(), "Date"))
        .append(" : ")
        .append(df.format(new Timestamp(System.currentTimeMillis())));

    Collection<Integer> users = alert.getRecipientUsers();
    int countMail = notifyUsers(users, alert.getAlertSubject(), message.toString(), attachments);

    m_summary.append(alert.getName()).append(" (EMails+Notes=").append(countMail).append(") - ");
    return valid;
  } //	processAlert