Ejemplo n.º 1
0
 public WSRecord(MetaFunctions _CFunc, WSEntity _entity, byte _role, WSSchema _schema, WSParamList _outFields = null, string _Name = null)
 {
     CFunc     = _CFunc;
     Name      = string.IsNullOrEmpty(_Name) ? GetType().Name : _Name;
     schema    = _schema;
     outFields = _outFields;
     entity    = _entity;
     role      = _role;
 }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
 public virtual bool Match(WSRequest Request, WSDataContext DBContext, MetaFunctions CFunc, WSSchema schema)
 {
     return(true);
 }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        private bool Validate(object obj, WSParam xParam, JsonWriter writer, JsonSerializer serializer, WSSchema schema, WSSource xSource, WSParamList outFields, ref WSStatus status, WSRequest Request, MetaFunctions CFunc)
        {
            try
            {
                if (this is WSDynamicEntity && schema == null)
                {
                    status.CODE = WSStatus.ERROR.CODE; status.AddNote("No schema defined", WSConstants.ACCESS_LEVEL.READ);
                }
                else if (xParam == null)
                {
                    status.CODE = WSStatus.ERROR.CODE; status.AddNote("Undefined parameters are not allowed", WSConstants.ACCESS_LEVEL.READ);
                }
                else
                {
                    bool IsOwner          = false;//TODO@ANDVO:2015-09-11 : ADD IsOwner validation check
                    int  paramAccessLevel = xParam.READ_ACCESS_MODE.ACCESS_LEVEL;
                    paramAccessLevel = (xSource != null && xSource.AccessLevel > paramAccessLevel) ? xSource.AccessLevel : paramAccessLevel;

                    if (Request.Security.AuthToken.User.role < paramAccessLevel && !IsOwner)
                    {
                        #region ACCESS DENIED HANDLING
                        if (xSource != null && xSource.ShowMessageInaccessible)
                        {
                            string accessNote = "Access denied";
                            status.CODE = WSStatus.ATTANTION.CODE;
                            status.AddNote(accessNote, WSConstants.ACCESS_LEVEL.READ);
                            WritePropName(writer, xParam.NAME);
                            serializer.Serialize(writer, accessNote);
                        }
                        #endregion
                    }
                    else
                    {
                        if (!WSParamList.IsEmpty(outFields) && !outFields.Any(a => a.Match(schema.Name)))
                        {
                        }
                        else
                        {
                            if (obj == null && !xParam.SkipEmpty)
                            {
                                obj         = string.Empty;
                                status.CODE = WSStatus.ATTANTION.CODE;
                                status.AddNote("Can not write NULL to [" + xParam.DISPLAY_NAME + "]. Value set to empty string.", WSConstants.ACCESS_LEVEL.READ);
                            }

                            if (obj != null)
                            {
                                status.AddNote("done", WSConstants.ACCESS_LEVEL.READ);
                                return(true);
                            }
                        }
                    }
                }
            }
            catch (Exception e) {
                status.CODE = WSStatus.ERROR.CODE; status.AddNote("Error(line" + e.LineNumber() + "- " + e.Message + ")");
                CFunc.RegError(GetType(), e, ref status);
            }
            return(false);
        }
Ejemplo n.º 6
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);
        }