Пример #1
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);
        }
Пример #2
0
        public WSParam GetXParam(string xName, Type DefaultFieldType = null, Func <Type, WSTableSource> getTSource = null)
        {
            WSParam param = null;

            try
            {
                if (!string.IsNullOrEmpty(xName))
                {
                    xName = xName.ToLower();
                    param = Params.FirstOrDefault(p => p is WSTableParam ? ((WSTableParam)p).Match(xName, null, getTSource) : p.Match(xName, null, getTSource));
                    if (param == null)
                    {
                        if (DefaultFieldType != null)
                        {
                            param = new WSTableParam(ReturnType, 0, xName.ToLower(), new WSColumnRef(xName), DefaultFieldType, CFunc);
                        }
                    }
                }
            }
            catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status); }
            return(param);
        }
Пример #3
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);
 }
Пример #4
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);
        }
Пример #5
0
        public override WSFilter GetFieldFilter(MetaFunctions CFunc, WSTableParam param, Expression parent, int level, string state = null, bool?negate = null)
        {
            try
            {
                ///**************************************************
                //* if(filter array contains any conditional 'IOJFilter'
                //* like : "[{option_name1:value1},{option_name2:value2}]" (example:"[{max:123},{min:123}]") , - then use 'CombineMode.AndAlso'
                //* else
                //* (if all values in the filter array are simple type values)
                //* like : "{value1,value2,value3,...}" ,- use 'CombineMode.Or'
                //* ***********************************************/
                WSCombineFilter filter = new WSCombineFilter(WSCombineFilter.SQLMode.OrElse, negate);

                foreach (WSJson j in Value)
                {
                    WSFilter jFilter = j.GetFieldFilter(CFunc, param, parent, level, state, negate);
                    filter.Save(jFilter);
                }

                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);
        }
Пример #6
0
        public override bool MatchEntity(MetaFunctions CFunc, WSDynamicEntity entity, WSTableSource src, string key = null, string matchOperation = null)
        {
            bool isMatch = true;

            try
            {
                foreach (WSJProperty jProp in Value)
                {
                    if (!jProp.MatchEntity(CFunc, entity, src, key))
                    {
                        isMatch = false;
                    }
                }
            }
            catch (Exception e) { isMatch = false; WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status); }
            return(isMatch);
        }
Пример #7
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);
        }
