示例#1
0
        /// <summary>
        /// Get the request body.
        /// </summary>
        /// <typeparam name="T">The data type.</typeparam>
        /// <param name="body">The body containg the data.</param>
        /// <param name="queryString">The query string.</param>
        /// <param name="queryBody">The query body.</param>
        /// <param name="bodyOriginal">The original body.</param>
        /// <returns>The new type.</returns>
        /// <exception cref="System.Exception"></exception>
        public static T RequestBody <T>(string body, NameValueCollection queryString, NameValueCollection queryBody, byte[] bodyOriginal)
        {
            T requestBody = default(T);

            // If the data format key query exists.
            if (queryString[DataFormatQueryStringKey] != null)
            {
                // Select the data format.
                switch (queryString[DataFormatQueryStringKey].ToLower())
                {
                case "query":
                    // Query data format.
                    requestBody = Nequeo.Serialisation.NameValue.Deserializer <T>(queryBody);
                    break;

                case "xml":
                    // Xml data format.
                    requestBody = RequestDeserialiseAction <T>(queryString, bodyOriginal).Body;
                    break;

                case "json":
                    // Json data format.
                    requestBody = JavaObjectNotation.JavaScriptDeserializer <T>(body);
                    break;

                default:
                    break;
                }
            }

            // Return the body data.
            return(requestBody);
        }
示例#2
0
        /// <summary>
        /// Parse the data according to the format.
        /// </summary>
        /// <param name="queryString">The query string.</param>
        /// <param name="queryBody">The query body.</param>
        /// <param name="bodyString">The body string.</param>
        /// <returns>The web request.</returns>
        /// <exception cref="System.Exception"></exception>
        public static WebRequest Request(NameValueCollection queryString, NameValueCollection queryBody, string bodyString)
        {
            WebRequest request = null;

            // If the data format key query exists.
            if (queryString[DataFormatQueryStringKey] != null)
            {
                // Select the data format.
                switch (queryString[DataFormatQueryStringKey].ToLower())
                {
                case "query":
                    // Query data format.
                    string requestName = (queryBody["name"] != null ? queryBody["name"] : "");
                    request = new WebRequest()
                    {
                        Name = requestName, Body = bodyString
                    };
                    break;

                case "xml":
                    // Deserialise the data.
                    XDocument xml  = Document.LoadDocumentParseXmlString(bodyString);
                    string    name = xml.Element("WebRequest").Element("Name").Value;
                    request = new WebRequest()
                    {
                        Name = name
                    };
                    break;

                case "json":
                    // Json data format.
                    request = JavaObjectNotation.JavaScriptDeserializer <WebRequest>(bodyString);
                    break;

                default:
                    // Create an empty request.
                    request = new WebRequest();
                    break;
                }
            }
            else
            {
                // Create an empty request.
                request = new WebRequest();
            }

            // Return the request.
            return(request);
        }
示例#3
0
        /// <summary>
        /// Create the response.
        /// </summary>
        /// <typeparam name="T">The body type.</typeparam>
        /// <param name="response">The response.</param>
        /// <param name="data">The body type data.</param>
        /// <param name="queryString">The query string.</param>
        /// <returns>The string response.</returns>
        /// <exception cref="System.Exception"></exception>
        public static string Response <T>(WebResponse response, T data, NameValueCollection queryString)
        {
            string responseData = "";

            // If the data format key query exists.
            if (queryString[DataFormatQueryStringKey] != null)
            {
                // Select the data format.
                switch (queryString[DataFormatQueryStringKey].ToLower())
                {
                case "xml":
                    // Serialise the data.
                    WebResponse <T> webResponse = new WebResponse <T>();
                    webResponse.ExplicitAssignment(response, data);

                    // Serialise the response data.
                    responseData = ResponseSerialiseAction <WebResponse <T> >(webResponse, queryString);
                    break;

                case "json":
                    // Json data format.
                    responseData = JavaObjectNotation.JavaScriptSerializer(response);
                    break;

                case "query":
                default:
                    // Query data format.
                    NameValueCollection col = Nequeo.Serialisation.NameValue.Serializer <WebResponse>(response);
                    responseData = Nequeo.Web.WebManager.CreateQueryString(col).Replace("body=", "");
                    break;
                }
            }
            else
            {
                // Query data format.
                NameValueCollection col = Nequeo.Serialisation.NameValue.Serializer <WebResponse>(response);
                responseData = Nequeo.Web.WebManager.CreateQueryString(col).Replace("body=", "");
            }

            // Return the body data.
            return(responseData);
        }
