// 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 Release : System.Web.UI.Page { public class CheckBoxTemplate : ITemplate { private string _ColumnName; public CheckBoxTemplate(string columnName) { this._ColumnName = columnName; } void ITemplate.InstantiateIn(Control container) { CheckBox checkBox = new CheckBox(); checkBox.DataBinding += new EventHandler(checkBox_DataBinding); container.Controls.Add(checkBox); } void checkBox_DataBinding(object sender, EventArgs e) { CheckBox checkBox = (CheckBox)sender; DataRow row = ((DataRowView)DataBinder.GetDataItem(checkBox.NamingContainer)).Row; checkBox.Visible = (bool)row[this._ColumnName]; } } public class HeadingTemplate : ITemplate { private string _ColumnName; private EventHandler _Click; public HeadingTemplate(string columnName, EventHandler click) { this._ColumnName = columnName; this._Click = click; } void ITemplate.InstantiateIn(Control container) { LinkButton linkButton = new LinkButton(); linkButton.Text = this._ColumnName; linkButton.Click += this._Click; container.Controls.Add(linkButton); } } protected void Page_Load(object sender, EventArgs e) { if (this.IsPostBack == false) { foreach (string host in Spludlow.Config.DevelopSourceHosts()) this.DropDownListSource.Items.Add(host); } } protected void ButtonStart_Click(object sender, EventArgs e) { this.GridViewRelease.DataSource = this._GetData(true); this.GridViewRelease.DataBind(); } protected void GridViewRelease_Init(object sender, EventArgs e) { DataTable table = this._GetData(false); foreach (DataColumn column in table.Columns) { if (column.ColumnName != "Application") { string host = column.ColumnName; TemplateField templateField = new TemplateField(); templateField.ItemTemplate = new CheckBoxTemplate(host); HeadingTemplate headingTemplate = new HeadingTemplate(host, new EventHandler(LinkButtonApplication_Click)); templateField.HeaderTemplate = headingTemplate; this.GridViewRelease.Columns.Add(templateField); } } this.GridViewRelease.DataSource = table; this.GridViewRelease.DataBind(); } protected void LinkButtonApplication_Click(object sender, EventArgs e) { LinkButton linkButton = (LinkButton)sender; List cells = new List(); if (linkButton.Parent is DataControlFieldHeaderCell) { string host = linkButton.Text; for (int index = 1; index < this.GridViewRelease.HeaderRow.Cells.Count; ++index) { string headerHost = this._LinkButtonText(this.GridViewRelease.HeaderRow.Cells[index]); if (headerHost == host) { foreach (GridViewRow row in this.GridViewRelease.Rows) cells.Add(row.Cells[index]); } } } else { string application = linkButton.Text; foreach (GridViewRow row in this.GridViewRelease.Rows) { string rowApplication = this._LinkButtonText(row.Cells[0]); if (rowApplication == application) { for (int index = 1; index < row.Cells.Count; ++index) cells.Add(row.Cells[index]); } } } DropDownList dropDownListMode = (DropDownList)this.GridViewRelease.HeaderRow.FindControl("DropDownListMode"); foreach (TableCell cell in cells) { if (cell.Controls.Count == 1) { CheckBox checkBox = (CheckBox)cell.Controls[0]; switch (dropDownListMode.SelectedValue) { case "On": checkBox.Checked = true; break; case "Off": checkBox.Checked = false; break; case "Toggle": checkBox.Checked = !checkBox.Checked; break; } } } } private DataTable _GetData(bool refresh) { string cacheName = "ReleaseData"; DataSet dataSet = null; if (refresh == false) dataSet = (DataSet)this.Cache[cacheName]; // Grid View init() requires template before pageLoad string sourceHost = null; if (this.DropDownListSource.SelectedIndex != -1) sourceHost = this.DropDownListSource.SelectedValue; if (dataSet == null) { dataSet = Spludlow.Release.Matrix(sourceHost); this.Cache[cacheName] = dataSet; } return dataSet.Tables[0]; } protected void ButtonSend_Click(object sender, EventArgs e) { List list = new List(); foreach (GridViewRow row in this.GridViewRelease.Rows) { for (int cellIndex = 1; cellIndex < row.Cells.Count; ++cellIndex) { TableCell cell = row.Cells[cellIndex]; if (cell.Controls.Count == 1) { CheckBox checkBox = (CheckBox)cell.Controls[0]; if (checkBox.Checked == true && checkBox.Visible == true) { string host = this._LinkButtonText(this.GridViewRelease.HeaderRow.Cells[cellIndex]); string package = this._LinkButtonText(row.Cells[0]); list.Add(new string[] { host, package }); } } } } string sourceHost = this.DropDownListSource.SelectedValue; Spludlow.Release.Send(sourceHost, list.ToArray()); StringBuilder text = new StringBuilder(); foreach (string[] words in list) { text.Append(sourceHost); text.Append(" -> "); text.Append(words[0]); text.Append(" : "); text.Append(words[1]); text.Append("
"); } this.LabelError.Text = text.ToString(); } private string _LinkButtonText(TableCell cell) { LinkButton linkButton = null; foreach (Control control in cell.Controls) { if (control is LinkButton) { linkButton = (LinkButton)control; break; } } if (linkButton == null) throw new ApplicationException("LinkButton not found."); return linkButton.Text; } }