Exemplo n.º 1
0
        private static Func <MyCustomSingleData, bool> GetExpression(object obj, string criteria)
        {
            var keyCriteria = GetKeyAndCriteria(criteria);
            var interpreter = FormulaInstanceInternalHelper.GetExpressionDelegate();

            return(interpreter.GetDelegate <Func <MyCustomSingleData, bool> >(keyCriteria.Item2, keyCriteria.Item1));
        }
Exemplo n.º 2
0
        private FunctionResult CalculateFormula(string expression, DP_DataRepository mainDataItem, DR_Requester requester, List <int> usedFormulaIDs = null)
        {
            FunctionResult result = new FunctionResult();

            //result.FormulaUsageParemeters = FormulaUsageParemeters;
            //FormulaInstance formulaInstance = null;
            try
            {
                var target = FormulaInstanceInternalHelper.GetExpressionEvaluator(mainDataItem, requester, false, usedFormulaIDs);

                result.Result = target.Calculate(expression);

                //formulaInstance = new FormulaInstance(mainDataItem, requester, usedFormulaIDs);
                //formulaInstance.PropertyGetCalled += FormulaInstance_PropertyGetCalled;
                //var instanceResult = formulaInstance.CalculateExpression(expression);
                //if (formulaInstance.Exceptions.Any())
                //    throw new Exception("instance Error");
                //else
                //    result.Result = instanceResult;
                //formulaInstance.PropertyGetCalled -= FormulaInstance_PropertyGetCalled;
            }
            catch (Exception ex)
            {
                result.Exception = new Exception("خطا در محاسبه فرمول" + Environment.NewLine + ex.Message);
            }
            return(result);
        }
        private Func <MyCustomSingleData, Int64> GetExpressionInt64(string criteria)
        {
            var keyCriteria = GetKeyAndCriteria(criteria);
            var interpreter = FormulaInstanceInternalHelper.GetExpressionDelegate(EntityID, Requester);

            return(interpreter.GetDelegate <Func <MyCustomSingleData, Int64> >(keyCriteria.Item2, keyCriteria.Item1));
        }
