Example #1
0
        /// <summary>
        /// Importe la décomposition vidéo spécifiée spécifié.
        /// </summary>
        /// <param name="mergeReferentials"><c>true</c> pour fusionner les référentiels.</param>
        /// <param name="videosDirectory">Le dossier où se situent les vidéos.</param>
        public async Task ImportVideoDecomposition(bool mergeReferentials, string videosDirectory)
        {
            var videoDecomposition = _import.ExportedVideoDecomposition;

            // Videos
            PrepareVideo(videosDirectory);

            // Referentials
            ImportReferentials(mergeReferentials);

            // Actions
            ImportActions();

            // Remapper les référentiels du projet, des actions et des vidéos
            RemapReferentials();

            FixupImportedActions();

            // Custom text & Numeric labels
            ImportCustomFieldsLabels();

            _context.DetectChanges();

            // Passer les entités mappées à non modifiées
            FixupReferentialsTrackingState();

            if (mergeReferentials)
            {
                ServicesDiagnosticsDebug.CheckNotInContext(_context, _referentialsToRemap.Keys);
                ServicesDiagnosticsDebug.CheckObjectStateManagerState(_context, System.Data.Entity.EntityState.Unchanged, _referentialsToRemap.Values);
            }

            await _context.SaveChangesAsync();

            ServicesDiagnosticsDebug.CheckReferentialsState();
        }
