/// <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;
        }
        /// <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;
            }
        }