Пример #8
0
        private WSStatus WriteJProperty(object obj, WSParam xParam, JsonWriter writer, JsonSerializer serializer, WSSchema schema, WSSource xSource, WSParamList outFields, List <Type> printedTypes, WSRequest Request, MetaFunctions CFunc, WSDataContext DBContext)
        {
            WSStatus status = WSStatus.NONE_Copy();

            try
            {
                if (Validate(obj, xParam, writer, serializer, schema, xSource, outFields, ref status, Request, CFunc))
                {
                    if (obj == null)
                    {
                        WritePropName(writer, ((schema != null && !string.IsNullOrEmpty(schema.Name)) ? schema.Name : xParam.NAME), true, PrintMode.ValueCell);
                        serializer.Serialize(writer, obj);
                    }
                    else if (obj is WSStatus || obj is WSStatus_JSON)
                    {
                        #region PRINT WSStatus
                        WSStatus_JSON json = obj is WSStatus_JSON ? (WSStatus_JSON)obj : ((WSStatus)obj).GetJson();
                        if (json != null)
                        {
                            WritePropName(writer, ((schema != null && !string.IsNullOrEmpty(schema.Name)) ? schema.Name : xParam.NAME), true, PrintMode.TableHeader);
                            serializer.Serialize(writer, json);
                        }
                        #endregion
                    }
                    else if (obj.GetType().IsSimple())
                    {
                        #region PRINT PRIMITIVE FIELD

                        if (obj is DateTime)
                        {
                            obj = ((DateTime)obj).ToString(WSConstants.DATE_FORMAT);
                        }
                        else if (obj is TimeSpan)
                        {
                            obj = ((TimeSpan)obj).ToString(WSConstants.TIMESPAN_FORMAT_SIMPLE);
                        }

                        WritePropName(writer, (schema != null && !string.IsNullOrEmpty(schema.Name)) ? schema.Name : xParam.NAME);
                        object _obj = null;
                        serializer.Serialize(writer, xParam.TryReadPrimitiveWithDefault(obj, string.Empty, out _obj) ? _obj : string.Empty);

                        #endregion
                    }
                    else if (obj.GetType().IsSimpleCollection())
                    {
                        #region PRINT PRIMITIVE COLLECTION
                        string key = (schema != null && !string.IsNullOrEmpty(schema.Name)) ? schema.Name : xParam.NAME;
                        status.AddNote("ready to searialize primitive fields (" + key + ")");

                        WritePropName(writer, key);
                        serializer.Serialize(writer, obj);

                        #endregion
                    }
                    else if (obj is WSRecord)
                    {
                        #region PRINT WSRecord

                        string pKey = (schema != null && !string.IsNullOrEmpty(schema.Name)) ? schema.Name : xParam.NAME;
                        WritePropName(writer, pKey);
                        ((WSRecord)obj).WriteJson(writer, serializer, printedTypes, Request, CFunc, DBContext);

                        #endregion
                    }
                    else if (obj.IsCollectionOf <WSRecord>())
                    {
                        #region PRINT WSRecord Collection

                        string pKey = (schema != null && !string.IsNullOrEmpty(schema.Name)) ? schema.Name : xParam.NAME;

                        WritePropName(writer, pKey);
                        writer.WriteStartArray();

                        IList list = obj as IList;
                        foreach (WSRecord record in list)
                        {
                            if (record != null)
                            {
                                record.WriteJson(writer, serializer, printedTypes, Request, CFunc, DBContext);
                            }
                        }

                        writer.WriteEndArray();
                        #endregion
                    }
                    else
                    {
                        #region PRINT ENTITY

                        bool printAllowed =
                            (this is WSStaticEntity)
                            ||
                            (
                                schema is WSEntityBaseSchema
                                &&
                                validateType(writer, xParam, obj, printedTypes, true, Request, CFunc)
                            );

                        if (printAllowed)
                        {
                            string pKey = (schema != null && !string.IsNullOrEmpty(schema.Name)) ? schema.Name : xParam.NAME;
                            WritePropName(writer, pKey, false);

                            #region PRINT WSEntity
                            if (obj is WSEntity)
                            {
                                if (obj is WSDynamicEntity && !((WSDynamicEntity)obj).Match(Request, DBContext, CFunc, schema))
                                {
                                    serializer.Serialize(writer, "NULL");
                                }
                                else
                                {
                                    ((WSEntity)obj).WriteJson(writer, serializer, schema, outFields, printedTypes, Request, CFunc, DBContext);
                                }
                            }
                            #endregion

                            #region PRINT Collection
                            else if (obj.IsCollectionOf <WSEntity>())
                            {
                                IList list  = obj as IList;
                                Type  eType = list.GetEntityType();

                                writer.WriteStartArray();
                                foreach (WSEntity entity in list)
                                {
                                    if (entity != null)
                                    {
                                        if (entity is WSDynamicEntity)
                                        {
                                            WSDynamicEntity dEntity = (WSDynamicEntity)entity;
                                            if (dEntity.Match(Request, DBContext, CFunc, schema))
                                            {
                                                entity.WriteJson(writer, serializer, schema, outFields, printedTypes, Request, CFunc, DBContext);
                                            }
                                        }
                                        else
                                        {
                                            entity.WriteJson(writer, serializer, schema, outFields, printedTypes, Request, CFunc, DBContext);
                                        }
                                    }
                                }
                                writer.WriteEndArray();
                            }
                            #endregion
                        }

                        #endregion
                    }
                    status.AddNote("done", WSConstants.ACCESS_LEVEL.READ);
                }
            }
            catch (Exception e)
            {
                status.CODE = WSStatus.ERROR.CODE;
                status.AddNote("Error(line" + e.LineNumber() + "- " + e.Message + ")");
                CFunc.RegError(GetType(), e, ref status);
            }
            return(status);
        }
