namespace CustomComponentExample
{
	using Softadmin.Api;
	using System.Data;

	/// <summary>
	/// This example component will calculate the sum of the "Value"-column returned from the procedure and return the result to the database.
	/// </summary>
	public class ExampleCustomComponent : CustomComponent
	{
		private int Sum { get; set; }

		public override void Execute()
		{
			var dataRequest = this.DbRequestFactory.CreateRequest("Get data"); // Prepare the SQL call to the menu item SQL binding.
			dataRequest.SetParameter("Action", "GetData"); // Add the Action parameter in addition to all parameters already existing on the binding.
			var dataSet = dataRequest.GetDataSet(); // Make the SQL call and fetch the returned result set.

			// Loop through the returned rows and perform the component task.
			if (dataSet != null && dataSet.Tables.Count > 0)
			{
				foreach (DataRow row in dataSet.Tables[0].Rows)
				{
					this.AddToSum(row);
				}
			}

			var successRequest = this.DbRequestFactory.CreateRequest("Report success"); // Prepare the SQL call to the menu item SQL binding.
			successRequest.SetParameter("Action", "Finished"); // Add the Action parameter in addition to all parameters already existing on the binding.
			successRequest.SetParameter("Success", true); // Add the Success parameter in addition to all parameters already existing on the binding.
			successRequest.SetParameter("Sum", this.Sum); // Add the Success parameter in addition to all parameters already existing on the binding.
			successRequest.ExecuteNonQuery(); // Make the SQL call and disregard what is returned from the procedure.
		}

		private void AddToSum(DataRow row)
		{
			this.Sum += (int)row["Value"];
		}
	}
}