Exemple #1
0
        private WSDynamicEntity ReadSession(/*WSDataContext ZoneContext*/)
        {
            WSDynamicEntity entity = null;

            if (Meta != null && !string.IsNullOrEmpty(SessionID) && ZoneContext != null)
            {
                long init_ticks = DateTime.Now.Ticks;
                try
                {
                    status.AddNote("Zone:" + Meta.Zone, WSConstants.ACCESS_LEVEL.READ);

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

                    var tObj = mInfo.MakeGenericMethod(new Type[] { Meta.SessionType }).Invoke(ZoneContext, new object[] { });

                    Func <WSDynamicEntity, bool> func = s => s.readPropertyValue(WSConstants.PARAMS.SESSIONID.NAME, "").ToString().ToLower().Equals(SessionID.ToLower());

                    System.Reflection.MethodInfo[] methods = typeof(Enumerable).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);

                    var method = methods.FirstOrDefault(m => m.Name == "FirstOrDefault" && m.GetParameters().Count() == 2).MakeGenericMethod(typeof(WSDynamicEntity));

                    entity = (WSDynamicEntity)method.Invoke(null, new object[] { tObj, func });
                }
                catch (Exception e) { CFunc.RegError(GetType(), e, ref status); }

                //if (ZoneContext != null) ZoneContext.Close(/*SessionID*/);

                TimeSpan ticks1 = new TimeSpan(DateTime.Now.Ticks - init_ticks); init_ticks = DateTime.Now.Ticks;
            }
            return(entity);
        }
 private List <dynamic> readEntity(MetaFunctions CFunc, WSJProperty jProp, WSDynamicEntity _entity, bool multydimentional = false)
 {
     try
     {
         if (_entity != null)
         {
             WSTableSource src   = (WSTableSource)_entity.getSource(CFunc);
             WSTableParam  param = src.DBParams.FirstOrDefault(p => p.Match(jProp.Key));
             PropertyInfo  pInfo = src.ReturnType.GetProperties().FirstOrDefault(p => p.Name.Equals(param.WSColumnRef.NAME));
             dynamic       val   = pInfo.GetValue(_entity, null);
             Type          pType = pInfo.PropertyType.GetEntityType();
             if (pInfo.PropertyType.IsCollectionOf <WSDynamicEntity>())
             {
                 IEnumerable <WSDynamicEntity> entities = (IEnumerable <WSDynamicEntity>)val;
                 List <dynamic> subItems = new List <dynamic>();
                 foreach (WSDynamicEntity iEntity in entities)
                 {
                     subItems.AddRange(read(CFunc, jProp.Value, iEntity, multydimentional));
                 }
                 return(subItems);
             }
             else
             {
                 return(read(CFunc, jProp.Value, val, multydimentional));
             }
         }
     }
     catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status, $"readEntity():284"); }
     return(null);
 }
Exemple #3
0
        public static void SetPropertyValue(this PropertyInfo prop, WSDynamicEntity entity, object val)
        {
            try
            {
                bool?is45 = null;
#if NET45
                prop.SetValue(entity, val);
                is45 = true;
#endif

#if NET4
                is45 = false;
                prop.SetValue(entity, val, null);
#endif

                if (is45 == null)
                {
                    //try {
                    //    prop.SetValue(entity, val);
                    //} catch {
                    prop.SetValue(entity, val, null);
                    //}
                }
            }
            catch (Exception e) { }
        }
