<commit_before>﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using Umbraco.Core.Models;
using Umbraco.Web.Mvc;
using Umbraco.Web.WebApi.Filters;
using Umbraco.Web.WebApi;
using System;
using System.Diagnostics;
using Umbraco.Web.Dynamics;
using Umbraco.Web.Models.TemplateQuery;

namespace Umbraco.Web.Editors
{
    

    /// <summary>
    /// The API controller used for building content queries within the template
    /// </summary>
    [PluginController("UmbracoApi")]
    [JsonCamelCaseFormatter]
    public class TemplateQueryController : UmbracoAuthorizedJsonController
    {
        public TemplateQueryController()
        { }

        public TemplateQueryController(UmbracoContext umbracoContext)
            :base(umbracoContext)
        { }


        private static readonly IEnumerable<OperathorTerm> Terms = new List<OperathorTerm>()
            {
                new OperathorTerm("is", Operathor.Equals, new [] {"string"}),
                new OperathorTerm("is not", Operathor.NotEquals, new [] {"string"}),
                new OperathorTerm("before", Operathor.LessThan, new [] {"datetime"}),
                new OperathorTerm("before (including selected date)", Operathor.LessThanEqualTo, new [] {"datetime"}),
                new OperathorTerm("after", Operathor.GreaterThan, new [] {"datetime"}),
                new OperathorTerm("after (including selected date)", Operathor.GreaterThanEqualTo, new [] {"datetime"}),
                new OperathorTerm("equals", Operathor.Equals, new [] {"int"}),
                new OperathorTerm("does not equal", Operathor.NotEquals, new [] {"int"}),
                new OperathorTerm("contains", Operathor.Contains, new [] {"string"}),
                new OperathorTerm("does not contain", Operathor.NotContains, new [] {"string"}),
                new OperathorTerm("greater than", Operathor.GreaterThan, new [] {"int"}),
                new OperathorTerm("greater than or equal to", Operathor.GreaterThanEqualTo, new [] {"int"}),
                new OperathorTerm("less than", Operathor.LessThan, new [] {"int"}),
                new OperathorTerm("less than or equal to", Operathor.LessThanEqualTo, new [] {"int"})
            };

        private static readonly IEnumerable<PropertyModel> Properties = new List<PropertyModel>()
            {
                new PropertyModel() { Name = "Id", Alias = "Id", Type = "int"  },
                new PropertyModel() { Name = "Name", Alias = "Name", Type = "string"  },
                //new PropertyModel() { Name = "Url", Alias = "url", Type = "string"  },
                new PropertyModel() { Name = "Created Date", Alias = "CreateDate", Type = "datetime"  },
                new PropertyModel() { Name = "Last Updated Date", Alias = "UpdateDate", Type = "datetime"  }

            };