示例#4
0
        /// <summary>
        /// Get the response body.
        /// </summary>
        /// <typeparam name="T">The body type.</typeparam>
        /// <param name="data">The body data.</param>
        /// <param name="queryString">The query string.</param>
        /// <returns>The body</returns>
        /// <exception cref="System.Exception"></exception>
        public static string ResponseBody <T>(T data, NameValueCollection queryString)
        {
            string responseBody = "";

            // If the data format key query exists.
            if (queryString[DataFormatQueryStringKey] != null)
            {
                // Select the data format.
                switch (queryString[DataFormatQueryStringKey].ToLower())
                {
                case "xml":
                    // Serialise the data.
                    responseBody = ResponseSerialiseAction <T>(data, queryString);
                    break;

                case "json":
                    // Json data format.
                    responseBody = JavaObjectNotation.JavaScriptSerializer(data);
                    break;

                case "query":
                default:
                    // Query data format.
                    NameValueCollection col = Nequeo.Serialisation.NameValue.Serializer <T>(data);
                    responseBody = Nequeo.Web.WebManager.CreateQueryString(col);
                    break;
                }
            }
            else
            {
                // Query data format.
                NameValueCollection col = Nequeo.Serialisation.NameValue.Serializer <T>(data);
                responseBody = Nequeo.Web.WebManager.CreateQueryString(col);
            }

            // Return the body data.
            return(responseBody);
        }
示例#5
0
 /// <summary>
 /// Serialise the current object type.
 /// </summary>
 /// <typeparam name="T">The type to examine.</typeparam>
 /// <param name="source">The current source object type.</param>
 /// <returns>The string of serialised data.</returns>
 public static string SerialiseJson <T>(this T source)
 {
     return(JavaObjectNotation.JavaScriptSerializer(source));
 }
示例#6
0
 /// <summary>
 /// Deserialise the current object type.
 /// </summary>
 /// <typeparam name="T">The type to examine.</typeparam>
 /// <param name="source">The current source object type.</param>
 /// <returns>The deserialised obejct type.</returns>
 public static T DeserialiseJson <T>(this string source)
 {
     return(JavaObjectNotation.JavaScriptDeserializer <T>(source));
 }
示例#7
0
        /// <summary>
        /// Deserialise the current object type.
        /// </summary>
        /// <typeparam name="T">The type to examine.</typeparam>
        /// <param name="source">The current source object type.</param>
        /// <returns>The deserialised obejct type.</returns>
        public static T DeserialiseJson <T>(this byte[] source)
        {
            string json = Encoding.Default.GetString(source);

            return(JavaObjectNotation.JavaScriptDeserializer <T>(json));
        }
