Example #1
0
        /////////////////////////////////////////////////////////////
        /// <summary>
        /// Get the project's sites
        /// </summary>
        public void SyncProjectSites(int project_id)
        {
            using (var ctx = new Entities())
            {
                Audit.Log(ctx, AuditType.TaskManager, string.Format("Getting sites for project: {0}", project_id), 0);

                var project = ctx.PROJECTs.FirstOrDefault(p => p.ID == project_id);

                var project_sites = ctx.PROJECTSITEs.Where(s => s.PROJECTID == project_id).ToList();

                // call API and get summary
                var client = new encoreSoapClient("encoreSoap", project.APIURL);

                client.BeginGetSites(
                    result =>
                {
                    var response = client.EndGetSites(result);
                    using (var entities = new Entities())
                    {
                        // sync project sites
                        foreach (var site in response)
                        {
                            var project_site =
                                project_sites.FirstOrDefault(s => s.SOURCEID == site.ID.ToString());
                            if (project_site == null)
                            {
                                entities.AddToPROJECTSITEs(new PROJECTSITE
                                {
                                    SOURCEID  = site.ID.ToString(),
                                    NAME      = site.Name,
                                    CREATEDON = DateTime.Now,
                                    PROJECTID = project.ID,
                                    DELETED   = 0
                                });
                            }
                        }

                        entities.SaveChanges();
                    }
                },
                    null
                    );

                ctx.SaveChanges();
            }
        }
Example #2
0
        /////////////////////////////////////////////////////////////////
        /// <summary>
        /// Get Template
        /// </summary>
        /////////////////////////////////////////////////////////////////
        private static string GetTemplate(string filename)
        {
            string template = string.Empty;

            var fullPathToFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filename);

            if (File.Exists(fullPathToFile))
            {
                using (var sr = new StreamReader(fullPathToFile))
                {
                    template = sr.ReadToEnd();
                }
            }
            else
            {
                Audit.Log(AuditType.TaskManager, "EmailCommands", string.Format("Could not load template {0}", fullPathToFile), 0);
            }

            return(template);
        }
Example #3
0
        /////////////////////////////////////////////////////////////////
        /// <summary>
        /// Send an email
        /// </summary>
        /// <param name="from">from email address</param>
        /// <param name="to">to email address</param>
        /// <param name="subject">subject of the email</param>
        /// <param name="message">Body of email</param>
        /// <returns>Send successfully</returns>
        /////////////////////////////////////////////////////////////////
        public static bool SendEmail(string from, string to, string subject, string message)
        {
            try
            {
                if (!string.IsNullOrEmpty(ConfigurationSettings.AppSettings["SMTPServer"]))
                {
                    var Mail = new MailMessage(from, to, subject, message)
                    {
                        IsBodyHtml = false
                    };

                    var smtp = new SmtpClient(ConfigurationSettings.AppSettings["SMTPServer"]);
                    smtp.Send(Mail);
                }

                return(true);
            }
            catch (Exception ex)
            {
                Audit.Log(AuditType.TaskManager, "SendMail", ex.Message, 0);
                return(false);
            }
        }
