<commit_before>﻿using DocumentFormat.OpenXml.Packaging;
using Signum.Engine.Basics;
using Signum.Engine.DynamicQuery;
using Signum.Engine.Maps;
using Signum.Engine.Operations;
using Signum.Entities;
using Signum.Entities.Basics;
using Signum.Entities.DynamicQuery;
using Signum.Entities.Word;
using Signum.Utilities;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Signum.Engine.UserAssets;
using Signum.Engine.Templating;
using Signum.Entities.Files;
using Signum.Utilities.DataStructures;
using DocumentFormat.OpenXml;
using W = DocumentFormat.OpenXml.Wordprocessing;
using System.Text.RegularExpressions;
using Signum.Utilities.ExpressionTrees;
using Signum.Entities.UserQueries;
using System.Data;
using Signum.Entities.Chart;
using Signum.Entities.Reflection;

namespace Signum.Engine.Word
{

    public interface IWordDataTableProvider
    {
        string Validate(string suffix, WordTemplateEntity template);
        DataTable GetDataTable(string suffix, WordTemplateLogic.WordContext context);
    }

    public static class WordTemplateLogic
    {
        public static bool AvoidSynchronize = false;

        public static ResetLazy<Dictionary<Lite<WordTemplateEntity>, WordTemplateEntity>> WordTemplatesLazy;

        public static ResetLazy<Dictionary<object, List<WordTemplateEntity>>> TemplatesByQueryName;

        public static Dictionary<WordTransformerSymbol, Action<WordContext, OpenXmlPackage>> Transformers = new Dictionary<WordTransformerSymbol, Action<WordContext, OpenXmlPackage>>();
        public static Dictionary<WordConverterSymbol, Func<WordContext, byte[], byte[]>> Converters = new Dictionary<WordConverterSymbol, Func<WordContext, byte[], byte[]>>();

        public static Dictionary<string, IWordDataTableProvider> ToDataTableProviders = new Dictionary<string, IWordDataTableProvider>();

        static Expression<Func<SystemWordTemplateEntity, IQueryable<WordTemplateEntity>>> WordTemplatesExpression =
            e => Database.Query<WordTemplateEntity>().Where(a => a.SystemWordTemplate == e);
        [ExpressionField]
        public static IQueryable<WordTemplateEntity> WordTemplates(this SystemWordTemplateEntity e)
        {
            return WordTemplatesExpression.Evaluate(e);
        }

        public static void Start(SchemaBuilder sb, DynamicQueryManager dqm)
        {
            if (sb.NotDefined(MethodInfo.GetCurrentMethod()))
            {
                sb.Include<WordTemplateEntity>()
                    .WithSave(WordTemplateOperation.Save)
                    .WithDelete(WordTemplateOperation.Delete)
                    .WithQuery(dqm, e => new
                    {
                        Entity = e,
                        e.Id,
                        e.Query,
                        e.Template.Entity.FileName
                    });

                SystemWordTemplateLogic.Start(sb, dqm);

                SymbolLogic<WordTransformerSymbol>.Start(sb, dqm, () => Transformers.Keys.ToHashSet());
                SymbolLogic<WordConverterSymbol>.Start(sb, dqm, () => Converters.Keys.ToHashSet());

                sb.Include<WordTransformerSymbol>()
                .WithQuery(dqm, f => new
                {
                    Entity = f,<commit_msg>add Name and Culture to WordTemplateEntity query<commit_after>﻿using DocumentFormat.OpenXml.Packaging;
using Signum.Engine.Basics;
using Signum.Engine.DynamicQuery;
using Signum.Engine.Maps;
using Signum.Engine.Operations;
using Signum.Entities;
using Signum.Entities.Basics;
using Signum.Entities.DynamicQuery;
using Signum.Entities.Word;
using Signum.Utilities;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Signum.Engine.UserAssets;
using Signum.Engine.Templating;
using Signum.Entities.Files;
using Signum.Utilities.DataStructures;
using DocumentFormat.OpenXml;
using W = DocumentFormat.OpenXml.Wordprocessing;
using System.Text.RegularExpressions;
using Signum.Utilities.ExpressionTrees;
using Signum.Entities.UserQueries;
using System.Data;
using Signum.Entities.Chart;
using Signum.Entities.Reflection;

namespace Signum.Engine.Word
{

    public interface IWordDataTableProvider
    {
        string Validate(string suffix, WordTemplateEntity template);
        DataTable GetDataTable(string suffix, WordTemplateLogic.WordContext context);
    }

    public static class WordTemplateLogic
    {
        public static bool AvoidSynchronize = false;

        public static ResetLazy<Dictionary<Lite<WordTemplateEntity>, WordTemplateEntity>> WordTemplatesLazy;

        public static ResetLazy<Dictionary<object, List<WordTemplateEntity>>> TemplatesByQueryName;

        public static Dictionary<WordTransformerSymbol, Action<WordContext, OpenXmlPackage>> Transformers = new Dictionary<WordTransformerSymbol, Action<WordContext, OpenXmlPackage>>();
        public static Dictionary<WordConverterSymbol, Func<WordContext, byte[], byte[]>> Converters = new Dictionary<WordConverterSymbol, Func<WordContext, byte[], byte[]>>();

        public static Dictionary<string, IWordDataTableProvider> ToDataTableProviders = new Dictionary<string, IWordDataTableProvider>();

        static Expression<Func<SystemWordTemplateEntity, IQueryable<WordTemplateEntity>>> WordTemplatesExpression =
            e => Database.Query<WordTemplateEntity>().Where(a => a.SystemWordTemplate == e);
        [ExpressionField]
        public static IQueryable<WordTemplateEntity> WordTemplates(this SystemWordTemplateEntity e)
        {
            return WordTemplatesExpression.Evaluate(e);
        }

        public static void Start(SchemaBuilder sb, DynamicQueryManager dqm)
        {
            if (sb.NotDefined(MethodInfo.GetCurrentMethod()))
            {
                sb.Include<WordTemplateEntity>()
                    .WithSave(WordTemplateOperation.Save)
                    .WithDelete(WordTemplateOperation.Delete)
                    .WithQuery(dqm, e => new
                    {
                        Entity = e,
                        e.Id,
                        e.Name,
                        e.Query,
                        e.Culture,
                        e.Template.Entity.FileName
                    });

                SystemWordTemplateLogic.Start(sb, dqm);

                SymbolLogic<WordTransformerSymbol>.Start(sb, dqm, () => Transformers.Keys.ToHashSet());
                SymbolLogic<WordConverterSymbol>.Start(sb, dqm, () => Converters.Keys.ToHashSet());

                sb.Include<WordTransformerSymbol>()
                .WithQuery(dqm, f => new
                {
                    Entity = f,
{'fraction_non_alphanumeric': 0.08812481941635365, 'fraction_numerical': 0.0, 'mean_word_length': 3.103734439834025, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 65, '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': '3742930', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1770, 'n_words': 363}