private void AddImplicitAntigens(IOrganizationService organisationService, Rarity_SourceAssociation raritySourceAssoc) { var sourceId = raritySourceAssoc.Source.Id; var rarityId = raritySourceAssoc.Rarity.Id; var targetAntigensForRarity = Helper.GetAntigensImpliedByRarity(organisationService, rarityId); var currentlyAssociatedAntigens = GetAssociatedSourceAntigens(organisationService, sourceId); var intersectEntitiesToBeCreated = new EntityReferenceCollection(); foreach (var implicitAntigen in targetAntigensForRarity) { var oldestActiveMatchSameResult = (from associatedAntigen in currentlyAssociatedAntigens where associatedAntigen.Antigen.Id == implicitAntigen.Antigen.Id && associatedAntigen.AntigenResult_OptionSetValue.Value == implicitAntigen.AntigenResult_OptionSetValue.Value select associatedAntigen).FirstOrDefault(); var oldestMatchNotExplicitOrImplied = (from associatedAntigen in currentlyAssociatedAntigens where associatedAntigen.Antigen.Id == implicitAntigen.Antigen.Id && associatedAntigen.Explicit == false && IsAntigenAssocImpliedByRarityAssoc(organisationService, associatedAntigen, raritySourceAssoc) == false select associatedAntigen).FirstOrDefault(); if (oldestActiveMatchSameResult != null) { //If this is the oldest association for that antigen with the same result var isAlreadyLinkedWithRarity = IsAntigenAssocImpliedByRarityAssoc( organisationService, oldestActiveMatchSameResult, raritySourceAssoc); var isAlreadyActive = oldestActiveMatchSameResult.Status == Antigen_SourceAssociation.eStatus.Active; if (!isAlreadyLinkedWithRarity) { //if its not already linked, then do so intersectEntitiesToBeCreated.Add(oldestActiveMatchSameResult.ToEntityReference()); } if (!isAlreadyActive) { // and if its not active, then make it so Helper.SetState( organisationService, oldestActiveMatchSameResult, new OptionSetValue((int)ProxyClasses.Antigen_SourceAssociation.eStatus.Active), new OptionSetValue((int)ProxyClasses.Antigen_SourceAssociation.eStatusReason.Active_Active)); } } else if (oldestMatchNotExplicitOrImplied != null) //Otherwise, find the oldest antigen association not explicit or implied, with any result/status { var isAlreadyActive = oldestActiveMatchSameResult.Status == Antigen_SourceAssociation.eStatus.Active; var isAlreadyLinkedWithRarity = IsAntigenAssocImpliedByRarityAssoc( organisationService, oldestMatchNotExplicitOrImplied, raritySourceAssoc); if (!isAlreadyActive) { //if its not already active // then Activate the record Helper.SetState( organisationService, oldestMatchNotExplicitOrImplied.ToEntityReference(), new OptionSetValue((int)ProxyClasses.Antigen_RarityAssociation.eStatus.Active), new OptionSetValue((int)ProxyClasses.Antigen_RarityAssociation.eStatusReason.Active_Active)); } if (!isAlreadyLinkedWithRarity) { //if its not already linked, then do so intersectEntitiesToBeCreated.Add(oldestMatchNotExplicitOrImplied.ToEntityReference()); } if ( (implicitAntigen.AntigenResult == ProxyClasses.Antigen_RarityAssociation.eAntigenResult_RarityContext.Present && oldestMatchNotExplicitOrImplied.AntigenResult != ProxyClasses.Antigen_SourceAssociation.eAntigenResult_SourceContext.Present) || (implicitAntigen.AntigenResult == ProxyClasses.Antigen_RarityAssociation.eAntigenResult_RarityContext.Absent && oldestMatchNotExplicitOrImplied.AntigenResult != ProxyClasses.Antigen_SourceAssociation.eAntigenResult_SourceContext.Absent)) { //and if the antigen result doesn't match //then update it oldestMatchNotExplicitOrImplied.AntigenResult = (implicitAntigen.AntigenResult == ProxyClasses.Antigen_RarityAssociation.eAntigenResult_RarityContext.Present) ? ProxyClasses.Antigen_SourceAssociation.eAntigenResult_SourceContext.Present : ProxyClasses.Antigen_SourceAssociation.eAntigenResult_SourceContext.Absent; organisationService.Update(oldestMatchNotExplicitOrImplied); } } else //no matching Antigen Source Association exists so... { //we'll create a new association var implicitAntigenSourceAssociation = new ProxyClasses.Antigen_SourceAssociation() { Source = new EntityReference(ProxyClasses.RareBloodSource.LogicalName, sourceId), Antigen = implicitAntigen.Antigen, AntigenResult = implicitAntigen.AntigenResult == ProxyClasses.Antigen_RarityAssociation.eAntigenResult_RarityContext.Present ? ProxyClasses.Antigen_SourceAssociation.eAntigenResult_SourceContext.Present : ProxyClasses.Antigen_SourceAssociation.eAntigenResult_SourceContext.Absent }; var implicitAntigenSourceAssociationId = organisationService.Create(implicitAntigenSourceAssociation); //and we'll link it to the rarity association intersectEntitiesToBeCreated.Add(new EntityReference(ProxyClasses.Antigen_SourceAssociation.LogicalName, implicitAntigenSourceAssociationId)); } } if (intersectEntitiesToBeCreated.Count() > 0) { //...we need to link this rarity assocation to the antigen associations organisationService.Associate( ProxyClasses.Rarity_SourceAssociation.LogicalName, raritySourceAssoc.Id, new Relationship("nhs_AntigenSrcAssoc_nhs_RaritySrcAssoc"), intersectEntitiesToBeCreated); } }
private bool IsAntigenAssocImpliedByRarityAssoc(IOrganizationService organisationService, Antigen_SourceAssociation antigenSourceAssociation, Rarity_SourceAssociation raritySourceAssociation) { var queryExpression = new QueryExpression() { EntityName = ProxyClasses.nhs_AnitenSrcAssoc_nhs_RaritySrcAssoc.LogicalName, ColumnSet = new ColumnSet(new string[] { ProxyClasses.nhs_AnitenSrcAssoc_nhs_RaritySrcAssoc.PrimaryIdAttribute, }), Criteria = new FilterExpression() { Filters = { new FilterExpression() { Conditions = { new ConditionExpression( ProxyClasses.nhs_AnitenSrcAssoc_nhs_RaritySrcAssoc.Properties.Nhs_antigensourceassociationid, ConditionOperator.Equal, antigenSourceAssociation.Antigen_SourceAssociationId), new ConditionExpression( ProxyClasses.nhs_AnitenSrcAssoc_nhs_RaritySrcAssoc.Properties.Nhs_raritysourceassociationid, ConditionOperator.Equal, raritySourceAssociation.Rarity_SourceAssociationId) } } } } }; var results = organisationService.RetrieveMultiple(queryExpression); if (results.Entities.Count > 0) { return(true); } else { return(false); } }