Example #1
0
        private bool validateType(JsonWriter writer, WSParam xParam, object obj, List <Type> printedTypes, bool ignoreCurcularity, WSRequest Request, MetaFunctions CFunc)
        {
            bool isValid = false;

            if (obj != null && Request.Security != null && Request.Security.IsValid && xParam != null)
            {
                byte          uRole            = Request.Security.IsLogged ? Request.Security.AuthToken.User.role : WSConstants.ACCESS_LEVEL.READ;
                int           paramAccessLevel = xParam.READ_ACCESS_MODE.ACCESS_LEVEL;
                WSTableSource xSource          = (WSTableSource)CFunc.GetSourceByType(obj.GetType());
                paramAccessLevel = (xSource != null && xSource.AccessLevel > paramAccessLevel) ? xSource.AccessLevel : paramAccessLevel;

                bool showCycledTypesMessage = uRole >= WSConstants.ACCESS_LEVEL.ADMIN && xSource != null && xSource.ShowMessageInaccessible;

                Type entityType = null;

                if (obj.IsCollection())
                {
                    if (obj.IsCollectionOf <WSEntity>())
                    {
                        entityType = (obj as IList).GetEntityType();
                    }
                    else if (obj.IsCollectionOf <WSRecord>() && (obj as IList).Count > 0)
                    {
                        entityType = ((WSRecord)(obj as IList)[0]).entity.GetType();
                    }
                }
                else if (obj is WSRecord)
                {
                    entityType = ((WSRecord)obj).entity.GetType();
                }
                else if (obj is WSEntity)
                {
                    entityType = obj.GetType();
                }

                bool Ignore = ignoreCurcularity ? false : (printedTypes != null && printedTypes.Any(t => t == entityType));
                if (Ignore)
                {
                    if (showCycledTypesMessage)
                    {
                        WritePropName(writer, xParam.NAME);
                        writer.WriteValue("[unavailable : cycled references detected]");
                    }
                }
                else
                {
                    if (entityType.IsValidDynamicEntity())
                    {
                        WSSource src = CFunc.GetSourceByType(entityType);
                        isValid = src != null && uRole >= src.AccessLevel;
                    }
                    else
                    {
                        isValid = true;
                    }
                }
            }
            return(isValid);
        }
Example #2
0
 public WSSource getSource(MetaFunctions CFunc)
 {
     if (_xSource == null)
     {
         _xSource = CFunc.GetSourceByType(GetType());
     }
     return(_xSource);
 }
Example #3
0
        private object getMemberValue(WSEntity entity, WSParam eParam, MetaFunctions CFunc)
        {
            WSTableSource            xSource  = (WSTableSource)CFunc.GetSourceByType(entity.GetType());
            IEnumerable <MemberInfo> eMembers = xSource.ReturnType.GetMembers().Where(m => m is PropertyInfo || m is FieldInfo);
            MemberInfo eField = eMembers.FirstOrDefault(m => eParam.Match(m.Name));

            return(eField is PropertyInfo ? ((PropertyInfo)eField).GetValue(entity, null) : eField is FieldInfo ? ((FieldInfo)eField).GetValue(entity) : null);
        }
Example #4
0
 private dynamic readPrimitive(MetaFunctions CFunc, WSJValue jVal, WSDynamicEntity _entity)
 {
     try
     {
         if (_entity != null)
         {
             Type          eType = _entity.GetType();
             WSTableSource src   = ((WSTableSource)CFunc.GetSourceByType(eType));
             WSTableParam  param = src.DBParams.FirstOrDefault(p => p.Match(jVal.Value));
             PropertyInfo  pInfo = eType.GetProperties().FirstOrDefault(p => p.Name.Equals(param.WSColumnRef.NAME));
             return(pInfo.GetValue(_entity, null));
         }
     }
     catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status, $"readPrimitive():373"); }
     return(null);
 }
