Esempio n. 1
0
        /// <summary>
        /// Set property value.
        /// </summary>
        /// <param name="instance">Instance where value is set.</param>
        /// <param name="target">Property type.</param>
        /// <param name="value">New value.</param>
        public static void SetValue(object instance, object target, object value)
        {
            PropertyInfo pi = target as PropertyInfo;

            if (pi != null)
            {
                if (value is IEnumerable)
                {
                    if (!pi.PropertyType.IsAssignableFrom(value.GetType()))
                    {
                        if (pi.PropertyType.IsArray)
                        {
                            int   pos   = 0;
                            Array items = Array.CreateInstance(GXInternal.GetPropertyType(pi.PropertyType), ((IList)value).Count);
                            foreach (object it in (IList)value)
                            {
                                items.SetValue(it, pos);
                                ++pos;
                            }
                            value = items;
                        }
#if !__MOBILE__
                        else if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition() == typeof(System.Data.Linq.EntitySet <>))
                        {
                            Type  listT = typeof(System.Data.Linq.EntitySet <>).MakeGenericType(new[] { GXInternal.GetPropertyType(pi.PropertyType) });
                            IList list  = (IList)GXJsonParser.CreateInstance(listT);
                            foreach (object it in (IList)value)
                            {
                                list.Add(it);
                            }
                            value = list;
                        }
#endif //__MOBILE__
                        else
                        {
                            Type  listT = typeof(List <>).MakeGenericType(new[] { GXInternal.GetPropertyType(pi.PropertyType) });
                            IList list  = (IList)GXJsonParser.CreateInstance(listT);
                            foreach (object it in (IList)value)
                            {
                                list.Add(it);
                            }
                            value = list;
                        }
                    }
                }
                pi.SetValue(instance, value, null);
            }
            else
            {
                FieldInfo fi = target as FieldInfo;
                fi.SetValue(instance, value);
            }
        }
Esempio n. 2
0
        public static GXDeleteArgs Remove <TItem, TDestination>(TItem[] items, TDestination[] collections)
        {
            object collectionId, id;

            if (items == null || collections == null || items.Length == 0 || collections.Length == 0)
            {
                throw new ArgumentNullException("Invalid value");
            }
            Type             itemType       = typeof(TItem);
            Type             collectionType = typeof(TDestination);
            GXSerializedItem si             = GXSqlBuilder.FindRelation(itemType, collectionType);

            if (si.Relation == null || si.Relation.RelationMapTable == null)
            {
                throw new ArgumentNullException("Invalid collection");
            }
            GXDeleteArgs args = Delete(si.Relation.RelationMapTable.Relation.PrimaryTable);

            args.Parent.Updated = true;
            GXSerializedItem siItem = GXSqlBuilder.FindRelation(collectionType, itemType);

            foreach (TDestination c in collections)
            {
                //Get collection id.
                collectionId = si.Relation.RelationMapTable.Relation.ForeignId.Get(c);
                foreach (TItem it in items)
                {
                    object target = GXJsonParser.CreateInstance(si.Relation.RelationMapTable.Relation.PrimaryTable);
                    si.Relation.RelationMapTable.Relation.PrimaryId.Set(target, collectionId);
                    //Get item id.
                    id = siItem.Relation.RelationMapTable.Relation.ForeignId.Get(it);
                    siItem.Relation.RelationMapTable.Relation.PrimaryId.Set(target, id);
                    Expression <Func <object, object> > t = q => target;
                    args.Where.List.Add(new KeyValuePair <WhereType, LambdaExpression>(WhereType.Or, t));
                }
            }
            return(args);
        }
Esempio n. 3
0
        public void ProcessRequest(HttpContext context)
        {
            InvokeHandler handler;

            if (context.Request.ContentType.Contains("json"))
            {
                switch (context.Request.HttpMethod)
                {
                case "GET":
                    handler = RestMethodInfo.Get;
                    break;

                case "POST":
                    handler = RestMethodInfo.Post;
                    break;

                case "PUT":
                    handler = RestMethodInfo.Put;
                    break;

                case "DELETE":
                    handler = RestMethodInfo.Delete;
                    break;

                default:
                    handler = null;
                    break;
                }
                if (handler == null)
                {
                    throw new HttpException(405, string.Format("Method '{0}' not allowed for {1}", context.Request.HttpMethod, RestMethodInfo.RequestType.Name));
                }
                object req;
                if (context.Request.HttpMethod == "POST")
                {
                    req = Parser.Deserialize(context.Request.InputStream, RestMethodInfo.RequestType);
                }
                else
                {
                    string data = "{" + context.Request.QueryString.ToString() + "}";
                    req = Parser.Deserialize(data, RestMethodInfo.RequestType);
                }
                //Get Rest class from cache.
                GXRestService target = RestMap[RestMethodInfo.RestClassType] as GXRestService;
                if (target == null)
                {
                    target = GXJsonParser.CreateInstance(RestMethodInfo.RestClassType) as GXRestService;
                    RestMap[RestMethodInfo.RestClassType] = target;
                }
                //Update user and DB info.

                //If proxy is used.
                string add = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
                if (add == null)
                {
                    add = context.Request.UserHostAddress;
                }
                target.Host = Host;
                target.User = context.User;
                target.Db   = Connection;
                object tmp   = handler(target, req);
                string reply = Parser.Serialize(tmp);
                context.Response.Write(reply);
                context.Response.ContentType = "json";
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Parse query DTO and return response DTO as string.
        /// </summary>
        /// <param name="method">Http method.</param>
        /// <param name="path">Command to execute.</param>
        /// <param name="data">Command data.</param>
        /// <returns>DTO result as string.</returns>
        static string GetReply(Hashtable messageMap, IPrincipal user, GXServer server, string hostAddress, string method, string path, string data)
        {
            InvokeHandler    handler;
            string           command;
            GXRestMethodInfo mi;

            lock (messageMap)
            {
                mi = GXGeneral.GetTypes(messageMap, path, out command);
            }
            if (mi == null)
            {
                throw new HttpException(405, string.Format("Rest method '{0}' not implemented.", command));
            }
            switch (method.ToUpper())
            {
            case "GET":
                handler = mi.Get;
                break;

            case "POST":
                handler = mi.Post;
                break;

            case "PUT":
                handler = mi.Put;
                break;

            case "DELETE":
                handler = mi.Delete;
                break;

            default:
                handler = null;
                break;
            }
            if (handler == null)
            {
                throw new HttpException(405, string.Format("Method '{0}' not allowed for {1}", method, command));
            }
            object req = server.Parser.Deserialize("{" + data + "}", mi.RequestType);
            //Get Rest class from cache.
            GXRestService target = server.RestMap[mi.RestClassType] as GXRestService;

            if (target == null)
            {
                target = GXJsonParser.CreateInstance(mi.RestClassType) as GXRestService;
                server.RestMap[mi.RestClassType] = target;
            }
            target.Host            = server.Host;
            target.User            = user;
            target.Db              = server.Connection;
            target.UserHostAddress = hostAddress;
            object tmp = handler(target, req);

            if (tmp == null)
            {
                throw new HttpException(405, string.Format("Command '{0}' returned null.", command));
            }
            return(server.Parser.SerializeToHttp(tmp));
        }