/// <summary> /// Uses System.Reflection to turn request strings into methods in the merchant, Admin or setup classes. /// </summary> /// <param name="decodedRequestString">The decoded request string.</param> /// <param name="adminMode">if set to <c>true</c> [Admin mode].</param> /// <returns></returns> public static Dictionary<string, object> JsonToMethod(string decodedRequestString, bool adminMode) { //("FUNCTION /w reflection JSONToMethod").debug(); Dictionary<string, object> o = new Dictionary<string, object>(); if(decodedRequestString.Length == 0) { o.Add("error", -8); o.Add("description", "missing arguments argument"); return o; } else { MethodInfo methodInfo; List<Object> args = new List<Object>(); string proc = ""; int x = 0; JArray req = null; try { req = JsonConvert.DeserializeObject<JArray>(decodedRequestString); } catch(Exception e) { o.Add("error", -7); o.Add("description", "JSON parse error. " + e.Message); return o; } List<object> argumentCollection = new List<object>(); proc = (string)req[0]; if(req[1].GetType() == typeof(JArray)) { JArray a = (JArray)req[1]; foreach(object q in a as JArray) { argumentCollection.Add(q.JTokenToGeneric()); } } else { o.Add("error", -6); o.Add("description", "missing second argument (method arguemnts)"); return o; } ParameterInfo[] pramInfo = null; if(proc.Length > 0) { methodInfo = null; if(adminMode) { methodInfo = typeof(Admin).GetMethod(proc); /* execute all plugins with an Admin class */ bool mexec = false; foreach(Plugin plugin in Main.Plugins) { Type[] cinfos = plugin.GetType().GetNestedTypes(); foreach(Type cinfo in cinfos) { if(cinfo.BaseType == typeof(Admin)) { MethodInfo innerMethodInfo = cinfo.GetMethod(proc); if(innerMethodInfo != null) { methodInfo = innerMethodInfo; mexec = true; break; } } } if(mexec) { break; } } } else { methodInfo = typeof(Merchant).GetMethod(proc); /* execute all plugins with an Merchant class */ bool mexec = false; foreach(Plugin plugin in Main.Plugins) { Type[] cinfos = plugin.GetType().GetNestedTypes(); foreach(Type cinfo in cinfos) { if(cinfo.BaseType == typeof(Merchant)) { MethodInfo innerMethodInfo = cinfo.GetMethod(proc); if(innerMethodInfo != null) { methodInfo = innerMethodInfo; mexec = true; break; } } } if(mexec) { break; } } } if(methodInfo == null) { o.Add("error", -5); o.Add("description", "method not found"); return o; } else { pramInfo = methodInfo.GetParameters(); } } else { o.Add("error", -4); o.Add("description", "method argument is missing"); return o; } if(argumentCollection != null) { foreach(object argument in argumentCollection) { if(argument != null) { if(pramInfo != null) { if(pramInfo.Length == x) { o.Add("error", -3); o.Add("description", "Too many arguments."); return o; } if(pramInfo[x].ParameterType == argument.GetType()) { args.Add(argument); } else { o.Add("error", -2); o.Add("description", "Argument " + x + ":" + pramInfo[x].Name + " expected type " + pramInfo[x].ParameterType.ToString() + ", " + " but got type " + argument.GetType().ToString() + "."); return o; } x++; } } else { o.Add("error", -9); o.Add("description", "Argument " + x + " was null."); return o; } } } if(args.Count == x) { try { o.Add(methodInfo.Name, methodInfo.Invoke(null, args.ToArray())); } catch(Exception e) { e = Main.getInnermostException(e); string msg = String.Format("Application Error {0} on Page:{1},Message:{2}, Stack Trace: {3}", 500, "Responder=>GetMethod exception", e.Message, e.StackTrace); o.Add("error", -500); o.Add("description", "Internal server error: " + e.Source + ": " + e.Message); } } else { o.Add("error", -1); o.Add("description", "Expected " + args.Count + " arguments, but got " + x + " arguments"); } return o; } }
/// <summary> /// Gets the child orders by order id. /// </summary> /// <param name="orderId">The order id.</param> /// <param name="cn">SQL connection.</param> /// <param name="trans">The trans.</param> /// <returns> /// A list of orders matching the serial ids. /// </returns> public static List<Order> GetChildOrdersByOrderId(int orderId, SqlConnection cn, SqlTransaction trans) { using(SqlCommand cmd = new SqlCommand("select orderId from orders with (nolock) where parentOrderId = @orderId", cn, trans)) { cmd.Parameters.Add("@orderId", SqlDbType.Int).Value = orderId; using(SqlDataReader d = cmd.ExecuteReader()) { List<int> orderIds = new List<int>(); while(d.Read()) { orderIds.Add(d.GetInt32(0)); } return GetOrdersByOrderIds(orderIds.ToArray(), cn, trans); } } }
/// <summary> /// Parses CSV and XLS files for import. /// </summary> /// <param name="args">{importFilePath:path,sheetNumber:XLS sheet number,ignoreBlanks:true/false}</param> /// <returns></returns> public static Dictionary<string, object> ImportWizard( Dictionary<string, object> args ) { Dictionary<string, object> j = new Dictionary<string, object>(); List<object> l = new List<object>(); string importFilePath = ""; bool ignoreBlanks = false; j.Add( "error", 0 ); j.Add( "description", "" ); if( !args.ContainsKey( "importFilePath" ) ) { j[ "error" ] = -2; j[ "description" ] = "Missing key: importFilePath"; return j; } else { importFilePath = ( string )args[ "importFilePath" ]; } int sheetNumber; if( !int.TryParse( args[ "sheetNumber" ].ToString(), out sheetNumber ) ) { sheetNumber = 0; } if( args.ContainsKey( "ignoreBlanks" ) ) { ignoreBlanks = ( bool )args[ "ignoreBlanks" ]; } try { if( !File.Exists( importFilePath ) ) { Exception e = new Exception( "File not found:" + importFilePath ); throw e; } OleDbConnection cn; OleDbCommand cmd; OleDbDataAdapter ad = new OleDbDataAdapter(); DataSet ds = new DataSet(); if( importFilePath.ToLower().EndsWith( ".xls" ) ) { string connection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + importFilePath + ";" + "Extended Properties='Excel 8.0;IMEX=1';"; cn = new OleDbConnection( connection ); cn.Open(); DataTable sheets = cn.GetOleDbSchemaTable( System.Data.OleDb.OleDbSchemaGuid.Tables, null ); if( sheetNumber > sheets.Rows.Count ) { Exception e = new Exception( "Only " + sheets.Rows.Count + " were found in the XLS file, but you selected sheet " + sheetNumber ); throw e; } cmd = new OleDbCommand( "select * from [" + sheets.Rows[ sheetNumber ].ItemArray[ 2 ] + "]", cn ); } else if( importFilePath.ToLower().EndsWith( ".csv" ) ) { string connection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path.GetDirectoryName( importFilePath ) + ";" + "Extended Properties='text;IMEX=1;HDR=Yes;FMT=Delimited(,)';"; cn = new OleDbConnection( connection ); cn.Open(); cmd = new OleDbCommand( "select * from [" + Path.GetFileName( importFilePath ) + "]", cn ); } else { Exception e = new Exception( "Unsupported file format." ); throw e; } List<string[]> parse = new List<string[]>(); using( OleDbDataReader d = cmd.ExecuteReader() ) { while( d.Read() ) { int h = d.FieldCount; List<string> lst = new List<string>(); for( int x = 0; h > x; x++ ) { lst.Add( d.GetValue( x ).ToString() ); } parse.Add( lst.ToArray() ); } } if( cn.State == ConnectionState.Open ) { cn.Close(); } j.Add( "parse", parse ); } catch( Exception e ) { j[ "error" ] = -1; j[ "description" ] = e.Message; } return j; }