Пример #9
0
        public override WSFilter GetFieldFilter(MetaFunctions CFunc, WSTableParam param, Expression parent, int level, string state = null, bool?negate = null)
        {
            Expression      member = Expression.Property(parent, param.WSColumnRef.NAME);
            WSCombineFilter filter = new WSCombineFilter();

            if (param != null && param.isValid)
            {
                try
                {
                    if (WSConstants.ALIACES.EXIST.Match(Value) || WSConstants.ALIACES.EMPTY.Match(Value) || WSConstants.ALIACES.IS_OWN.Match(Value))
                    {
                        state = Value;
                    }

                    #region Read WSEntity (Ready)
                    if (!string.IsNullOrEmpty(state) && (param.DataType.IsSameOrSubclassOf(typeof(WSEntity)) || param.DataType.IsCollectionOf <WSEntity>()))
                    {
                        if (param.DataType.IsSameOrSubclassOf(typeof(WSEntity)) && WSEntityFFilter.OPERATIONS.STATE_OPERATIONS.Any(x => x.Match(state)))
                        {
                            filter.Save(new WSEntityFFilter(param, member, WSEntityFFilter.OPERATIONS.STATE_OPERATIONS.FirstOrDefault(x => x.Match(state)))
                            {
                                Value = null
                            });
                        }
                        else if (param.DataType.IsCollectionOf <WSEntity>() && WSEntityListFFilter.OPERATIONS.STATE_OPERATIONS.Any(x => x.Match(state)))
                        {
                            filter.Save(new WSEntityListFFilter(param, member, WSEntityListFFilter.OPERATIONS.STATE_OPERATIONS.FirstOrDefault(x => x.Match(state)))
                            {
                                Value = null
                            });
                        }
                    }
                    #endregion

                    #region Read primitive
                    else
                    {
                        dynamic dVal = null;

                        #region Numeric (Ready)
                        if (param.DataType.IsNumeric())
                        {
                            #region SPECIAL CASES
                            #region If Exists (Ready)
                            if (WSConstants.ALIACES.EXIST.Match(Value) && param.DataType.IsNullable())
                            {
                                filter.Save(new WSNumericFFilter(param, member, WSNumericFFilter.OPERATIONS.NotEqual)
                                {
                                    Value = null
                                });
                            }
                            #endregion
                            #region If Empty (Ready)
                            else if (WSConstants.ALIACES.EMPTY.Match(Value) && param.DataType.IsNullable())
                            {
                                filter.Save(new WSNumericFFilter(param, member, null)
                                {
                                    Value = null
                                });
                            }
                            #endregion
                            #endregion

                            #region If Value (Ready)
                            else
                            {
                                if (param.DataType.Read(Value, out dVal))
                                {
                                    Type        dType     = dVal == null ? param.DataType : dVal.GetType();
                                    WSOperation operation = WSNumericFFilter.OPERATIONS.GetOperation(state);

                                    if (operation.Match("max") && dType.IsCollection())
                                    {
                                        dVal = ((List <dynamic>)dVal).Max(x => x);
                                    }
                                    else if (operation.Match("min") && dType.IsCollection())
                                    {
                                        dVal = ((List <dynamic>)dVal).Min(x => x);
                                    }

                                    filter.Save(new WSNumericFFilter(param, member, operation)
                                    {
                                        Value = dVal
                                    });
                                }
                            }
                            #endregion
                        }
                        #endregion
                        #region bool (Ready)
                        else if (typeof(bool?).IsAssignableFrom(param.DataType))
                        {
                            #region SPECIAL CASES
                            #region If Exists (Ready)
                            if (WSConstants.ALIACES.EXIST.Match(Value) && param.DataType.IsNullable())
                            {
                                filter.Save(new WSBoolFFilter(param, member, WSBoolFFilter.OPERATIONS.NotEqual)
                                {
                                    Value = null
                                });
                            }
                            #endregion
                            #region If Empty (Ready)
                            else if (WSConstants.ALIACES.EMPTY.Match(Value) && param.DataType.IsNullable())
                            {
                                filter.Save(new WSBoolFFilter(param, member, null)
                                {
                                    Value = null
                                });
                            }
                            #endregion
                            #endregion

                            #region If Value (Ready)
                            else
                            {
                                if (param.DataType.Read(Value, out dVal))
                                {
                                    filter.Save(new WSBoolFFilter(param, member, WSBoolFFilter.OPERATIONS.GetOperation(state))
                                    {
                                        Value = dVal
                                    });
                                }
                            }
                            #endregion
                        }
                        #endregion
                        #region string (Ready)
                        else if (typeof(string).IsAssignableFrom(param.DataType))
                        {
                            #region SPECIAL CASES
                            #region If Exists (Ready)
                            if (WSConstants.ALIACES.EXIST.Match(Value))
                            {
                                if (param.DataType.IsNullable())
                                {
                                    filter.Save(new WSStringFFilter(param, member, WSStringFFilter.OPERATIONS.NotEqual)
                                    {
                                        Value = null
                                    });
                                }
                                filter.Save(new WSStringFFilter(param, member, WSStringFFilter.OPERATIONS.NotEqual)
                                {
                                    Value = string.Empty
                                });
                            }
                            #endregion
                            #region If Empty (Ready)
                            else if (WSConstants.ALIACES.EMPTY.Match(Value))
                            {
                                WSCombineFilter cFilter = new WSCombineFilter(WSCombineFilter.SQLMode.Or);;
                                if (param.DataType.IsNullable())
                                {
                                    cFilter.Save(new WSStringFFilter(param, member, null)
                                    {
                                        Value = null
                                    });
                                }
                                cFilter.Save(new WSStringFFilter(param, member, null)
                                {
                                    Value = string.Empty
                                });
                                filter.Save(cFilter);
                            }
                            #endregion
                            #endregion

                            #region If Value (Ready)
                            else if (param.DataType.IsNullable() || !string.IsNullOrEmpty(Value))
                            {
                                if (param.DataType.Read(Value, out dVal))
                                {
                                    filter.Save(new WSStringFFilter(param, member, WSStringFFilter.OPERATIONS.GetOperation(state))
                                    {
                                        Value = dVal
                                    });
                                }
                            }
                            #endregion
                        }
                        #endregion
                        #region Guid (Ready)
                        else if (typeof(Guid?).IsAssignableFrom(param.DataType))
                        {
                            #region SPECIAL CASES
                            #region If Exists (Ready)
                            if (WSConstants.ALIACES.EXIST.Match(Value) && param.DataType.IsNullable())
                            {
                                filter.Save(new WSGuidFFilter(param, member, WSGuidFFilter.OPERATIONS.NotEqual)
                                {
                                    Value = null
                                });
                            }
                            #endregion
                            #region If Empty (Ready)
                            else if (WSConstants.ALIACES.EMPTY.Match(Value) && param.DataType.IsNullable())
                            {
                                filter.Save(new WSGuidFFilter(param, member, null)
                                {
                                    Value = null
                                });
                            }
                            #endregion
                            #endregion

                            #region If Value (Ready)
                            else
                            {
                                if (param.DataType.Read(Value, out dVal, null, WSConstants.GUID_LIST_SEPARATORS))
                                {
                                    filter.Save(new WSGuidFFilter(param, member, WSGuidFFilter.OPERATIONS.GetOperation(state))
                                    {
                                        Value = dVal
                                    });
                                }
                            }
                            #endregion
                        }
                        #endregion
                        #region DateTime (Ready)
                        else if (typeof(DateTime?).IsAssignableFrom(param.DataType))
                        {
                            #region SPECIAL CASES
                            #region If Exists (Ready)
                            if (WSConstants.ALIACES.EXIST.Match(Value) && param.DataType.IsNullable())
                            {
                                filter.Save(new WSDateFFilter(param, member, WSDateFFilter.OPERATIONS.NotEqual)
                                {
                                    Value = null
                                });
                            }
                            #endregion
                            #region If Empty (Ready)
                            else if (WSConstants.ALIACES.EMPTY.Match(Value) && param.DataType.IsNullable())
                            {
                                filter.Save(new WSDateFFilter(param, member, null)
                                {
                                    Value = null
                                });
                            }
                            #endregion
                            #endregion

                            #region If Value (Ready)
                            else
                            {
                                WSOperation operation =
                                    Value.IsTrue() ?
                                    WSDateFFilter.OPERATIONS.LessOrEqual :
                                    (Value.IsFalse() && !(param.DataType.IsNullable())) ?
                                    WSDateFFilter.OPERATIONS.GreaterThanOrEqual :
                                    WSDateFFilter.OPERATIONS.GetOperation(state);
                                WSDateFFilter mainFilter = null;
                                if (operation != null && operation.Match(WSDateFFilter.OPERATIONS.WeekDayEqual))
                                {
                                    mainFilter = new WSDateFFilter(param, member, operation)
                                    {
                                        Value = Value
                                    };
                                }
                                else if (param.DataType.Read(Value, out dVal, new char[] { }, WSConstants.DATE_LIST_SEPARATORS, param.WSColumnRef.NAME))
                                {
                                    Type dType = dVal == null ? param.DataType : dVal.GetType();
                                    if (dType.IsCollection() && operation != null)
                                    {
                                        dVal = operation.Match(WSDateFFilter.OPERATIONS.LessOrEqual) ? ((List <dynamic>)dVal).Max(x => x) : operation.Match(WSDateFFilter.OPERATIONS.GreaterThanOrEqual) ? ((List <dynamic>)dVal).Min(x => x) : dVal;
                                    }
                                    mainFilter = new WSDateFFilter(param, member, operation)
                                    {
                                        Value = dVal
                                    };
                                }

                                if (param.DataType.IsNullable())
                                {
                                    WSCombineFilter cf = new WSCombineFilter(WSCombineFilter.SQLMode.AndAlso);

                                    if (mainFilter.Value != null)
                                    {
                                        cf.Save(new WSDateFFilter(param, member, WSDateFFilter.OPERATIONS.NotEqual)
                                        {
                                            Value = null
                                        });
                                    }

                                    cf.Save(mainFilter);

                                    filter.Save(cf);
                                }
                                else
                                {
                                    filter.Save(mainFilter);
                                }
                            }
                            #endregion
                        }
                        #endregion
                        #region TimeSpan (Ready)
                        else if (typeof(TimeSpan?).IsAssignableFrom(param.DataType))
                        {
                            #region SPECIAL CASES
                            #region If Exists (Ready)
                            if (WSConstants.ALIACES.EXIST.Match(Value) && param.DataType.IsNullable())
                            {
                                filter.Save(new WSTimeFFilter(param, member, WSTimeFFilter.OPERATIONS.NotEqual)
                                {
                                    Value = null
                                });
                            }
                            #endregion
                            #region If Empty (Ready)
                            else if (WSConstants.ALIACES.EMPTY.Match(Value) && param.DataType.IsNullable())
                            {
                                filter.Save(new WSTimeFFilter(param, member, null)
                                {
                                    Value = null
                                });
                            }
                            #endregion
                            #endregion

                            #region If Value (Ready)
                            else
                            {
                                if (param.DataType.Read(Value, out dVal, new char[] { }, WSConstants.DATE_LIST_SEPARATORS))
                                {
                                    WSOperation operation = WSTimeFFilter.OPERATIONS.GetOperation(state);

                                    Type dType = dVal == null ? param.DataType : dVal.GetType();
                                    if (dType.IsCollection() && operation != null)
                                    {
                                        dVal = operation.Match("max") ? ((List <dynamic>)dVal).Max(x => x) : operation.Match("min") ? ((List <dynamic>)dVal).Min(x => x) : dVal;
                                    }

                                    if (param.DataType.IsNullable())
                                    {
                                        WSCombineFilter cf = new WSCombineFilter(WSCombineFilter.SQLMode.AndAlso);

                                        if (Value != null)
                                        {
                                            cf.Save(new WSTimeFFilter(param, member, WSTimeFFilter.OPERATIONS.NotEqual)
                                            {
                                                Value = null
                                            });
                                        }

                                        cf.Save(new WSTimeFFilter(param, member, operation)
                                        {
                                            Value = dVal
                                        });

                                        filter.Save(cf);
                                    }
                                    else
                                    {
                                        filter.Save(new WSTimeFFilter(param, member, operation)
                                        {
                                            Value = dVal
                                        });
                                    }
                                }
                            }
                            #endregion
                        }
                        #endregion
                    }
                    #endregion
                }
                catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); if (CFunc != null)
                                      {
                                          CFunc.RegError(GetType(), e, ref status);
                                      }
                }
            }
            return(filter.Any() ? (filter.Count == 1 && !filter.Negate) ? filter.FirstOrDefault() : filter : null);
        }
