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

namespace Signum.Web
{
    public abstract class EntitySettings
    {
        public virtual string WebTypeName { get; set; }
        public Func<UrlHelper, Type, int?, string> ViewRoute { get; set; }

        public abstract Type StaticType { get; }
     
        public abstract Delegate UntypedMappingLine { get; }
        public abstract Delegate UntypedMappingMain { get; }

        internal abstract bool OnIsCreable(bool isSearchEntity);
        internal abstract bool OnIsViewable();
        internal abstract bool OnIsNavigable(bool isSearchEntity);
        internal abstract bool OnIsReadonly();

        public abstract string OnPartialViewName(ModifiableEntity entity);

        protected static List<string> SaveProtectedErrors = new List<string>();

        protected static void Manager_Initializing()
        {
            throw new InvalidOperationException("EntitySettings inconsitencies: \r\n" + SaveProtectedErrors.ToString("\r\n"));
        }
    }

    public class EntitySettings<T> : EntitySettings where T : IdentifiableEntity
    {
        public override string WebTypeName
        {
            get { return TypeLogic.GetCleanName(typeof(T)); }
            set { throw new InvalidOperationException(); }
        }

        public override Type StaticType
        {
            get { return typeof(T); }
        }

        public Mapping<T> MappingLine { get; set; }
        public Mapping<T> MappingMain { get; set; }

        public EntityWhen IsCreable { get; set; }
        public bool IsViewable { get; set; }
        public EntityWhen IsNavigable { get; set; }
        public bool IsReadonly { get; set; }

        public override Delegate UntypedMappingLine { get { return MappingLine; } }
        public override Delegate UntypedMappingMain { get { return MappingMain; } }

        public Func<T, string> PartialViewName { get; set; }

        public override string OnPartialViewName(ModifiableEntity entity)
        {
            return PartialViewName((T)entity);
        }
        
        public EntitySettings(EntityType entityType)
        {
            switch (entityType)
            {
                case EntityType.SystemString:
                    AssertSaveProtected(entityType, false);
                    IsCreable = EntityWhen.Never;
                    IsViewable = false;
                    IsNavigable = EntityWhen.Never;
                    IsReadonly = true;
                    MappingMain = MappingLine = new EntityMapping<T>(false).GetValue;
                    break;
                case EntityType.System:
                    AssertSaveProtected(entityType, false);
                    IsCreable = EntityWhen.Never;
                    IsViewable = true;
                    IsNavigable = EntityWhen.Always;
                    IsReadonly = true;
                    MappingMain = MappingLine = new EntityMapping<T>(false).GetValue;
                    break;

                case EntityType.String:
                    AssertSaveProtected(entityType, true);
                    IsCreable = EntityWhen.IsSearchEntity;
                    IsViewable = false;
                    IsNavigable = EntityWhen.IsSearchEntity;
                    MappingMain = new EntityMapping<T>(true).GetValue;
                    MappingLine = new EntityMapping<T>(false).GetValue;
                    break;
                case EntityType.Shared:
                    AssertSaveProtected(entityType, true);
                    IsCreable = EntityWhen.Always;
                    IsViewable = true;
                    IsNavigable = EntityWhen.Always;
                    MappingMain = MappingLine = new EntityMapping<T>(true).GetValue;
                    break;
                case EntityType.Main:
                    AssertSaveProtected(entityType, true);
                    IsCreable = EntityWhen.IsSearchEntity;
                    IsViewable = true;
                    IsNavigable = EntityWhen.Always;
                    MappingMain = MappingLine = new EntityMapping<T>(true).GetValue;
                    break;

                case EntityType.Part:
                    AssertSaveProtected(entityType, false);
                    IsCreable = EntityWhen.IsLine;
                    IsViewable = true;
                    IsNavigable = EntityWhen.Always;
                    MappingMain = MappingLine = new EntityMapping<T>(true).GetValue;
                    break;

                case EntityType.SharedPart:
                    AssertSaveProtected(entityType, false);
                    IsCreable = EntityWhen.IsLine;
                    IsViewable = true;
                    IsNavigable = EntityWhen.Always;
                    MappingMain = MappingLine = new EntityMapping<T>(true).GetValue;
                    break;
             
                default:
                    break;
            }
        }

