Exemple #1
0
        internal OrganizationResponse Execute(OrganizationRequest request, EntityReference userRef, PluginContext parentPluginContext)
        {
            // Setup
            dataMethods.HandleInternalPreOperations(request, userRef);

            var primaryRef = Mappings.GetPrimaryEntityReferenceFromRequest(request);

            // Create the plugin context
            var pluginContext = new PluginContext()
            {
                UserId            = userRef.Id,
                InitiatingUserId  = userRef.Id,
                MessageName       = RequestNameToMessageName(request.RequestName),
                Depth             = 1,
                OrganizationName  = dataMethods.OrganizationName,
                OrganizationId    = dataMethods.OrganizationId,
                PrimaryEntityName = primaryRef?.LogicalName,
            };

            if (primaryRef != null)
            {
                pluginContext.PrimaryEntityId = dataMethods.GetEntityId(primaryRef).GetValueOrDefault();
            }

            foreach (var prop in request.Parameters)
            {
                pluginContext.InputParameters[prop.Key] = prop.Value;
            }
            if (parentPluginContext != null)
            {
                pluginContext.ParentContext = parentPluginContext;
                pluginContext.Depth         = parentPluginContext.Depth + 1;
            }
            var buRef = GetBusinessUnit(userRef);

            Console.WriteLine($"User GUID: {userRef.Id}");
            Console.WriteLine($"BU GUID: {buRef.Id}");
            pluginContext.BusinessUnitId = buRef.Id;

            Mappings.RequestToEventOperation.TryGetValue(request.GetType(), out EventOperation? eventOp);

            var    entityInfo = GetEntityInfo(request);
            Entity preImage   = null;
            Entity postImage  = null;

            var settings = MockupExecutionContext.GetSettings(request);

            // Validation
            if (!settings.SetUnsettableFields && (request is UpdateRequest || request is CreateRequest))
            {
                var entity = request is UpdateRequest ? (request as UpdateRequest).Target : (request as CreateRequest).Target;
                dataMethods.RemoveUnsettableAttributes(request.RequestName, entity);
            }


            // Pre operation
            if (settings.TriggerProcesses && entityInfo != null)
            {
                preImage = TryRetrieve(primaryRef);
                if (preImage != null)
                {
                    primaryRef.Id = preImage.Id;
                }
                if (eventOp.HasValue)
                {
                    pluginManager.Trigger(eventOp.Value, ExecutionStage.PreOperation, entityInfo.Item1, preImage, postImage, pluginContext, this);
                    workflowManager.Trigger(eventOp.Value, ExecutionStage.PreOperation, entityInfo.Item1, preImage, postImage, pluginContext, this);
                }
            }
            // Core operation
            OrganizationResponse response = ExecuteRequest(request, userRef, parentPluginContext);


            // Post operation
            if (settings.TriggerProcesses && entityInfo != null)
            {
                postImage = TryRetrieve(primaryRef);
                if (eventOp.HasValue)
                {
                    pluginManager.Trigger(eventOp.Value, ExecutionStage.PostOperation, entityInfo.Item1, preImage, postImage, pluginContext, this);
                    workflowManager.Trigger(eventOp.Value, ExecutionStage.PostOperation, entityInfo.Item1, preImage, postImage, pluginContext, this);
                }
                workflowManager.ExecuteWaitingWorkflows(pluginContext, this);
            }

            return(response);
        }