Example #1
0
 internal WSJObject getBaseJson(byte UserRole)
 {
     if (Source != null && !Source.IsBase)
     {
         WSJArray    jFields = new WSJArray();
         WSJProperty jProp   = new WSJProperty(Name.ToLower(), jFields);
         WSJObject   json    = new WSJObject(new List <WSJProperty> {
             jProp
         });
         try
         {
             if (Fields != null && Fields.Any())
             {
                 IEnumerable <WSMemberSchema> acessibleSchemaes = Fields
                                                                  .Where(x => x is WSPrimitiveFieldSchema && UserRole >= ((WSPrimitiveFieldSchema)x).param.READ_ACCESS_MODE.ACCESS_LEVEL);
                 if (acessibleSchemaes != null && acessibleSchemaes.Any())
                 {
                     IEnumerable <WSJson> _fields = acessibleSchemaes.Select(f => new WSJValue(((WSPrimitiveFieldSchema)f).param.DISPLAY_NAME.ToLower()));
                     jFields.Value = _fields != null && _fields.Any(x => x != null) ? _fields.Where(x => x != null).ToList() : new List <WSJson>();
                 }
             }
         }
         catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); Func.RegError(GetType(), e, ref status); }
         return(json);
     }
     return(null);
 }
Example #2
0
 public static WSJson ToJson(this string jText)
 {
     lock (ToJsonLock)
     {
         WSJson json = null;
         try
         {
             jText = jText == null ? null : jText.Trim();
             if (!string.IsNullOrEmpty(jText))
             {
                 //WSJObject
                 if ('{' == jText.FirstOrDefault() && '}' == jText.LastOrDefault())
                 {
                     json = JsonConvert.DeserializeObject <WSJson>(jText, new WSFilterConverter());
                 }
                 //WSJArray
                 else if ('[' == jText.FirstOrDefault() && ']' == jText.LastOrDefault())
                 {
                     WSJObject jObj = (WSJObject)JsonConvert.DeserializeObject <WSJson>("{json:" + jText + "}", new WSFilterConverter());
                     json = jObj.Value[0].Value;
                 }
                 //WSJValue
                 else
                 {
                     json = new WSJValue(jText);
                 }
             }
         }
         catch (Exception) { }
         return(json);
     }
 }
Example #3
0
 private void proceedRootParam(WSJObject jObj)
 {
     if (jObj.Value.Any())
     {
         proceedRootParam(jObj.Value[0]);
     }
 }
Example #4
0
        private void setUp(WSJProperty _Json)//Json = > {source:[id,name,...]}  or {source:{id:1,name:abc,...}}  or {source:{filters:[],fields:{id:1,name:abc,...}}}
        {
            if (Source != null && _Json.IsValid)
            {
                OriginalJson = new WSJObject(new List <WSJProperty> {
                    _Json
                });
                Name = _Json.Key;

                List <WSJson> jFields = new List <WSJson>();

                CombineMode        = _Json.Value is WSJObject ? WSCombineFilter.SQLMode.AndAlso : _Json.Value is WSJArray ? CombineMode = WSCombineFilter.SQLMode.OrElse : WSCombineFilter.SQLMode.AndAlso;
                Fields.CombineMode = CombineMode;

                if (_Json.Value is WSJObject)       //Json = > {source:/***{JFIELDS}***/}  or {source:/***{filters:JFIELDS,fields:JFIELDS}***/}
                {                                   //                     ---------                      --------------------------------
                    jFields.AddRange(((WSJObject)_Json.Value).Value);
                }
                else if (_Json.Value is WSJArray)   //Json = > {source:/***[JFIELDS]***/}  or {source:/***[{filters:JFIELDS},{fields:JFIELDS}]***/}
                {                                   //                     ---------                      ------------------------------------
                    jFields.AddRange(((WSJArray)_Json.Value).Value);
                }
                else if (_Json.Value is WSJValue)   //Json = > {source:/***[JFIELD]***/}  or {source:/***[{filters:JFIELD},{fields:JFIELD}]***/}
                {                                   //                     ---------                      ------------------------------------
                    jFields.Add(_Json.Value);
                }

                foreach (WSJson item in jFields)
                {
                    proceedRootParam(item);
                }
            }
        }
