private bool IsRelAllowed(CdmE2ERelationship rel, CdmRelationshipDiscoveryStyle option) { if (option == CdmRelationshipDiscoveryStyle.None) { return(false); } else if (option == CdmRelationshipDiscoveryStyle.Exclusive) { string absoluteFromEntString = this.Ctx.Corpus.Storage.CreateAbsoluteCorpusPath(rel.FromEntity, this); // only true if from and to entities are both found in the entities list of this folio bool fromEntInManifest = this.Entities.Where(x => { return(this.Ctx.Corpus.Storage.CreateAbsoluteCorpusPath(x.EntityPath, this) == absoluteFromEntString); }).ToList().Count > 0; string absoluteToEntString = this.Ctx.Corpus.Storage.CreateAbsoluteCorpusPath(rel.ToEntity, this); bool toEntInManifest = this.Entities.Where(x => { return(this.Ctx.Corpus.Storage.CreateAbsoluteCorpusPath(x.EntityPath, this) == absoluteToEntString); }).ToList().Count > 0; return(fromEntInManifest && toEntInManifest); } else { return(true); } }
/// <summary> /// Populates the relationships that the entities in the current manifest are involved in. /// </summary> public async Task PopulateManifestRelationshipsAsync(CdmRelationshipDiscoveryStyle option = CdmRelationshipDiscoveryStyle.All) { using (Logger.EnterScope(nameof(CdmManifestDefinition), Ctx, nameof(PopulateManifestRelationshipsAsync))) { this.Relationships.Clear(); HashSet <string> relCache = new HashSet <string>(); if (this.Entities != null) { foreach (CdmEntityDeclarationDefinition entDec in this.Entities) { string entPath = await this.GetEntityPathFromDeclaration(entDec, this); CdmEntityDefinition currEntity = await this.Ctx.Corpus.FetchObjectAsync <CdmEntityDefinition>(entPath); if (currEntity == null) { continue; } // handle the outgoing relationships List <CdmE2ERelationship> outgoingRels = this.Ctx.Corpus.FetchOutgoingRelationships(currEntity); if (outgoingRels != null) { foreach (CdmE2ERelationship rel in outgoingRels) { string cacheKey = rel2CacheKey(rel); if (!relCache.Contains(cacheKey) && this.IsRelAllowed(rel, option)) { this.Relationships.Add(this.LocalizeRelToManifest(rel)); relCache.Add(cacheKey); } } } List <CdmE2ERelationship> incomingRels = this.Ctx.Corpus.FetchIncomingRelationships(currEntity); if (incomingRels != null) { foreach (CdmE2ERelationship inRel in incomingRels) { // get entity object for current toEntity CdmEntityDefinition currentInBase = await this.Ctx.Corpus.FetchObjectAsync <CdmEntityDefinition>(inRel.ToEntity, this); if (currentInBase == null) { continue; } // create graph of inheritance for to currentInBase // graph represented by an array where entity at i extends entity at i+1 List <CdmEntityDefinition> toInheritanceGraph = new List <CdmEntityDefinition>(); while (currentInBase != null) { var resOpt = new ResolveOptions { WrtDoc = currentInBase.InDocument }; currentInBase = currentInBase.ExtendsEntity?.FetchObjectDefinition <CdmEntityDefinition>(resOpt); if (currentInBase != null) { toInheritanceGraph.Add(currentInBase); } } // add current incoming relationship string cacheKey = rel2CacheKey(inRel); if (!relCache.Contains(cacheKey) && this.IsRelAllowed(inRel, option)) { this.Relationships.Add(this.LocalizeRelToManifest(inRel)); relCache.Add(cacheKey); } // if A points at B, A's base classes must point at B as well foreach (CdmEntityDefinition baseEntity in toInheritanceGraph) { List <CdmE2ERelationship> incomingRelsForBase = this.Ctx.Corpus.FetchIncomingRelationships(baseEntity); if (incomingRelsForBase != null) { foreach (CdmE2ERelationship inRelBase in incomingRelsForBase) { CdmE2ERelationship newRel = new CdmE2ERelationship(this.Ctx, "") { FromEntity = inRelBase.FromEntity, FromEntityAttribute = inRelBase.FromEntityAttribute, ToEntity = inRel.ToEntity, ToEntityAttribute = inRel.ToEntityAttribute }; string baseRelCacheKey = rel2CacheKey(newRel); if (!relCache.Contains(baseRelCacheKey) && this.IsRelAllowed(newRel, option)) { this.Relationships.Add(this.LocalizeRelToManifest(newRel)); relCache.Add(baseRelCacheKey); } } } } } } } if (this.SubManifests != null) { foreach (CdmManifestDeclarationDefinition subManifestDef in this.SubManifests) { var corpusPath = this.Ctx.Corpus.Storage.CreateAbsoluteCorpusPath(subManifestDef.Definition, this); var subManifest = await this.Ctx.Corpus.FetchObjectAsync <CdmManifestDefinition>(corpusPath); await subManifest.PopulateManifestRelationshipsAsync(option); } } } } }