Exemplo n.º 4
0
        private void SetMainFormulaObject(DP_DataRepository mainDataItem)
        {
            MainFormulaObject = GetMainFormulaObject(mainDataItem);
            var entity     = bizTableDrivedEntity.GetPermissionedEntity(Requester, MainFormulaObject.DataItem.TargetEntityID);
            var properties = FormulaInstanceInternalHelper.GetProperties(entity, null, false, true);

            MainFormulaObject.SetProperties(properties);
            MainFormulaObject.PropertyGetCalled += BindableTypeDescriptor_PropertyGetCalled;
        }
        private void BindableTypeDescriptor_PropertyGetCalled(object sender, PropertyGetArg e)
        {
            if (e.PropertyInfo.ValueSearched == false)
            {
                if (e.PropertyInfo.PropertyType == PropertyType.Relationship)
                {
                    //روابط به طور پیش فرض موقع ساخته شدن چه تعریف فرمول و چه محاسبه مقدار نمیگیرند و اینجا مقدار میگیرند
                    e.PropertyInfo.ValueSearched = true;

                    var newObject = FormulaInstanceInternalHelper.GetNewFormulaObject(e.PropertyInfo);
                    newObject.DataItem           = new DP_DataRepository(e.PropertyInfo.PropertyRelationship.EntityID2, e.PropertyInfo.PropertyRelationship.Entity2);
                    newObject.PropertyGetCalled += BindableTypeDescriptor_PropertyGetCalled;
                    //newObject.PropertySetChanged += FormulaObject_PropertySetChanged;
                    //newObject.PropertyChanged += FormulaObject_PropertyChanged;
                    var entity = bizTableDrivedEntity.GetPermissionedEntity(Requester, newObject.DataItem.TargetEntityID);

                    if (e.PropertyInfo.PropertyRelationship.TypeEnum == Enum_RelationshipType.OneToMany)
                    {
                        var list = FormulaInstanceInternalHelper.GetNewFormulaObjectList(e.PropertyInfo);
                        list.Add(newObject);
                        e.PropertyInfo.Value = list;


                        foreach (CustomObject item in (e.PropertyInfo.Value as IList))
                        {  //بهتر نوشته شود.برای لیست لازم نیست هر دفعه خصوصیات خوانده شوند
                            if (item.PropertiesLoaded == false)
                            {
                                var properties = FormulaInstanceInternalHelper.GetProperties(entity, e.PropertyInfo, true, false);
                                item.SetProperties(properties);
                            }
                        }
                    }
                    else
                    {
                        e.PropertyInfo.Value = newObject;

                        if ((e.PropertyInfo.Value as CustomObject).PropertiesLoaded == false)
                        {
                            var properties = FormulaInstanceInternalHelper.GetProperties(entity, e.PropertyInfo, true, false);
                            (e.PropertyInfo.Value as CustomObject).SetProperties(properties);
                        }
                    }
                }
                else
                {
                    throw new Exception("Sfsdf");
                }
            }
            //برای همه پراپرتی ها صدا زده میشود که در قسمت ساختن درخت خصوصیات استفاده میشود
            if (PropertyGetCalled != null)
            {
                PropertyGetCalled(sender, e);
            }
        }
        private void SetMainFormulaObject(DP_DataRepository mainDataItem, TableDrivedEntityDTO mainEntity)
        {
            MainFormulaObject = GetMainFormulaObject(mainDataItem);

            var properties = FormulaInstanceInternalHelper.GetProperties(mainEntity, null, true, true);

            MainFormulaObject.SetProperties(properties);
            MainFormulaObject.PropertyGetCalled += BindableTypeDescriptor_PropertyGetCalled;
            //MainFormulaObject.PropertySetChanged += FormulaObject_PropertySetChanged;
            //MainFormulaObject.PropertyChanged += FormulaObject_PropertyChanged;
        }
        public FormulaObject(DP_DataRepository dataItem, DR_Requester requester, bool isDefinitionFormulaObject = false)
        {
            Requester = requester;
            bool getFromDB = false;

            if (isDefinitionFormulaObject == false)
            {
                if (!dataItem.IsNewItem && MyDataHelper.DataItemPrimaryKeysHaveValue(dataItem) && !MyDataHelper.DataItemNonPrimaryKeysHaveValues(dataItem))
                {
                    getFromDB = true;
                }
            }
            if (getFromDB)
            {
                SearchRequestManager searchProcessor = new SearchRequestManager();
                DP_SearchRepository  searchDataItem  = new DP_SearchRepository(dataItem.TargetEntityID);
                foreach (var property in dataItem.GetProperties())
                {
                    searchDataItem.Phrases.Add(new SearchProperty()
                    {
                        ColumnID = property.ColumnID, Value = property.Value
                    });
                }

                //سکوریتی داده اعمال میشود
                //یعنی ممکن است به خود داده دسترسی نداشته باشد و یا حتی به بعضی از فیلدها و روابط
                DR_SearchFullDataRequest request = new DR_SearchFullDataRequest(Requester, searchDataItem);
                var result = searchProcessor.Process(request);
                if (result.Result == Enum_DR_ResultType.SeccessfullyDone)
                {
                    dataItem = result.ResultDataItems.FirstOrDefault(); // searchProcessor.GetDataItemsByListOFSearchProperties(Requester, searchDataItem).FirstOrDefault();
                }
                else if (result.Result == Enum_DR_ResultType.ExceptionThrown)
                {
                    throw (new Exception(result.Message));
                }
            }
            DataItem = dataItem;
            this.PropertyGetCalled   += FormulaObject_PropertyGetCalled;
            IsDefinitionFormulaObject = isDefinitionFormulaObject;

            var entity     = bizTableDrivedEntity.GetPermissionedEntity(Requester, DataItem.TargetEntityID);
            var properties = FormulaInstanceInternalHelper.GetProperties(entity, null, false, true);

            SetProperties(properties);
        }