Exemple #4
0
        public override bool MatchEntity(MetaFunctions CFunc, WSDynamicEntity entity, WSTableSource src, string key = null, string matchOperation = null)
        {
            bool _match = true;//insure that the empty array gives true

            try
            {
                foreach (WSJson jItem in Value)
                {
                    _match = false;//insure that the NOT empty array gives false on failure
                    if (jItem is WSJValue && !string.IsNullOrEmpty(key))
                    {
                        _match = ((WSJValue)jItem).MatchEntity(CFunc, entity, src, key, WSFieldFilter.GLOBAL_OPERATIONS.Equal.NAME);
                    }
                    else if (jItem is WSJObject)
                    {
                        _match = ((WSJObject)jItem).MatchEntity(CFunc, entity, src, key);
                    }
                    else if (jItem is WSJArray)
                    {
                        _match = ((WSJArray)jItem).MatchEntity(CFunc, entity, src, key);
                    }
                    if (_match)
                    {
                        break;
                    }
                }
            }
            catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status); }
            return(_match);
        }
        public List <dynamic> read(MetaFunctions CFunc, WSJson json, WSDynamicEntity _entity = null, bool multydimentional = false)
        {
            List <dynamic> result = new List <dynamic>();

            try
            {
                _entity = _entity == null ? this : _entity;
                if (json is WSJValue)
                {
                    dynamic val = readPrimitive(CFunc, (WSJValue)json, _entity); if (val != null)
                    {
                        result.Add(val);
                    }
                }
                else if (json is WSJArray)
                {
                    foreach (WSJson item in ((WSJArray)json).Value)
                    {
                        List <dynamic> val = read(CFunc, item, _entity, multydimentional);
                        if (multydimentional)
                        {
                            result.Add(val);
                        }
                        else
                        {
                            result.AddRange(val);
                        }
                    }
                }
                else if (json is WSJObject)
                {
                    List <dynamic> val = readEntity(CFunc, ((WSJObject)json).Value[0], _entity, multydimentional);
                    if (val != null && val.Any())
                    {
                        if (multydimentional)
                        {
                            result.Add(val);
                        }
                        else
                        {
                            result.AddRange(val);
                        }
                    }
                }
            }
            catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status, $"read():357"); }
            return(result);
        }
 private dynamic readPrimitive(MetaFunctions CFunc, WSJValue jVal, WSDynamicEntity _entity)
 {
     try
     {
         if (_entity != null)
         {
             Type          eType = _entity.GetType();
             WSTableSource src   = ((WSTableSource)CFunc.GetSourceByType(eType));
             WSTableParam  param = src.DBParams.FirstOrDefault(p => p.Match(jVal.Value));
             PropertyInfo  pInfo = eType.GetProperties().FirstOrDefault(p => p.Name.Equals(param.WSColumnRef.NAME));
             return(pInfo.GetValue(_entity, null));
         }
     }
     catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status, $"readPrimitive():373"); }
     return(null);
 }
        internal WSAccessKeyEntity GenerateWSAccessKey()
        {
            WSAccessKeyEntity key = null;

            if (Request.Security.IsLogged && Request.Security.AuthToken.User.role >= WSConstants.ACCESS_LEVEL.ADMIN && Request.INPUT.Any(x => WSConstants.ALIACES.USER_ID.Match(x.Key)))
            {
                using (WSDataContext DBContext = GetInternalContext(Request.Meta.DB, Request.ID, $"{GetType().Name}.GenerateWSAccessKey()"))
                {
                    string            UserID      = Request.INPUT.ReadValue(WSConstants.ALIACES.USER_ID, out UserID) ? UserID : null;
                    Func <Type, bool> userFunc    = a => a.Name.Equals("User");
                    PropertyInfo      sessionProp = /*Request.*/ DBContext
                                                    .GetType()
                                                    .GetProperties()
                                                    .FirstOrDefault(x => x.PropertyType.GetGenericTypeArguments().Any(userFunc));

                    Type userType = sessionProp == null ? null : sessionProp
                                    .PropertyType
                                    .GetGenericTypeArguments()
                                    .FirstOrDefault(userFunc);

                    if (userType != null)
                    {
                        System.Reflection.MethodInfo mInfo = /*Request.*/ DBContext.GetType().GetMethod("GetTable", new Type[] { });

                        var UObj = mInfo.MakeGenericMethod(new Type[] { userType }).Invoke(/*Request.*/ DBContext, new object[] { });

                        Func <WSDynamicEntity, bool> func = s => s.getIdentities(ClientFunctions).FirstOrDefault(i => i.Key.ToLower().Equals("userid")).Value.ToString().ToLower().Equals(UserID.ToLower());

                        System.Reflection.MethodInfo[] methods = typeof(Enumerable).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);

                        var method = methods.FirstOrDefault(m => m.Name == "FirstOrDefault" && m.GetParameters().Count() == 2).MakeGenericMethod(typeof(WSDynamicEntity));

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

                        if (user != null)
                        {
                            object pass = null;
                            if (user.TryReadPropertyValue("Password", out pass))
                            {
                                key = new WSAccessKeyEntity(UserID.ToLower(), Request.Security.generateKey(new string[] { UserID.ToLower(), pass.ToString() }));
                            }
                        }
                    }
                }
            }
            return(key);
        }
        private bool Match(ClientFunctions CFunc, WSDynamicEntity refEntity)
        {
            try
            {
                if (refEntity == null)
                {
                    return(false);
                }
                else if (refEntity.GetType() != GetType())
                {
                    return(false);
                }
                else
                {
                    Type          orgType = GetType();
                    WSTableSource orgSrc  = (WSTableSource)getSource(CFunc);
                    IEnumerable <WSTableParam> orgParams = orgSrc.DBParams.Where(p => p.DataType.IsSimple());

                    Type          refType = refEntity.GetType();
                    WSTableSource refSrc  = ((WSTableSource)CFunc.GetSourceByType(refType));
                    IEnumerable <WSTableParam> refParams = refSrc.DBParams.Where(p => p.DataType.IsSimple());

                    IEnumerable <WSTableParam> orgExceptParams = orgParams.Where(p1 => !refParams.Any(p2 => p2.Match(p1)));
                    IEnumerable <WSTableParam> refExceptParams = refParams.Where(p1 => !orgParams.Any(p2 => p2.Match(p1)));

                    if (orgExceptParams.Any() || refExceptParams.Any())
                    {
                        return(false);
                    }
                    else
                    {
                        foreach (WSTableParam param in orgParams)
                        {
                            object orgInfo = orgType.GetProperties().FirstOrDefault(p => p.Name.Equals(param.WSColumnRef.NAME)).GetValue(this, null);
                            object refInfo = refType.GetProperties().FirstOrDefault(p => p.Name.Equals(param.WSColumnRef.NAME)).GetValue(refEntity, null);
                            if (!(orgInfo == null && refInfo == null) && !orgInfo.ToString().Equals(refInfo.ToString()))
                            {
                                return(false);
                            }
                        }
                        return(true);
                    }
                }
            }
            catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status, $"Match():321"); }
            return(false);
        }
