예제 #1
0
        public override HttpResponseMessage GetResponse()
        {
            Gale.REST.Queryable.Primitive.Result response = base.Execute();

            Gale.REST.Queryable.OData.Results.Result odata_response;
            odata_response = new Gale.REST.Queryable.OData.Results.JsonResult(response, this.Kql.offset, this.Kql.limit);

            //TODO: CHECK VIA CONTENT-TYPE THE RESPONSE FORMMATER JsonResult or TableResult?

            return(Request.CreateResponse(odata_response));
        }
예제 #2
0
        private Result _Execute(String query, Gale.Db.IDataActions databaseFactory)
        {
            System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            timer.Start();

            //-------------------------------------------------------------------------------------
            //---[ DATABASE CALL
            using (Gale.Db.DataService svc = new Gale.Db.DataService(query))
            {
                System.Data.DataTable    db_data       = null;
                System.Data.DataTable    db_pagination = null;
                Gale.Db.EntityRepository rep           = null;

                try
                {
                    //Create the repository
                    rep = this.DatabaseFactory.ExecuteSql(svc);

                    db_data       = rep.GetRawTable(0);
                    db_pagination = rep.GetRawTable(1);
                }
                catch (System.Exception ex)
                {
                    string message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                    throw new Gale.Exception.GaleException("API_DB_ERROR", message);
                }

                //--------[ GET ALL DATA IN AN ARRAY
                var data = new List <List <Object> >();
                SortedList <int, Field> ordinalsFields = new SortedList <int, Field>();

                var columns_count = db_data.Columns.Count;

                #region ORDINAL COLUMN SEARCH
                //SEARCH FOR ORDINAL'S
                foreach (Field field in _reflectedModel.SelectedFields)
                {
                    for (var column_index = 0; column_index < columns_count; column_index++)
                    {
                        if (field.Key.ToLower() == db_data.Columns[column_index].ColumnName.ToLower())
                        {
                            ordinalsFields.Add(db_data.Columns[column_index].Ordinal, field);
                            break;
                        }
                    }
                }
                #endregion


                if (db_pagination.Rows.Count != 1)
                {
                    throw new Gale.Exception.GaleException("API003");
                }
                int total = Convert.ToInt32(db_pagination.Rows[0]["total"]);

                #region TABLE FORMAT

                //Save all data from the foreign table for the descriptor's
                SortedList <Type, Gale.Db.IEntityTable> _foreignTableDatas = new SortedList <Type, Gale.Db.IEntityTable>();

                //Starting Fetching Data
                for (var row_index = 0; row_index < db_data.Rows.Count; row_index++)
                {
                    List <Object> item = new List <Object>();

                    foreach (int ordinal in ordinalsFields.Keys)
                    {
                        Field field = ordinalsFields[ordinal];

                        Object db_value = db_data.Rows[row_index][ordinal];

                        if (db_value is DateTime)
                        {
                            db_value = DateTime.SpecifyKind((DateTime)db_value, DateTimeKind.Local);
                        }

                        //If is FK , try to get the Descriptor, if not have descriptor, send Encripted Value :S
                        if (field.Specification == Field.SpecificationEnum.Fk)
                        {
                            Table table = _reflectedModel.Constraints.First(constraint => constraint.ThisField == field).Table;

                            if (table.Descriptor != null)
                            {
                                Gale.Db.IEntityTable tableData;
                                _foreignTableDatas.TryGetValue(table.Type, out tableData);

                                #region CREATE DATA TABLE FROM THE CURRENT SOURCE IF NOT EXIST YET
                                if (tableData == null)
                                {
                                    //Create Constraint Table Data
                                    System.Reflection.MethodInfo baseMethod = (from t in rep.GetType().GetMethods()
                                                                               where
                                                                               t.GetGenericArguments().Count() > 0 &&
                                                                               t.Name == "GetModel" && t.GetParameters().Count() == 0
                                                                               select t).FirstOrDefault();

                                    System.Reflection.MethodInfo GetModelMethod = baseMethod.MakeGenericMethod(table.Type);

                                    tableData = (Gale.Db.IEntityTable)GetModelMethod.Invoke(rep, null);
                                    _foreignTableDatas.Add(table.Type, tableData);
                                }
                                #endregion


                                //GET Constraint Function Expression to Get the FK Descriptor
                                Object _item = tableData.GetType().GetMethod("get_Item").Invoke(tableData, new object[] { row_index });
                                db_value = table.Descriptor.DynamicInvoke(_item).ToString();
                            }
                        }

                        item.Add(db_value);    //Column Value
                    }
                    data.Add(item);
                }
                #endregion

                timer.Stop();
                var response = new Gale.REST.Queryable.Primitive.Result(total, timer.Elapsed, _reflectedModel.SelectedFields, data);


                return(response);
            }
            //-------------------------------------------------------------------------------------
        }