Example #5
0
        internal WSJObject getFieldsJson(byte UserRole, List <Type> readChildes = null, string aliace = null)
        {
            if (UserRole <= WSConstants.ACCESS_LEVEL.READ)
            {
                return(getBaseJson(UserRole));
            }                                                                                //make sure hide multilevel for not registered users
            else if (Source != null && !Source.IsBase)
            {
                WSJArray    jFields = new WSJArray();
                WSJProperty jProp   = new WSJProperty(string.IsNullOrEmpty(aliace) ? Name.ToLower() : aliace, jFields);
                WSJObject   json    = new WSJObject(new List <WSJProperty> {
                    jProp
                });
                if (Fields != null && Fields.Any())
                {
                    IEnumerable <WSBaseSchema> acessibleSchemaes = Fields.Where(x =>
                                                                                (x is WSPrimitiveFieldSchema && UserRole >= ((WSPrimitiveFieldSchema)x).param.READ_ACCESS_MODE.ACCESS_LEVEL)
                                                                                ||
                                                                                (x is WSEntityFieldSchema && UserRole >= ((WSEntityFieldSchema)x).SOURCE.AccessLevel)
                                                                                );
                    if (acessibleSchemaes != null && acessibleSchemaes.Any())
                    {
                        List <WSJson> _fields = new List <WSJson>();
                        foreach (WSBaseSchema f in acessibleSchemaes)
                        {
                            if (f is WSPrimitiveFieldSchema)
                            {
                                _fields.Add(new WSJValue(((WSPrimitiveFieldSchema)f).param.DISPLAY_NAME.ToLower()));
                            }
                            else if (f is WSEntityFieldSchema)
                            {
                                List <Type> _readChildes = new List <Type>()
                                {
                                };
                                if (readChildes != null)
                                {
                                    _readChildes.AddRange(readChildes);
                                }

                                if (!_readChildes.Any(p => p == Source.ReturnType))
                                {
                                    _readChildes.Add(Source.ReturnType);
                                    _fields.Add(((WSEntityFieldSchema)f).SOURCE.BaseSchema.getFullJson(UserRole, _readChildes, ((WSEntityFieldSchema)f).param.DISPLAY_NAME.ToLower()));
                                }
                            }
                        }
                        jFields.Value = _fields != null && _fields.Any(x => x != null) ? _fields.Where(x => x != null).ToList() : new List <WSJson>();
                    }
                }
                return(json);
            }
            return(null);
        }
