Beispiel #1
0
        }       //	processAlert

        /**
         *  Send Email / Notice
         *  @param AD_User_ID user
         *	@param alert alert
         *	@param message message text
         *	@return true if sent (or previously sent)
         */
        private bool SendInfo(int AD_User_ID, MAlert alert, String message)
        {
            if (m_recipients.Contains(AD_User_ID))
            {
                return(false);
            }
            m_recipients.Add(AD_User_ID);
            //
            bool   success          = false;
            MUser  to               = MUser.Get(alert.GetCtx(), AD_User_ID);
            String NotificationType = to.GetNotificationType();

            if (Util.IsEmpty(NotificationType))
            {
                NotificationType = X_AD_User.NOTIFICATIONTYPE_EMail;
            }
            //	Send Mail
            if (X_AD_User.NOTIFICATIONTYPE_EMail.Equals(NotificationType) ||
                X_AD_User.NOTIFICATIONTYPE_EMailPlusNotice.Equals(NotificationType))
            {
                success = m_client.SendEMail(AD_User_ID, alert.GetAlertSubject(), message, null);
                if (!success)
                {
                    log.Warning("EMail failed: " + to);
                    NotificationType = X_AD_User.NOTIFICATIONTYPE_Notice;
                }
            }
            //	Send Note
            if (X_AD_User.NOTIFICATIONTYPE_Notice.Equals(NotificationType) ||
                X_AD_User.NOTIFICATIONTYPE_EMailPlusNotice.Equals(NotificationType))
            {
                int   AD_Message_ID = 1040;     //	AlertNotice
                MNote note          = new MNote(alert.GetCtx(), AD_Message_ID, AD_User_ID,
                                                X_AD_Alert.Table_ID, alert.GetAD_Alert_ID(),
                                                alert.GetAlertSubject(), message, null);
                success = note.Save();
            }
            return(success);
        }       //	sendInfo
Beispiel #2
0
        }       //	doWork

        /**
         *  Process Alert
         *	@param alert alert
         *	@return true if processed
         */
        private bool ProcessAlert(MAlert alert)
        {
            if (!alert.IsValid())
            {
                log.Info("Invalid: " + alert);
                return(false);
            }
            log.Info("" + alert);
            m_recipients.Clear();

            StringBuilder message = new StringBuilder(alert.GetAlertMessage())
                                    .Append(Env.NL);
            //	Context
            Ctx ctx = alert.GetCtx();

            ctx.SetAD_Client_ID(alert.GetAD_Client_ID());
            ctx.SetAD_Org_ID(alert.GetAD_Org_ID());
            //
            bool valid     = true;
            bool processed = false;

            MAlertRule[] rules = alert.GetRules(false);
            for (int i = 0; i < rules.Length; i++)
            {
                if (i > 0)
                {
                    message.Append(Env.NL).Append("================================").Append(Env.NL);
                }
                //Trx trx = null;		//	assume r/o

                MAlertRule rule = rules[i];
                if (!rule.IsValid())
                {
                    log.Config("Invalid: " + rule);
                    continue;
                }
                log.Fine("" + rule);

                //	Pre
                String sql = rule.GetPreProcessing();
                if (sql != null && sql.Length > 0)
                {
                    int no = DB.ExecuteQuery(sql);
                    if (no == -1)
                    {
                        ValueNamePair error = VLogger.RetrieveError();
                        rule.SetErrorMsg("Pre=" + error.GetName());
                        m_errors.Append("Pre=" + error.GetName());
                        rule.SetIsValid(false);
                        rule.Save();
                        valid = false;
                        break;
                    }
                }       //	Pre

                //	The processing
                ctx.SetAD_Role_ID(0);
                ctx.SetAD_User_ID(0);
                sql = rule.GetSql();
                if (alert.IsEnforceRoleSecurity() ||
                    alert.IsEnforceClientSecurity())
                {
                    int AD_Role_ID = alert.GetFirstAD_Role_ID();
                    if (AD_Role_ID == -1)
                    {
                        AD_Role_ID = alert.GetFirstUserAD_Role_ID();
                    }
                    if (AD_Role_ID != -1)
                    {
                        String tableName      = rule.GetTableName();
                        bool   fullyQualified = MRole.SQL_FULLYQUALIFIED;
                        if (Util.IsEmpty(tableName))
                        {
                            fullyQualified = MRole.SQL_NOTQUALIFIED;
                        }
                        MRole role = MRole.Get(ctx, AD_Role_ID, 0, false);
                        sql = role.AddAccessSQL(sql, tableName,
                                                fullyQualified, MRole.SQL_RO);
                        ctx.SetAD_Role_ID(AD_Role_ID);
                    }
                    if (alert.GetFirstAD_User_ID() != -1)
                    {
                        ctx.SetAD_User_ID(alert.GetFirstAD_User_ID());
                    }
                }

                try
                {
                    String text = ListSqlSelect(sql);
                    if (text != null && text.Length > 0)
                    {
                        message.Append(text);
                        processed = true;
                        int index = text.IndexOf(":");
                        if (index > 0 && index < 5)
                        {
                            m_summary.Append(text.Substring(0, index));
                        }
                    }
                }
                catch (Exception e)
                {
                    rule.SetErrorMsg("Select=" + e.Message);
                    m_errors.Append("Select=" + e.Message);
                    rule.SetIsValid(false);
                    rule.Save();
                    valid = false;
                    break;
                }

                //	Post
                sql = rule.GetPostProcessing();
                if (sql != null && sql.Length > 0)
                {
                    int no = DB.ExecuteQuery(sql);
                    if (no == -1)
                    {
                        ValueNamePair error = VLogger.RetrieveError();
                        rule.SetErrorMsg("Post=" + error.GetName());
                        m_errors.Append("Post=" + error.GetName());
                        rule.SetIsValid(false);
                        rule.Save();
                        valid = false;
                        break;
                    }
                } //	Post
            }     //	 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);
            }

            //	Send Message
            int countRecipient = 0;

            MAlertRecipient[] recipients = alert.GetRecipients(false);
            for (int i = 0; i < recipients.Length; i++)
            {
                MAlertRecipient recipient = recipients[i];
                if (recipient.GetAD_User_ID() >= 0)             //	System == 0
                {
                    if (SendInfo(recipient.GetAD_User_ID(), alert, message.ToString()))
                    {
                        countRecipient++;
                    }
                }
                if (recipient.GetAD_Role_ID() >= 0)             //	SystemAdministrator == 0
                {
                    MUserRoles[] urs = MUserRoles.GetOfRole(GetCtx(), recipient.GetAD_Role_ID());
                    for (int j = 0; j < urs.Length; j++)
                    {
                        MUserRoles ur = urs[j];
                        if (!ur.IsActive())
                        {
                            continue;
                        }
                        if (SendInfo(ur.GetAD_User_ID(), alert, message.ToString()))
                        {
                            countRecipient++;
                        }
                    }
                }
            }

            m_summary.Append(alert.GetName()).Append(" (Recipients=").Append(countRecipient).Append(") - ");
            return(valid);
        }       //	processAlert