Example #4
0
        /////////////////////////////////////////////////////////////
        /// <summary>
        /// Check current report queue and request and report data
        /// </summary>
        public void ProcessReportQueue()
        {
            using (var ctx = new Entities())
            {
                var process_status = RequestStatus.Pending.ToString();

                var tasks = ctx.REPORTPROJECTREQUESTs.Where(r => r.STATUS == process_status).ToList();

                foreach (var task in tasks)
                {
                    Audit.Log(ctx, AuditType.TaskManager, string.Format("Getting data for task {0}", task.ID), 0);

                    var task_id = task.ID;
                    task.STATUS = RequestStatus.InProgress.ToString();

                    // get report
                    var report = ctx.REPORTs.FirstOrDefault(r => r.ID == task.REPORTID);
                    report.REQUESTSTATUS = RequestStatus.InProgress.ToString();
                    ctx.SaveChanges();

                    var report_id = report.ID;

                    // get project
                    var project = ctx.PROJECTs.FirstOrDefault(p => p.ID == task.PROJECTID);

                    // get fields
                    var report_field_ids = ctx.VWREPORTFIELDs.Where(u => u.REPORTID == task.REPORTID).Select(u => u.ID).ToList();

                    var project_fields = ctx.PROJECTFIELDs.Where(f => f.PROJECTID == project.ID && report_field_ids.Contains(f.FIELDID.Value));

                    // get sites
                    var report_site_ids = ctx.VWREPORTSITEs.Where(u => u.REPORTID == task.REPORTID).Select(u => u.ID).ToList();

                    var project_sites = ctx.PROJECTSITEs.Where(s => s.PROJECTID == project.ID && report_site_ids.Contains(s.SITEID.Value));

                    // call API and get data
                    var client = new encoreSoapClient("encoreSoap", project.APIURL);

                    var data_store = new DataStore.DataStore(ConfigurationSettings.AppSettings["datastore_path"]);

                    try
                    {
                        var project_field_source_ids = project_fields.Select(f => f.SOURCEID).ToList();
                        var project_sites_source_ids = project_sites.Select(s => s.SOURCEID).ToList();

                        client.BeginGetData(
                            project_field_source_ids.Select(int.Parse).ToArray(),
                            project_sites_source_ids.Select(int.Parse).ToArray(),
                            report.REPORTDATEFROM,
                            report.REPORTDATETO,
                            result =>
                        {
                            var response = client.EndGetData(result);

                            //  populate data store
                            var data_rows = response.Rows.Select(row => new DataRow
                            {
                                ReportID    = report_id,
                                RowDateTime = row.RowDate,
                                SiteID      = row.SiteID,
                                Fields      = row.Values.Select(r => r.FieldID).ToList(),
                                Values      = row.Values.Select(r => r.Value).ToList()
                            }).ToList();

                            data_store.Store(data_rows);

                            using (var entities = new Entities())
                            {
                                var this_task    = entities.REPORTPROJECTREQUESTs.FirstOrDefault(r => r.ID == task_id);
                                this_task.STATUS = RequestStatus.Complete.ToString();

                                // send email
                                var rpt           = entities.REPORTs.FirstOrDefault(r => r.ID == task_id);
                                rpt.REQUESTSTATUS = RequestStatus.Complete.ToString();

                                var owner = entities.SYSTEMUSERs.FirstOrDefault(s => s.ID == rpt.SYSTEMUSERID);
                                if (owner != null && !string.IsNullOrEmpty(owner.EMAIL))
                                {
                                    // send email
                                    SendReportCompelted(owner, report_id);
                                }

                                // check tasks for report
                                var report_tasks =
                                    entities.REPORTPROJECTREQUESTs.Where(r => r.REPORTID == task.REPORTID).ToList();

                                if (report_tasks.All(r => r.STATUS == RequestStatus.Complete.ToString()))
                                {
                                    var this_report =
                                        entities.REPORTs.FirstOrDefault(r => r.ID == this_task.REPORTID);
                                    this_report.REQUESTSTATUS = RequestStatus.Complete.ToString();
                                }
                                entities.SaveChanges();
                            }
                        },
                            null
                            );
                    }
                    catch (Exception ex)
                    {
                        task.STATUS = RequestStatus.Failed.ToString();
                        Audit.Log(ctx, AuditType.TaskManager, string.Format("Get Data for task {0} failed with exception: {1}", task.ID, ex.Message), 0);
                        ctx.SaveChanges();
                    }
                }
            }
        }
Example #5
0
        /////////////////////////////////////////////////////////////
        /// <summary>
        /// Check all projects and get latest summary data
        /// </summary>
        public void SyncProjectSummaries()
        {
            using (var ctx = new Entities())
            {
                Audit.Log(ctx, AuditType.TaskManager, string.Format("Getting project summaries"), 0);

                var projects = ctx.PROJECTs.Where(p => p.DELETED == 0 && !string.IsNullOrEmpty(p.APIURL));

                foreach (var project in projects)
                {
                    // call API and get summary
                    var client = new encoreSoapClient("encoreSoap", project.APIURL);

                    client.BeginGetSummary(
                        result =>
                    {
                        var response = client.EndGetSummary(result);

                        using (var entities = new Entities())
                        {
                            foreach (var item in response)
                            {
                                foreach (var field in item.NumberOfRows)
                                {
                                    var currentSummary = entities.PROJECTSITESUMMARies.FirstOrDefault(
                                        s => s.PROJECTID == project.ID && s.FIELDID == field.FieldID &&
                                        s.SITEID == item.SiteID);

                                    if (currentSummary != null)
                                    {
                                        currentSummary.VALUEMAXDATE = item.MaxDate;
                                        currentSummary.VALUEMINDATE = item.MinDate;
                                        currentSummary.ROWCOUNT     = string.IsNullOrEmpty(field.Value)
                                                                              ? (short)0
                                                                              : (short)
                                                                      (int.Parse(field.Value));
                                        currentSummary.MODIFIEDON = DateTime.Now;
                                    }
                                    else
                                    {
                                        entities.AddToPROJECTSITESUMMARies(
                                            new PROJECTSITESUMMARY
                                        {
                                            CREATEDON    = DateTime.Now,
                                            SITEID       = (short)item.SiteID,
                                            PROJECTID    = project.ID,
                                            VALUEMAXDATE = item.MaxDate,
                                            VALUEMINDATE = item.MinDate,
                                            FIELDID      = (short)field.FieldID,
                                            ROWCOUNT     =
                                                string.IsNullOrEmpty(field.Value)
                                                                    ? (short)0
                                                                    : (short)
                                                (int.Parse(field.Value)),
                                            MODIFIEDON = DateTime.Now
                                        });
                                    }
                                }
                            }

                            entities.SaveChanges();
                        }
                    },
                        null
                        );
                }
            }
        }