Example #5
0
        private string LoadJson()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("\"Name\":\"" + ReturnType.Name + "\"");
            sb.Append(",\"ReturnType\":\"" + ReturnType.FullName + "\"");
            sb.Append(",\"SecurityZone\":\"" + SecurityZone + "\"");

            sb.Append("," + base.Json);

            sb.Append(",\"Params\":[");
            Func <WSParam, bool>   isAccessibleParam = x => x.READ_ACCESS_MODE.ACCESS_LEVEL <= UserRole;
            Func <WSParam, string> jFunc             = (x => "{" + (x is WSTableParam ? ((WSTableParam)x).Json : x.Json) + "}");

            if (Params != null && Params.Any(isAccessibleParam))
            {
                List <string> paramList = new List <string>();
                foreach (WSParam param in Params.Where(isAccessibleParam))
                {
                    if (param is WSTableParam)
                    {
                        WSTableParam tParam = (WSTableParam)param;
                        if (tParam.IsAssociation)
                        {
                            WSSource src = CFunc.GetSourceByType(tParam.DataType.GetEntityType());
                            if (src != null && src.AccessLevel <= UserRole)
                            {
                                paramList.Add("{" + tParam.Json + "}");
                            }
                        }
                        else
                        {
                            paramList.Add("{" + tParam.Json + "}");
                        }
                    }
                    else
                    {
                        paramList.Add("{" + param.Json + "}");
                    }
                }
                sb.Append(paramList.Aggregate((a, b) => a + "," + b));
            }
            sb.Append("]");

            return(sb.ToString());
        }
Example #6
0
 public override WSFilter GetOptionFilter(MetaFunctions CFunc, Expression parent, int level, string state = null, bool?negate = null)
 {
     try
     {
         if (Value.IsTrue() || Value.IsFalse())
         {
             return(new WSBoolOFilter(this));
         }
         else if (WSConstants.SPECIAL_CASES.Any(x => x.Match(Value)))
         {
             state = Value;
             if (parent.Type.IsSameOrSubclassOf(typeof(WSEntity)) || parent.Type.IsCollectionOf <WSEntity>())
             {
                 WSTableSource Source = (WSTableSource)CFunc.GetSourceByType/*<WSTableSource>*/ (parent.Type);
                 if (parent.Type.IsSameOrSubclassOf(typeof(WSEntity)) && WSEntityFFilter.OPERATIONS.STATE_OPERATIONS.Any(x => x.Match(state)))
                 {
                     return(new WSEntityFilter(Source, parent, WSEntityFFilter.OPERATIONS.STATE_OPERATIONS.FirstOrDefault(x => x.Match(state))));
                 }
             }
         }
     }
     catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status); }
     return(null);
 }
Example #7
0
        public override WSFilter GetOptionFilter(MetaFunctions CFunc, Expression parent, int level, string state = null, bool?negate = null)
        {
            WSCombineFilter filter = new WSCombineFilter(WSCombineFilter.SQLMode.AndAlso);

            try
            {
                if (Value != null)
                {
                    if (WSConstants.ALIACES.NOT.Match(Key))
                    {
                        return(Value.GetOptionFilter(CFunc, parent, level, state, true));
                    }
                    else if (WSConstants.ALIACES.ANY.Match(Key))
                    {
                        return(Value.GetOptionFilter(CFunc, parent, level, Key, true));
                    }
                    else
                    {
                        if (Value is WSJValue)
                        {
                            filter.Save(((WSJValue)Value).GetOptionFilter(CFunc, parent, level, state, negate));
                        }
                        else
                        {
                            WSTableSource PSource  = (WSTableSource)CFunc.GetSourceByType(parent.Type.GetEntityType());
                            WSTableParam  subParam = PSource == null ? null : (WSTableParam)PSource.GetXParam(Key);

                            //TODO@ANDVO:2016-11-15: implement deep filtering
                        }
                        return(filter.Any() ? (filter.Count == 1 && !filter.Negate) ? filter.FirstOrDefault() : filter : null);
                    }
                }
            }
            catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status); }
            return(null);
        }