Example #6
0
        public override bool Match(WSJson json, out WSStatus status)
        {
            status = WSStatus.NONE_Copy();
            try
            {
                if (json == null)
                {
                    status = WSStatus.ERROR_Copy(); return(false);
                }
                else if (!(json is WSJObject))
                {
                    status = WSStatus.ERROR_Copy(); return(false);
                }
                else
                {
                    WSJObject            jObj  = (WSJObject)json;
                    IEnumerable <string> keys  = Value.Select(v1 => v1.Key);
                    IEnumerable <string> keys1 = jObj.Value.Select(v1 => v1.Key);
                    if (keys1.Any(p1 => !keys.Any(p => p1.Equals(p))))
                    {
                        status = WSStatus.ERROR_Copy();
                        status.AddNote($"An original service generates response with additional properties:[{keys1.Where(p1 => !keys.Any(p => p1.Equals(p))).Aggregate((a,b)=>a+","+b)}] which is not exists in current object.");
                    }
                    else if (keys.Count() != keys1.Count())
                    {
                        status = WSStatus.ERROR_Copy();
                        status.AddNote($"The current service generates response with additional properties:[{keys.Where(p1 => !keys1.Any(p => p1.Equals(p))).Aggregate((a, b) => a + "," + b)}] which is not exists in original object.");
                    }
                    else
                    {
                        foreach (WSJProperty jProp1 in jObj.Value)
                        {
                            WSJProperty jProp = Value.FirstOrDefault(x => x.Key.Equals(jProp1.Key));

                            WSStatus pStatus = jProp.Match(jProp1, out pStatus) ? WSStatus.SUCCESS_Copy() : pStatus;

                            status = pStatus;

                            if (pStatus.CODE != WSStatus.SUCCESS.CODE)
                            {
                                return(false);
                            }
                        }
                        return(!status.childs.Any(x => x.CODE != WSStatus.SUCCESS.CODE));
                    }
                }
            }
            catch (Exception) { }
            return(status.CODE == WSStatus.SUCCESS.CODE);
        }
        protected override WSJson ToJson(JToken token)
        {
            WSJson json = null;

            if (token != null)
            {
                try
                {
                    if (token is JValue)
                    {
                        JValue inner = (JValue)token;
                        json = new WSJValue(inner.Value == null ? null : inner.Value.ToString());
                    }
                    else if (token is JProperty)
                    {
                        JProperty inner = (JProperty)token;
                        json = new WSJProperty(inner.Name, ToJson(inner.Value));
                    }
                    else if (token is JArray)
                    {
                        JArray   inner  = (JArray)token;
                        WSJArray jArray = new WSJArray();
                        jArray.Value.AddRange(inner.Select(x => ToJson(x)).Where(x => x.IsValid));
                        json = jArray;
                    }
                    else if (token is JObject)
                    {
                        JObject inner = (JObject)token;
                        IEnumerable <WSJProperty> props = inner.Children <JProperty>().Select(x => new WSJProperty(x.Name, ToJson(x.Value)));
                        WSJObject jObject = new WSJObject(props.ToList());
                        json = jObject;
                    }
                }
                catch (Exception) { }
            }
            return(json);
        }
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);
        }
 public WSEntityListSchema(WSTableParam _Param, WSJProperty _Json, MetaFunctions _Func, WSEntitySchema _Parent) : base(_Func, _Parent)
 {
     Param = _Param;
     if (_Json != null && !_Json.IsEmpty)
     {
         if (WSEntityFilter.OPERATIONS.STATE_OPERATIONS.Any(x => x.Match(_Json.Key)))
         {
             IOBaseOptions.Save(new WSJObject(new List <WSJProperty>()
             {
                 _Json
             }));
         }
         else if (_Json.Value != null)
         {
             if (_Json.Value is WSJArray)
             {
                 WSJArray arr  = (WSJArray)_Json.Value;
                 WSJArray temp = new WSJArray();
                 foreach (WSJson item in arr.Value)
                 {
                     if (item is WSJValue && WSEntityFilter.OPERATIONS.STATE_OPERATIONS.Any(x => x.Match(((WSJValue)item).Value)))
                     {
                         IOBaseOptions.Save(item);
                     }
                     else
                     {
                         temp.Value.Add(item);
                     }
                 }
                 _Json.Value = temp;
             }
             else if (_Json.Value is WSJObject)
             {
                 WSJObject          obj       = (WSJObject)_Json.Value;
                 List <WSJProperty> tempItems = new List <WSJProperty>();
                 WSJObject          temp      = new WSJObject(tempItems);
                 WSJObject          baseLocal = new WSJObject(tempItems);
                 foreach (WSJProperty item in obj.Value)
                 {
                     if (WSEntityFilter.OPERATIONS.STATE_OPERATIONS.Any(x => x.Match(item.Key)))
                     {
                         baseLocal.Value.Add(item);
                     }
                     else if (item.Value is WSJValue && WSEntityFilter.OPERATIONS.STATE_OPERATIONS.Any(x => x.Match(((WSJValue)item.Value).Value)))
                     {
                         baseLocal.Value.Add(item);
                     }
                     else if (item.Value is WSJArray && !item.Value.IsEmpty && !((WSJArray)item.Value).Value.Any(v => !(v is WSJValue) || !WSEntityFilter.OPERATIONS.STATE_OPERATIONS.Any(x => x.Match(((WSJValue)v).Value))))
                     {
                         baseLocal.Value.Add(item);
                     }
                     else if (item.Value is WSJObject && !item.Value.IsEmpty && !((WSJObject)item.Value).Value.Any(p => !(p.Value is WSJValue) || !WSEntityFilter.OPERATIONS.STATE_OPERATIONS.Any(x => x.Match(((WSJValue)p.Value).Value))))
                     {
                         baseLocal.Value.Add(item);
                     }
                     else
                     {
                         temp.Value.Add(item);
                     }
                 }
                 _Json.Value = temp;
                 if (!baseLocal.IsEmpty)
                 {
                     IOBaseOptions.Save(baseLocal);
                 }
             }
         }
     }
     if (Param != null)
     {
         EntitySchema = new WSEntitySchema((WSTableSource)Func.GetSourceByType(Param.DataType.GetEntityType()), _Json, Func, Parent);
         if (EntitySchema != null)
         {
             Name = EntitySchema.Name;
         }
     }
 }
Example #10
0
 private void proceedFieldFilter(WSJObject jObj, ref WSFieldFilters filters)
 {
     //TODO@ANDVO : make it possible to add all comming properties!!!
     proceedFieldFilter(jObj.Value[0], ref filters);
 }