public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
        {
            var req     = request as FetchXmlToQueryExpressionRequest;
            var service = ctx.GetOrganizationService();
            FetchXmlToQueryExpressionResponse response = new FetchXmlToQueryExpressionResponse();

            response["Query"] = XrmFakedContext.TranslateFetchXmlDocumentToQueryExpression(ctx, XrmFakedContext.ParseFetchXml(req.FetchXml));;
            return(response);
        }
Пример #2
0
        public OrganizationResponse Execute(OrganizationRequest req, XrmFakedContext ctx)
        {
            var           request  = req as RetrieveMultipleRequest;
            List <Entity> list     = null;
            PagingInfo    pageInfo = null;
            int?          topCount = null;

            if (request.Query is QueryExpression)
            {
                var qe = request.Query as QueryExpression;

                pageInfo = qe.PageInfo;
                topCount = qe.TopCount;

                //need to request 1 extra to get a fill if there are more records
                if (topCount != null)
                {
                    qe.TopCount = topCount + 1;
                }


                if (qe.PageInfo.Count > 0)
                {
                    qe.TopCount = qe.PageInfo.Count + 1;
                }

                var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, request.Query as QueryExpression);
                list = linqQuery.ToList();
            }
            else if (request.Query is FetchExpression)
            {
                var fetchXml        = (request.Query as FetchExpression).Query;
                var xmlDoc          = XrmFakedContext.ParseFetchXml(fetchXml);
                var queryExpression = XrmFakedContext.TranslateFetchXmlDocumentToQueryExpression(ctx, xmlDoc);

                pageInfo = queryExpression.PageInfo;
                topCount = queryExpression.TopCount;

                //need to request 1 extra to get a fill if there are more records
                if (topCount != null)
                {
                    queryExpression.TopCount = topCount + 1;
                }
                if (queryExpression.PageInfo.Count > 0)
                {
                    queryExpression.TopCount = queryExpression.PageInfo.Count + 1;
                }

                var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, queryExpression);
                list = linqQuery.ToList();

                if (xmlDoc.IsAggregateFetchXml())
                {
                    list = XrmFakedContext.ProcessAggregateFetchXml(ctx, xmlDoc, list);
                }
            }
            else if (request.Query is QueryByAttribute)
            {
                //We instantiate a QueryExpression to be executed as we have the implementation done already
                var query = request.Query as QueryByAttribute;
                var qe    = new QueryExpression(query.EntityName);

                qe.ColumnSet = query.ColumnSet;
                qe.Criteria  = new FilterExpression();
                for (var i = 0; i < query.Attributes.Count; i++)
                {
                    qe.Criteria.AddCondition(new ConditionExpression(query.Attributes[i], ConditionOperator.Equal, query.Values[i]));
                }

                pageInfo = qe.PageInfo;
                topCount = qe.TopCount;

                //need to request 1 extra to be able check if there are more records
                if (topCount != null)
                {
                    qe.TopCount = topCount + 1;
                }
                if (qe.PageInfo.Count > 0)
                {
                    qe.TopCount = qe.PageInfo.Count + 1;
                }


                //QueryExpression now done... execute it!
                var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe as QueryExpression);
                list = linqQuery.ToList();
            }
            else
            {
                throw PullRequestException.NotImplementedOrganizationRequest(request.Query.GetType());
            }


            list.ForEach(e => e.ApplyDateBehaviour(ctx));
            list.ForEach(e => PopulateFormattedValues(e));
            var recordCount = list.Count();
            var pageSize    = recordCount;
            int pageNumber  = 1;

            if (pageInfo != null && pageInfo.PageNumber > 0 && pageInfo.Count > 0)
            {
                pageSize   = pageInfo.Count;
                pageNumber = pageInfo.PageNumber;
            }
            else if (topCount != null)
            {
                pageSize = topCount.Value;
            }

            var response = new RetrieveMultipleResponse
            {
                Results = new ParameterCollection
                {
                    { "EntityCollection", new EntityCollection(list.Take(pageSize).ToList()) }
                }
            };

            response.EntityCollection.MoreRecords = recordCount > pageSize;
            if (response.EntityCollection.MoreRecords)
            {
                var first = response.EntityCollection.Entities.First();
                var last  = response.EntityCollection.Entities.Last();
                response.EntityCollection.PagingCookie = String.Format(
                    "<cookie page=\"{0}\"><{1}id last=\"{2}\" first=\"{3}\" /></cookie>",
                    pageNumber, first.LogicalName, last.Id.ToString("B").ToUpper(), first.Id.ToString("B").ToUpper());
            }

            return(response);
        }