Example #8
0
        public WSStatus WriteJson(JsonWriter writer, JsonSerializer serializer, WSSchema schema, WSParamList outFields, List <Type> printedTypes, WSRequest Request, MetaFunctions CFunc, WSDataContext DBContext)
        {
            WSStatus status = WSStatus.NONE_Copy();

            WSEntitySchema eSchema = null;
            WSSource       xSource = CFunc.GetSourceByType(GetType());

            if (schema != null && schema is WSEntityBaseSchema)
            {
                if (schema is WSEntitySchema)
                {
                    eSchema = (WSEntitySchema)schema;
                }
                else if (schema is WSEntityListSchema)
                {
                    eSchema = ((WSEntityListSchema)schema).EntitySchema;
                }
            }

            if (eSchema == null && this is WSDynamicEntity)
            {
                if (xSource != null && xSource is WSTableSource && ((WSTableSource)xSource).BaseSchema != null)
                {
                    eSchema = ((WSTableSource)xSource).BaseSchema;
                }
            }

            #region Read if in 'COLLAPSED' mode
            bool collapsedMode = false;
            if (eSchema != null)
            {
                IEnumerable <WSJObject>  _JOptions = eSchema.IOBaseOptions.Value.OfType <WSJObject>();
                Func <WSJProperty, bool> func      = v => WSConstants.ALIACES.OPTION_COLLECT.Match(v.Key);
                WSJObject takeJOption = _JOptions.FirstOrDefault(x => x.Value.Any(func));
                if (takeJOption != null && takeJOption.IsValid)
                {
                    collapsedMode = true;
                    status.childs.Add(WriteCollapsedValues(writer, serializer, this, xSource, takeJOption.Value.FirstOrDefault(func).Value, Request, CFunc));
                }
            }
            #endregion

            if (!collapsedMode)
            {
                writer.WriteStartObject();
                if (this is WSStaticEntity || (eSchema != null && eSchema.Fields.Any()))
                {
                    List <Type> postPrintedTypes = printedTypes.Select(x => x).ToList();
                    if (!postPrintedTypes.Any(x => x == GetType()))
                    {
                        postPrintedTypes.Add(GetType());
                    }
                    status.childs.Add(
                        WriteJMembers(
                            GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance).Where(m => m is PropertyInfo || m is FieldInfo),
                            writer,
                            serializer,
                            eSchema,
                            xSource,
                            outFields,
                            printedTypes,
                            Request,
                            CFunc,
                            DBContext
                            )
                        );
                }
                writer.WriteEndObject();
            }
            return(status);
        }
Example #9
0
        private WSStatus WriteCollapsedValues(JsonWriter writer, JsonSerializer serializer, WSEntity entity, WSSource xSource, WSJson collapseOption, WSRequest Request, MetaFunctions CFunc)
        {
            WSStatus status = WSStatus.NONE_Copy();

            try
            {
                /*******************************************************************************
                 *
                 * //  TODO @ANDVO : 2016-02-15 : IMPORTANT!!! => Implement security check like this :
                 *
                 * WSStatus status = Validate(obj, xParam, writer, serializer, security, schema);
                 * if (status.CODE == WSStatus.SUCCESS.CODE)
                 * {
                 */

                if (entity != null && collapseOption != null && collapseOption.IsValid)
                {
                    WSTableSource childSource = (WSTableSource)CFunc.GetSourceByType(entity.GetType());

                    object  fieldValue = null;
                    WSParam field      = null;
                    if (collapseOption is WSJValue)
                    {
                        string fieldName = ((WSJValue)collapseOption).Value;
                        field      = entity.GetParam(xSource, fieldName);
                        fieldValue = getMemberValue(entity, field, CFunc);

                        WSPrimitiveFieldSchema fieldSchema = new WSPrimitiveFieldSchema(CFunc, (WSTableParam)field, new WSJProperty(fieldName, new WSJArray()), /*((WSTableSource)entity.getSource())*/ childSource.BaseSchema);
                        if (Validate(fieldValue, field, writer, serializer, childSource.BaseSchema, childSource, null, ref status, Request, CFunc))
                        {
                            object _obj = null;
                            serializer.Serialize(writer, field.TryReadPrimitiveWithDefault(fieldValue, string.Empty, out _obj) ? _obj : string.Empty);
                            writer.Flush();
                            status = WSStatus.SUCCESS_Copy();
                        }
                    }
                    else if (collapseOption is WSJObject)
                    {
                        WSJProperty collapseSrc = ((WSJObject)collapseOption).Value.FirstOrDefault();
                        field      = entity.GetParam(childSource, collapseSrc.Key);
                        fieldValue = getMemberValue(entity, field, CFunc);
                        if (Validate(fieldValue, field, writer, serializer, childSource.BaseSchema, childSource, null, ref status, Request, CFunc))
                        {
                            if (fieldValue == null)
                            {
                                serializer.Serialize(writer, "NULL");
                                writer.Flush();
                            }
                            else if (fieldValue is WSEntity)
                            {
                                WSTableSource fieldSource = (WSTableSource)CFunc.GetSourceByType(fieldValue.GetType());
                                status = WriteCollapsedValues(writer, serializer, (WSEntity)fieldValue, fieldSource, collapseSrc.Value, Request, CFunc);
                            }
                            else if (fieldValue.IsCollectionOf <WSEntity>())
                            {
                                WSTableSource fieldSource = (WSTableSource)CFunc.GetSourceByType(fieldValue.GetType().GetEntityType());
                                if (!((IEnumerable <WSEntity>)fieldValue).Any())
                                {
                                    serializer.Serialize(writer, "NULL");
                                    writer.Flush();
                                }
                                else
                                {
                                    foreach (WSEntity eItem in (IEnumerable <WSEntity>)fieldValue)
                                    {
                                        status.childs.Add(WriteCollapsedValues(writer, serializer, eItem, fieldSource, collapseSrc.Value, Request, CFunc));
                                    }
                                    status = status.IsPositive ? WSStatus.SUCCESS_Copy() : WSStatus.ERROR_Copy();
                                }
                            }
                        }
                    }
                }


                /*}
                 *
                 *******************************************************************************/
            }
            catch (Exception e) { CFunc.RegError(GetType(), e, ref status); }
            return(status);
        }
