Beispiel #1
0
        void PrepareVideo(string videosDirectory)
        {
            var video = _import.ExportedVideoDecomposition.Video;

            video.VideoId  = default(int);
            video.Process  = _targetScenario.Project.Process;
            video.FilePath = IOHelper.ChangeDirectory(video.FilePath, videosDirectory);
        }
Beispiel #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);
        }