// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Text; public partial class Call : System.Web.UI.Page { private const string _StateKey = "Spludlow-Intranet.Call"; protected void Page_Load(object sender, EventArgs e) { Spludlow.WebControls.DisableOnClick(this); if (this.IsPostBack == false) { foreach (string host in Spludlow.Config.Hosts()) this.DropDownListHost.Items.Add(host); this.GridViewFlags.DataSource = Spludlow.CallFlag.List(); this.GridViewFlags.DataBind(); this._UpdateTargets(); } } protected void ButtonSearch_Click(object sender, EventArgs e) { DataSet dataSet = Spludlow.Reflections.Search(this.DropDownListHost.SelectedValue, this.TextBoxSerach.Text); this._DisplayMethods(dataSet, null, null); } private void _DisplayMethods(DataSet dataSet, string parameterText, string constructorText) { DataView view = new DataView(dataSet.Tables[0]); view.RowFilter = "IsConstructor = 0"; DataRow methodRow = this._SelectRowParameterText(this.GridViewMethods, view, parameterText); this.GridViewMethods.DataSource = view; this.GridViewMethods.DataBind(); this.GridViewConstructors.SelectedIndex = -1; this.GridViewConstructors.DataSource = null; this.GridViewConstructors.DataBind(); if (methodRow == null) return; if ((bool)methodRow["IsStatic"] == true) { this._DisplayParamters(); return; } view.RowFilter = "(IsConstructor = 1)"; bool auto = false; if (view.Count <= 1) { if (view.Count == 1) this.GridViewConstructors.SelectedIndex = 0; auto = true; } this.GridViewConstructors.DataSource = view; this.GridViewConstructors.DataBind(); if (auto == true) this._DisplayParamters(); } private DataRow _SelectRowParameterText(GridView gridView, DataView view, string parameterText) { if (parameterText == null) { gridView.SelectedIndex = -1; return null; } for (int index = 0; index < view.Count; ++index) { string rowText = (string)view[index]["ParameterText"]; if (rowText == parameterText) { gridView.SelectedIndex = index; return view[index].Row; } } return null; } protected void GridViewMethods_SelectedIndexChanged(object sender, EventArgs e) { int index = this.GridViewMethods.SelectedIndex; string assembly = (string)this.GridViewMethods.DataKeys[index]["Assembly"]; string type = (string)this.GridViewMethods.DataKeys[index]["Type"]; string method = (string)this.GridViewMethods.DataKeys[index]["Method"]; string parameterText = (string)this.GridViewMethods.DataKeys[index]["ParameterText"]; string host = this.DropDownListHost.SelectedValue; DataSet dataSet = (DataSet)Spludlow.Call.Now(host, "Spludlow", "Spludlow.Reflections", "MethodMembers", new object[] { assembly, type, method }); this.GridViewParameters.DataSource = null; this.GridViewParameters.DataBind(); this._DisplayMethods(dataSet, parameterText, null); } protected void GridViewConstructors_SelectedIndexChanged(object sender, EventArgs e) { int index = this.GridViewMethods.SelectedIndex; this._DisplayParamters(); } public DataRow[] _SelectedMethodConstructor() { int methodIndex = this.GridViewMethods.SelectedIndex; string assembly = (string)this.GridViewMethods.DataKeys[methodIndex]["Assembly"]; string type = (string)this.GridViewMethods.DataKeys[methodIndex]["Type"]; string method = (string)this.GridViewMethods.DataKeys[methodIndex]["Method"]; string parameterText = (string)this.GridViewMethods.DataKeys[methodIndex]["ParameterText"]; int constructorIndex = this.GridViewConstructors.SelectedIndex; string constructorText = ""; if (constructorIndex != -1) constructorText = (string)this.GridViewConstructors.DataKeys[constructorIndex]["ParameterText"]; string host = this.DropDownListHost.SelectedValue; DataSet dataSet = (DataSet)Spludlow.Call.Now(host, "Spludlow", "Spludlow.Reflections", "MethodMembers", new object[] { assembly, type, method }); DataTable methodTable = dataSet.Tables[0]; DataRow[] rows; rows = methodTable.Select("(IsConstructor = 0) AND (ParameterText = '" + parameterText + "')"); if (rows.Length != 1) throw new ApplicationException("Did not find 1 method:\t" + parameterText); DataRow methodRow = rows[0]; DataRow constructorRow = null; if (constructorText.Length > 0) { rows = methodTable.Select("(IsConstructor = 1) AND (ParameterText = '" + constructorText + "')"); if (rows.Length != 1) throw new ApplicationException("Did not find 1 constructor:\t" + constructorText); constructorRow = rows[0]; } return new DataRow[] { methodRow, constructorRow }; } private void _DisplayParamters() { DataRow[] rows = _SelectedMethodConstructor(); DataRow methodRow = rows[0]; DataRow constructorRow = rows[1]; DataTable table = Spludlow.Data.TextTable.ReadText(new string[] { "Index Name Type", "String String String", }); this._AddParameterRow(table, methodRow, "P"); this._AddParameterRow(table, constructorRow, "C"); this.GridViewParameters.DataSource = table; this.GridViewParameters.DataBind(); } private void _AddParameterRow(DataTable table, DataRow row, string prefix) { if (row == null) return; string[][] parameters = (string[][])row["Parameters"]; for (int index = 0; index < parameters.Length; ++index) { string[] parameter = parameters[index]; string type = parameter[0]; string name = parameter[2]; if (Spludlow.SimpleEncoding.IsSimple(type) == true) type = Spludlow.SimpleEncoding.ShortTypeName(type); else type = "@" + type; table.Rows.Add(new object[] { prefix + index.ToString("00"), name, type }); } } protected void GridViewParameters_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType != DataControlRowType.DataRow) return; TextBox textBox = (TextBox)e.Row.FindControl("TextBoxParameter"); DataRow row = ((DataRowView)e.Row.DataItem).Row; string type = (string)row["Type"]; if (type.EndsWith("[]") == true) { textBox.Height = 128; textBox.TextMode = TextBoxMode.MultiLine; } if (type.StartsWith("String") == true || type.StartsWith("@") == true) { textBox.Width = 512; } } protected void ButtonMakeText_Click(object sender, EventArgs e) { List paramters = new List(); List constructorArgs = new List(); foreach (GridViewRow gridRow in this.GridViewParameters.Rows) { if (gridRow.RowType != DataControlRowType.DataRow) continue; string indexText = gridRow.Cells[0].Text; string name = gridRow.Cells[1].Text; string typeText = gridRow.Cells[2].Text; TextBox textBox = (TextBox)gridRow.Cells[3].FindControl("TextBoxParameter"); string textData = textBox.Text.Trim(); string[] paramter; if (typeText.StartsWith("@") == false) { if (typeText.EndsWith("[]") == true) textData = textData.Replace(Environment.NewLine, "\t"); // In web forms arrays are seperated by CR not TAB typeText = "System." + typeText; Type paramterType = Type.GetType(typeText, true); object data = Spludlow.SimpleEncoding.Decode(textData, paramterType.Name); paramter = Spludlow.Parameters.EncodeParameter(data, false, false); } else { paramter = new string[] { typeText, textData }; } paramter = new string[] { paramter[0], paramter[1], name }; if (indexText.StartsWith("P") == true) paramters.Add(paramter); else constructorArgs.Add(paramter); } string address = this.DropDownListTarget.SelectedValue; int methodIndex = this.GridViewMethods.SelectedIndex; string assembly = (string)this.GridViewMethods.DataKeys[methodIndex]["Assembly"]; string type = (string)this.GridViewMethods.DataKeys[methodIndex]["Type"]; string method = (string)this.GridViewMethods.DataKeys[methodIndex]["Method"]; Spludlow.CallMethod callMethod = new Spludlow.CallMethod(address, assembly, type, method); callMethod.Parameters = paramters.ToArray(); callMethod.ConstructorArguments = constructorArgs.ToArray(); string[] flags = Spludlow.WebControls.CheckedKeysString(this.GridViewFlags, "CheckBoxFlag", "Abbreviation"); callMethod.Flags = Spludlow.CallFlag.Parse(flags); string callText = Spludlow.CallText.Write2(callMethod, callMethod.Parameters, callMethod.ConstructorArguments); this.TextBoxCallText.Text = "# " + DateTime.Now.ToString() + Environment.NewLine + callText; } protected void ButtonRunText_Click(object sender, EventArgs e) { try { Spludlow.CallMethod callMethod = Spludlow.CallText.Read(this.TextBoxCallText.Text); object result = Spludlow.Call.Run(callMethod); this.LabelError.Text = DateTime.Now.ToString() + " Method Ran from Call Text"; this.DisplayDataResult.Display(result); } catch (Exception ee) { this.LabelError.Text = DateTime.Now.ToString() + " Error Running Call " + ee.Message; this.DisplayDataResult.Display(ee.ToString()); } } protected void DropDownListHost_SelectedIndexChanged(object sender, EventArgs e) { this._UpdateTargets(); } private void _UpdateTargets() { string host = this.DropDownListHost.SelectedValue; string[] queues = Spludlow.Status.WatchedQueues(host); this.DropDownListTarget.Items.Clear(); foreach (string queue in queues) this.DropDownListTarget.Items.Add(host + ":" + queue); this.DropDownListTarget.Items.Add(host); int index = this.DropDownListTarget.Items.IndexOf(new ListItem(host + ":Run")); if (index != -1) this.DropDownListTarget.SelectedIndex = index; } }