using System;
using System.Collections.Generic;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Newtonsoft.Json;
namespace PracticeGateway.Plugin
{
public class AssociateEntitiesPostOperationUpdate : PluginBase
{
public AssociateEntitiesPostOperationUpdate(string unsecure, string secure)
: base(typeof(AssociateEntitiesPostOperationUpdate))
{
}
protected override void ExecuteCdsPlugin(ILocalPluginContext localContext)
{
if (localContext == null)
throw new InvalidPluginExecutionException(nameof(localContext));
ITracingService tracingService = localContext.TracingService;
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService currentUserService = localContext.CurrentUserService;
try
{
tracingService.Trace("Starting AssociateEntitiesPostOperationUpdate");
if (context.MessageName != "Associate")
throw new InvalidPluginExecutionException($"Unexpected message: {context.MessageName}");
Utils.Utils utils = new Utils.Utils(currentUserService, tracingService);
if (!utils.ValidateContactAssignmentRelationship(context, tracingService, out EntityReference contact, out EntityReference assignment))
{
return;
}
tracingService.Trace($"Contact {contact.Id} associated with Assignment {assignment.Id}");
Entity assignmentEntity = currentUserService.Retrieve("tt_assignment", assignment.Id, new ColumnSet("tt_name"));
string assignmentName = assignmentEntity.GetAttributeValue<string>("tt_name");
tracingService.Trace($"Assignment Name: {assignmentName}");
// Load the assignment-to-web-role configuration from tt_configuration
var assignmentConfig = utils.GetAssignmentConfiguration(currentUserService, tracingService);
if (assignmentConfig == null)
{
return;
}
// Retrieve the list of web roles associated with this assignment
List<string> webRoles = utils.GetWebRolesForAssignment(assignmentConfig, assignmentName, tracingService);
if (webRoles.Count == 0)
{
tracingService.Trace($"No web roles configured for assignment '{assignmentName}'.");
return;
}
tracingService.Trace($"Found {webRoles.Count} web roles to process for assignment '{assignmentName}'.");
// Loop through each web role and associate it with the contact if not already linked
foreach (var webRoleName in webRoles)
{
Entity webRole = utils.GetWebRoleByName(currentUserService, webRoleName);
if (webRole == null)
{
tracingService.Trace($"Web Role '{webRoleName}' not found.");
continue;
}
if (utils.IsContactAssociatedWithWebRole(currentUserService, contact.Id, webRole.Id))
{
tracingService.Trace($"Contact already associated with Web Role '{webRoleName}', skipping.");
continue;
}
currentUserService.Associate(
"contact",
contact.Id,
new Relationship("adx_webrole_contact"),
new EntityReferenceCollection { new EntityReference("adx_webrole", webRole.Id) }
);
tracingService.Trace($"Successfully associated contact with Web Role '{webRoleName}'.");
}
tracingService.Trace("Finished AssociateEntitiesPostOperationUpdate");
}
catch (Exception ex)
{
tracingService.Trace($"Error in AssociateEntitiesPostOperationUpdate: {ex}");
throw new InvalidPluginExecutionException("Plugin execution failed.", ex);
}
}
}
}