Пример #3
0
        public OrganizationResponse Execute(OrganizationRequest req, XrmFakedContext ctx)
        {
            var             request  = req as RetrieveMultipleRequest;
            List <Entity>   list     = null;
            PagingInfo      pageInfo = null;
            QueryExpression qe;

            string entityName = null;

            if (request.Query is QueryExpression)
            {
                qe         = (request.Query as QueryExpression).Clone();
                entityName = qe.EntityName;

                var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe);
                list = linqQuery.ToList();
            }
            else if (request.Query is FetchExpression)
            {
                var fetchXml = (request.Query as FetchExpression).Query;
                var xmlDoc   = XrmFakedContext.ParseFetchXml(fetchXml);
                qe         = XrmFakedContext.TranslateFetchXmlDocumentToQueryExpression(ctx, xmlDoc);
                entityName = qe.EntityName;

                var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe);
                list = linqQuery.ToList();

                if (xmlDoc.IsAggregateFetchXml())
                {
                    list = XrmFakedContext.ProcessAggregateFetchXml(ctx, xmlDoc, list);
                }
            }
            else if (request.Query is QueryByAttribute)
            {
                // We instantiate a QueryExpression to be executed as we have the implementation done already
                var query = request.Query as QueryByAttribute;
                qe         = new QueryExpression(query.EntityName);
                entityName = qe.EntityName;

                qe.ColumnSet = query.ColumnSet;
                qe.Criteria  = new FilterExpression();
                for (var i = 0; i < query.Attributes.Count; i++)
                {
                    qe.Criteria.AddCondition(new ConditionExpression(query.Attributes[i], ConditionOperator.Equal, query.Values[i]));
                }

                qe.PageInfo = query.PageInfo;

                // QueryExpression now done... execute it!
                var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe);
                list = linqQuery.ToList();
            }
            else
            {
                throw PullRequestException.NotImplementedOrganizationRequest(request.Query.GetType());
            }

            if (qe.Distinct)
            {
                list = GetDistinctEntities(list);
            }

            // Handle the top count before taking paging into account
            if (qe.TopCount != null && qe.TopCount.Value < list.Count)
            {
                list = list.Take(qe.TopCount.Value).ToList();
            }

            // Handle TotalRecordCount here?
            int totalRecordCount = -1;

            if (qe?.PageInfo?.ReturnTotalRecordCount == true)
            {
                totalRecordCount = list.Count;
            }

            // Handle paging
            var pageSize = ctx.MaxRetrieveCount;

            pageInfo = qe.PageInfo;
            int pageNumber = 1;

            if (pageInfo != null && pageInfo.PageNumber > 0)
            {
                pageNumber = pageInfo.PageNumber;
                pageSize   = pageInfo.Count == 0 ? ctx.MaxRetrieveCount : pageInfo.Count;
            }

            // Figure out where in the list we need to start and how many items we need to grab
            int numberToGet   = pageSize;
            int startPosition = 0;

            if (pageNumber != 1)
            {
                startPosition = (pageNumber - 1) * pageSize;
            }

            if (list.Count < pageSize)
            {
                numberToGet = list.Count;
            }
            else if (list.Count - pageSize * (pageNumber - 1) < pageSize)
            {
                numberToGet = list.Count - (pageSize * (pageNumber - 1));
            }

            var recordsToReturn = startPosition + numberToGet > list.Count ? new List <Entity>() : list.GetRange(startPosition, numberToGet);

            recordsToReturn.ForEach(e => e.ApplyDateBehaviour(ctx));
            recordsToReturn.ForEach(e => PopulateFormattedValues(e));

            var response = new RetrieveMultipleResponse
            {
                Results = new ParameterCollection
                {
                    { "EntityCollection", new EntityCollection(recordsToReturn) }
                }
            };

            response.EntityCollection.EntityName       = entityName;
            response.EntityCollection.MoreRecords      = (list.Count - pageSize * pageNumber) > 0;
            response.EntityCollection.TotalRecordCount = totalRecordCount;

            if (response.EntityCollection.MoreRecords)
            {
                var first = response.EntityCollection.Entities.First();
                var last  = response.EntityCollection.Entities.Last();
                response.EntityCollection.PagingCookie = $"<cookie page=\"{pageNumber}\"><{first.LogicalName}id last=\"{last.Id.ToString("B").ToUpper()}\" first=\"{first.Id.ToString("B").ToUpper()}\" /></cookie>";
            }

            return(response);
        }
Пример #4
0
        public OrganizationResponse Execute(OrganizationRequest req, XrmFakedContext ctx)
        {
            var request = req as RetrieveMultipleRequest;

            if (request.Query is QueryExpression)
            {
                var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, request.Query as QueryExpression);
                var list      = linqQuery.ToList();
                list.ForEach(e => PopulateFormattedValues(e));

                var response = new RetrieveMultipleResponse
                {
                    Results = new ParameterCollection
                    {
                        { "EntityCollection", new EntityCollection(list) }
                    }
                };
                return(response);
            }
            else if (request.Query is FetchExpression)
            {
                var fetchXml        = (request.Query as FetchExpression).Query;
                var xmlDoc          = XrmFakedContext.ParseFetchXml(fetchXml);
                var queryExpression = XrmFakedContext.TranslateFetchXmlDocumentToQueryExpression(ctx, xmlDoc);

                var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, queryExpression);
                var list      = linqQuery.ToList();

                if (xmlDoc.IsAggregateFetchXml())
                {
                    list = XrmFakedContext.ProcessAggregateFetchXml(ctx, xmlDoc, list);
                }
                list.ForEach(e => PopulateFormattedValues(e));

                var response = new RetrieveMultipleResponse
                {
                    Results = new ParameterCollection
                    {
                        { "EntityCollection", new EntityCollection(list) }
                    }
                };
                return(response);
            }
            else if (request.Query is QueryByAttribute)
            {
                //We instantiate a QueryExpression to be executed as we have the implementation done already
                var query = request.Query as QueryByAttribute;
                var qe    = new QueryExpression(query.EntityName);

                qe.ColumnSet = query.ColumnSet;
                qe.Criteria  = new FilterExpression();
                for (var i = 0; i < query.Attributes.Count; i++)
                {
                    qe.Criteria.AddCondition(new ConditionExpression(query.Attributes[i], ConditionOperator.Equal, query.Values[i]));
                }

                //QueryExpression now done... execute it!
                var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe as QueryExpression);
                var list      = linqQuery.ToList();
                list.ForEach(e => PopulateFormattedValues(e));

                var response = new RetrieveMultipleResponse
                {
                    Results = new ParameterCollection
                    {
                        { "EntityCollection", new EntityCollection(list) }
                    }
                };
                return(response);
            }
            else
            {
                throw PullRequestException.NotImplementedOrganizationRequest(request.Query.GetType());
            }
        }