<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Signum.Engine.Operations;
using Signum.Entities.Operations;
using Signum.Utilities;
using Signum.Entities;
using System.Web;
using Signum.Entities.Basics;
using Signum.Web.Extensions.Properties;

namespace Signum.Web.Operations
{
    public static class OperationClient
    {
        public static OperationManager Manager { get; private set; }

        public static void Start(OperationManager operationManager)
        {
            Manager = operationManager;

            ButtonBarHelper.GetButtonBarElement += Manager.ButtonBar_GetButtonBarElement;

            Constructor.ConstructorManager.GeneralConstructor += Manager.ConstructorManager_GeneralConstructor;

            ButtonBarHelper.GetButtonBarForQueryName += Manager.ButtonBar_GetButtonBarForQueryName;
        }
    }

    public class OperationManager
    {
        public Dictionary<Enum, WebMenuItem> Settings = new Dictionary<Enum, WebMenuItem>();

        internal List<WebMenuItem> ButtonBar_GetButtonBarElement(HttpContextBase httpContext, object entity, string mainControlUrl)
        {
            IdentifiableEntity ident = entity as IdentifiableEntity;

            if (ident == null)
                return null;

            var list = OperationLogic.ServiceGetEntityOperationInfos(ident);

            if (list == null || list.Count == 0)
                return null;

            var dic = (from oi in list
                       let os = (EntityOperationSettings)Settings.TryGetC(oi.Key)
                       where os == null || os.IsVisible == null || os.IsVisible(ident)
                       select new { OperationInfo = oi, OperationSettings = os }).ToDictionary(a => a.OperationInfo.Key);

            if (dic.Count == 0)
                return null;

            List<WebMenuItem> items = new List<WebMenuItem>();

            foreach (OperationInfo oi in list)
            {
                if (dic.ContainsKey(oi.Key))
                {
                    WebMenuItem item = new WebMenuItem
                    {
                        AltText = GetText(oi.Key, dic[oi.Key].OperationSettings),
                        Id = dic[oi.Key].OperationSettings.TryCC(os => os.Id),
                        ImgSrc = GetImage(oi.Key, dic[oi.Key].OperationSettings),
                        OnClick = dic[oi.Key].OperationSettings.TryCC(os => os.OnClick),
                        OnServerClickAjax = GetServerClickAjax(httpContext, oi, dic[oi.Key].OperationSettings, ident),
                        OnServerClickPost = GetServerClickPost(httpContext, oi, dic[oi.Key].OperationSettings, ident),
                        Enabled = dic[oi.Key].OperationInfo.CanExecute == null,
                    };
                    Dictionary<string, object> settings = dic[oi.Key].OperationSettings.TryCC(os => os.HtmlProps);
                    if (settings != null) 
                        item.HtmlProps.AddRange(settings);

                    items.Add(item);
                }
            }
            
            return items;
        }

        internal List<WebMenuItem> ButtonBar_GetButtonBarForQueryName(HttpContextBase httpContext, object queryName, Type entityType)
        {
            if (entityType == null || queryName == null)
                return null;

            var list = OperationLogic.ServiceGetQueryOperationInfos(entityType);

            if (list == null || list.Count == 0)
                return null;

            var dic = (from oi in list
                       let os = (ConstructorFromManySettings)Settings.TryGetC(oi.Key)
                       where os == null || os.IsVisible == null || os.IsVisible(queryName, oi)
                       select new {OperationInfo = oi, OperationSettings = os}).ToDictionary(a => a.OperationInfo.Key);

            if (dic.Count == 0)
                return null;

            List<WebMenuItem> items = new List<WebMenuItem>();

            foreach (OperationInfo oi in list)
            {
                if (dic.ContainsKey(oi.Key))
                {
                    WebMenuItem item = new WebMenuItem
                    {
                        AltText = GetText(oi.Key, dic[oi.Key].OperationSettings),
                        Id = dic[oi.Key].OperationSettings.TryCC(os => os.Id),
                        ImgSrc = GetImage(oi.Key, dic[oi.Key].OperationSettings),
                        OnClick = dic[oi.Key].OperationSettings.TryCC(os => os.OnClick),
                        OnServerClickAjax = GetServerClickAjax(httpContext, oi.Key, dic[oi.Key].OperationSettings, queryName, entityType),
                        OnServerClickPost = dic[oi.Key].OperationSettings.TryCC(os => os.OnServerClickPost),
                        Enabled = dic[oi.Key].OperationInfo.CanExecute == null,
                    };
                    Dictionary<string, object> settings = dic[oi.Key].OperationSettings.TryCC(os => os.HtmlProps);
                    if (settings != null)
                        item.HtmlProps.AddRange(settings);

                    items.Add(item);
                }
            }

            return items;
        }

        protected internal virtual string GetText(Enum key, WebMenuItem os)
        {
            if (os != null && os.AltText != null)
                return os.AltText;

            return key.NiceToString();
        }

        protected internal virtual string GetImage(Enum key, WebMenuItem os)
        {
            if (os != null && os.ImgSrc != null)
                return os.ImgSrc;

            return null;
        }<commit_msg>(TFS CS 4208)<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Signum.Engine.Operations;
using Signum.Entities.Operations;
using Signum.Utilities;
using Signum.Entities;
using System.Web;
using Signum.Entities.Basics;
using Signum.Web.Extensions.Properties;

namespace Signum.Web.Operations
{
    public static class OperationClient
    {
        public static OperationManager Manager { get; private set; }