Пример #10
0
        public override bool MatchEntity(MetaFunctions CFunc, WSDynamicEntity entity, WSTableSource src, string key = null, string matchOperation = null)
        {
            bool isMatch = true;

            try
            {
                if (!string.IsNullOrEmpty(key) && IsValid)
                {
                    matchOperation = matchOperation == null ? WSFieldFilter.GLOBAL_OPERATIONS.Equal.NAME : matchOperation;

                    WSTableParam param = src.DBPrimitiveParams.FirstOrDefault(p => p.Match(key));

                    if (param != null)
                    {
                        isMatch = Match(entity.GetType().GetProperty(param.WSColumnRef.NAME).GetValue(entity, null), matchOperation, param.DataType);
                    }
                }
            }
            catch (Exception e) { isMatch = false;  WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status); }
            return(isMatch);
        }
Пример #11
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);
        }
Пример #12
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);
        }
Пример #13
0
        internal override bool applyInternal(WSRequest Request, MetaFunctions CFunc)
        {
            try
            {
                if (Value != null && Value.IsValid)
                {
                    if (Value is WSJObject)
                    {
                        Func <WSJProperty, bool> READFunc = v => v.Key.StartsWith("$") && WSConstants.COMMAND_KEYS.READ.Match(v.Key.TrimStart(new char[] { '$' }));

                        WSJProperty READProperty = ((WSJObject)Value).Value.FirstOrDefault(READFunc);

                        if (READProperty != null)
                        {
                            string commandKey = READProperty.Key.TrimStart(new char[] { '$' });
                            WSJson temp       = new WSJArray();


                            #region MATCH / READ properties
                            //if (prop.Value is WSJObject && prop.Value.IsValid)
                            //{
                            //    #region apply $match command
                            //    if (WSConstants.COMMAND_KEYS.MATCH.Match(commandKey))
                            //    {
                            //        WSJProperty jMatch = ((WSJObject)prop.Value).Value[0];

                            //        #region SET $currentuser validation Filter
                            //        if (WSConstants.COMMAND_KEYS.CURRENT_USER.Match(jMatch.Key))
                            //        {
                            //            WSJson jUser = jMatch.Value.Clone();
                            //            if (jUser != null && jUser.IsValid)
                            //            {
                            //                Value[i] = new WSJValue(Request.Security.IsValidUser(jUser) ? "1" : "0");
                            //            }
                            //        }
                            //        #endregion
                            //    }
                            //    #endregion

                            //    #region apply $read command
                            //    else if (WSConstants.COMMAND_KEYS.READ.Match(commandKey))
                            //    {
                            //        WSJProperty jTarget = ((WSJObject)prop.Value).Value[0];
                            //        string targetKey = jTarget.Key.TrimStart(new char[] { '$' });

                            //        #region SET $currentuser validation Filter
                            //        if (WSConstants.COMMAND_KEYS.CURRENT_USER.Match(targetKey))
                            //        {
                            //            List<WSJson> items = new List<WSJson> { };
                            //            try
                            //            {
                            //                items.AddRange(
                            //                    (Request.Security.WSCurrentUser != null && Request.Security.WSCurrentUser.entity != null ? Request.Security.WSCurrentUser.entity.read(CFunc, jTarget.Value) : new List<dynamic> { })
                            //                    .Select(x =>
                            //                        new WSJValue((x as object).ToString())
                            //                    )
                            //                );
                            //            }
                            //            catch (Exception e) { CFunc.RegError(GetType(), e, ref Request.status); }
                            //            Value[i] = new WSJArray(items);
                            //        }
                            //        #endregion
                            //    }
                            //    #endregion

                            //    continue;
                            //}
                            #endregion


                            #region apply 'READ' property
                            if (WSConstants.COMMAND_KEYS.READ.Match(commandKey) && READProperty.Value.IsValid)
                            {
                                if (READProperty.Value is WSJObject)
                                {
                                    List <WSJProperty> props = ((WSJObject)READProperty.Value).Value;
                                    if (props != null && props.Any())
                                    {
                                        Func <WSJProperty, bool> CurUserFunc  = v => WSConstants.COMMAND_KEYS.CURRENT_USER.Match(v.Key);
                                        Func <WSJProperty, bool> ExplicitFunc = v => WSConstants.COMMAND_KEYS.EXPLICIT.Match(v.Key);

                                        if (props.Any(CurUserFunc))
                                        {
                                            #region SET $currentuser validation Filter
                                            WSJProperty CurrentUser = props.FirstOrDefault(CurUserFunc);
                                            if (CurrentUser != null /* && WSConstants.COMMAND_KEYS.CURRENT_USER.Match(CurrentUser.Key)*/)
                                            {
                                                List <WSJson> items = new List <WSJson> {
                                                };
                                                try
                                                {
                                                    items.AddRange(
                                                        (Request.Security.WSCurrentUser != null && Request.Security.WSCurrentUser.entity != null ? Request.Security.WSCurrentUser.entity.read(CFunc, CurrentUser.Value) : new List <dynamic> {
                                                    })
                                                        .Select(x =>
                                                                new WSJValue((x as object).ToString())
                                                                )
                                                        );
                                                }
                                                catch (Exception e) { CFunc.RegError(GetType(), e, ref Request.status); }
                                                temp = new WSJArray(items);
                                            }
                                            #endregion
                                        }
                                    }
                                }
                            }
                            #endregion


                            Value = temp;
                            return(true);
                        }
                    }
                }
                Value.apply(Request, CFunc);
            }
            catch (Exception e) { CFunc.RegError(GetType(), e, ref Request.status); }
            return(false);
        }
