private void SetExplicit(IOrganizationService organisationService, Guid antigenSourceAssociationId, bool isExplicit = true) { var antigenSourceAssociation = new Antigen_SourceAssociation(); antigenSourceAssociation.Id = antigenSourceAssociationId; antigenSourceAssociation.Explicit = isExplicit; organisationService.Update(antigenSourceAssociation); }
private bool CheckIfImpliedByRarities(IOrganizationService organisationService, Antigen_SourceAssociation antigenSourceAssociation) { var antigenIdList = new List <Guid>(); var queryExpression = new QueryExpression() { EntityName = ProxyClasses.nhs_AnitenSrcAssoc_nhs_RaritySrcAssoc.LogicalName, ColumnSet = new ColumnSet(new string[] { ProxyClasses.nhs_AnitenSrcAssoc_nhs_RaritySrcAssoc.PrimaryIdAttribute, ProxyClasses.nhs_AnitenSrcAssoc_nhs_RaritySrcAssoc.Properties.Nhs_antigensourceassociationid, ProxyClasses.nhs_AnitenSrcAssoc_nhs_RaritySrcAssoc.Properties.Nhs_raritysourceassociationid }), Criteria = new FilterExpression() { Filters = { new FilterExpression() { Conditions = { new ConditionExpression( ProxyClasses.nhs_AnitenSrcAssoc_nhs_RaritySrcAssoc.Properties.Nhs_antigensourceassociationid, ConditionOperator.Equal, antigenSourceAssociation.Id ) } } } } }; var results = organisationService.RetrieveMultiple(queryExpression); if (results.Entities.Count() > 0) { //if there are any implying rarities for this antigen return(true); } else { // but if there are none, then its not implied return(false); } }
private ExecuteMultipleRequest AlignAntigenSourceAssociations(IOrganizationService organisationService, ParsedFileRow parsedFileRow, EntityReference rareBloodSource, ExecuteMultipleRequest executeMultipleRequest) { var sourceAntigens = Helper.GetAntigenAssociationsForSource(organisationService, rareBloodSource.Id); //Query the records prior to any create/update/remove actions var antigenSourceAssociationsToCreate = parsedFileRow.AntigenSourceAssociations.Where(pfr => sourceAntigens.All(source => pfr.Antigen.Id != source.Antigen.Id)); var antigenSourceAssociationsToUpdate = sourceAntigens.Where(source => parsedFileRow.AntigenSourceAssociations.Any(pfr => pfr.Antigen.Id == source.Antigen.Id)); var antigenSourceAssociationsToRemove = sourceAntigens.Where(source => parsedFileRow.AntigenSourceAssociations.All(pfr => pfr.Antigen.Id != source.Antigen.Id)); foreach (var antigenSourceAssociation in antigenSourceAssociationsToCreate) { antigenSourceAssociation.Source = rareBloodSource; antigenSourceAssociation.Explicit = true; executeMultipleRequest.Requests.Add( new CreateRequest() { Target = antigenSourceAssociation }); } List <EntityReference> antigensAlreadyRepresented = new List <EntityReference>(); foreach (var antigenSourceAssociation in antigenSourceAssociationsToUpdate) { //=========//=========//=========//=========// //========= Section 1 - Gather Data //=========//=========//=========//=========// //Finds the oldest instance of antigen associations matching the file upload //test whether the antigen association is active bool isAssociationActive = antigenSourceAssociation.Status == Antigen_SourceAssociation.eStatus.Active; //test whether it is explicit bool isAssociationExplicit = antigenSourceAssociation.Explicit.Value; //test whether it is implied by rarities bool isAssociationImpliedByRarities = CheckIfImpliedByRarities(organisationService, antigenSourceAssociation); //get the antigen association details from the parsed file row Antigen_SourceAssociation uploadedAntigenDetails = ( from a in parsedFileRow.AntigenSourceAssociations where a.Antigen.Id == antigenSourceAssociation.Antigen.Id select a ).First(); //test whether the result is already correct bool isResultCorrect = antigenSourceAssociation.AntigenResult == uploadedAntigenDetails.AntigenResult; //find the oldest existing antigen association //that we can reuse (i.e. result is already matching, or the result can be amended as it's not implied) Antigen_SourceAssociation oldestReusableMatchingAssociation = ( antigenSourceAssociationsToUpdate.Where( asatu => asatu.Antigen.Id == antigenSourceAssociation.Antigen.Id //where either the result is the same && (asatu.AntigenResult.Value == uploadedAntigenDetails.AntigenResult.Value //or it's not implied (i.e. its result can be updated) || CheckIfImpliedByRarities(organisationService, asatu) == false) ) ).FirstOrDefault(); //=========//=========//=========//=========//=========//=========// //========= Section 2 - Test the values to determine next action //=========//=========//=========//=========//=========//=========// //=== First Preference ===// if (oldestReusableMatchingAssociation != null && antigenSourceAssociation.Id == oldestReusableMatchingAssociation.Id) { //if this is the oldest association with the same result for this antigen/source combo if (!isAssociationActive || !isAssociationExplicit || !isResultCorrect) { //and if its either inactive or not marked as explicit or has the incorrect result //then update it antigenSourceAssociation.Explicit = true; antigenSourceAssociation.AntigenResult = uploadedAntigenDetails.AntigenResult; antigenSourceAssociation.Status = Antigen_SourceAssociation.eStatus.Active; antigenSourceAssociation.StatusReason = Antigen_SourceAssociation.eStatusReason.Active_Active; //and add the update to the execution queue. executeMultipleRequest.Requests.Add( new UpdateRequest() { Target = antigenSourceAssociation } ); //And record this antigen as processed antigensAlreadyRepresented.Add(antigenSourceAssociation.Antigen); } } else { //if this is not the oldest antigen association that we can reuse if (isAssociationActive && !isAssociationImpliedByRarities) // if it's active //and it's not implied by other rarities { //then it should simply be deactivated. executeMultipleRequest.Requests.Add( new SetStateRequest() { EntityMoniker = antigenSourceAssociation.ToEntityReference(), State = new OptionSetValue((int)Antigen_SourceAssociation.eStatus.Inactive), Status = new OptionSetValue((int)Antigen_SourceAssociation.eStatusReason.RemovedByImport_Inactive) }); } //test whether this antigen has been associated with the source already bool isAntigenAlreadyRepresented = antigensAlreadyRepresented.Where( newAntigens => newAntigens.Id == antigenSourceAssociation.Antigen.Id ).FirstOrDefault() != null; //if there is no reusable antigen association //and the antigen has not already been represented against this source if (oldestReusableMatchingAssociation == null && isAntigenAlreadyRepresented == false) { //then we need to create a new antigen-source association //to highlight the contradiction with the existing ones. uploadedAntigenDetails.Source = rareBloodSource; uploadedAntigenDetails.Explicit = true; executeMultipleRequest.Requests.Add( new CreateRequest() { Target = uploadedAntigenDetails }); //and record the fact that this antigen is now represented for this upload antigensAlreadyRepresented.Add(antigenSourceAssociation.Antigen); } } } foreach (var antigenSourceAssociation in antigenSourceAssociationsToRemove) { bool isImpliedByRarities = CheckIfImpliedByRarities(organisationService, antigenSourceAssociation); bool isActive = antigenSourceAssociation.Status == Antigen_SourceAssociation.eStatus.Active; if (!isImpliedByRarities && isActive) { //if it isn't implied, but is still active //then we can just deactivate it. executeMultipleRequest.Requests.Add( new SetStateRequest() { EntityMoniker = antigenSourceAssociation.ToEntityReference(), State = new OptionSetValue((int)Antigen_SourceAssociation.eStatus.Inactive), Status = new OptionSetValue((int)Antigen_SourceAssociation.eStatusReason.RemovedByImport_Inactive) }); } } return(executeMultipleRequest); }
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); } }