示例#8
0
        public string GetJSonDataTableService(int?iDisplayStart, int iDisplayLength, string sSearch, bool bEscapeRegex, int iColumns,
                                              int iSortingCols, int iSortCol_0, string sSortDir_0, string sEcho, string extensionName)
        {
            // Create a new json datatable service object.
            JSonDataTableService dataTableObject =
                new JSonDataTableService()
            {
                sEcho = Convert.ToInt32(sEcho).ToString()
            };

            // Default data to send back.
            string jSonData = "{ \"sEcho\": " + sEcho + ", \"iTotalRecords\": 0, \"iTotalDisplayRecords\": 0, \"aaData\": [] }";

            try
            {
                // Create a new json datatable service object.
                dataTableObject = new JSonDataTableService()
                {
                    sEcho = Convert.ToInt32(sEcho).ToString()
                };

                // Get the configuration data.
                ConnectionStringExtensionElement[] items = ConnectionStringExtensionConfigurationManager.ConnectionStringExtensionElements();
                if (items != null)
                {
                    if (items.Count() > 0)
                    {
                        // For each service host  configuration find
                        // the corresponding service type.
                        foreach (ConnectionStringExtensionElement item in items)
                        {
                            if (item.Name.ToLower() == extensionName.ToLower())
                            {
                                // Get the current type name
                                // and create a instance of the type.
                                Type   typeName         = Type.GetType(item.TypeName, true, true);
                                object typeNameInstance = Activator.CreateInstance(typeName);

                                if (DynamicDataType == null)
                                {
                                    DynamicDataType = this;
                                }

                                if (DynamicDataType != null)
                                {
                                    if (DynamicDataType.GetType().FullName.ToLower() == typeNameInstance.GetType().FullName.ToLower())
                                    {
                                        Type dataAccessProviderType = Type.GetType(item.DataAccessProvider, true, true);
                                        ConnectionContext.ConnectionType     connectionType     = ConnectionContext.ConnectionTypeConverter.GetConnectionType(item.ConnectionType);
                                        ConnectionContext.ConnectionDataType connectionDataType = ConnectionContext.ConnectionTypeConverter.GetConnectionDataType(item.ConnectionDataType);

                                        // Build the current data object type and
                                        // the select data model generic type.
                                        Type dataType        = Type.GetType(item.DataObjectTypeName, true, true);
                                        Type listGenericType = typeof(SelectDataGenericBase <>);

                                        // Create the generic type parameters
                                        // and create the genric type.
                                        Type[] typeArgs = { dataType };
                                        Type   listGenericTypeConstructor = listGenericType.MakeGenericType(typeArgs);

                                        // Create an instance of the data access provider
                                        Nequeo.Data.DataType.IDataAccess dataAccess = ((Nequeo.Data.DataType.IDataAccess)Activator.CreateInstance(dataAccessProviderType));

                                        // Add the genric type contructor parameters
                                        // and create the generic type instance.
                                        object[] parameters  = new object[] { item.ConnectionName, connectionType, connectionDataType, dataAccess };
                                        object   listGeneric = Activator.CreateInstance(listGenericTypeConstructor, parameters);

                                        // Get the current sorting column use this column as the
                                        // searc
                                        string[]     columnNames  = item.JsonDataTableColumnNames.Split(new char[] { '|' }, StringSplitOptions.None);
                                        PropertyInfo propertyInfo = dataType.GetProperty(columnNames[iSortCol_0]);
                                        string       name         = propertyInfo.Name;

                                        // Get the current object.
                                        Object[] args      = null;
                                        Object[] countArgs = null;

                                        // If a search text exits.
                                        if (!String.IsNullOrEmpty(sSearch))
                                        {
                                            args = new Object[]
                                            {
                                                (iDisplayStart != null ? (int)iDisplayStart : 0),
                                                iDisplayLength,
                                                name + (String.IsNullOrEmpty(sSortDir_0) ? " ASC" : " " + sSortDir_0.ToUpper()),
                                                "(SqlQueryMethods.Like(" + name + ", \"" + sSearch + "%\"))"
                                            };

                                            // Get the current object.
                                            countArgs = new Object[]
                                            {
                                                name + " LIKE '" + sSearch + "%'"
                                            };
                                        }
                                        else
                                        {
                                            args = new Object[]
                                            {
                                                (iDisplayStart != null ? (int)iDisplayStart : 0),
                                                iDisplayLength,
                                                name + (String.IsNullOrEmpty(sSortDir_0) ? " ASC" : " " + sSortDir_0.ToUpper())
                                            };
                                        }

                                        // Add the current data row to the
                                        // business object collection.
                                        object retCount = listGeneric.GetType().InvokeMember("GetRecordCount",
                                                                                             BindingFlags.DeclaredOnly | BindingFlags.Public |
                                                                                             BindingFlags.Instance | BindingFlags.InvokeMethod,
                                                                                             null, listGeneric, countArgs);

                                        // Add the current data row to the
                                        // business object collection.
                                        object ret = listGeneric.GetType().InvokeMember("SelectData",
                                                                                        BindingFlags.DeclaredOnly | BindingFlags.Public |
                                                                                        BindingFlags.Instance | BindingFlags.InvokeMethod,
                                                                                        null, listGeneric, args);

                                        // Cast the data object type as an enumerable object,
                                        // get the enumerator.
                                        System.Collections.IEnumerable itemsRet    = (System.Collections.IEnumerable)ret;
                                        System.Collections.IEnumerator dataObjects = itemsRet.GetEnumerator();

                                        // Create the data array.
                                        string[,] aaData =
                                            new string[
                                                (iDisplayLength <= Convert.ToInt32((long)retCount) ? iDisplayLength : Convert.ToInt32((long)retCount)),
                                                columnNames.Length];

                                        int z = 0;
                                        // Iterate through the collection.
                                        while (dataObjects.MoveNext())
                                        {
                                            // If the current index equals the
                                            // selected index then return
                                            // the data object type.
                                            // Get the property.
                                            for (int i = 0; i < columnNames.Length; i++)
                                            {
                                                // Get the property info the current column
                                                string       columnValue = string.Empty;
                                                PropertyInfo property    = dataObjects.Current.GetType().GetProperty(columnNames[i]);

                                                try
                                                {
                                                    // Get the current value of the property
                                                    columnValue = property.GetValue(dataObjects.Current, null).ToString();
                                                }
                                                catch { }

                                                // Add the value to the data two dimentinal array.
                                                aaData[z, i] = columnValue.Trim();
                                            }

                                            // Increamnt the row count.
                                            z++;
                                        }
                                        dataObjects.Reset();

                                        dataTableObject.iTotalRecords        = Convert.ToInt32((long)retCount);
                                        dataTableObject.iTotalDisplayRecords = Convert.ToInt32((long)retCount);
                                        dataTableObject.aaData = aaData;

                                        // Serialse the data to a JSON format.
                                        jSonData = JavaObjectNotation.JSonCustomSerializer(dataTableObject);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;
                LogHandler.WriteTypeMessage(errorMessage, typeof(DynamicData).GetMethod("GetJSonDataTableService"));
            }
            // Return serialised json data.
            return(jSonData);
        }