        public QueryResultModel PostTemplateQuery(QueryModel model)
        {
            var umbraco = new UmbracoHelper(UmbracoContext);

            var queryResult = new QueryResultModel();

            var sb = new StringBuilder();
            var indention = Environment.NewLine + "\t\t\t\t\t\t";
      
            sb.Append("Model.Content.Site()");
            var timer = new Stopwatch();
            
            timer.Start();

            var currentPage = umbraco.TypedContentAtRoot().FirstOrDefault();
            timer.Stop();


            var pointerNode = currentPage;

            // adjust the "FROM"
            if (model != null && model.Source.Id > 0)
            {
                var targetNode = umbraco.TypedContent(model.Source.Id);

                if (targetNode != null)
                {
                    var aliases = this.GetChildContentTypeAliases(targetNode, currentPage).Reverse();

                    foreach (var contentTypeAlias in aliases)
                    {
                        timer.Start();

                        pointerNode = pointerNode.FirstChild(x => x.DocumentTypeAlias == contentTypeAlias);

                        if (pointerNode == null) break;

                        timer.Stop();

                        sb.AppendFormat(".FirstChild(\"{0}\")", contentTypeAlias);
                    }

                    if (pointerNode == null || pointerNode.Id != model.Source.Id)
                    {
                        // we did not find the path
                        sb.Clear();
                        sb.AppendFormat("Umbraco.Content({0})", model.Source.Id);
                        pointerNode = targetNode;
                    }
                }
            }
                
            // TYPE to return if filtered by type            
            IEnumerable<IPublishedContent> contents;
            if (model != null && string.IsNullOrEmpty(model.ContentType.Alias) == false)
            {
                timer.Start();

                contents = pointerNode.Children.OfTypes(new[] { model.ContentType.Alias });

                timer.Stop();
                // TODO change to .Children({0})
                sb.AppendFormat(".Children(\"{0}\")", model.ContentType.Alias);
            }
            else
            {
                timer.Start();
                contents = pointerNode.Children;
                timer.Stop();
                sb.Append(".Children");
            }

            //setup 2 clauses, 1 for returning, 1 for testing
            var clause = string.Empty;
            var tokenizedClause = string.Empty;

            // WHERE
            var token = 0;

            if (model != null)
            {
                model.Filters = model.Filters.Where(x => x.ConstraintValue != null);

                foreach (var condition in model.Filters)
                {
                    if(string.IsNullOrEmpty( condition.ConstraintValue)) continue;<commit_msg>Fixes to issues with query generation<commit_after>﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using Umbraco.Core.Models;
using Umbraco.Web.Mvc;
using Umbraco.Web.WebApi.Filters;
using Umbraco.Web.WebApi;
using System;
using System.Diagnostics;
using Umbraco.Web.Dynamics;
using Umbraco.Web.Models.TemplateQuery;

namespace Umbraco.Web.Editors
{
    

    /// <summary>
    /// The API controller used for building content queries within the template
    /// </summary>
    [PluginController("UmbracoApi")]
    [JsonCamelCaseFormatter]
    public class TemplateQueryController : UmbracoAuthorizedJsonController
    {
        public TemplateQueryController()
        { }

        public TemplateQueryController(UmbracoContext umbracoContext)
            :base(umbracoContext)
        { }


        private static readonly IEnumerable<OperathorTerm> Terms = new List<OperathorTerm>()
            {
                new OperathorTerm("is", Operathor.Equals, new [] {"string"}),
                new OperathorTerm("is not", Operathor.NotEquals, new [] {"string"}),
                new OperathorTerm("before", Operathor.LessThan, new [] {"datetime"}),
                new OperathorTerm("before (including selected date)", Operathor.LessThanEqualTo, new [] {"datetime"}),
                new OperathorTerm("after", Operathor.GreaterThan, new [] {"datetime"}),
                new OperathorTerm("after (including selected date)", Operathor.GreaterThanEqualTo, new [] {"datetime"}),
                new OperathorTerm("equals", Operathor.Equals, new [] {"int"}),
                new OperathorTerm("does not equal", Operathor.NotEquals, new [] {"int"}),
                new OperathorTerm("contains", Operathor.Contains, new [] {"string"}),
                new OperathorTerm("does not contain", Operathor.NotContains, new [] {"string"}),
                new OperathorTerm("greater than", Operathor.GreaterThan, new [] {"int"}),
                new OperathorTerm("greater than or equal to", Operathor.GreaterThanEqualTo, new [] {"int"}),
                new OperathorTerm("less than", Operathor.LessThan, new [] {"int"}),
                new OperathorTerm("less than or equal to", Operathor.LessThanEqualTo, new [] {"int"})
            };

        private static readonly IEnumerable<PropertyModel> Properties = new List<PropertyModel>()
            {
                new PropertyModel() { Name = "Id", Alias = "Id", Type = "int"  },
                new PropertyModel() { Name = "Name", Alias = "Name", Type = "string"  },
                //new PropertyModel() { Name = "Url", Alias = "url", Type = "string"  },
                new PropertyModel() { Name = "Created Date", Alias = "CreateDate", Type = "datetime"  },
                new PropertyModel() { Name = "Last Updated Date", Alias = "UpdateDate", Type = "datetime"  }

            };

        public QueryResultModel PostTemplateQuery(QueryModel model)
        {
            var umbraco = new UmbracoHelper(UmbracoContext);

            var queryResult = new QueryResultModel();

            var sb = new StringBuilder();
            var indention = Environment.NewLine + "\t\t\t\t\t\t";
      
            sb.Append("Model.Content.Site()");
            var timer = new Stopwatch();
            
            timer.Start();

            var currentPage = umbraco.TypedContentAtRoot().FirstOrDefault();
            timer.Stop();


            var pointerNode = currentPage;

            // adjust the "FROM"
            if (model != null && model.Source.Id > 0)
            {
                var targetNode = umbraco.TypedContent(model.Source.Id);

                if (targetNode != null)
                {
                    var aliases = this.GetChildContentTypeAliases(targetNode, currentPage).Reverse();

                    foreach (var contentTypeAlias in aliases)
                    {
                        timer.Start();

                        pointerNode = pointerNode.FirstChild(x => x.DocumentTypeAlias == contentTypeAlias);

                        if (pointerNode == null) break;

                        timer.Stop();

                        sb.AppendFormat(".FirstChild(\"{0}\")", contentTypeAlias);
                    }

                    if (pointerNode == null || pointerNode.Id != model.Source.Id)
                    {
                        // we did not find the path
                        sb.Clear();
                        sb.AppendFormat("Umbraco.TypedContent({0})", model.Source.Id);
                        pointerNode = targetNode;
                    }
                }
            }
                
            // TYPE to return if filtered by type            
            IEnumerable<IPublishedContent> contents;
            if (model != null && string.IsNullOrEmpty(model.ContentType.Alias) == false)
            {
                timer.Start();

                contents = pointerNode.Children.OfTypes(new[] { model.ContentType.Alias });

                timer.Stop();
                // TODO change to .Children({0})
                sb.AppendFormat(".Children(\"{0}\")", model.ContentType.Alias);
            }
            else
            {
                timer.Start();
                contents = pointerNode.Children;
                timer.Stop();
                sb.Append(".Children()");
            }

            //setup 2 clauses, 1 for returning, 1 for testing
            var clause = string.Empty;
            var tokenizedClause = string.Empty;

            // WHERE
            var token = 0;

            if (model != null)
            {
                model.Filters = model.Filters.Where(x => x.ConstraintValue != null);

                foreach (var condition in model.Filters)
                {
                    if(string.IsNullOrEmpty( condition.ConstraintValue)) continue;
{'fraction_non_alphanumeric': 0.11617685081444454, 'fraction_numerical': 0.0015513229337240368, 'mean_word_length': 1.7536782154722355, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3308387', 'n_tokens_mistral': 3023, 'n_tokens_neox': 2842, 'n_words': 696}