Exemplo n.º 8
0
        private void BindableTypeDescriptor_PropertyGetCalled(object sender, PropertyGetArg e)
        {
            if (e.PropertyInfo.ValueSearched == false)
            {
                if (e.PropertyInfo.PropertyType == PropertyType.Relationship)
                {
                    List <DP_DataRepository> relatedDataItems = GetRelatedDataItems((sender as CustomObject).DataItem, e.PropertyInfo.PropertyRelationship, e.PropertyInfo.PropertyRelationshipProperties);
                    if (e.PropertyInfo.PropertyRelationship.TypeEnum == Enum_RelationshipType.OneToMany)
                    {
                        var list   = FormulaInstanceInternalHelper.GetNewFormulaObjectList(e.PropertyInfo);
                        var entity = bizTableDrivedEntity.GetPermissionedEntity(Requester, e.PropertyInfo.PropertyRelationship.EntityID2);

                        foreach (var relatedDataItem in relatedDataItems)
                        {
                            var newObject = FormulaInstanceInternalHelper.GetNewFormulaObject(e.PropertyInfo);
                            newObject.DataItem = relatedDataItem;
                            list.Add(newObject);
                            newObject.PropertyGetCalled += BindableTypeDescriptor_PropertyGetCalled;
                            //newObject.PropertySetChanged += FormulaObject_PropertySetChanged;
                            //newObject.PropertyChanged += FormulaObject_PropertyChanged;
                            //////newObject.PropertySetChanged += (sender1, e1) => NewObject_PropertySetChanged(sender1, e1, sender as FormulaObject);
                            //////newObject.PropertyGetCalled += (sender1, e1) => NewObject_PropertyGetCalled(sender1, e1, sender as FormulaObject);
                        }
                        e.PropertyInfo.Value = list;
                        var properties = FormulaInstanceInternalHelper.GetProperties(entity, e.PropertyInfo, false);

                        foreach (CustomObject item in (e.PropertyInfo.Value as IList))
                        {  //بهتر نوشته شود.برای لیست لازم نیست هر دفعه خصوصیات خوانده شوند
                            if (item.PropertiesLoaded == false)
                            {
                                item.SetProperties(properties);
                            }
                        }
                    }
                    else if (relatedDataItems.Any())
                    {
                        var entity     = bizTableDrivedEntity.GetPermissionedEntity(Requester, e.PropertyInfo.PropertyRelationship.EntityID2);
                        var properties = FormulaInstanceInternalHelper.GetProperties(entity, e.PropertyInfo, false);
                        var newObject  = FormulaInstanceInternalHelper.GetNewFormulaObject(e.PropertyInfo);
                        newObject.DataItem = relatedDataItems.First();

                        newObject.PropertyGetCalled += BindableTypeDescriptor_PropertyGetCalled;
                        //newObject.PropertySetChanged += FormulaObject_PropertySetChanged;
                        //newObject.PropertyChanged += FormulaObject_PropertyChanged;
                        e.PropertyInfo.Value = newObject;

                        if ((e.PropertyInfo.Value as CustomObject).PropertiesLoaded == false)
                        {
                            (e.PropertyInfo.Value as CustomObject).SetProperties(properties);
                        }
                    }
                    e.PropertyInfo.ValueSearched = true;
                }
                else if (e.PropertyInfo.PropertyType == PropertyType.Column)
                {
                    e.PropertyInfo.ValueSearched = true;
                    EntityInstanceProperty property = (sender as CustomObject).DataItem.GetProperty(e.PropertyInfo.ID);
                    if (property != null)
                    {
                        e.PropertyInfo.Value = property.Value;
                    }
                }
                else if (e.PropertyInfo.PropertyType == PropertyType.FormulaParameter)
                {
                    if (UsedFormulaIDs != null && UsedFormulaIDs.Contains(e.PropertyInfo.ParameterFormulaID))
                    {
                        AddException("Loop");
                    }
                    UsedFormulaIDs.Add(e.PropertyInfo.ParameterFormulaID);
                    Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        e.PropertyInfo.ValueSearched = true;
                        var value = FormulaFunctionHandler.CalculateFormula(e.PropertyInfo.ParameterFormulaID, (sender as CustomObject).DataItem, Requester, false, UsedFormulaIDs);
                        if (string.IsNullOrEmpty(value.Exception))
                        {
                            //if ((e.PropertyInfo.Context is FormulaDTO) && (e.PropertyInfo.Context as FormulaDTO).ValueCustomType != ValueCustomType.None)
                            //    e.PropertyInfo.Value = GetCustomTypePropertyValue(e.PropertyInfo, (e.PropertyInfo.Context as FormulaDTO).ValueCustomType, value.Result);
                            //else
                            e.PropertyInfo.Value     = value.Result;
                            e.FormulaUsageParemeters = value.FormulaUsageParemeters;
                        }
                        else
                        {
                            AddException(value.Exception);
                        }
                    });
                }
                else if (e.PropertyInfo.PropertyType == PropertyType.State)
                {
                    Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        DP_DataRepository dataItem   = (sender as CustomObject).DataItem;
                        e.PropertyInfo.ValueSearched = true;
                        var result = StateHandler.GetStateResult(e.PropertyInfo.ID, (sender as CustomObject).DataItem, Requester);
                        if (string.IsNullOrEmpty(result.Message))
                        {
                            e.PropertyInfo.Value = result.Result;
                        }
                        else
                        {
                            AddException(result.Message);
                        }
                    });
                }
                else if (e.PropertyInfo.PropertyType == PropertyType.Code)
                {
                    Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        DP_DataRepository dataItem   = (sender as CustomObject).DataItem;
                        e.PropertyInfo.ValueSearched = true;
                        var result = CodeFunctionHandler.GetCodeFunctionResult(Requester, e.PropertyInfo.ID, dataItem);
                        if (result.ExecutionException == null)
                        {
                            e.PropertyInfo.Value = result.Result;
                        }
                        else
                        {
                            AddException(result.ExecutionException.Message);
                            e.PropertyInfo.Value = "";
                        }

                        //if ((e.PropertyInfo.Context is CodeFunctionDTO) && (e.PropertyInfo.Context as CodeFunctionDTO).ValueCustomType != ValueCustomType.None)
                        //    e.PropertyInfo.Value = GetCustomTypePropertyValue(e.PropertyInfo, (e.PropertyInfo.Context as CodeFunctionDTO).ValueCustomType, result.Result);
                        //else
                    });
                }
            }
            if (PropertyGetCalled != null)
            {
                PropertyGetCalled(sender, e);
            }
        }