Example #2
0
        /// <summary>
        /// Importe le projet spécifié.
        /// </summary>
        /// <param name="context">Le contexte.</param>
        /// <param name="import">Le projet exporté.</param>
        /// <param name="mergeReferentials"><c>true</c> pour fusionner les référentiels.</param>
        /// <param name="videosDirectory">Le dossier où se situent les vidéos.</param>
        /// <returns>Le projet créé.</returns>
        private async Task <Project> ImportProject(KsmedEntities context, ProjectImport import, bool mergeReferentials, string videosDirectory)
        {
            // Projet
            var p = import.ExportedProject.Project;

            p.ProjectId = default(int);
            MarkAsAdded(p);

            // Ajouter l'utilisateur courant en tant qu'analyste créateur
            var owner = await context.Users.FirstAsync(u => !u.IsDeleted && u.Username == _securityContext.CurrentUser.Username);

            p.Process.Owner = owner;
            p.Process.UserRoleProcesses.Add(new UserRoleProcess()
            {
                User     = owner,
                RoleCode = KnownRoles.Analyst,
            });

            // Videos
            foreach (var video in p.Process.Videos)
            {
                if (video.DefaultResourceId.HasValue && video.DefaultResource == null)
                {
                    // Bug présent dans certains exports de la version 2.5.0.0. Supprimer le lien vers la ressource par défaut.
                    video.DefaultResourceId = null;
                }

                video.VideoId = default(int);
                MarkAsAdded(video);
                video.FilePath = IOHelper.ChangeDirectory(video.FilePath, videosDirectory);
            }

            var referentialsToRemap = new Dictionary <IActionReferential, IActionReferential>();

            if (!mergeReferentials)
            {
                // Référentiels process
                foreach (var refe in import.ExportedProject.ReferentialsProject)
                {
                    MarkAsAdded(refe);
                }

                // Référentiels standard
                foreach (var refe in import.ExportedProject.ReferentialsStandard)
                {
                    var refProject = ReferentialsFactory.CopyToNewProject(refe);

                    // Associer au process
                    refProject.Process = p.Process;

                    referentialsToRemap[refe] = refProject;
                }
            }
            else
            {
                // Référentiels process
                foreach (var refe in import.ExportedProject.ReferentialsProject)
                {
                    // Ajouter au tableau de remap s'il y a une correspondance.
                    if (import.ProjectReferentialsMergeCandidates.ContainsKey(refe))
                    {
                        referentialsToRemap[refe] = import.ProjectReferentialsMergeCandidates[refe];
                    }
                    else
                    {
                        MarkAsAdded(refe);
                    }
                }

                // Référentiels standard
                foreach (var refe in import.ExportedProject.ReferentialsStandard)
                {
                    if (import.StandardReferentialsMergeCandidates.ContainsKey(refe))
                    {
                        referentialsToRemap[refe] = import.StandardReferentialsMergeCandidates[refe];
                    }
                    else
                    {
                        var refProject = ReferentialsFactory.CopyToNewProject(refe);

                        // Associer au process
                        refProject.Process = p.Process;

                        referentialsToRemap[refe] = refProject;
                    }
                }
            }


            // Scénarios
            foreach (var scenario in p.Scenarios.Where(s => s.OriginalScenarioId.HasValue))
            {
                // Remapper l'original
                scenario.Original = p.Scenarios.Single(s => s.ScenarioId == scenario.OriginalScenarioId);
            }

            foreach (var scenario in p.Scenarios)
            {
                foreach (var action in scenario.Actions.Where(a => a.OriginalActionId.HasValue))
                {
                    // Remapper l'original
                    action.Original = p.Scenarios.SelectMany(s => s.Actions).Single(a => a.ActionId == action.OriginalActionId);
                }
            }

            foreach (var scenario in p.Scenarios)
            {
                scenario.ScenarioId = default(int);
                MarkAsAdded(scenario);

                // Supprimer le WebPublicationGuid
                scenario.WebPublicationGuid = null;

                // Actions
                foreach (var action in scenario.Actions)
                {
                    action.ActionId = default(int);
                    MarkAsAdded(action);

                    // Actions réduites
                    if (action.IsReduced)
                    {
                        MarkAsAdded(action.Reduced);
                    }
                }
            }

            // Remapper les référentiels du projet, des actions et des vidéos
            foreach (var oldReferential in referentialsToRemap.Keys)
            {
                ReferentialsHelper.UpdateReferentialReferences(p, oldReferential, referentialsToRemap[oldReferential]);
            }

            foreach (var scenario in p.Scenarios)
            {
                if (scenario.Original != null)
                {
                    context.Scenarios.ApplyChanges(scenario);
                    ObjectContextExt.SetRelationShipReferenceValue(context, scenario, scenario.Original, s => s.OriginalScenarioId);

                    foreach (var action in scenario.Actions)
                    {
                        if (action.Original != null)
                        {
                            context.KActions.ApplyChanges(action);
                            ObjectContextExt.SetRelationShipReferenceValue(context, action, action.Original, a => a.OriginalActionId);
                        }
                    }
                }
            }

            var resources = p.Scenarios.SelectMany(s => s.Actions).Select(a => a.Resource).Distinct().ToArray();

            context.Projects.ApplyChanges(p);

            if (mergeReferentials)
            {
                ServicesDiagnosticsDebug.CheckNotInContext(context, referentialsToRemap.Keys);
                ServicesDiagnosticsDebug.CheckObjectStateManagerState(context, EntityState.Unchanged, referentialsToRemap.Values);
            }

            ServicesDiagnosticsDebug.CheckReferentialsState();

            await context.SaveChangesAsync();

            return(p);
        }
