Example #1
0
        static void RThread()
        {
            do
            {
                try
                {
                    using (SQLLib sql = SQLTest.ConnectSQL("Fox SDC Server for Reporting", 0))
                    {
                        if (sql == null)
                        {
                            Pause();
                            if (StopThread == true)
                            {
                                break;
                            }
                        }
                        string ErrorMessage;
                        if (MailSender.CheckConfig() == false)
                        {
                            Pause();
                            if (StopThread == true)
                            {
                                break;
                            }
                            continue;
                        }

                        if (string.IsNullOrWhiteSpace(SettingsManager.Settings.EMailAdminTo) == true)
                        {
                            Pause();
                            if (StopThread == true)
                            {
                                break;
                            }
                            continue;
                        }

                        SqlDataReader dr;
                        List <string> ConcernedMachineIDs;

                        #region Normal Admin Report

                        if (SettingsManager.Settings.LastScheduleRanAdmin == null)
                        {
                            SettingsManager.Settings.LastScheduleRanAdmin = DateTime.UtcNow;
                            SettingsManager.SaveApplySettings2(sql, SettingsManager.Settings);
                        }
                        else
                        {
                            DateTime?Planned = Scheduler.GetNextRunDate(SettingsManager.Settings.LastScheduleRanAdmin.Value, SettingsManager.Settings.EMailAdminScheduling);
                            if (RunAdminNow == true)
                            {
                                Planned     = DateTime.UtcNow.AddMinutes(-1);
                                RunAdminNow = false;
                            }

                            if (Planned != null)
                            {
                                if (Planned.Value < DateTime.UtcNow) //is in the past - run now (may also be a "miss")
                                {
                                    dr = sql.ExecSQLReader("Select distinct machineid from Reporting where (Flags & @f1)!=0 AND (Flags & @f2)=0",
                                                           new SQLParam("@f1", ReportingFlags.ReportToAdmin),
                                                           new SQLParam("@f2", ReportingFlags.AdminReported));

                                    ConcernedMachineIDs = new List <string>();

                                    while (dr.Read())
                                    {
                                        ConcernedMachineIDs.Add(Convert.ToString(dr["MachineID"]));
                                    }
                                    dr.Close();

                                    if (ConcernedMachineIDs.Count > 0)
                                    {
                                        byte[] PDFFile = RenderReport.RenderMachineReport(sql, ConcernedMachineIDs, null, null, ReportingFlagsPaper.ReportAdmin, "PDF");
                                        if (PDFFile == null)
                                        {
                                            FoxEventLog.WriteEventLog("Admin Report has no data.", System.Diagnostics.EventLogEntryType.Error);
                                        }
                                        else
                                        {
                                            string Text    = SettingsManager.Settings.EMailAdminText;
                                            string Subject = SettingsManager.Settings.EMailAdminSubject;
                                            Text = Text.Replace("{URGENT}", "").
                                                   Replace("{NMACHINESS}", ConcernedMachineIDs.Count == 1 ? "" : "s").
                                                   Replace("{NMACHINES}", ConcernedMachineIDs.Count.ToString());
                                            Subject = Subject.Replace("{URGENT}", "").
                                                      Replace("{NMACHINESS}", ConcernedMachineIDs.Count == 1 ? "" : "s").
                                                      Replace("{NMACHINES}", ConcernedMachineIDs.Count.ToString());
                                            if (MailSender.SendEMailAdmin(Subject, Text, new List <System.Net.Mail.Attachment> {
                                                new System.Net.Mail.Attachment(new MemoryStream(PDFFile), ReportAttachementFilename)
                                            }, System.Net.Mail.MailPriority.Normal, out ErrorMessage, SettingsManager.Settings.EMailAdminIsHTML) == false)
                                            {
                                                FoxEventLog.WriteEventLog("Cannot send Admin E-Mail: " + ErrorMessage, System.Diagnostics.EventLogEntryType.Error);
                                            }
                                        }
                                    }

                                    SettingsManager.Settings.LastScheduleRanAdmin = DateTime.UtcNow;
                                    SettingsManager.SaveApplySettings2(sql, SettingsManager.Settings);
                                }
                            }
                            else
                            {
                                //update anyways
                                SettingsManager.Settings.LastScheduleRanAdmin = DateTime.UtcNow;
                                SettingsManager.SaveApplySettings2(sql, SettingsManager.Settings);
                            }
                        }

                        #endregion

                        #region Urgent Admin

                        dr = sql.ExecSQLReader("Select distinct machineid from Reporting where (Flags & @f1)!=0 AND (Flags & @f2)=0",
                                               new SQLParam("@f1", ReportingFlags.UrgentForAdmin),
                                               new SQLParam("@f2", ReportingFlags.UrgentAdminReported));

                        ConcernedMachineIDs = new List <string>();

                        while (dr.Read())
                        {
                            ConcernedMachineIDs.Add(Convert.ToString(dr["MachineID"]));
                        }
                        dr.Close();

                        if (ConcernedMachineIDs.Count > 0)
                        {
                            byte[] PDFFile = RenderReport.RenderMachineReport(sql, ConcernedMachineIDs, null, null, ReportingFlagsPaper.UrgentAdmin, "PDF");
                            if (PDFFile == null)
                            {
                                FoxEventLog.WriteEventLog("Urgent Admin Report has no data.", System.Diagnostics.EventLogEntryType.Error);
                            }
                            else
                            {
                                string Text    = SettingsManager.Settings.EMailAdminText;
                                string Subject = SettingsManager.Settings.EMailAdminSubject;
                                Text = Text.Replace("{URGENT}", "Urgent ").
                                       Replace("{NMACHINESS}", ConcernedMachineIDs.Count == 1 ? "" : "s").
                                       Replace("{NMACHINES}", ConcernedMachineIDs.Count.ToString());
                                Subject = Subject.Replace("{URGENT}", "Urgent ").
                                          Replace("{NMACHINESS}", ConcernedMachineIDs.Count == 1 ? "" : "s").
                                          Replace("{NMACHINES}", ConcernedMachineIDs.Count.ToString());
                                if (MailSender.SendEMailAdmin(Subject, Text, new List <System.Net.Mail.Attachment> {
                                    new System.Net.Mail.Attachment(new MemoryStream(PDFFile), ReportAttachementFilename)
                                }, System.Net.Mail.MailPriority.High, out ErrorMessage, SettingsManager.Settings.EMailAdminIsHTML) == false)
                                {
                                    FoxEventLog.WriteEventLog("Cannot send Urgent Admin E-Mail: " + ErrorMessage, System.Diagnostics.EventLogEntryType.Error);
                                }
                            }
                        }

                        #endregion

                        #region Normal Client Report

                        if (Settings.Default.UseContract == true)
                        {
                            if (SettingsManager.Settings.LastScheduleRanClient == null)
                            {
                                SettingsManager.Settings.LastScheduleRanClient = DateTime.UtcNow;
                                SettingsManager.SaveApplySettings2(sql, SettingsManager.Settings);
                            }
                            else
                            {
                                DateTime?Planned = Scheduler.GetNextRunDate(SettingsManager.Settings.LastScheduleRanClient.Value, SettingsManager.Settings.EMailClientScheduling);
                                if (Planned != null)
                                {
                                    if (Planned.Value < DateTime.UtcNow) //is in the past - run now (may also be a "miss")
                                    {
                                        dr = sql.ExecSQLReader(@"select ComputerAccounts.MachineID,ComputerAccounts.ContractID,EMail from ComputerAccounts
                                        inner join Contracts on Contracts.ContractID = ComputerAccounts.ContractID
                                        where Disabled = 0 and MachineID in (Select distinct machineid from Reporting where (Flags & @f1) != 0 AND(Flags & @f2) = 0) and EMail is not null and EMail !=''",
                                                               new SQLParam("@f1", ReportingFlags.ReportToClient),
                                                               new SQLParam("@f2", ReportingFlags.ClientReported));

                                        List <ConcernedMachineIDsClient> ConcernedMachineIDC = new List <ConcernedMachineIDsClient>();
                                        List <string> ContractIDs = new List <string>();

                                        while (dr.Read())
                                        {
                                            ConcernedMachineIDsClient m = new ConcernedMachineIDsClient();
                                            m.ContractID = Convert.ToString(dr["ContractID"]);
                                            m.MachineID  = Convert.ToString(dr["MachineID"]);
                                            m.EMail      = Convert.ToString(dr["EMail"]);
                                            ConcernedMachineIDC.Add(m);
                                            if (ContractIDs.Contains(m.ContractID) == false)
                                            {
                                                ContractIDs.Add(m.ContractID);
                                            }
                                        }
                                        dr.Close();

                                        foreach (string ContractID in ContractIDs)
                                        {
                                            List <string> ConcernedMachineIDsForClient = new List <string>();
                                            string        EMail = "";
                                            foreach (ConcernedMachineIDsClient m in ConcernedMachineIDC)
                                            {
                                                if (m.ContractID == ContractID)
                                                {
                                                    ConcernedMachineIDsForClient.Add(m.MachineID);
                                                }
                                                if (string.IsNullOrWhiteSpace(EMail) == true)
                                                {
                                                    EMail = m.EMail;
                                                }
                                            }

                                            if (string.IsNullOrWhiteSpace(EMail) == true)
                                            {
                                                continue;
                                            }

                                            if (ConcernedMachineIDsForClient.Count > 0)
                                            {
                                                byte[] PDFFile = RenderReport.RenderMachineReport(sql, ConcernedMachineIDsForClient, null, null, ReportingFlagsPaper.ReportClient, "PDF");
                                                if (PDFFile == null)
                                                {
                                                    FoxEventLog.WriteEventLog("Client Report has no data.", System.Diagnostics.EventLogEntryType.Error);
                                                }
                                                else
                                                {
                                                    string Text    = SettingsManager.Settings.EMailClientText;
                                                    string Subject = SettingsManager.Settings.EMailClientSubject;
                                                    Text = Text.Replace("{URGENT}", "").
                                                           Replace("{NMACHINESS}", ConcernedMachineIDsForClient.Count == 1 ? "" : "s").
                                                           Replace("{NMACHINES}", ConcernedMachineIDsForClient.Count.ToString());
                                                    Subject = Subject.Replace("{URGENT}", "").
                                                              Replace("{NMACHINESS}", ConcernedMachineIDsForClient.Count == 1 ? "" : "s").
                                                              Replace("{NMACHINES}", ConcernedMachineIDsForClient.Count.ToString());
                                                    if (MailSender.SendEMailClient(EMail, Subject, Text, new List <System.Net.Mail.Attachment> {
                                                        new System.Net.Mail.Attachment(new MemoryStream(PDFFile), ReportAttachementFilename)
                                                    }, System.Net.Mail.MailPriority.High, out ErrorMessage, SettingsManager.Settings.EMailClientIsHTML) == false)
                                                    {
                                                        FoxEventLog.WriteEventLog("Cannot send Client E-Mail: " + ErrorMessage, System.Diagnostics.EventLogEntryType.Error);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    //update anyways
                                    SettingsManager.Settings.LastScheduleRanClient = DateTime.UtcNow;
                                    SettingsManager.SaveApplySettings2(sql, SettingsManager.Settings);
                                }
                            }
                        }

                        #endregion

                        #region Urgent Client

                        if (Settings.Default.UseContract == true)
                        {
                            dr = sql.ExecSQLReader(@"select ComputerAccounts.MachineID,ComputerAccounts.ContractID,EMail from ComputerAccounts
                                inner join Contracts on Contracts.ContractID = ComputerAccounts.ContractID
                                where Disabled = 0 and MachineID in (Select distinct machineid from Reporting where (Flags & @f1) != 0 AND(Flags & @f2) = 0) and EMail is not null and EMail !=''",
                                                   new SQLParam("@f1", ReportingFlags.UrgentForClient),
                                                   new SQLParam("@f2", ReportingFlags.UrgentClientReported));

                            List <ConcernedMachineIDsClient> ConcernedMachineIDC = new List <ConcernedMachineIDsClient>();
                            List <string> ContractIDs = new List <string>();

                            while (dr.Read())
                            {
                                ConcernedMachineIDsClient m = new ConcernedMachineIDsClient();
                                m.ContractID = Convert.ToString(dr["ContractID"]);
                                m.MachineID  = Convert.ToString(dr["MachineID"]);
                                m.EMail      = Convert.ToString(dr["EMail"]);
                                ConcernedMachineIDC.Add(m);
                                if (ContractIDs.Contains(m.ContractID) == false)
                                {
                                    ContractIDs.Add(m.ContractID);
                                }
                            }
                            dr.Close();

                            foreach (string ContractID in ContractIDs)
                            {
                                List <string> ConcernedMachineIDsForClient = new List <string>();
                                string        EMail = "";
                                foreach (ConcernedMachineIDsClient m in ConcernedMachineIDC)
                                {
                                    if (m.ContractID == ContractID)
                                    {
                                        ConcernedMachineIDsForClient.Add(m.MachineID);
                                    }
                                    if (string.IsNullOrWhiteSpace(EMail) == true)
                                    {
                                        EMail = m.EMail;
                                    }
                                }

                                if (string.IsNullOrWhiteSpace(EMail) == true)
                                {
                                    continue;
                                }

                                if (ConcernedMachineIDsForClient.Count > 0)
                                {
                                    byte[] PDFFile = RenderReport.RenderMachineReport(sql, ConcernedMachineIDsForClient, null, null, ReportingFlagsPaper.UrgentClient, "PDF");
                                    if (PDFFile == null)
                                    {
                                        FoxEventLog.WriteEventLog("Urgent Client Report has no data.", System.Diagnostics.EventLogEntryType.Error);
                                    }
                                    else
                                    {
                                        string Text    = SettingsManager.Settings.EMailClientText;
                                        string Subject = SettingsManager.Settings.EMailClientSubject;
                                        Text = Text.Replace("{URGENT}", "Urgent ").
                                               Replace("{NMACHINESS}", ConcernedMachineIDsForClient.Count == 1 ? "" : "s").
                                               Replace("{NMACHINES}", ConcernedMachineIDsForClient.Count.ToString());
                                        Subject = Subject.Replace("{URGENT}", "Urgent ").
                                                  Replace("{NMACHINESS}", ConcernedMachineIDsForClient.Count == 1 ? "" : "s").
                                                  Replace("{NMACHINES}", ConcernedMachineIDsForClient.Count.ToString());
                                        if (MailSender.SendEMailClient(EMail, Subject, Text, new List <System.Net.Mail.Attachment> {
                                            new System.Net.Mail.Attachment(new MemoryStream(PDFFile), ReportAttachementFilename)
                                        }, System.Net.Mail.MailPriority.High, out ErrorMessage, SettingsManager.Settings.EMailClientIsHTML) == false)
                                        {
                                            FoxEventLog.WriteEventLog("Cannot send Urgent Client E-Mail: " + ErrorMessage, System.Diagnostics.EventLogEntryType.Error);
                                        }
                                    }
                                }
                            }
                        }

                        #endregion
                    }
                }
                catch (Exception ee)
                {
                    FoxEventLog.WriteEventLog("Cannot process reporting data\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error);
                }

                Pause();
                if (StopThread == true)
                {
                    break;
                }
            } while (StopThread == false);
        }
Example #2
0
        public RESTStatus GetPaperData(SQLLib sql, ReportPaperRequest req, NetworkConnectionInfo ni)
        {
            if (ni.HasAcl(ACLFlags.ChangeServerSettings) == false)
            {
                ni.Error   = "Access denied";
                ni.ErrorID = ErrorFlags.AccessDenied;
                return(RESTStatus.Denied);
            }

            if (req == null)
            {
                ni.Error   = "Invalid data";
                ni.ErrorID = ErrorFlags.InvalidData;
                return(RESTStatus.Fail);
            }

            if (string.IsNullOrWhiteSpace(req.Name) == true)
            {
                ni.Error   = "Invalid data";
                ni.ErrorID = ErrorFlags.InvalidData;
                return(RESTStatus.Fail);
            }

            if (SupportedNames.Contains(req.Name) == false)
            {
                ni.Error   = "Invalid data";
                ni.ErrorID = ErrorFlags.InvalidData;
                return(RESTStatus.Fail);
            }

            RetPaper = new NetByte();

            switch (req.Name)
            {
            case "COMPUTERREPORT":
                if (req.MachineIDs == null)
                {
                    ni.Error   = "Invalid data";
                    ni.ErrorID = ErrorFlags.InvalidData;
                    return(RESTStatus.Fail);
                }
                if (req.MachineIDs.Count == 0)
                {
                    ni.Error   = "Invalid data";
                    ni.ErrorID = ErrorFlags.InvalidData;
                    return(RESTStatus.Fail);
                }

                List <string> MachinesOK = new List <string>();

                foreach (string m in req.MachineIDs)
                {
                    string          Query     = "";
                    List <SQLParam> SQLParams = new List <SQLParam>();

                    lock (ni.sqllock)
                    {
                        if (Computers.MachineExists(sql, m) == false)
                        {
                            continue;
                        }
                    }

                    if (req.From == null && req.To == null)
                    {
                        Query = "Select count(*) from Reporting where machineid=@mid";
                        SQLParams.Add(new SQLParam("@mid", m));
                    }
                    if (req.From != null && req.To == null)
                    {
                        Query = "Select count(*) from Reporting where machineid=@mid AND Reported>=@d1";
                        SQLParams.Add(new SQLParam("@mid", m));
                        SQLParams.Add(new SQLParam("@d1", req.From.Value));
                    }
                    if (req.From == null && req.To != null)
                    {
                        Query = "Select count(*) from Reporting where machineid=@mid AND Reported<=@d1";
                        SQLParams.Add(new SQLParam("@mid", m));
                        SQLParams.Add(new SQLParam("@d1", req.To.Value));
                    }
                    if (req.From != null && req.To != null)
                    {
                        Query = "Select count(*) from Reporting where machineid=@mid and Reported between @d1 and @d2";
                        SQLParams.Add(new SQLParam("@mid", m));
                        SQLParams.Add(new SQLParam("@d1", req.From.Value));
                        SQLParams.Add(new SQLParam("@d2", req.To.Value));
                    }

                    lock (ni.sqllock)
                    {
                        if (Convert.ToInt32(sql.ExecSQLScalar(Query, SQLParams.ToArray())) == 0)
                        {
                            continue;
                        }
                    }
                    MachinesOK.Add(m);
                }

                if (MachinesOK.Count == 0)
                {
                    ni.Error   = "Machines has no report";
                    ni.ErrorID = ErrorFlags.NoData;
                    return(RESTStatus.Fail);
                }

                lock (ni.sqllock)
                {
                    RetPaper.Data = RenderReport.RenderMachineReport(sql, MachinesOK, req.From, req.To, ReportingFlagsPaper.ReReport, "PDF");
                }
                break;
            }
            return(RESTStatus.Success);
        }