|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Entities.Reflection;
using System.Linq.Expressions;
using Signum.Utilities.Reflection;
using System.Reflection;
using Signum.Utilities;
using Signum.Utilities.ExpressionTrees;
using Signum.Entities.Properties;
namespace Signum.Entities.DynamicQuery
{
[Serializable]
public class EntityPropertyToken : QueryToken
{
public PropertyInfo PropertyInfo { get; private set; }
public bool IsSpecial { get; private set; }
public static QueryToken IdProperty(QueryToken parent)
{
return new EntityPropertyToken(parent, ReflectionTools.GetPropertyInfo((IdentifiableEntity e) => e.Id));
}
public static QueryToken ToStrProperty(QueryToken parent)
{
return new EntityPropertyToken(parent, ReflectionTools.GetPropertyInfo((IdentifiableEntity e) => e.ToStr));
}
internal EntityPropertyToken(QueryToken parent, PropertyInfo pi, bool isSpecial = false)
: base(parent)
{
if (pi == null)
throw new ArgumentNullException("pi");
this.PropertyInfo = pi;
this.IsSpecial = isSpecial;
}
public override Type Type
{
get { return BuildLite(PropertyInfo.PropertyType).Nullify(); }
}
public override string ToString()
{
if (IsSpecial)
return "[{0}]".Formato(PropertyInfo.NiceName());
return PropertyInfo.NiceName();
}
public override string Key
{
get { return PropertyInfo.Name; }
}
protected override Expression BuildExpressionInternal(BuildExpressionContext context)
{
var baseExpression = Parent.BuildExpression(context);
if (PropertyInfo.Is((IdentifiableEntity ident) => ident.Id) ||
PropertyInfo.Is((IdentifiableEntity ident) => ident.ToStr))
{
baseExpression = ExtractEntity(baseExpression, true);
return Expression.Property(baseExpression, PropertyInfo.Name).Nullify(); // Late binding over Lite or Identifiable
}
baseExpression = ExtractEntity(baseExpression, false);
Expression result = Expression.Property(baseExpression, PropertyInfo);
return BuildLite(result).Nullify();
}
protected override QueryToken[] SubTokensInternal()
{
if (PropertyInfo.PropertyType.UnNullify() == typeof(DateTime))
{
PropertyRoute route = this.GetPropertyRoute();
if (route != null)
{
var att = Validator.GetOrCreatePropertyPack(route.Parent.Type, route.PropertyInfo.Name).TryCC(pp =>
pp.Validators.OfType<DateTimePrecissionValidatorAttribute>().SingleOrDefault());
if (att != null)
{
return DateTimeProperties(this, att.Precision);
}
}
}
return SubTokensBase(PropertyInfo.PropertyType, Implementations());
}
public override Implementations Implementations()
{
return GetPropertyRoute().GetImplementations();
}
public override string Format
{
get { return Reflector.FormatString(this.GetPropertyRoute()); }
}
public override string Unit
{
get { return PropertyInfo.SingleAttribute<UnitAttribute>().TryCC(u => u.UnitName); }
}
public override bool IsAllowed()
{
PropertyRoute route = GetPropertyRoute();
return Parent.IsAllowed() && (route == null || route.IsAllowed());
}
public override PropertyRoute GetPropertyRoute()
{
PropertyRoute parent = Parent.GetPropertyRoute();
if (parent == null)
{
Type type = Reflector.ExtractLite(Parent.Type); //Because Parent.Type is always a lite
if (type != null)
return PropertyRoute.Root(type).Add(PropertyInfo);
return null;
}
else
{
Type type = Reflector.ExtractLite(parent.Type); //Because Add doesn't work with lites
if (type != null)
return PropertyRoute.Root(type).Add(PropertyInfo);
return parent.Add(PropertyInfo);
}
}
public override string NiceName()
{
return PropertyInfo.NiceName() + Resources.Of + Parent.ToString();
}
public override QueryToken Clone()
{
return new EntityPropertyToken(Parent.Clone(), PropertyInfo);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.
www.signumframework.com
I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...