using System; using System.Linq; using System.Linq.Expressions; using Artemis.Core.DataModelExpansions; using Artemis.Core.Services; using Artemis.Storage.Entities.Profile.DataBindings; using Newtonsoft.Json; namespace Artemis.Core { /// /// Modifies a data model value in a way defined by the modifier type /// public class DataBindingModifier { private DataBinding _dataBinding; /// /// Creates a new instance of the class /// /// The type of the parameter, can either be dynamic (based on a data model value) or static public DataBindingModifier(ProfileRightSideType parameterType) { ParameterType = parameterType; Entity = new DataBindingModifierEntity(); ApplyToEntity(); } internal DataBindingModifier(DataBinding dataBinding, DataBindingModifierEntity entity) { DataBinding = dataBinding; Entity = entity; ApplyToDataBindingModifier(); } /// /// Gets the data binding this modifier is applied to /// public DataBinding DataBinding { get => _dataBinding; internal set { _dataBinding = value; CreateExpression(); } } /// /// Gets the type of modifier that is being applied /// public DataBindingModifierType ModifierType { get; private set; } /// /// Gets the type of the parameter, can either be dynamic (based on a data model value) or static /// public ProfileRightSideType ParameterType { get; private set; } /// /// Gets the position at which the modifier appears on the data binding /// public int Order { get; internal set; } /// /// Gets the currently used instance of the parameter data model /// public DataModel ParameterDataModel { get; private set; } /// /// Gets the path of the parameter property in the /// public string ParameterPropertyPath { get; private set; } /// /// Gets the parameter static value, only used it is /// /// public object ParameterStaticValue { get; private set; } /// /// A compiled expression tree that when given a matching data model returns the value of the modifiers parameter /// public Func CompiledParameterAccessor { get; set; } internal DataBindingModifierEntity Entity { get; set; } /// /// Applies the modifier to the provided value /// /// The value to apply the modifier to, should be of the same type as the data binding target /// The modified value public object Apply(object currentValue) { if (ModifierType == null) return currentValue; if (!ModifierType.SupportsParameter) return ModifierType.Apply(currentValue, null); if (ParameterType == ProfileRightSideType.Dynamic && CompiledParameterAccessor != null) { var value = CompiledParameterAccessor(ParameterDataModel); return ModifierType.Apply(currentValue, value); } if (ParameterType == ProfileRightSideType.Static) return ModifierType.Apply(currentValue, ParameterStaticValue); return currentValue; } /// /// Updates the modifier type of the modifier and re-compiles the expression /// /// public void UpdateModifierType(DataBindingModifierType modifierType) { // Calling CreateExpression will clear compiled expressions if (modifierType == null) { ModifierType = null; CreateExpression(); return; } var targetType = DataBinding.TargetProperty.PropertyType; if (!modifierType.SupportsType(targetType)) { throw new ArtemisCoreException($"Cannot apply modifier type {modifierType.GetType().Name} to this modifier because " + $"it does not support this data binding's type {targetType.Name}"); } ModifierType = modifierType; CreateExpression(); } /// /// Updates the parameter of the modifier, makes the modifier dynamic and re-compiles the expression /// /// The data model of the parameter /// The path pointing to the parameter inside the data model public void UpdateParameter(DataModel dataModel, string path) { if (dataModel != null && path == null) throw new ArtemisCoreException("If a data model is provided, a path is also required"); if (dataModel == null && path != null) throw new ArtemisCoreException("If path is provided, a data model is also required"); if (dataModel != null) { if (!dataModel.ContainsPath(path)) throw new ArtemisCoreException($"Data model of type {dataModel.GetType().Name} does not contain a property at path '{path}'"); } ParameterType = ProfileRightSideType.Dynamic; ParameterDataModel = dataModel; ParameterPropertyPath = path; CreateExpression(); } /// /// Updates the parameter of the modifier, makes the modifier static and re-compiles the expression /// /// The static value to use as a parameter public void UpdateParameter(object staticValue) { ParameterType = ProfileRightSideType.Static; ParameterDataModel = null; ParameterPropertyPath = null; var targetType = DataBinding.TargetProperty.PropertyType; // If not null ensure the types match and if not, convert it if (staticValue != null && staticValue.GetType() == targetType) ParameterStaticValue = staticValue; else if (staticValue != null) ParameterStaticValue = Convert.ChangeType(staticValue, targetType); // If null create a default instance for value types or simply make it null for reference types else if (targetType.IsValueType) ParameterStaticValue = Activator.CreateInstance(targetType); else ParameterStaticValue = null; CreateExpression(); } internal void Initialize(IDataModelService dataModelService, IDataBindingService dataBindingService) { // Modifier type if (Entity.ModifierTypePluginGuid != null && ModifierType == null) { var modifierType = dataBindingService.GetModifierType(Entity.ModifierTypePluginGuid.Value, Entity.ModifierType); if (modifierType != null) UpdateModifierType(modifierType); } // Dynamic parameter if (ParameterType == ProfileRightSideType.Dynamic && Entity.ParameterDataModelGuid != null && ParameterDataModel == null) { var dataModel = dataModelService.GetPluginDataModelByGuid(Entity.ParameterDataModelGuid.Value); if (dataModel != null && dataModel.ContainsPath(Entity.ParameterPropertyPath)) UpdateParameter(dataModel, Entity.ParameterPropertyPath); } // Static parameter else if (ParameterType == ProfileRightSideType.Static && Entity.ParameterStaticValue != null && ParameterStaticValue == null) { // Use the target type so JSON.NET has a better idea what to do var targetType = DataBinding.TargetProperty.PropertyType; object staticValue; try { staticValue = JsonConvert.DeserializeObject(Entity.ParameterStaticValue, targetType); } // If deserialization fails, use the type's default catch (JsonSerializationException e) { dataBindingService.LogModifierDeserializationFailure(this, e); staticValue = Activator.CreateInstance(targetType); } UpdateParameter(staticValue); } } internal void ApplyToEntity() { // Modifier Entity.ModifierType = ModifierType?.GetType().Name; Entity.ModifierTypePluginGuid = ModifierType?.PluginInfo.Guid; // General Entity.Order = Order; Entity.ParameterType = (int) ParameterType; // Parameter Entity.ParameterDataModelGuid = ParameterDataModel?.PluginInfo.Guid; Entity.ParameterPropertyPath = ParameterPropertyPath; Entity.ParameterStaticValue = JsonConvert.SerializeObject(ParameterStaticValue); } internal void ApplyToDataBindingModifier() { // Modifier type is done during Initialize // General Order = Entity.Order; ParameterType = (ProfileRightSideType) Entity.ParameterType; // Parameter is done during initialize } private void CreateExpression() { CompiledParameterAccessor = null; if (ModifierType == null || DataBinding == null) return; if (ParameterType == ProfileRightSideType.Dynamic && ModifierType.SupportsParameter) { if (ParameterDataModel == null) return; // If the right side value is null, the constant type cannot be inferred and must be provided based on the data binding target var parameterAccessor = CreateAccessor(ParameterDataModel, ParameterPropertyPath, "parameter", out var rightSideParameter); var lambda = Expression.Lambda>(Expression.Convert(parameterAccessor, typeof(object)), rightSideParameter); CompiledParameterAccessor = lambda.Compile(); } } private Expression CreateAccessor(DataModel dataModel, string path, string parameterName, out ParameterExpression parameter) { var listType = dataModel.GetListTypeInPath(path); if (listType != null) throw new ArtemisCoreException($"Cannot create a regular accessor at path {path} because the path contains a list"); parameter = Expression.Parameter(typeof(object), parameterName + "DataModel"); return path.Split('.').Aggregate( Expression.Convert(parameter, dataModel.GetType()), // Cast to the appropriate type Expression.Property ); } } }