<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Utilities;
using Signum.Entities.Reflection;
using Signum.Entities;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq.Expressions;
using Signum.Engine;
using Signum.Web.Controllers;

namespace Signum.Web
{
    public static class EntityInfoHelper
    {
        public static MvcHtmlString HiddenLite(this HtmlHelper helper, string name, Lite<IEntity> lite)
        {
            return helper.Hidden(name, lite.KeyLong());
        }

        public static MvcHtmlString HiddenRuntimeInfo(this HtmlHelper helper, TypeContext tc)
        {
            return helper.Hidden(tc.Compose(EntityBaseKeys.RuntimeInfo), tc.RuntimeInfo().TryToString());
        }

        public static MvcHtmlString HiddenRuntimeInfo<T, S>(this HtmlHelper helper, TypeContext<T> parent, Expression<Func<T, S>> property)
        {
            TypeContext<S> typeContext = Common.WalkExpression(parent, property);
            return helper.HiddenRuntimeInfo(typeContext);
        }
    }

    public class RuntimeInfo
    {
        public Type EntityType { get; private set; }
        public PrimaryKey? IdOrNull { get; private set; }
        public bool IsNew { get; private set; }
        public long? Ticks { get; private set; }

        public RuntimeInfo(Type type, PrimaryKey? idOrNull, bool isNew, long? ticks) 
        {
            if(type == null)
                throw new ArgumentNullException("type"); 

            this.EntityType = type;
            this.IdOrNull = idOrNull;
            this.IsNew = isNew;
            this.Ticks = ticks;
        }

        public RuntimeInfo(Lite<IEntity> lite) :
            this(lite.EntityType, lite.IdOrNull, lite.IdOrNull == null, null)
        {
        }

        public RuntimeInfo(EmbeddedEntity entity): 
            this(entity.GetType(), null, false, null)
        {
        }

        public RuntimeInfo(IEntity entity)
            : this(entity.GetType(), entity.IdOrNull, entity.IsNew,
                 entity is Entity ? ((Entity)entity).Ticks : (long?)null)
        {
        }

        public override string ToString()
        {
            if (IdOrNull != null && IsNew)
                throw new ArgumentException("Invalid RuntimeInfo parameters: IdOrNull={0} and IsNew=true".FormatWith(IdOrNull));

            if (EntityType != null && EntityType.IsLite())
                throw new ArgumentException("RuntimeInfo's RuntimeType cannot be of type Lite. Use ExtractLite or construct a RuntimeInfo<T> instead");

            return "{0};{1};{2};{3}".FormatWith(
                Navigator.ResolveWebTypeName(EntityType),
                IdOrNull.TryToString(),
                IsNew ? "n" : "o",
                Ticks
                );
        }

        public static RuntimeInfo FromFormValue(string formValue)
        {
            if(string.IsNullOrEmpty(formValue))
                return null;

            string[] parts = formValue.Split(new[] { ";" }, StringSplitOptions.None);
            if (parts.Length != 4)
                throw new ArgumentException("Incorrect sfRuntimeInfo format: {0}".FormatWith(formValue));

            string entityTypeString = parts[0];
<commit_msg>remove EntityInfo validation for New entities with Id set<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Utilities;
using Signum.Entities.Reflection;
using Signum.Entities;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq.Expressions;
using Signum.Engine;
using Signum.Web.Controllers;

namespace Signum.Web
{
    public static class EntityInfoHelper
    {
        public static MvcHtmlString HiddenLite(this HtmlHelper helper, string name, Lite<IEntity> lite)
        {
            return helper.Hidden(name, lite.KeyLong());
        }

        public static MvcHtmlString HiddenRuntimeInfo(this HtmlHelper helper, TypeContext tc)
        {
            return helper.Hidden(tc.Compose(EntityBaseKeys.RuntimeInfo), tc.RuntimeInfo().TryToString());
        }

        public static MvcHtmlString HiddenRuntimeInfo<T, S>(this HtmlHelper helper, TypeContext<T> parent, Expression<Func<T, S>> property)
        {
            TypeContext<S> typeContext = Common.WalkExpression(parent, property);
            return helper.HiddenRuntimeInfo(typeContext);
        }
    }

    public class RuntimeInfo
    {
        public Type EntityType { get; private set; }
        public PrimaryKey? IdOrNull { get; private set; }
        public bool IsNew { get; private set; }
        public long? Ticks { get; private set; }

        public RuntimeInfo(Type type, PrimaryKey? idOrNull, bool isNew, long? ticks) 
        {
            if(type == null)
                throw new ArgumentNullException("type"); 

            this.EntityType = type;
            this.IdOrNull = idOrNull;
            this.IsNew = isNew;
            this.Ticks = ticks;
        }

        public RuntimeInfo(Lite<IEntity> lite) :
            this(lite.EntityType, lite.IdOrNull, lite.IdOrNull == null, null)
        {
        }

        public RuntimeInfo(EmbeddedEntity entity): 
            this(entity.GetType(), null, false, null)
        {
        }

        public RuntimeInfo(IEntity entity)
            : this(entity.GetType(), entity.IdOrNull, entity.IsNew,
                 entity is Entity ? ((Entity)entity).Ticks : (long?)null)
        {
        }

        public override string ToString()
        {
            if (EntityType != null && EntityType.IsLite())
                throw new ArgumentException("RuntimeInfo's RuntimeType cannot be of type Lite. Use ExtractLite or construct a RuntimeInfo<T> instead");

            return "{0};{1};{2};{3}".FormatWith(
                Navigator.ResolveWebTypeName(EntityType),
                IdOrNull.TryToString(),
                IsNew ? "n" : "o",
                Ticks
                );
        }

        public static RuntimeInfo FromFormValue(string formValue)
        {
            if(string.IsNullOrEmpty(formValue))
                return null;

            string[] parts = formValue.Split(new[] { ";" }, StringSplitOptions.None);
            if (parts.Length != 4)
                throw new ArgumentException("Incorrect sfRuntimeInfo format: {0}".FormatWith(formValue));

            string entityTypeString = parts[0];

{'fraction_non_alphanumeric': 0.09826589595375723, 'fraction_numerical': 0.0023433838462740197, 'mean_word_length': 2.2513966480446927, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '956635', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1719, 'n_words': 428}