        public static void Start(OperationManager operationManager)
        {
            Manager = operationManager;

            ButtonBarHelper.GetButtonBarElement += Manager.ButtonBar_GetButtonBarElement;

            Constructor.ConstructorManager.GeneralConstructor += Manager.ConstructorManager_GeneralConstructor;

            ButtonBarHelper.GetButtonBarForQueryName += Manager.ButtonBar_GetButtonBarForQueryName;
        }
    }

    public class OperationManager
    {
        public Dictionary<Enum, WebMenuItem> Settings = new Dictionary<Enum, WebMenuItem>();

        internal List<WebMenuItem> ButtonBar_GetButtonBarElement(HttpContextBase httpContext, object entity, string mainControlUrl)
        {
            IdentifiableEntity ident = entity as IdentifiableEntity;

            if (ident == null)
                return null;

            var list = OperationLogic.ServiceGetEntityOperationInfos(ident);

            if (list == null || list.Count == 0)
                return null;

            var dic = (from oi in list
                       let os = (EntityOperationSettings)Settings.TryGetC(oi.Key)
                       where os == null || os.IsVisible == null || os.IsVisible(ident)
                       select new { OperationInfo = oi, OperationSettings = os }).ToDictionary(a => a.OperationInfo.Key);

            if (dic.Count == 0)
                return null;

            List<WebMenuItem> items = new List<WebMenuItem>();

            foreach (OperationInfo oi in list)
            {
                if (dic.ContainsKey(oi.Key))
                {
                    WebMenuItem item = new WebMenuItem
                    {
                        Text = GetText(oi.Key, dic[oi.Key].OperationSettings),
                        AltText = dic[oi.Key].OperationInfo.CanExecute ?? GetText(oi.Key, dic[oi.Key].OperationSettings),
                        Id = dic[oi.Key].OperationSettings.TryCC(os => os.Id),
                        ImgSrc = GetImage(oi.Key, dic[oi.Key].OperationSettings),
                        OnClick = dic[oi.Key].OperationSettings.TryCC(os => os.OnClick),
                        OnServerClickAjax = GetServerClickAjax(httpContext, oi, dic[oi.Key].OperationSettings, ident),
                        OnServerClickPost = GetServerClickPost(httpContext, oi, dic[oi.Key].OperationSettings, ident),
                        Enabled = dic[oi.Key].OperationInfo.CanExecute == null,
                    };
                    Dictionary<string, object> settings = dic[oi.Key].OperationSettings.TryCC(os => os.HtmlProps);
                    if (settings != null) 
                        item.HtmlProps.AddRange(settings);

                    items.Add(item);
                }
            }
            
            return items;
        }

        internal List<WebMenuItem> ButtonBar_GetButtonBarForQueryName(HttpContextBase httpContext, object queryName, Type entityType)
        {
            if (entityType == null || queryName == null)
                return null;

            var list = OperationLogic.ServiceGetQueryOperationInfos(entityType);

            if (list == null || list.Count == 0)
                return null;

            var dic = (from oi in list
                       let os = (ConstructorFromManySettings)Settings.TryGetC(oi.Key)
                       where os == null || os.IsVisible == null || os.IsVisible(queryName, oi)
                       select new {OperationInfo = oi, OperationSettings = os}).ToDictionary(a => a.OperationInfo.Key);

            if (dic.Count == 0)
                return null;

            List<WebMenuItem> items = new List<WebMenuItem>();

            foreach (OperationInfo oi in list)
            {
                if (dic.ContainsKey(oi.Key))
                {
                    WebMenuItem item = new WebMenuItem
                    {
                        Text = GetText(oi.Key, dic[oi.Key].OperationSettings),
                        AltText = dic[oi.Key].OperationInfo.CanExecute ?? GetText(oi.Key, dic[oi.Key].OperationSettings),
                        Id = dic[oi.Key].OperationSettings.TryCC(os => os.Id),
                        ImgSrc = GetImage(oi.Key, dic[oi.Key].OperationSettings),
                        OnClick = dic[oi.Key].OperationSettings.TryCC(os => os.OnClick),
                        OnServerClickAjax = GetServerClickAjax(httpContext, oi.Key, dic[oi.Key].OperationSettings, queryName, entityType),
                        OnServerClickPost = dic[oi.Key].OperationSettings.TryCC(os => os.OnServerClickPost),
                        Enabled = dic[oi.Key].OperationInfo.CanExecute == null,
                    };
                    Dictionary<string, object> settings = dic[oi.Key].OperationSettings.TryCC(os => os.HtmlProps);
                    if (settings != null)
                        item.HtmlProps.AddRange(settings);

                    items.Add(item);
                }
            }

            return items;
        }

        protected internal virtual string GetText(Enum key, WebMenuItem os)
        {
            if (os.TryCC(o => o.Text) != null)
                return os.Text;

            return key.NiceToString();
        }

        protected internal virtual string GetImage(Enum key, WebMenuItem os)
        {
            if (os != null && os.ImgSrc != null)
                return os.ImgSrc;

            return null;
        }
{'fraction_non_alphanumeric': 0.09280762564991335, 'fraction_numerical': 0.0010398613518197574, 'mean_word_length': 1.7570473005255614, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 42, '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': '3865427', 'n_tokens_mistral': 2941, 'n_tokens_neox': 2779, 'n_words': 621}