Esempio n. 1
0
        public static string GetSqlConnectionString(this PluginExecutionContext context)
        {
            var retrieverService = (IEntityDataSourceRetrieverService)context.ServiceProvider.GetService(typeof(IEntityDataSourceRetrieverService));
            var sourceEntity     = retrieverService.RetrieveEntityDataSource();

            return(sourceEntity[dataSourceSqlConnectionStringAttribute].ToString());
        }
        public virtual void Execute(IServiceProvider serviceProvider)
        {
            try
            {
                PluginExecutionContext context = new PluginExecutionContext(serviceProvider);

                switch (context.PluginContext.MessageName)
                {
                case "Create":
                    ExecuteCreate(context);

                    break;

                case "Delete":
                    ExecuteDelete(context);

                    break;

                case "Retrieve":
                    ExecuteRetrieve(context);

                    break;

                case "RetrieveMultiple":
                    ExecuteRetrieveMultiple(context);

                    break;

                case "Update":
                    ExecuteUpdate(context);

                    break;

                case "Associate":
                    ExecuteAssociate(context);

                    break;

                case "Disassociate":
                    ExecuteDisassociate(context);
                    break;

                default:
                    throw new NotImplementedException($"The message: {context.PluginContext.MessageName} is not supported");
                }
            }
            catch (Exception e)
            {
                throw new InvalidPluginExecutionException(e.Message, e);
            }
        }
        private EntityCollection GetEntitiesFromSql(PluginExecutionContext context, GenericMapper mapper, string sql, int pageSize, int pageNumber)
        {
            context.Trace($"SQL: {sql}");
            EntityCollection collection = new EntityCollection();

            using (SqlConnection sqlConnection = new SqlConnection(context.GetSqlConnectionString()))
            {
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sqlConnection);
                DataSet        dataSet        = new DataSet();
                sqlConnection.Open();
                sqlDataAdapter.Fill(dataSet, "SqlData");
                sqlConnection.Close();
                context.Trace($"Records Retrieved: {dataSet.Tables[0].Rows.Count}", Array.Empty <object>());
                collection = mapper.CreateEntities(dataSet, pageSize, pageNumber);
            }

            return(collection);
        }
        /// <summary>
        /// Handles the entity retrieve message.
        /// </summary>
        /// <param name="context">The context.</param>
        public override void HandleRetrieveMessage(PluginExecutionContext context)
        {
            base.HandleRetrieveMessage(context);
            var    mapper = new GenericMapper(context);
            Entity entity = new Entity(context.PluginContext.PrimaryEntityName);

            if (mapper != null)
            {
                string sql = $"SELECT * FROM {context.PluginContext.PrimaryEntityName} WITH(NOLOCK) WHERE {mapper.PrimaryEntityMetadata.PrimaryIdAttribute} = '{mapper.MapToVirtualEntityValue(mapper.PrimaryEntityMetadata.PrimaryIdAttribute, context.PluginContext.PrimaryEntityId)}'";
                sql = mapper.MapVirtualEntityAttributes(sql);

                var entities = this.GetEntitiesFromSql(context, mapper, sql, 1, 1);
                if (entities.Entities != null && entities.Entities.Count > 0)
                {
                    entity = entities.Entities[0];
                }
            }

            // Set output parameter
            context.PluginContext.OutputParameters["BusinessEntity"] = entity;
        }
 private void ExecuteDisassociate(PluginExecutionContext context)
 {
     if (IsPreOperation(context))
     {
         HandlePreDisassociateMessage(context);
     }
     else if (IsPostOperation(context))
     {
         if (IsAsyncPlugin(context))
         {
             HandlePostDisassociateAsyncMessage(context);
         }
         else
         {
             HandlePostDisassociateMessage(context);
         }
     }
     else
     {
         ThrowMessageNotFoundException(context.PluginContext.MessageName, context.PluginContext.Stage);
     }
 }
 private void ExecuteRetrieveMultiple(PluginExecutionContext context)
 {
     if (IsPreOperation(context))
     {
         HandlePreRetrieveMultipleMessage(context);
     }
     else if (IsPostOperation(context))
     {
         if (IsAsyncPlugin(context))
         {
             HandlePostRetrieveMultipleAsyncMessage(context);
         }
         else
         {
             HandlePostRetrieveMultipleMessage(context);
         }
     }
     else
     {
         HandleRetrieveMultipleMessage(context);
     }
 }
        /// <summary>
        /// Handles the entity retrieve multiple message.
        /// </summary>
        /// <param name="context">The context.</param>
        public override void HandleRetrieveMultipleMessage(PluginExecutionContext context)
        {
            base.HandleRetrieveMultipleMessage(context);

            var query = context.PluginContext.InputParameters["Query"];

            if (query != null)
            {
                var mapper = new GenericMapper(context);

                EntityCollection collection = new EntityCollection();
                string           fetchXml   = string.Empty;
                if (query is QueryExpression qe)
                {
                    var convertRequest = new QueryExpressionToFetchXmlRequest();
                    convertRequest.Query = (QueryExpression)qe;
                    var response = (QueryExpressionToFetchXmlResponse)context.Service.Execute(convertRequest);
                    fetchXml = response.FetchXml;
                }
                else if (query is FetchExpression fe)
                {
                    fetchXml = fe.Query;
                }

                if (!string.IsNullOrEmpty(fetchXml))
                {
                    context.Trace($"Pre FetchXML: {fetchXml}");

                    var metadata = new AttributeMetadataCache(context.Service);
                    var fetch    = Deserialize(fetchXml);
                    mapper.MapFetchXml(fetch);

                    //Store page info before converting
                    int page  = -1;
                    int count = -1;
                    if (!string.IsNullOrEmpty(fetch.page))
                    {
                        page       = Int32.Parse(fetch.page);
                        fetch.page = string.Empty;
                    }

                    if (!string.IsNullOrEmpty(fetch.count))
                    {
                        count       = Int32.Parse(fetch.count);
                        fetch.count = string.Empty;
                    }

                    var sql = FetchXml2Sql.Convert(context.Service, metadata, fetch, new FetchXml2SqlOptions {
                        PreserveFetchXmlOperatorsAsFunctions = false
                    }, out _);

                    sql = mapper.MapVirtualEntityAttributes(sql);
                    context.Trace($"SQL: {sql}");

                    if (page != -1 && count != -1)
                    {
                        collection = this.GetEntitiesFromSql(context, mapper, sql, count, page);
                    }
                    else
                    {
                        collection = this.GetEntitiesFromSql(context, mapper, sql, -1, 1);
                    }
                }
                context.Trace($"Records Returned: {collection.Entities.Count}");
                context.PluginContext.OutputParameters["BusinessEntityCollection"] = collection;
            }
        }
 private bool IsAsyncPlugin(PluginExecutionContext context)
 {
     return(context.PluginContext.Mode == (int)PluginMode.Async);
 }
 private bool IsPostOperation(PluginExecutionContext context)
 {
     return(context.PluginContext.Stage == (int)PluginStages.PostOperation);
 }
 private bool IsPreOperation(PluginExecutionContext context)
 {
     return(context.PluginContext.Stage == (int)PluginStages.PreValidation || context.PluginContext.Stage == (int)PluginStages.PreOperation);
 }
 /// <summary>
 /// Handles the entity post disassociate asynchronous message.
 /// </summary>
 /// <param name="context">The plugin execution context.</param>
 public virtual void HandlePostDisassociateAsyncMessage(PluginExecutionContext context)
 {
 }
 /// <summary>
 /// Handles the entity pre disassociate message.
 /// </summary>
 /// <param name="context">The plugin execution context.</param>
 public virtual void HandlePreDisassociateMessage(PluginExecutionContext context)
 {
 }
 /// <summary>
 /// Handles the entity post update message.
 /// </summary>
 /// <param name="context">The plugin execution context.</param>
 public virtual void HandlePostUpdateMessage(PluginExecutionContext context)
 {
 }
 /// <summary>
 /// Handles the entity retrieve multiple message.
 /// </summary>
 /// <param name="context">The context.</param>
 public virtual void HandleRetrieveMultipleMessage(PluginExecutionContext context)
 {
 }
 /// <summary>
 /// Handles the entity post retrieve asynchronous message.
 /// </summary>
 /// <param name="context">The plugin execution context.</param>
 public virtual void HandlePostRetrieveAsyncMessage(PluginExecutionContext context)
 {
 }
 /// <summary>
 /// Handles the entity pre delete message.
 /// </summary>
 /// <param name="context">The plugin execution context.</param>
 public virtual void HandlePreDeleteMessage(PluginExecutionContext context)
 {
 }