Esempio n. 1
0
        /// <summary>
        ///     Imports the entity
        /// </summary>
        /// <param name="tenantId"></param>
        /// <param name="importSource"></param>
        /// <param name="settings"></param>
        /// <param name="context"></param>
        internal IEnumerable <Guid> ImportEntity(long tenantId, IDataSource importSource, EntityXmlImportSettings settings, IProcessingContext context)
        {
            IList <Guid> rootGuids = GetRootGuidsFromMetadata(importSource, context);

            using (IDataSource baseline = GetBaselineSourceForImport(tenantId, rootGuids))
                using (TenantMergeTarget target = new TenantMergeTarget
                {
                    TenantId = tenantId,
                    IgnoreExternalReferences = true
                })
                {
                    /////
                    // Copy the data
                    /////
                    using (var processor = new MergeProcessor(context)
                    {
                        OldVersion = baseline,
                        NewVersion = importSource,
                        Target = target
                    })
                    {
                        processor.MergeData( );

                        CheckForMissingDependencies(context, settings);

                        target.Commit( );
                    }
                }

            CacheManager.ClearCaches(tenantId);

            return(rootGuids);
        }
Esempio n. 2
0
        /// <summary>
        ///     Imports the tenant.
        /// </summary>
        /// <returns>the tenant id</returns>
        public static void InstallGlobalTenant( )
        {
            IProcessingContext context = new ProcessingContext( );

            context.Report.Action = AppLibraryAction.InstallGlobal;
            context.Report.Arguments.Add(new KeyValuePair <string, string>("Tenant Id", "Global"));

            IDictionary <Guid, Guid> appToAppVer = new Dictionary <Guid, Guid>( );

            // Get application versions
            // It is assumed that if there is no global tenant, then there is only one version of each of the core apps
            using (IDatabaseContext dbContext = DatabaseContext.GetContext(false))
                using (IDbCommand command = dbContext.CreateCommand())
                {
                    command.CommandText = "select FromUid AppUid, AppVerUid from AppRelationship where ToUid = @solution and TypeUid = @isOfType";
                    command.AddParameterWithValue("@solution", Guids.Solution);
                    command.AddParameterWithValue("@isOfType", Guids.IsOfType);

                    using (IDataReader reader = command.ExecuteReader( ))
                    {
                        // Load appVerId for apps from app library
                        while (reader.Read( ))
                        {
                            Guid appId    = reader.GetGuid(0);
                            Guid appVerId = reader.GetGuid(1);
                            appToAppVer[appId] = appVerId;
                        }
                    }
                }

            // Install apps
            Guid[] coreApps = new[]
            {
                Guids.CoreSolution,
                Guids.ConsoleSolution,
                Guids.CoreDataSolution,
                Guids.SystemSolution
            };

            // Copy system application content from the app library into the global tenant
            foreach (Guid appId in coreApps)
            {
                // Get the AppVerId
                Guid appVerId;
                if (!appToAppVer.TryGetValue(appId, out appVerId))
                {
                    throw new Exception($"Aborting: System app {0} was not present in app library.");
                }

                context.WriteInfo($"Installing app {appId} package {appVerId} to global tenant.");

                IDataSource empty  = new EmptySource( );
                IDataSource source = new LibraryAppSource
                {
                    AppId    = appId,
                    AppVerId = appVerId,
                    AppName  = appId.ToString( )
                };
                TenantMergeTarget target = new TenantMergeTarget
                {
                    TenantId = 0
                };

                using (empty)
                    using (source)
                        using ( target )
                        {
                            MergeProcessor processor = new MergeProcessor(context)
                            {
                                OldVersion = empty,
                                NewVersion = source,
                                Target     = target
                            };
                            processor.MergeData( );

                            target.Commit( );
                        }
            }


            // Copy metadata from the tenant back into the application library
            foreach (Guid appId in coreApps)
            {
                Guid appVerId = appToAppVer[appId];

                long        solutionEntityId = Entity.GetIdFromUpgradeId(appId);
                IDataSource metadataSource   = new TenantAppSource
                {
                    SolutionId = solutionEntityId,
                    TenantId   = 0
                };
                LibraryAppTarget metadataTarget = new LibraryAppTarget
                {
                    ApplicationVersionId = appVerId
                };
                using (metadataSource)
                    using ( metadataTarget )
                    {
                        CopyProcessor processor = new CopyProcessor(metadataSource, metadataTarget, context);
                        processor.CopyMetadataOnly = true;
                        processor.MigrateData( );

                        metadataTarget.Commit( );
                    }

                context.WriteInfo($"Installed app {appId} to global tenant.");
            }
        }