Пример #14
0
        public void WriteXml(XmlWriter writer)
        {
            try
            {
                writer.WriteStartElement(Name);
                Type type           = GetType();
                bool writeAllFields = WSParamList.IsEmpty(outFields);

                foreach (PropertyInfo x in type.GetProperties())
                {
                    string xName = x.Name.ToLower();

                    WSParam xParam = GetParam(x.Name, x.PropertyType);
                    if (xParam != null && role >= xParam.READ_ACCESS_MODE.ACCESS_LEVEL)
                    {
                        if (writeAllFields)
                        {
                            writer.WriteStartElement(xName);
                            WriteXmlValue(x.GetValue(this, null), null, x.PropertyType, writer);
                            writer.WriteEndElement();
                        }
                        else
                        {
                            WSParam outputParam = outFields.FirstOrDefault(a => a.Match(xName));
                            if (outputParam != null)
                            {
                                writer.WriteStartElement(xName);
                                WriteXmlValue(x.GetValue(this, null), outputParam, x.PropertyType, writer);
                                writer.WriteEndElement();
                            }
                        }
                    }
                }
                foreach (FieldInfo x in type.GetFields())
                {
                    string xName = x.Name.ToLower();

                    WSParam xParam = GetParam(x.Name, x.FieldType);
                    if (xParam != null && role >= xParam.READ_ACCESS_MODE.ACCESS_LEVEL)
                    {
                        if (writeAllFields)
                        {
                            writer.WriteStartElement(xName);
                            WriteXmlValue(x.GetValue(this), null, x.FieldType, writer);
                            writer.WriteEndElement();
                        }
                        else
                        {
                            WSParam outputParam = outFields.FirstOrDefault(a => a.Match(xName));
                            if (outputParam != null)
                            {
                                writer.WriteStartElement(xName);
                                WriteXmlValue(x.GetValue(this), outputParam, x.FieldType, writer);
                                writer.WriteEndElement();
                            }
                        }
                    }
                }
                writer.WriteEndElement();
            }
            catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status); }
        }
