示例#1
0
        /// <summary>
        /// Starts a new window awaiter
        /// </summary>
        /// <returns></returns>
        public static Task <KprocessExportWindow> StartNew()
        {
            var continuationTaskSource = new TaskCompletionSource <KprocessExportWindow>();

            _windowInstance          = continuationTaskSource.Task;
            _cancellationTokenSource = new CancellationTokenSource();

            var newWindowThread = new Thread(new ThreadStart(() =>
            {
                var window = new KprocessExportWindow();
                continuationTaskSource.SetResult(window);
                window.Show();
                System.Windows.Threading.Dispatcher.Run();
            }));

            newWindowThread.SetApartmentState(ApartmentState.STA);
            newWindowThread.IsBackground = true;
            newWindowThread.Start();

            return(_windowInstance);
        }
示例#2
0
        private async Task <(string packagePath_Archive, string packagePath_Redistribuable)> DoExport(RestitutionData data, string tempExcelExportPath)
        {
            try
            {
                var dialog1 = DialogFactory.GetDialogView <IMessageDialog>();
                if (dialog1.Show(
                        LocalizationManager.GetString("ExtKp_Dlg_DoYouWantToLaunchExport"), //"Etes-vous sûr de vouloir lancer l'export ?"
                        LocalizationManager.GetString("ExtKp_Dlg_LaunchExport"),            //"Lancement de l'export",
                        MessageDialogButton.YesNo,
                        MessageDialogImage.Question) == MessageDialogResult.Yes)
                {
                    var currentScenario = data.Scenarios.First(scenario => scenario.ScenarioId == ViewModel.CurrentScenario.ScenarioId);
                    var referentials    = ServiceBus.Get <IReferentialsService>().GetApplicationReferentials();

                    KprocessExportWindow.StartNew();

                    var criticalPath = currentScenario.Project.ScenariosCriticalPath.SingleOrDefault(s => s.Id == currentScenario.ScenarioId);

                    var      settingsService = ServiceBus.Get <ISettingsService>();
                    Settings settings        = settingsService.LoadExtensionApplicationSettings <Settings>(KprocessExtension.Id);

                    // On vérifie qu'il y a au moins un type d'export actif
                    if (!(settings?.ArchivageIsEnabled == true || settings?.RedistribuableIsEnabled == true))
                    {
                        throw new OperationCanceledException(_NO_MODE_SELECTED);
                    }

                    // On vérifie qu'il y a au moins un export actif
                    if (!(settings?.ExcelExportIsEnabled == true || settings?.VideoExportIsEnabled == true))
                    {
                        throw new OperationCanceledException(_NO_EXPORT_SELECTED);
                    }

                    #region Repertoire Export Excel

                    string videoPath = currentScenario.Actions
                                       .Where(a => a.Video != null)
                                       .Select(a => a.Video.FilePath)
                                       .FirstOrDefault(File.Exists);

                    string exportDefaultPath;
                    if (settings?.ArchivageIsEnabled == true)
                    {
                        exportDefaultPath = settings?.DefaultExportDirectory_Archivage;
                    }
                    else
                    {
                        exportDefaultPath = settings?.DefaultExportDirectory_Redistribuable;
                    }

                    string ExcelExportDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

                    if (!string.IsNullOrWhiteSpace(exportDefaultPath))
                    {
                        ExcelExportDirectory = exportDefaultPath;
                    }
                    else if (!string.IsNullOrWhiteSpace(videoPath))
                    {
                        ExcelExportDirectory = Path.GetDirectoryName(videoPath);
                    }

                    #endregion

                    #region Repertoire Export video

                    string VideoExportDirectory = settings?.ArchivageIsEnabled == true ?
                                                  settings?.DefaultExportVideoDirectory :
                                                  null;

                    #endregion

                    string modelFilePath            = null;
                    bool   multiThreadVideoEncoding = false;
                    try
                    {
                        modelFilePath = settings?.ExportExcelModelPath;
                    }
                    catch { }

                    try
                    {
                        multiThreadVideoEncoding = (settings?.MultiThreading).GetValueOrDefault();
                    }
                    catch { }

                    if (!string.IsNullOrWhiteSpace(modelFilePath) && File.Exists(modelFilePath))
                    {
                        this.TraceDebug(string.Format("Lancement de l'export avec le modèle '{0}'", modelFilePath));
                        using (var exporter = new Exporter(modelFilePath))
                        {
                            string previousVersionFile = null;
                            this.TraceDebug("exporter.Open()");
                            exporter.Open(currentScenario, referentials, settings.ExcelExportIsEnabled);
                            if (settings?.ExcelExportIsEnabled == true)
                            {
                                CheckCancellation();
                                var dialog = new Microsoft.Win32.OpenFileDialog()
                                {
                                    Title            = LocalizationManager.GetString("ExtKp_Dlg_SelectPreviousVersionFile"),
                                    Filter           = "(*.xlsm)|*.xlsm",
                                    RestoreDirectory = true,
                                    InitialDirectory = exportDefaultPath
                                };

                                if (dialog.ShowDialog().GetValueOrDefault())
                                {
                                    previousVersionFile = dialog.FileNames.FirstOrDefault();
                                }
                            }

                            this.TraceDebug("exporter.Export()");

                            var nomFichierFinal_FileVersion = exporter.Export(tempExcelExportPath, previousVersionFile, ExcelExportDirectory, settingsService, VideoExportDirectory);
                            CheckCancellation();

                            var dossierFinal  = nomFichierFinal_FileVersion.nomFichierFinal.Split('\\')[0];
                            var excelFileName = $"{nomFichierFinal_FileVersion.nomFichierFinal.Split('\\')[1]}";

                            ExcelExportDirectory = Path.Combine(ExcelExportDirectory, dossierFinal);

                            if (settings?.ArchivageIsEnabled == true)
                            {
                                if (string.IsNullOrWhiteSpace(VideoExportDirectory))
                                {
                                    VideoExportDirectory = Path.Combine(
                                        ExcelExportDirectory,
                                        $"{(settings?.ExcelExportIsEnabled == true ? "v" : string.Empty)}{nomFichierFinal_FileVersion.FileVersion}",
                                        Exporter._VIDEOS_DIRECTORY_NAME);
                                }
                                else
                                {
                                    VideoExportDirectory = Path.Combine(
                                        VideoExportDirectory,
                                        dossierFinal,
                                        $"{(settings?.ExcelExportIsEnabled == true ? "v" : string.Empty)}{nomFichierFinal_FileVersion.FileVersion}",
                                        Exporter._VIDEOS_DIRECTORY_NAME);
                                }
                            }
                            else
                            {
                                VideoExportDirectory = Path.Combine(ExcelExportDirectory, Exporter._VIDEOS_DIRECTORY_NAME);
                            }

                            this.TraceDebug("Finalizing excel process...");
                            //exporter.FinalizeExcel();
                            CheckCancellation();

                            this.TraceDebug("exporter.Package()");
                            //zipFilePath = Path.Combine(zipFileDirectory, string.Format("{0}.zip", fileName));
                            exporter.Package(ExcelExportDirectory, excelFileName, VideoExportDirectory, multiThreadVideoEncoding);

                            _errorCount = exporter.ErrorCount;

                            if (settings?.ArchivageIsEnabled == true && settings?.RedistribuableIsEnabled == true)
                            {
                                string archiveFolder = ExcelExportDirectory;
                                if (settings?.ExcelExportIsEnabled == false && !string.IsNullOrEmpty(settings?.DefaultExportVideoDirectory))
                                {
                                    var paths = VideoExportDirectory.Split('\\').ToList();
                                    paths.RemoveAt(paths.Count - 1);
                                    paths.RemoveAt(paths.Count - 1);
                                    archiveFolder = Path.Combine(paths.ToArray());
                                }
                                return(archiveFolder, Path.Combine(settings?.DefaultExportDirectory_Redistribuable, $"{dossierFinal} - {(settings?.ExcelExportIsEnabled == true ? "v" : string.Empty)}{nomFichierFinal_FileVersion.FileVersion}"));
                            }
                            else if (settings?.ArchivageIsEnabled == true)
                            {
                                return(ExcelExportDirectory, null);
                            }
                            else
                            {
                                return(null, ExcelExportDirectory);
                            }
                        }
                    }
                    else
                    {
                        throw new OperationCanceledException(_MODEL_FILE_NOT_EXISTS,
                                                             new Exception(string.Format(LocalizationManager.GetString("ExtKp_Error_ExcelModelPath"), modelFilePath ?? LocalizationManager.GetString("ExtKp_Error_Undefined"))));
                    }
                }
                else
                {
                    throw new OperationCanceledException();
                }
            }
            catch (Exception e)
            {
                if (e.InnerException is OperationCanceledException)
                {
                    throw e.InnerException;
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                KprocessExportWindow.StopCurrent();
            }
        }