Exemple #9
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);
        }
        public bool IsRelationTo(ClientFunctions CFunc, WSDynamicEntity _RelationTEntity, ref WSStatus _statusLines, IEnumerable <Type> _refTypes = null)
        {
            bool _IsRelationTo = false;

            try
            {
                if (_RelationTEntity != null)
                {
                    WSTableSource orgSrc = (WSTableSource)getSource(CFunc);
                    WSTableSource relSrc = (WSTableSource)_RelationTEntity.getSource(CFunc);
                    if (relSrc != null)
                    {
                        WSTableParam refParam = orgSrc.DBParams.FirstOrDefault(x => x.DataType == relSrc.ReturnType);
                        if (refParam == null)
                        {
                            IEnumerable <Type>         refTypes = _refTypes == null ? new List <Type>() : _refTypes.Select(x => x);
                            IEnumerable <WSTableParam> eParams  = orgSrc.DBParams.Any() ? orgSrc.DBParams.Where(x => x.DataType.IsValidDynamicEntity() && !x.DataType.IsCollection() && !refTypes.Any(t => t == x.DataType)) : null;
                            if (eParams != null && eParams.Any())
                            {
                                foreach (WSTableParam eParam in eParams)
                                {
                                    object          pValue  = null;
                                    WSDynamicEntity pEntity = TryReadPropertyValue(eParam.WSColumnRef.NAME, out pValue, null) ? (WSDynamicEntity)pValue : null;

                                    _IsRelationTo = pEntity.IsRelationTo(CFunc, _RelationTEntity, ref _statusLines, refTypes);

                                    if (_IsRelationTo)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            object          PValue    = null;
                            WSDynamicEntity RefEntity = TryReadPropertyValue(refParam.WSColumnRef.NAME, out PValue, null) ? (WSDynamicEntity)PValue : null;

                            _IsRelationTo = RefEntity != null && _RelationTEntity.Match(CFunc, RefEntity);
                        }
                    }
                }
            }
            catch (Exception e) { CFunc.RegError(GetType(), e, ref _statusLines, $"IsRelationTo():256"); }
            return(_IsRelationTo);
        }
        public WSDynamicEntity getRelatedParent <A>(ClientFunctions CFunc, ref WSStatus _statusLines, IEnumerable <Type> _refTypes = null)
        {
            WSDynamicEntity relEntity = null;

            try
            {
                WSTableSource orgSrc = (WSTableSource)getSource(CFunc);
                WSTableSource relSrc = (WSTableSource)CFunc.GetSourceByType(typeof(A));
                if (relSrc != null)
                {
                    WSTableParam refParam = orgSrc.DBParams.FirstOrDefault(x => x.DataType == relSrc.ReturnType);
                    if (refParam == null)
                    {
                        IEnumerable <Type>         refTypes = _refTypes == null ? new List <Type>() : _refTypes.Select(x => x);
                        IEnumerable <WSTableParam> eParams  = orgSrc.DBParams.Any() ? orgSrc.DBParams.Where(x => x.DataType.IsValidDynamicEntity() && !x.DataType.IsCollection() && !refTypes.Any(t => t == x.DataType)) : null;
                        if (eParams != null && eParams.Any())
                        {
                            foreach (WSTableParam eParam in eParams)
                            {
                                object          pValue  = null;
                                WSDynamicEntity pEntity = TryReadPropertyValue(eParam.WSColumnRef.NAME, out pValue, null) ? (WSDynamicEntity)pValue : null;

                                relEntity = pEntity.getRelatedParent <A>(CFunc, ref _statusLines, refTypes);

                                if (relEntity != null)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        object PValue = null;
                        relEntity = TryReadPropertyValue(refParam.WSColumnRef.NAME, out PValue, null) ? (WSDynamicEntity)PValue : null;
                    }
                }
            }
            catch (Exception e) { CFunc.RegError(GetType(), e, ref _statusLines, $"getRelatedParent():211"); }
            return(relEntity);
        }
 public override bool Equals(object obj)
 {
     try
     {
         if (obj == null)
         {
             return(false);
         }
         if (obj.GetType() != GetType())
         {
             return(false);
         }
         WSTableSource eSchema = WSServerMeta.SYSTEM_SOURCES.GetSourceByType(GetType());
         if (eSchema == null)
         {
             return(base.Equals(obj));
         }
         else
         {
             WSDynamicEntity eObj = (WSDynamicEntity)obj;
             foreach (WSTableParam param in eSchema.DBPrimitiveParams)
             {
                 object p1 = readPropertyValue(param.WSColumnRef.NAME);
                 object p2 = eObj.readPropertyValue(param.WSColumnRef.NAME);
                 if (p1 != null)
                 {
                     if (!p1.Equals(p2))
                     {
                         return(false);
                     }
                 }
                 else if (p2 != null)
                 {
                     return(false);
                 }
             }
         }
     }
     catch (Exception) { return(false); }
     return(true);
 }
Exemple #13
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);
        }
Exemple #14
0
        private WSSession ReadWSSession(string dbName)
        {
            WSSession session = null;

            if (!string.IsNullOrEmpty(dbName))
            {
                try
                {
                    WSSecurityMeta meta = SecurityMap[dbName];
                    session = new WSSession(Request.SessionID, meta);

                    /**********************
                     * ANDVO@NOTE:
                     * DO NOT do [ZoneContext = Request.ZoneContext;]
                     * because it will use Request's zone, when it MUST BE zone for the argument:'dbName' !!!
                     * */
                    using (WSDataContext ZoneContext = GetInternalContext(meta.Zone, Request.ID, $"{GetType().Name}.ReadWSSession('{dbName}')"))
                    {
                        if (ZoneContext != null && !ZoneContext.IsDisposed && ZoneContext.Connection.State == System.Data.ConnectionState.Open)
                        {
                            if (meta.SessionType != null)
                            {
                                MethodInfo mInfo = ZoneContext.GetType().GetMethod("GetTable", new Type[] { });

                                var tObj = mInfo.MakeGenericMethod(new Type[] { meta.SessionType }).Invoke(ZoneContext, new object[] { });

                                Func <WSDynamicEntity, bool> func = s => s.readPropertyValue(WSConstants.PARAMS.SESSIONID.NAME, "").ToString().ToLower().Equals(Request.SessionID.ToLower());

                                MethodInfo[] methods = typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public);

                                var method = methods.FirstOrDefault(m => m.Name == "FirstOrDefault" && m.GetParameters().Count() == 2).MakeGenericMethod(typeof(WSDynamicEntity));

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

                                if (sessionEntity != null)
                                {
                                    WSDynamicEntity userEntity = (WSDynamicEntity)sessionEntity.GetType().GetProperties().Single(x => x.PropertyType == meta.UserType).GetValue(sessionEntity, null);

                                    if (userEntity != null)
                                    {
                                        object _id        = userEntity.TryReadPropertyValue("UserID", out _id) ? _id : null;
                                        object _email     = userEntity.TryReadPropertyValue("Email", out _email) ? _email : null;
                                        object _firstname = userEntity.TryReadPropertyValue("FirstName", out _firstname) ? _firstname : null;
                                        object _lastname  = userEntity.TryReadPropertyValue("LastName", out _lastname) ? _lastname : null;
                                        object _isactive  = userEntity.TryReadPropertyValue("IsActive", out _isactive) ? _isactive : null;
                                        object _login     = userEntity.TryReadPropertyValue("Login", out _login) ? _login : null;

                                        WSDynamicEntity roleEntity = (WSDynamicEntity)userEntity.GetType().GetProperties().FirstOrDefault(x => x.PropertyType == meta.RoleType).GetValue(userEntity, null);

                                        if (roleEntity != null)
                                        {
                                            object _role     = roleEntity.TryReadPropertyValue("ID", out _role) ? _role : null;
                                            object _roleName = roleEntity.TryReadPropertyValue("Name", out _roleName) ? _roleName : null;

                                            int    id    = int.TryParse(_id.ToString(), out id) ? id : -1;
                                            string email = _email != null?_email.ToString() : string.Empty;

                                            string login = _login != null?_login.ToString() : string.Empty;

                                            string firstname = _firstname != null?_firstname.ToString() : string.Empty;

                                            string lastname = _lastname != null?_lastname.ToString() : string.Empty;

                                            bool   isactive = bool.TryParse(_isactive.ToString(), out isactive) ? isactive : false;
                                            byte   role     = byte.TryParse(_role == null ? null : _role.ToString(), out role) ? role : WSConstants.DEFAULT_USER_ROLE;
                                            string roleName = _roleName != null?_roleName.ToString() : string.Empty;

                                            session.user = new WSUserToken()
                                            {
                                                id        = id,
                                                email     = email,
                                                login     = login,
                                                firstname = firstname,
                                                lastname  = lastname,
                                                isactive  = isactive,
                                                role      = role,
                                                roleName  = roleName
                                            };
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e) { RegError(GetType(), e, ref LoadStatus); }
            }
            return(session);
        }
        public bool TrySetRecordValue(string colName, object newValue, WSDataContext DBContext, MetaFunctions CFunc, Func <Exception, bool> AddError = null)
        {
            bool done = false;

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

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

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

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

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

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

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

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

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

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

                    if (!done)
                    {
                        pInfo.SetPropertyValue(this, orgValue);
                    }
                }
            }
            return(done);
        }
Exemple #16
0
 public abstract bool MatchEntity(MetaFunctions CFunc, WSDynamicEntity entity, WSTableSource src, string key = null, string matchOperation = null);
Exemple #17
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);
        }
Exemple #18
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);
        }