Пример #15
0
        internal override bool applyInternal(WSRequest Request, MetaFunctions CFunc)
        {
            if (Request != null)
            {
                try
                {
                    if (Value != null && Value.Any())
                    {
                        for (int i = 0; i < Value.Count; i++)
                        {
                            if (Value[i] is WSJObject)
                            {
                                WSJProperty prop = ((WSJObject)Value[i]).Value[0];
                                if (prop.Key.StartsWith("$"))
                                {
                                    string commandKey = prop.Key.TrimStart(new char[] { '$' });

                                    if (prop.Value is WSJObject && prop.Value.IsValid)
                                    {
                                        #region apply $match command
                                        if (WSConstants.COMMAND_KEYS.MATCH.Match(commandKey))
                                        {
                                            WSJProperty jMatch = ((WSJObject)prop.Value).Value[0];

                                            #region SET $currentuser validation Filter
                                            if (WSConstants.COMMAND_KEYS.CURRENT_USER.Match(jMatch.Key))
                                            {
                                                WSJson jUser = jMatch.Value.Clone();
                                                if (jUser != null && jUser.IsValid)
                                                {
                                                    Value[i] = new WSJValue(Request.Security.IsValidUser(jUser) ? "1" : "0");
                                                }
                                            }
                                            #endregion
                                        }
                                        #endregion

                                        #region apply $read command
                                        else if (WSConstants.COMMAND_KEYS.READ.Match(commandKey))
                                        {
                                            WSJProperty jTarget   = ((WSJObject)prop.Value).Value[0];
                                            string      targetKey = jTarget.Key.TrimStart(new char[] { '$' });

                                            #region SET $currentuser validation Filter
                                            if (WSConstants.COMMAND_KEYS.CURRENT_USER.Match(targetKey))
                                            {
                                                List <WSJson> items = new List <WSJson> {
                                                };
                                                try
                                                {
                                                    items.AddRange(
                                                        (Request.Security.WSCurrentUser != null && Request.Security.WSCurrentUser.entity != null ? Request.Security.WSCurrentUser.entity.read(CFunc, jTarget.Value) : new List <dynamic> {
                                                    })
                                                        .Select(x =>
                                                                new WSJValue((x as object).ToString())
                                                                )
                                                        );
                                                }
                                                catch (Exception e) { CFunc.RegError(GetType(), e, ref Request.status); }
                                                Value[i] = new WSJArray(items);
                                            }
                                            #endregion
                                        }
                                        #endregion

                                        continue;
                                    }
                                }
                            }
                            Value[i].apply(Request, CFunc);
                        }
                    }
                    return(true);
                }
                catch (Exception e) { CFunc.RegError(GetType(), e, ref Request.status); }
            }
            return(false);
        }