Example #10
0
        public virtual bool Match(string key, IEnumerable <WSTableSource> sources = null, Func <Type, WSTableSource> getTSource = null, bool TypeMatchAllowed = true)
        {
            try
            {
                if (!string.IsNullOrEmpty(NAME) && !string.IsNullOrEmpty(key))
                {
                    key = key.ToLower();
                    int _CODE = -1;
                    if (CODE >= 0)
                    {
                        if (NAME.Equals(key))
                        {
                            return(true);
                        }
                        else if (DISPLAY_NAME.Equals(key))
                        {
                            return(true);
                        }
                        else if (ALIACES != null && ALIACES.Contains(key))
                        {
                            return(true);
                        }
                        else if (this is WSTableParam)
                        {
                            WSTableParam p = (WSTableParam)this;
                            if (p.WSColumnRef.NAME.ToLower().Equals(key))
                            {
                                return(true);
                            }
                            else if (TypeMatchAllowed && p.DataType.IsValidDynamicEntity())
                            {
                                WSTableSource src = null;
                                if (getTSource != null)
                                {
                                    src = getTSource(p.DataType.GetEntityType());
                                }

                                else if (sources != null)
                                {
                                    src = sources.FirstOrDefault(s => s.ReturnType == p.DataType.GetEntityType());
                                }

                                else if (func != null)
                                {
                                    src = (WSTableSource)func.GetSourceByType(p.DataType.GetEntityType());
                                }

                                if (src != null && src.Match(key))
                                {
                                    return(true);
                                }
                            }
                        }
                        else if (int.TryParse(key, out _CODE) && _CODE == CODE)
                        {
                            return(true);
                        }
                    }
                    ;
                }
            }
            catch (Exception) { }
            return(false);
        }
