Exemplo n.º 1
0
 public void buttonItemSearchRun_Click(object sender, EventArgs e)
 {
     if (Controllers.StationManager.Instance.SelectedStation != null)
     {
         using (ToolForms.FormProgress form = new ToolForms.FormProgress())
         {
             form.laProgress.Text = "Searching Programs...";
             form.TopMost = true;
             System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate()
             {
                 _searchResult = Controllers.StationManager.Instance.SelectedStation.Search(FormMain.Instance.dateEditSearchDateStart.DateTime, FormMain.Instance.dateEditSearchDateEnd.DateTime, FormMain.Instance.comboBoxEditSearchPrograms.EditValue != null ? FormMain.Instance.comboBoxEditSearchPrograms.EditValue.ToString() : null);
                 this.Invoke((MethodInvoker)delegate()
                 {
                     gridControlPrograms.DataSource = new BindingList<CoreObjects.ProgramActivity>(_searchResult);
                     gridControlPrograms.Focus();
                     FormMain.Instance.ribbonBarSearchOutput.Enabled = _searchResult.Length > 0;
                 });
             }));
             form.Show();
             Application.DoEvents();
             thread.Start();
             while (thread.IsAlive)
                 Application.DoEvents();
             form.Close();
         }
     }
 }
        public void LoadData(bool forceRemote = false)
        {
            using (ToolForms.FormProgress form = new ToolForms.FormProgress())
            {
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate()
                {
                    FormMain.Instance.Invoke((MethodInvoker)delegate()
                    {
                        form.laProgress.Text = "Loading Programs...";
                        form.TopMost = true;
                        form.Show();
                        Application.DoEvents();
                    });

                    foreach (CoreObjects.Station station in _stations)
                        station.ReleaseResources();
                    _stations.Clear();
                    ListManager.Instance.FCC.Clear();
                    ListManager.Instance.Type.Clear();

                    if (forceRemote)
                    {
                        AppManager.Instance.DeleteFolder(new DirectoryInfo(ConfigurationClasses.SettingsManager.Instance.StationsRootPath));
                        if (!Directory.Exists(ConfigurationClasses.SettingsManager.Instance.StationsRootPath))
                            Directory.CreateDirectory(ConfigurationClasses.SettingsManager.Instance.StationsRootPath);
                    }
                    else
                    {
                        LoadStations();
                        ListManager.Instance.LoadLists();
                    }

                    if (ListManager.Instance.FCC.Count == 0 || ListManager.Instance.Type.Count == 0)
                        ListManager.Instance.LoadRemoteLists();

                    FormMain.Instance.Invoke((MethodInvoker)delegate()
                    {
                        form.Hide();
                        Application.DoEvents();
                    });
                    if (!this.StationsLoaded && !ConfigurationClasses.SettingsManager.Instance.OfflineMode)
                    {
                        FormMain.Instance.Invoke((MethodInvoker)delegate()
                        {
                            form.laProgress.Text = "Connecting to the server...";
                            form.TopMost = true;
                            form.Show();
                            Application.DoEvents();
                        });

                        string[] availableStations = ServiceManager.GetStationsList();
                        string[] selectedStations = new string[] { };

                        FormMain.Instance.Invoke((MethodInvoker)delegate()
                        {
                            form.Hide();
                            Application.DoEvents();
                        });

                        if (availableStations.Length > 0)
                        {
                            FormMain.Instance.Invoke((MethodInvoker)delegate()
                            {
                                using (ToolForms.FormSelectStations selectForm = new ToolForms.FormSelectStations())
                                {
                                    selectForm.AvailableStations.AddRange(availableStations);
                                    selectForm.ActionName = "Download";
                                    if (selectForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                                    {
                                        selectedStations = selectForm.SelectedStations;
                                    }
                                }

                                form.laProgress.Text = "Downloading Selected Stations...";
                                form.TopMost = true;
                                form.Show();
                                Application.DoEvents();
                            });

                            DownloadStations(selectedStations);

                            FormMain.Instance.Invoke((MethodInvoker)delegate()
                            {
                                form.Hide();
                                Application.DoEvents();
                            });
                        }
                        else
                        {
                            FormMain.Instance.Invoke((MethodInvoker)delegate()
                            {
                                AppManager.Instance.ShowWarning("Server is unavailable");
                                Application.DoEvents();
                            });
                        }
                    }
                    else if (!forceRemote)
                    {
                        List<string> stationsForUpdate = new List<string>();
                        stationsForUpdate.AddRange(_stations.Where(x => x.NeedToUpdate).Select(x => x.Name));
                        if (stationsForUpdate.Count > 0)
                        {
                            if (ConfigurationClasses.SettingsManager.Instance.AlwaysDownload)
                            {
                                FormMain.Instance.Invoke((MethodInvoker)delegate()
                                {
                                    form.laProgress.Text = "Downloading Stations...";
                                    form.TopMost = true;
                                    form.Show();
                                    Application.DoEvents();
                                });

                                DownloadStations(stationsForUpdate.ToArray());

                                FormMain.Instance.Invoke((MethodInvoker)delegate()
                                {
                                    form.Hide();
                                    Application.DoEvents();
                                });
                            }
                            else if (!ConfigurationClasses.SettingsManager.Instance.AlwaysCancelDownload)
                            {
                                DialogResult questionResult = DialogResult.Cancel;
                                FormMain.Instance.Invoke((MethodInvoker)delegate()
                                {
                                    using (ToolForms.FormDownloadWarning downloadwarning = new ToolForms.FormDownloadWarning())
                                    {
                                        questionResult = downloadwarning.ShowDialog();
                                    }
                                });

                                if (questionResult == DialogResult.OK)
                                {
                                    FormMain.Instance.Invoke((MethodInvoker)delegate()
                                    {
                                        form.laProgress.Text = "Downloading Stations...";
                                        form.TopMost = true;
                                        form.Show();
                                        Application.DoEvents();
                                    });

                                    DownloadStations(stationsForUpdate.ToArray());

                                    FormMain.Instance.Invoke((MethodInvoker)delegate()
                                    {
                                        form.Hide();
                                        Application.DoEvents();
                                    });
                                }
                            }
                        }
                    }
                    FormMain.Instance.Invoke((MethodInvoker)delegate()
                    {
                        FormMain.Instance.TabSchedule.LoadPage();
                        FormMain.Instance.TabSearch.LoadPage();
                    });
                }));
                thread.Start();

                while (thread.IsAlive)
                    Application.DoEvents();

                form.Close();
            }
        }
        public void ReportWeekSchedule(string stationName, CoreObjects.Week[] weeks, bool convertToPDF, bool landscape)
        {
            using (ToolForms.FormProgress form = new ToolForms.FormProgress())
            {
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate()
                {
                    FormMain.Instance.Invoke((MethodInvoker)delegate()
                    {
                        form.laProgress.Text = "Generating Program Schedule...";
                        form.TopMost = true;
                        form.Show();
                        Application.DoEvents();
                    });

                    CoreObjects.Station station = _stations.Where(x => x.Name.Equals(stationName)).FirstOrDefault();
                    if (station != null)
                    {
                        InteropClasses.ExcelHelper excelHelper = new InteropClasses.ExcelHelper();

                        if (!Directory.Exists(ConfigurationClasses.SettingsManager.Instance.OutputCache))
                            Directory.CreateDirectory(ConfigurationClasses.SettingsManager.Instance.OutputCache);

                        string destinationPath = Path.Combine(ConfigurationClasses.SettingsManager.Instance.OutputCache, string.Format("{0}.{1}", new string[] { DateTime.Now.ToString("MMddyy-hhmmtt"), convertToPDF ? "pdf" : "xls" }));

                        List<CoreObjects.Day[]> daysByWeeks = new List<CoreObjects.Day[]>();
                        foreach (CoreObjects.Week week in weeks)
                            daysByWeeks.Add(station.GetDays(week.DateStart, week.DateEnd));

                        excelHelper.ReportWeekSchedule(daysByWeeks.ToArray(), destinationPath, convertToPDF, landscape);

                        if (File.Exists(destinationPath))
                            Process.Start(destinationPath);
                    }

                    FormMain.Instance.Invoke((MethodInvoker)delegate()
                    {
                        form.Hide();
                        Application.DoEvents();
                    });

                }));
                thread.Start();

                while (thread.IsAlive)
                    Application.DoEvents();

                form.Close();
            }
        }
        public void ReportActivityList(CoreObjects.ProgramActivity[] activities, bool convertToPDF)
        {
            using (ToolForms.FormProgress form = new ToolForms.FormProgress())
            {
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate()
                {
                    FormMain.Instance.Invoke((MethodInvoker)delegate()
                    {
                        form.laProgress.Text = "Generating Program List...";
                        form.TopMost = true;
                        form.Show();
                        Application.DoEvents();
                    });

                    InteropClasses.ExcelHelper excelHelper = new InteropClasses.ExcelHelper();

                    if (!Directory.Exists(ConfigurationClasses.SettingsManager.Instance.OutputCache))
                        Directory.CreateDirectory(ConfigurationClasses.SettingsManager.Instance.OutputCache);

                    string destinationPath = Path.Combine(ConfigurationClasses.SettingsManager.Instance.OutputCache, string.Format("{0}.{1}", new string[] { DateTime.Now.ToString("MMddyy-hhmmtt"), convertToPDF ? "pdf" : "xls" }));

                    excelHelper.ReportActivityList(activities, destinationPath, convertToPDF);

                    if (File.Exists(destinationPath))
                        Process.Start(destinationPath);

                    FormMain.Instance.Invoke((MethodInvoker)delegate()
                    {
                        form.Hide();
                        Application.DoEvents();
                    });

                }));
                thread.Start();

                while (thread.IsAlive)
                    Application.DoEvents();

                form.Close();
            }
        }
        public void UploadData()
        {
            using (ToolForms.FormProgress form = new ToolForms.FormProgress())
            {
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate()
                {
                    List<string> selectedStations = new List<string>();

                    FormMain.Instance.Invoke((MethodInvoker)delegate()
                    {
                        using (ToolForms.FormSelectStations selectForm = new ToolForms.FormSelectStations())
                        {
                            selectForm.AvailableStations.AddRange(GetStationList());
                            selectForm.ActionName = "Upload";
                            selectForm.DefaultSelectedStation = this.SelectedStation.Name;
                            if (selectForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                                selectedStations.AddRange(selectForm.SelectedStations);
                        }
                        Application.DoEvents();
                    });

                    bool uploadSuccessfully = selectedStations.Count > 0;
                    bool somethingDownloaded = false;
                    foreach (string stationName in selectedStations)
                    {
                        CoreObjects.Station station = _stations.Where(x => x.Name.Equals(stationName)).FirstOrDefault();
                        if (station != null)
                        {
                            FormMain.Instance.Invoke((MethodInvoker)delegate()
                            {
                                form.laProgress.Text = "Getting station information...";
                                form.TopMost = true;
                                form.Show();
                                Application.DoEvents();
                            });


                            CoreObjects.StationInformation stationInformation = ServiceManager.GetStationInfo(station.Name);

                            FormMain.Instance.Invoke((MethodInvoker)delegate()
                            {
                                form.Hide();
                                Application.DoEvents();
                            });

                            DialogResult result = DialogResult.OK;
                            if (stationInformation != null)
                            {
                                if (stationInformation.LastModified > station.LastLoaded)
                                {
                                    FormMain.Instance.Invoke((MethodInvoker)delegate()
                                    {
                                        using (ToolForms.FormUploadWarning warningForm = new ToolForms.FormUploadWarning())
                                        {
                                            result = warningForm.ShowDialog();
                                        }
                                        Application.DoEvents();
                                    });
                                }
                            }
                            if (result == DialogResult.OK)
                            {
                                FormMain.Instance.Invoke((MethodInvoker)delegate()
                                {
                                    form.laProgress.Text = "Uploading station...";
                                    form.TopMost = true;
                                    form.Show();
                                    Application.DoEvents();
                                });

                                uploadSuccessfully &= ServiceManager.UploadStation(station);

                                FormMain.Instance.Invoke((MethodInvoker)delegate()
                                {
                                    form.Hide();
                                    Application.DoEvents();
                                });
                            }
                            else if (result == DialogResult.Cancel)
                            {
                                FormMain.Instance.Invoke((MethodInvoker)delegate()
                                {
                                    form.laProgress.Text = "Downloading station...";
                                    form.TopMost = true;
                                    form.Show();
                                    Application.DoEvents();
                                });

                                DownloadStations(new string[] { stationName });

                                somethingDownloaded = true;

                                FormMain.Instance.Invoke((MethodInvoker)delegate()
                                {
                                    form.Hide();
                                    Application.DoEvents();
                                });
                            }
                        }
                    }
                    FormMain.Instance.Invoke((MethodInvoker)delegate()
                    {
                        if (somethingDownloaded)
                            LoadStation(null, this.SelectedStation.Name);

                        Application.DoEvents();

                        if (uploadSuccessfully)
                        {
                            AppManager.Instance.ShowInformation("Operation completed successfully");
                        }
                        else
                            AppManager.Instance.ShowWarning("Operation was not complited");
                    });
                }));
                thread.Start();

                while (thread.IsAlive)
                    Application.DoEvents();

                form.Close();
            }
        }