        void AssertSaveProtected(EntityType entityType, bool saveProtected)
        {
            var current = OperationLogic.IsSaveProtected(typeof(T));

            if (current != saveProtected)
            {
                SaveProtectedErrors.Add("{0} is {1} but is {2}save protected".Formato(typeof(T).Name, entityType, current ? "" : "NOT "));
                Navigator.Manager.Initializing -= new Action(Manager_Initializing);
                Navigator.Manager.Initializing += new Action(Manager_Initializing);
            }<commit_msg>Negocio.Entities.Caja y Negocio.Entities.Bancos con operaciones<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Entities;
using Signum.Engine;
using System.Web.Mvc;
using Signum.Engine.Maps;
using Signum.Entities.Reflection;
using Signum.Utilities;
using Signum.Engine.Basics;
using Signum.Engine.Operations;
using Signum.Utilities.ExpressionTrees;
using Signum.Web.Operations;

namespace Signum.Web
{
    public abstract class EntitySettings
    {
        public virtual string WebTypeName { get; set; }
        public Func<UrlHelper, Type, int?, string> ViewRoute { get; set; }

        public abstract Type StaticType { get; }
     
        public abstract Delegate UntypedMappingLine { get; }
        public abstract Delegate UntypedMappingMain { get; }

        internal abstract bool OnIsCreable(bool isSearchEntity);
        internal abstract bool OnIsViewable();
        internal abstract bool OnIsNavigable(bool isSearchEntity);
        internal abstract bool OnIsReadonly();

        public abstract string OnPartialViewName(ModifiableEntity entity);

        protected static List<string> SaveProtectedErrors = new List<string>();

        protected static void Manager_Initializing()
        {
            throw new InvalidOperationException("EntitySettings inconsitencies: \r\n" + SaveProtectedErrors.Order().ToString("\r\n"));
        }
    }

    public class EntitySettings<T> : EntitySettings where T : IdentifiableEntity
    {
        public override string WebTypeName
        {
            get { return TypeLogic.GetCleanName(typeof(T)); }
            set { throw new InvalidOperationException(); }
        }

        public override Type StaticType
        {
            get { return typeof(T); }
        }

        public Mapping<T> MappingLine { get; set; }
        public Mapping<T> MappingMain { get; set; }

        public EntityWhen IsCreable { get; set; }
        public bool IsViewable { get; set; }
        public EntityWhen IsNavigable { get; set; }
        public bool IsReadonly { get; set; }

        public override Delegate UntypedMappingLine { get { return MappingLine; } }
        public override Delegate UntypedMappingMain { get { return MappingMain; } }

        public Func<T, string> PartialViewName { get; set; }

        public override string OnPartialViewName(ModifiableEntity entity)
        {
            return PartialViewName((T)entity);
        }
        
        public EntitySettings(EntityType entityType)
        {
            switch (entityType)
            {
                case EntityType.SystemString:
                    AssertSaveProtected(entityType, false);
                    IsCreable = EntityWhen.Never;
                    IsViewable = false;
                    IsNavigable = EntityWhen.Never;
                    IsReadonly = true;
                    MappingMain = MappingLine = new EntityMapping<T>(false).GetValue;
                    break;
                case EntityType.System:
                    AssertSaveProtected(entityType, false);
                    IsCreable = EntityWhen.Never;
                    IsViewable = true;
                    IsNavigable = EntityWhen.Always;
                    IsReadonly = true;
                    MappingMain = MappingLine = new EntityMapping<T>(false).GetValue;
                    break;

                case EntityType.String:
                    AssertSaveProtected(entityType, true);
                    IsCreable = EntityWhen.IsSearchEntity;
                    IsViewable = false;
                    IsNavigable = EntityWhen.IsSearchEntity;
                    MappingMain = new EntityMapping<T>(true).GetValue;
                    MappingLine = new EntityMapping<T>(false).GetValue;
                    break;
                case EntityType.Shared:
                    AssertSaveProtected(entityType, true);
                    IsCreable = EntityWhen.Always;
                    IsViewable = true;
                    IsNavigable = EntityWhen.Always;
                    MappingMain = MappingLine = new EntityMapping<T>(true).GetValue;
                    break;
                case EntityType.Main:
                    AssertSaveProtected(entityType, true);
                    IsCreable = EntityWhen.IsSearchEntity;
                    IsViewable = true;
                    IsNavigable = EntityWhen.Always;
                    MappingMain = MappingLine = new EntityMapping<T>(true).GetValue;
                    break;

                case EntityType.Part:
                    AssertSaveProtected(entityType, false);
                    IsCreable = EntityWhen.IsLine;
                    IsViewable = true;
                    IsNavigable = EntityWhen.Always;
                    MappingMain = MappingLine = new EntityMapping<T>(true).GetValue;
                    break;

                case EntityType.SharedPart:
                    AssertSaveProtected(entityType, false);
                    IsCreable = EntityWhen.IsLine;
                    IsViewable = true;
                    IsNavigable = EntityWhen.Always;
                    MappingMain = MappingLine = new EntityMapping<T>(true).GetValue;
                    break;
             
                default:
                    break;
            }
        }

        void AssertSaveProtected(EntityType entityType, bool saveProtected)
        {
            var current = OperationLogic.IsSaveProtected(typeof(T));

            if (current != saveProtected)
            {
                SaveProtectedErrors.Add("{0} is {1} but is {2}save protected".Formato(typeof(T).FullName, entityType, current ? "" : "NOT "));
                Navigator.Manager.Initializing -= new Action(Manager_Initializing);
                Navigator.Manager.Initializing += new Action(Manager_Initializing);
            }
{'fraction_non_alphanumeric': 0.07401372212692968, 'fraction_numerical': 0.0005145797598627787, 'mean_word_length': 1.7658918406072106, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '5611475', 'n_tokens_mistral': 2960, 'n_tokens_neox': 2762, 'n_words': 662}