/// <summary> /// Create a baseline source for importing data into a tenant. /// </summary> /// <remarks> /// Check the tenant to see if the root entity is present. /// If it is, then perform an export to use the existing contents as a baseline, so it gets updated. /// If it's not, then use an empty source so that everything gets added. /// </remarks> /// <param name="tenantId">Target tenant.</param> /// <param name="rootGuids">UpgradeID of entity to be targeted, if present.</param> /// <returns></returns> private IDataSource GetBaselineSourceForImport(long tenantId, IEnumerable <Guid> rootGuids) { IDataSource baseline; using (new TenantAdministratorContext(tenantId)) { IDictionary <Guid, long> ids = UpgradeIdProvider.GetIdsFromUpgradeIds(rootGuids); if (ids.Count == 0) { baseline = new EmptySource( ); } else { baseline = new TenantGraphSource { TenantId = tenantId, RootEntities = ids.Values.ToList( ) }; } } return(baseline); }
/// <summary> /// Sets up this instance. /// </summary> void IDataSource.Setup(IProcessingContext context) { if (RootEntities == null) { throw new InvalidOperationException("RootEntities is not set."); } // Perform demand on root entity(ies). if (DemandReadPermission) { EntityAccessControlService.Demand(RootEntities.Select(id => new EntityRef(id)).ToList( ), new[] { Permissions.Read }); } using (new TenantAdministratorContext(TenantId)) { _aliasFieldId = WellKnownAliases.CurrentTenant.Alias; _reverseAliasFieldId = WellKnownAliases.CurrentTenant.ReverseAlias; // Get the instance to be exported IEnumerable <IEntity> instances = EntityRepository.Get(RootEntities); ICollection <long> typeIds = instances.Select(inst => inst.TypeIds.FirstOrDefault( )).Distinct().ToList(); if (typeIds.Count == 0) { typeIds = new[] { WellKnownAliases.CurrentTenant.Resource } } ; // Generate a cloning request factory for loading the data CloneEntityMemberRequestFactory requestFactory = new CloneEntityMemberRequestFactory(EntityRepository); EntityMemberRequest memberRequest = requestFactory.CreateRequest(typeIds); EntityRequest entityRequest = new EntityRequest { Request = memberRequest, Entities = RootEntities.Select(id => new EntityRef(id)) }; // Load data, unsecured, via EntityInfoService cache _bulkResult = BulkResultCache.GetBulkResult(entityRequest); // Load all UpgradeIDs IEnumerable <long> allIds = _bulkResult.AllEntities.Keys .Union(_bulkResult.Relationships.Keys.Select(k => k.TypeId)) .Union(_bulkResult.FieldValues.Keys.Select(k => k.FieldId)); _idToUpgradeId = UpgradeIdProvider.GetUpgradeIds(allIds); IEnumerable <long> relTypes = _bulkResult.Relationships.Keys.Select(key => key.TypeId).Distinct( ); IEnumerable <Relationship> relationships = EntityRepository.Get <Relationship>(relTypes); _relationshipTypeCache = relationships.ToDictionary( rel => _idToUpgradeId[rel.Id], rel => new RelationshipTypeEntry { CloneAction = rel.CloneAction_Enum, ReverseCloneAction = rel.ReverseCloneAction_Enum, Alias = rel.Alias, ReverseAlias = rel.ReverseAlias }); LoadDocumentCaches(context); } // Read permissions - for reading internal entities if (DemandReadPermission) { _canRead = BulkRequestResultSecurityHelper.GetEntityReadability(Factory.EntityAccessControlService, _bulkResult); } else { _canRead = id => true; } } /// <summary> /// Loads the application metadata. /// </summary> /// <param name="context">The context.</param> /// <returns></returns> /// <exception cref="System.InvalidOperationException">@Invalid package Id</exception> Metadata IDataSource.GetMetadata(IProcessingContext context) { List <Guid> roots = new List <Guid>( ); foreach (long rootId in RootEntities) { Guid rootGuid; if (_idToUpgradeId.TryGetValue(rootId, out rootGuid)) { roots.Add(rootGuid); } } var metadata = new Metadata { AppName = "Exported data", AppVerId = Guid.Empty, AppId = Guid.Empty, Description = "Exported data", Name = "Exported data", Version = "1.0", RootEntities = roots, //Dependencies = solutionDependencies, Type = SourceType.DataExport, PlatformVersion = SystemInfo.PlatformVersion, RelationshipTypeCallback = GetRelationshipMetadata }; return(metadata); }