Exemplo n.º 9
0
 //FormulaDTO Formula { set; get; }
 public I_ExpressionEvaluator GetExpressionEvaluator(DP_DataRepository dataItem, DR_Requester requester, bool definition, List <int> usedFormulaIDs = null)
 {
     return(FormulaInstanceInternalHelper.GetExpressionEvaluator(dataItem, requester, definition, usedFormulaIDs));
 }
        public override bool TryGetMember(GetMemberBinder binder,
                                          out object result)
        {
            if (m_properties.ContainsKey(binder.Name))
            {
                var property = m_properties.FirstOrDefault(x => x.Key == binder.Name).Value;
                OnProperyCalled(property);
                if (!property.ValueSearched)
                {
                    property.ValueSearched = true;
                    if (Definition)
                    {
                        if (property.PropertyType == PropertyType.Relationship)
                        {
                            var dataItem   = new DP_DataRepository(property.PropertyRelationship.EntityID2, property.PropertyRelationship.Entity2);
                            var entity     = bizTableDrivedEntity.GetPermissionedEntity(Requester, dataItem.TargetEntityID);
                            var properties = FormulaInstanceInternalHelper.GetProperties(Requester, entity, property, Definition);


                            //newObject.PropertyGetCalled += BindableTypeDescriptor_PropertyGetCalled;
                            //newObject.PropertySetChanged += FormulaObject_PropertySetChanged;
                            //newObject.PropertyChanged += FormulaObject_PropertyChanged;
                            if (property.PropertyRelationship.TypeEnum == Enum_RelationshipType.OneToMany)
                            {
                                var newObject = GetCustomSingleData(property, dataItem, Requester, Definition, properties, UsedFormulaIDs);
                                //var list = new List<MyCustomSingleData>();
                                //DataItems.Add(newObject);
                                property.Value = new MyCustomMultipleData(Requester, entity.ID, new List <MyCustomSingleData>()
                                {
                                    newObject
                                });
                            }
                            else
                            {
                                var newObject = GetCustomSingleData(property, dataItem, Requester, Definition, properties, UsedFormulaIDs);
                                property.Value = newObject;
                            }
                        }
                        else
                        {
                            property.Value = GetPropertyDefaultValue(property);
                        }
                    }
                    else
                    {
                        if (property.PropertyType == PropertyType.Relationship)
                        {
                            var entity     = bizTableDrivedEntity.GetPermissionedEntity(Requester, property.PropertyRelationship.EntityID2);
                            var properties = FormulaInstanceInternalHelper.GetProperties(Requester, entity, property, Definition);
                            if (property.PropertyRelationshipProperties == null || property.PropertyRelationshipProperties.Count == 0)
                            {
                                throw new Exception("adasdasd");
                            }
                            List <DP_DataRepository> relatedDataItems = GetRelatedDataItems(DataItem, property.PropertyRelationship, property.PropertyRelationshipProperties);

                            if (property.PropertyRelationship.TypeEnum == Enum_RelationshipType.OneToMany)
                            {
                                var list = new List <MyCustomSingleData>();

                                foreach (var relatedDataItem in relatedDataItems)
                                {
                                    list.Add(GetCustomSingleData(property, relatedDataItem, Requester, Definition, GetCloneProperties(properties), UsedFormulaIDs));
                                }
                                var newObject = new MyCustomMultipleData(Requester, entity.ID, list);
                                //    DataItems.Add(newObject);
                                //    //newObject.PropertyGetCalled += BindableTypeDescriptor_PropertyGetCalled;
                                //}
                                property.Value = newObject;
                            }
                            else if (relatedDataItems.Any())
                            {
                                var newObject = GetCustomSingleData(property, relatedDataItems.First(), Requester, Definition, properties, UsedFormulaIDs);

                                //newObject.PropertyGetCalled += BindableTypeDescriptor_PropertyGetCalled;
                                //newObject.PropertySetChanged += FormulaObject_PropertySetChanged;
                                //newObject.PropertyChanged += FormulaObject_PropertyChanged;
                                property.Value = newObject;
                            }
                        }
                        else
                        {
                            if (property.PropertyType == PropertyType.Column)
                            {
                                EntityInstanceProperty dataproperty = DataItem.GetProperty(property.ID);
                                if (dataproperty != null)
                                {
                                    property.Value = dataproperty.Value;
                                }
                                else
                                {
                                    throw new Exception("Date property" + " " + property.Name + " not found!");
                                }
                            }
                            else if (property.PropertyType == PropertyType.FormulaParameter)
                            {
                                var formula = property.Context as FormulaDTO;
                                if (UsedFormulaIDs != null && UsedFormulaIDs.Contains(formula.ID))
                                {
                                    throw new Exception("Loop");
                                }
                                if (UsedFormulaIDs == null)
                                {
                                    UsedFormulaIDs = new List <int>();
                                }
                                UsedFormulaIDs.Add(formula.ID);
                                Application.Current.Dispatcher.Invoke((Action) delegate
                                {
                                    var value = FormulaFunctionHandler.CalculateFormula(formula.ID, DataItem, Requester, UsedFormulaIDs);
                                    if (value.Exception == null)
                                    {
                                        //if ((property.Context is FormulaDTO) && (property.Context as FormulaDTO).ValueCustomType != ValueCustomType.None)
                                        //    property.Value = GetCustomTypePropertyValue(property, (property.Context as FormulaDTO).ValueCustomType, value.Result);
                                        //else
                                        property.Value = value.Result;
                                        //   e.FormulaUsageParemeters = value.FormulaUsageParemeters;
                                    }
                                    else
                                    {
                                        throw value.Exception;
                                    }
                                });
                            }
                            else if (property.PropertyType == PropertyType.State)
                            {
                                Application.Current.Dispatcher.Invoke((Action) delegate
                                {
                                    DP_DataRepository dataItem = DataItem;

                                    var stateresult = StateHandler.GetStateResult(property.ID, DataItem, Requester);
                                    if (string.IsNullOrEmpty(stateresult.Message))
                                    {
                                        property.Value = stateresult.Result;
                                    }
                                    else
                                    {
                                        throw new Exception(stateresult.Message);
                                    }
                                });
                            }
                            else if (property.PropertyType == PropertyType.Code)
                            {
                                Application.Current.Dispatcher.Invoke((Action) delegate
                                {
                                    DP_DataRepository dataItem = DataItem;

                                    var coderesult = CodeFunctionHandler.GetCodeFunctionResult(Requester, property.ID, dataItem);
                                    if (coderesult.Exception == null)
                                    {
                                        property.Value = coderesult.Result;
                                    }
                                    else
                                    {
                                        throw coderesult.Exception;
                                    }

                                    //if ((property.Context is CodeFunctionDTO) && (property.Context as CodeFunctionDTO).ValueCustomType != ValueCustomType.None)
                                    //    property.Value = GetCustomTypePropertyValue(property, (property.Context as CodeFunctionDTO).ValueCustomType, result.Result);
                                    //else
                                });
                            }
                            else if (property.PropertyType == PropertyType.DBFunction)
                            {
                                Application.Current.Dispatcher.Invoke((Action) delegate
                                {
                                    DP_DataRepository dataItem = DataItem;

                                    var coderesult = DatabaseFunctionHandler.GetDatabaseFunctionValue(Requester, property.ID, dataItem);
                                    if (coderesult.Exception == null)
                                    {
                                        property.Value = coderesult.Result;
                                    }
                                    else
                                    {
                                        throw coderesult.Exception;
                                    }

                                    //if ((property.Context is CodeFunctionDTO) && (property.Context as CodeFunctionDTO).ValueCustomType != ValueCustomType.None)
                                    //    property.Value = GetCustomTypePropertyValue(property, (property.Context as CodeFunctionDTO).ValueCustomType, result.Result);
                                    //else
                                });
                            }
                        }
                    }
                }

                result = property.Value;
                return(true);
            }
            else
            {
                throw new Exception("Property" + " " + binder.Name + " not found!");
            }
        }