Example #11
0
        public override Expression SortTable <TEntity>(MetaFunctions CFunc, WSDataContext dc, List <PropertyInfo> parents, Expression expression, ref WSStatus iostatus)
        {
            try
            {
                if (dc != null)
                {
                    parents = parents != null ? parents : new List <PropertyInfo>();
                    Type          srcType    = parents.Any() ? parents.LastOrDefault().PropertyType.IsCollection() ? parents.LastOrDefault().PropertyType.GetEntityType() : parents.LastOrDefault().PropertyType : typeof(TEntity);
                    ITable        initSource = srcType == null ? null : dc.GetTable(typeof(TEntity));
                    ITable        source     = srcType == null ? null : dc.GetTable(srcType);
                    WSTableSource schema     = srcType == null ? null : (WSTableSource)CFunc.GetSourceByType(srcType);
                    if (schema != null)
                    {
                        WSParam param = schema.GetXParam(Value);

                        if (param != null && param is WSTableParam)
                        {
                            WSTableParam tParam   = (WSTableParam)param;
                            PropertyInfo property = srcType.GetProperties().FirstOrDefault(p => tParam.WSColumnRef.NAME.Equals(p.Name));
                            if (property == null)
                            {
                                iostatus.AddNote(string.Format("No PropertyInfo found for : [{0}]", tParam.DISPLAY_NAME));
                            }
                            else
                            {
                                parents.Add(property);

                                if (tParam.DataType.IsSimple() && tParam.IsSortable)
                                {
                                    expression = SortPrimitiveType <TEntity>(initSource, source, param, false, parents, expression, ref iostatus);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status); }
            return(expression);
        }
Example #12
0
        public override WSFilter GetFieldFilter(MetaFunctions CFunc, WSTableParam param, Expression parent, int level, string state = null, bool?negate = null)
        {
            WSCombineFilter filter = new WSCombineFilter(WSCombineFilter.SQLMode.AndAlso);

            try
            {
                if (param != null && param.isValid && (param.DataType.IsNullable() || Value != null))
                {
                    if (param.DataType.IsSimple())
                    {
                        bool localNegate = WSConstants.ALIACES.NOT.Match(state);
                        negate = negate == null ? localNegate : localNegate != negate;
                        WSFilter pFilter = Value.GetFieldFilter(CFunc, param, parent, level, Key, negate);
                        filter.Save(pFilter);
                    }
                    else if (param.DataType.IsSameOrSubclassOf(typeof(WSEntity)) || param.DataType.IsCollectionOf <WSEntity>())
                    {
                        if (WSConstants.ALIACES.NOT.Match(Key))
                        {
                            return(Value.GetFieldFilter(CFunc, param, parent, level, state, true));
                        }
                        else if (WSConstants.ALIACES.ANY.Match(Key))
                        {
                            return(Value.GetFieldFilter(CFunc, param, parent, level, Key, true));
                        }
                        else
                        {
                            WSTableSource PSource = (WSTableSource)CFunc.GetSourceByType/*<WSTableSource>*/ (param.DataType.GetEntityType());

                            WSTableParam subParam = PSource == null ? null : (WSTableParam)PSource.GetXParam(Key);
                            Expression   member   = Expression.Property(parent, param.WSColumnRef.NAME);
                            if (subParam == null && WSConstants.SPECIAL_CASES.Any(c => c.Match(Key)))
                            {
                                if (param.DataType.IsSameOrSubclassOf(typeof(WSEntity)))
                                {
                                    return(new WSEntityFFilter(param, member, WSEntityFFilter.OPERATIONS.STATE_OPERATIONS.FirstOrDefault(x => x.Match(Key)))
                                    {
                                        Value = null
                                    });
                                }
                                else if (param.DataType.IsCollectionOf <WSEntity>())
                                {
                                    return(new WSEntityListFFilter(param, member, WSEntityListFFilter.OPERATIONS.STATE_OPERATIONS.FirstOrDefault(x => x.Match(Key)))
                                    {
                                        Value = null
                                    });
                                }
                            }
                            else
                            {
                                WSFilter subFilter = null;
                                if (param.DataType.IsSameOrSubclassOf(typeof(WSEntity)))
                                {
                                    filter.Save(new WSEntityFFilter(param, member, WSEntityFFilter.OPERATIONS.NotEqual)
                                    {
                                        Value = null
                                    });

                                    subFilter = Value.GetFieldFilter(CFunc, subParam, member, level, Key);
                                    if (subFilter != null && subFilter.IsValid)
                                    {
                                        filter.Save(new WSEntityFFilter(param, member, WSEntityFFilter.OPERATIONS.Filter)
                                        {
                                            Value = subFilter
                                        });
                                    }
                                }
                                else if (param.DataType.IsCollectionOf <WSEntity>())
                                {
                                    level++;

                                    Type elemType          = param.DataType.GetEntityType();
                                    ParameterExpression id = Expression.Parameter(elemType, level.ToHex());
                                    subFilter = Value.GetFieldFilter(CFunc, subParam, id, level, Key, negate);
                                    if (subFilter != null && subFilter.IsValid)
                                    {
                                        dynamic subExpr = subFilter.GetType().GetMethod("ToLambda").MakeGenericMethod(new Type[] { elemType }).Invoke(subFilter, new object[] { id });

                                        filter.Save(new WSEntityListFFilter(subParam, member, WSEntityListFFilter.OPERATIONS.Any)
                                        {
                                            Value = (subExpr == null) ? true : subExpr
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status); }
            return(filter.Any() ? (filter.Count == 1 && !filter.Negate) ? filter.FirstOrDefault() : filter : null);
        }
Example #13
0
        public override bool MatchEntity(MetaFunctions CFunc, WSDynamicEntity entity, WSTableSource src, string key = null, string matchOperation = null)
        {
            bool isMatch = true;

            try
            {
                if (!IsValid)
                {
                    isMatch = false;
                }
                else
                {
                    if (string.IsNullOrEmpty(key))
                    {
                        key = Key;
                    }
                    else
                    {
                        matchOperation = Key;
                    }

                    if (Value is WSJValue)
                    {
                        isMatch = ((WSJValue)Value).MatchEntity(CFunc, entity, src, key, matchOperation);
                    }
                    else
                    {
                        if (src.DBAssociationParams.Any(p => p.Match(Key)))
                        {
                            WSTableParam param = src.DBAssociationParams.FirstOrDefault(p => p.Match(Key));
                            src = (WSTableSource)CFunc.GetSourceByType(param.DataType.GetEntityType());
                            object oEntity = entity.GetType().GetProperty(param.WSColumnRef.NAME).GetValue(entity, null);
                            if (oEntity.GetType().IsCollectionOf <WSDynamicEntity>())
                            {
                                foreach (WSDynamicEntity e in (IEnumerable <WSDynamicEntity>)oEntity)
                                {
                                    if (Value is WSJObject)
                                    {
                                        if (((WSJObject)Value).MatchEntity(CFunc, e, src))
                                        {
                                            isMatch = true;
                                        }
                                    }
                                    else if (Value is WSJArray)
                                    {
                                        if (((WSJArray)Value).MatchEntity(CFunc, e, src))
                                        {
                                            isMatch = true;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (Value is WSJObject)
                                {
                                    isMatch = ((WSJObject)Value).MatchEntity(CFunc, (WSDynamicEntity)oEntity, src);
                                }
                                else if (Value is WSJArray)
                                {
                                    isMatch = ((WSJArray)Value).MatchEntity(CFunc, (WSDynamicEntity)oEntity, src);
                                }
                            }
                        }
                        else
                        {
                            if (Value is WSJObject)
                            {
                                isMatch = ((WSJObject)Value).MatchEntity(CFunc, entity, src, key);
                            }
                            else if (Value is WSJArray)
                            {
                                isMatch = ((WSJArray)Value).MatchEntity(CFunc, entity, src, key);
                            }
                        }
                    }
                }
            }
            catch (Exception e) { isMatch = false; WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status); }
            return(isMatch);
        }
Example #14
0
        public override bool Match(WSRequest Request, WSDataContext DBContext, MetaFunctions CFunc, WSSchema schema)
        {
            bool ok = false;

            try
            {
                WSEntitySchema eSchema = null;
                if (schema is WSEntitySchema)
                {
                    eSchema = (WSEntitySchema)schema;
                }
                else if (schema is WSEntityListSchema)
                {
                    eSchema = ((WSEntityListSchema)schema).EntitySchema;
                }

                if (eSchema != null)
                {
                    bool baseFilterMatch = true;
                    if (eSchema.Source.BaseFilter != null && eSchema.Source.BaseFilter.IsValid && eSchema.Source.BaseFilter.apply(Request, CFunc))
                    {
                        baseFilterMatch = eSchema.Source.BaseFilter.MatchEntity(CFunc, this, ((WSTableSource)CFunc.GetSourceByType(GetType())));
                    }

                    bool dynamicFilterMatch = true;
                    if (eSchema.Fields != null || eSchema.Fields != null)
                    {
                        dynamicFilterMatch = MatchFields(Request, CFunc, eSchema) && MatchFilters(Request, CFunc, eSchema);
                    }

                    ok = baseFilterMatch && dynamicFilterMatch;
                }
            }
            catch (Exception e) {
                if (Request != null)
                {
                    CFunc.RegError(GetType(), e, ref Request.status, $"Match():434");
                }
                else
                {
                    WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status, $"Match():435");
                }
            }
            return(ok);
        }
Example #15
0
        public bool TrySetRecordValue(string colName, object newValue, WSDataContext DBContext, MetaFunctions CFunc, Func <Exception, bool> AddError = null)
        {
            bool done = false;

            if (!string.IsNullOrEmpty(colName))
            {
                PropertyInfo pInfo = GetType().GetProperty(colName);
                if (pInfo != null)
                {
                    object orgValue = pInfo.GetValue(this, null);
                    newValue = fixSpecialCaseValue(colName, newValue, pInfo.PropertyType);
                    try
                    {
                        if (
                            orgValue == newValue ||
                            (orgValue == null && newValue == null) ||
                            (orgValue != null && newValue != null && orgValue.ToString().Equals(newValue.ToString()))
                            )
                        {
                            return(true);
                        }
                        else
                        {
                            if (newValue == null)
                            {
                                if (pInfo.PropertyType.IsNullable())
                                {
                                    pInfo.SetPropertyValue(this, null); done = true;
                                }
                            }
                            else
                            {
                                object newValueConverted = pInfo.PropertyType.IsAssignableFrom(newValue.GetType()) ? newValue : null;
                                if (newValueConverted != null || pInfo.PropertyType.Read(newValue, out newValueConverted, null, null, pInfo.Name))
                                {
                                    try
                                    {
                                        bool           IsAssiciation  = false;
                                        PropertyInfo   association    = null;
                                        WSTableSource  associationSrc = null;
                                        WSTableParam   associationKey = null;
                                        PropertyInfo[] props          = GetType().GetProperties();
                                        foreach (PropertyInfo prop in props)
                                        {
                                            IEnumerable <CustomAttributeData> cAttrs = prop.CustomAttributesData();
                                            foreach (CustomAttributeData cad in cAttrs)
                                            {
                                                CustomAttributeNamedArgument IsForeignKey = cad.NamedArguments.FirstOrDefault(x => x.MemberInfo.Name.Equals("IsForeignKey"));
                                                if (IsForeignKey != null && IsForeignKey.TypedValue.Value != null && (true.ToString()).Equals(IsForeignKey.TypedValue.Value.ToString()))
                                                {
                                                    CustomAttributeNamedArgument cana = cad.NamedArguments.FirstOrDefault(x => x.MemberInfo.Name.Equals("ThisKey"));
                                                    if (cana != null && pInfo.Name.Equals(cana.TypedValue.Value == null ? null : cana.TypedValue.Value.ToString()))
                                                    {
                                                        CustomAttributeNamedArgument canaKey = cad.NamedArguments.FirstOrDefault(x => x.MemberInfo.Name.Equals("OtherKey"));
                                                        if (canaKey != null && canaKey.TypedValue.Value != null)
                                                        {
                                                            IsAssiciation  = true;
                                                            association    = prop;
                                                            associationSrc = (WSTableSource)CFunc.GetSourceByType(association.PropertyType);
                                                            associationKey = (WSTableParam)associationSrc.GetXParam(canaKey.TypedValue.Value.ToString());
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        if (IsAssiciation)
                                        {
                                            string pName = pInfo.Name;

                                            ParameterExpression paramExp = Expression.Parameter(association.PropertyType, "x");
                                            WSCombineFilter     filter   = new WSCombineFilter(WSCombineFilter.SQLMode.AndAlso);

                                            filter.Save(new WSJValue(newValueConverted.ToString()).GetFieldFilter(CFunc, associationKey, paramExp, 0));

                                            object subExpr = (Expression)filter.GetType().GetMethod("ToLambda").MakeGenericMethod(new Type[] { association.PropertyType }).Invoke(filter, new object[] { paramExp });

                                            MethodInfo mInfo = DBContext.GetType().GetMethod("GetTable", new Type[] { });

                                            var tObj = mInfo.MakeGenericMethod(new Type[] { association.PropertyType }).Invoke(DBContext, new object[] { });

                                            Func <WSDynamicEntity, bool> func = s => s.readPropertyValue(associationKey.WSColumnRef.NAME).ToString().Equals(newValueConverted.ToString());

                                            var method = typeof(Enumerable).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)
                                                         .FirstOrDefault(m => m.Name == "FirstOrDefault" && m.GetParameters().Count() == 2).MakeGenericMethod(typeof(WSDynamicEntity));

                                            WSDynamicEntity newAssociation = (WSDynamicEntity)method.Invoke(null, new object[] { tObj, func });

                                            if (newAssociation != null)
                                            {
                                                association.SetPropertyValue(this, newAssociation); done = true;
                                            }
                                        }
                                        else
                                        {
                                            pInfo.SetPropertyValue(this, newValueConverted); done = true;
                                        }
                                    }
                                    catch (Exception e1) { AddError(e1); }
                                }
                            }
                        }
                    }
                    catch (Exception e) { AddError(e); }

                    if (!done)
                    {
                        pInfo.SetPropertyValue(this, orgValue);
                    }
                }
            }
            return(done);
        }