Example #3
0
        /// <summary>
        /// Exporte le projet spécifié.
        /// </summary>
        private void ExportVideoDecomposition(int projectId, int scenarioId, int videoId, int targetProjectId, bool merge)
        {
            var service = new ImportExportService();

            string fileName = string.Format("out{0}.xml", fileNumber++);

            KProcess.Ksmed.Business.Dtos.Export.VideoDecompositionExport oldVideoExport;
            using (var context = KProcess.Ksmed.Data.ContextFactory.GetNewContext())
            {
                oldVideoExport = new VideoDecompositionExporter(context, projectId, scenarioId, videoId).CreateExport();
            }

            var       mre    = new System.Threading.ManualResetEvent(false);
            Stream    stream = null;
            Exception e      = null;

            service.ExportVideoDecomposition(projectId, scenarioId, videoId, d =>
            {
                stream = d;
                mre.Set();
            }, ex =>
            {
                e = ex;
                mre.Set();
            });

            mre.WaitOne();
            AssertExt.IsExceptionNull(e);
            Assert.IsNotNull(stream);

            using (var reader = new StreamReader(stream))
            {
                string xml = reader.ReadToEnd();
                File.WriteAllText(fileName, xml);

                Assert.IsNotNull(xml);
            }
            stream.Close();

            Initialization.SetCurrentUser("paula");

            // Ouvrir le fichier
            mre.Reset();
            service.PredictMergedReferentialsVideoDecomposition(targetProjectId, File.OpenRead(fileName), vdi =>
            {
                service.ImportVideoDecomposition(vdi, merge, TestContext.DeploymentDirectory, targetProjectId, success =>
                {
                    Assert.IsTrue(success);
                    mre.Set();
                }, ex =>
                {
                    e = ex;
                    mre.Set();
                });
            }, ex =>
            {
                e = ex;
                mre.Set();
            });

            mre.WaitOne();
            AssertExt.IsExceptionNull(e);

            // Récupérer le numéro de scénario initial du projet
            Scenario targetScenario;
            int      newVideoId;

            using (var context = KProcess.Ksmed.Data.ContextFactory.GetNewContext())
            {
                targetScenario = context.Scenarios.Single(s => s.ProjectId == targetProjectId && s.StateCode == KnownScenarioStates.Draft && s.NatureCode == KnownScenarioNatures.Initial);
                var oldVideoName = context.Videos.Single(v => v.VideoId == videoId).Name;
                newVideoId = context.Videos.Where(v => v.ProjectId == targetProjectId && v.Name == oldVideoName)
                             .AsEnumerable().Last().VideoId;
            }

            //Réexporter la décompo
            KProcess.Ksmed.Business.Dtos.Export.VideoDecompositionExport newVideoExport;
            using (var context = KProcess.Ksmed.Data.ContextFactory.GetNewContext())
            {
                newVideoExport = new VideoDecompositionExporter(context, targetScenario.ProjectId, targetScenario.ScenarioId, newVideoId).CreateExport();
            }

            // Comparer les valeurs

            // Vidéo
            var oldVideo = oldVideoExport.Video;
            var newVideo = newVideoExport.Video;

            AssertVideo(oldVideo, newVideo);

            // Référentiels
            var p1RerentialsProject  = ReferentialsHelper.GetAllReferentialsProject(oldVideoExport.Actions).ToArray();
            var p1RerentialsStandard = ReferentialsHelper.GetAllReferentialsStandardUsed(oldVideoExport.Actions).ToArray();

            var p2RerentialsProject  = ReferentialsHelper.GetAllReferentialsProject(newVideoExport.Actions).ToArray();
            var p2RerentialsStandard = ReferentialsHelper.GetAllReferentialsStandardUsed(newVideoExport.Actions).ToArray();

            Assert.IsTrue(p1RerentialsProject.Length <= p2RerentialsProject.Length);
            Assert.AreEqual(p1RerentialsProject.Length + p1RerentialsStandard.Length, p2RerentialsProject.Length);
            Assert.AreEqual(0, p2RerentialsStandard.Length);

            // Vérifier que toutes les actions de l'ancien export soient également maintenant dans le projet de destination
            var oldActions = oldVideoExport.Actions.OrderBy(a => WBSHelper.GetParts(a.WBS), new WBSHelper.WBSComparer()).ToArray();
            var newActions = newVideoExport.Actions.OrderBy(a => WBSHelper.GetParts(a.WBS), new WBSHelper.WBSComparer()).ToArray();

            Assert.AreEqual(oldActions.Length, newActions.Length);

            // Actions
            for (int j = 0; j < oldActions.Length; j++)
            {
                var oldAction = oldActions[j];
                var newAction = newActions[j];

                AssertAction(oldAction, newAction);

                // Actions réduites
                AssertActionReduced(oldAction.Reduced, newAction.Reduced);
            }

            ServicesDiagnosticsDebug.CheckReferentialsState();
        }