Skip to content

Commit

Permalink
Added GetDataRowCommand and GetDataRowValueCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
openbots-ff committed Mar 24, 2020
1 parent ff8a0cc commit 7ff1808
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 1 deletion.
2 changes: 1 addition & 1 deletion taskt/Core/Automation/Commands/ExcelGetRangeCommandASDT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class ExcelGetRangeCommandAsDT : ScriptCommand
[Attributes.PropertyAttributes.PropertyUISelectionOption("Yes")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("No")]
[Attributes.PropertyAttributes.InputSpecification("When selected, the column headers from the specified spreadsheet range are also extracted.")]
[Attributes.PropertyAttributes.SampleUsage("Select from **Datatable** or **Delimited String**")]
[Attributes.PropertyAttributes.SampleUsage("Select from **Yes** or **No**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_AddHeaders { get; set; }

Expand Down
113 changes: 113 additions & 0 deletions taskt/Core/Automation/Commands/GetDataRowCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System;
using System.Linq;
using System.Xml.Serialization;
using System.Data;
using System.Windows.Forms;
using System.Collections.Generic;
using taskt.UI.Forms;
using taskt.UI.CustomControls;

namespace taskt.Core.Automation.Commands
{
[Serializable]
[Attributes.ClassAttributes.Group("DataTable Commands")]
[Attributes.ClassAttributes.Description("This command allows you to add a datarow to a DataTable")]
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to add a datarow to a DataTable.")]
[Attributes.ClassAttributes.ImplementationDescription("")]
public class GetDataRowCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the DataTable Name")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter a existing DataTable to add rows to.")]
[Attributes.PropertyAttributes.SampleUsage("**myData**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_DataTableName { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please enter the index of the DataRow")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter a valid DataRow index value")]
[Attributes.PropertyAttributes.SampleUsage("0 or [vIndex]")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_DataRowIndex { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Assign to Variable")]
[Attributes.PropertyAttributes.InputSpecification("Select or provide a variable from the variable list")]
[Attributes.PropertyAttributes.SampleUsage("**vSomeVariable**")]
[Attributes.PropertyAttributes.Remarks("If you have enabled the setting **Create Missing Variables at Runtime** then you are not required to pre-define your variables, however, it is highly recommended.")]
public string v_UserVariableName { get; set; }

public GetDataRowCommand()
{
this.CommandName = "GetDataRowCommand";
this.SelectionName = "Get DataRow";
this.CommandEnabled = true;
this.CustomRendering = true;

}

public override void RunCommand(object sender)
{
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
var dataSetVariable = LookupVariable(engine);
DataTable dataTable = (DataTable)dataSetVariable.VariableValue;

var rowIndex = v_DataRowIndex.ConvertToUserVariable(sender);
int index = int.Parse(rowIndex);

DataRow row = dataTable.Rows[index];

Script.ScriptVariable newDataRow = new Script.ScriptVariable
{
VariableName = v_UserVariableName,
VariableValue = row
};

//Overwrites variable if it already exists
if (engine.VariableList.Exists(x => x.VariableName == newDataRow.VariableName))
{
Script.ScriptVariable temp = engine.VariableList.Where(x => x.VariableName == newDataRow.VariableName).FirstOrDefault();
engine.VariableList.Remove(temp);
}
engine.VariableList.Add(newDataRow);

}
private Script.ScriptVariable LookupVariable(Core.Automation.Engine.AutomationEngineInstance sendingInstance)
{
//search for the variable
var requiredVariable = sendingInstance.VariableList.Where(var => var.VariableName == v_DataTableName).FirstOrDefault();

//if variable was not found but it starts with variable naming pattern
if ((requiredVariable == null) && (v_DataTableName.StartsWith(sendingInstance.engineSettings.VariableStartMarker)) && (v_DataTableName.EndsWith(sendingInstance.engineSettings.VariableEndMarker)))
{
//reformat and attempt
var reformattedVariable = v_DataTableName.Replace(sendingInstance.engineSettings.VariableStartMarker, "").Replace(sendingInstance.engineSettings.VariableEndMarker, "");
requiredVariable = sendingInstance.VariableList.Where(var => var.VariableName == reformattedVariable).FirstOrDefault();
}

return requiredVariable;
}
public override List<Control> Render(frmCommandEditor editor)
{
base.Render(editor);

RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_DataTableName", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_DataRowIndex", this, editor));
RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_UserVariableName", this));
var VariableNameControl = CommandControls.CreateStandardComboboxFor("v_UserVariableName", this).AddVariableNames(editor);
RenderedControls.AddRange(CommandControls.CreateUIHelpersFor("v_UserVariableName", this, new Control[] { VariableNameControl }, editor));
RenderedControls.Add(VariableNameControl);

return RenderedControls;
}



public override string GetDisplayValue()
{
return base.GetDisplayValue() + $" [Get DataRow '{v_DataRowIndex}' from '{v_DataTableName}', Store In: '{v_UserVariableName}']";
}
}
}
131 changes: 131 additions & 0 deletions taskt/Core/Automation/Commands/GetDataRowValueCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System;
using System.Linq;
using System.Xml.Serialization;
using System.Data;
using System.Windows.Forms;
using System.Collections.Generic;
using taskt.UI.Forms;
using taskt.UI.CustomControls;

namespace taskt.Core.Automation.Commands
{
[Serializable]
[Attributes.ClassAttributes.Group("DataTable Commands")]
[Attributes.ClassAttributes.Description("This command allows you to add a datarow to a DataTable")]
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to add a datarow to a DataTable.")]
[Attributes.ClassAttributes.ImplementationDescription("")]
public class GetDataRowValueCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the DataRow Name")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter a existing DataTable to add rows to.")]
[Attributes.PropertyAttributes.SampleUsage("**myData**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_DataRowName { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Select value by Index or Column Name")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Index")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Column Name")]
[Attributes.PropertyAttributes.InputSpecification("Select whether the DataRow value should be found by index or column name")]
[Attributes.PropertyAttributes.SampleUsage("Select from **Index** or **Column Name**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_Option { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please enter the index of the DataRow")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter a valid DataRow index value")]
[Attributes.PropertyAttributes.SampleUsage("0 or [vIndex]")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_DataValueIndex { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Assign to Variable")]
[Attributes.PropertyAttributes.InputSpecification("Select or provide a variable from the variable list")]
[Attributes.PropertyAttributes.SampleUsage("**vSomeVariable**")]
[Attributes.PropertyAttributes.Remarks("If you have enabled the setting **Create Missing Variables at Runtime** then you are not required to pre-define your variables, however, it is highly recommended.")]
public string v_UserVariableName { get; set; }

public GetDataRowValueCommand()
{
this.CommandName = "GetDataRowValueCommand";
this.SelectionName = "Get DataRow Value";
this.CommandEnabled = true;
this.CustomRendering = true;
v_Option = "Index";

}

public override void RunCommand(object sender)
{
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
var dataRowVariable = LookupVariable(engine);
var variableList = engine.VariableList;
DataRow dataRow;
//check if currently looping through datatable using BeginListLoopCommand
if (dataRowVariable.VariableValue is DataTable && engine.VariableList.Exists(x => x.VariableName == "Loop.CurrentIndex"))
{
var loopIndexVariable = engine.VariableList.Where(x => x.VariableName == "Loop.CurrentIndex").FirstOrDefault();
int loopIndex = int.Parse(loopIndexVariable.VariableValue.ToString());
dataRow = ((DataTable)dataRowVariable.VariableValue).Rows[loopIndex-1];
}

else dataRow = (DataRow)dataRowVariable.VariableValue;

var valueIndex = v_DataValueIndex.ConvertToUserVariable(sender);
string value = "";
if (v_Option == "Index")
{
int index = int.Parse(valueIndex);
value = dataRow[index].ToString();

}
else if (v_Option == "Column Name")
{
string index = valueIndex;
value = dataRow.Field<string>(index);
}

value.StoreInUserVariable(sender, v_UserVariableName);

}
private Script.ScriptVariable LookupVariable(Core.Automation.Engine.AutomationEngineInstance sendingInstance)
{
//search for the variable
var requiredVariable = sendingInstance.VariableList.Where(var => var.VariableName == v_DataRowName).FirstOrDefault();

//if variable was not found but it starts with variable naming pattern
if ((requiredVariable == null) && (v_DataRowName.StartsWith(sendingInstance.engineSettings.VariableStartMarker)) && (v_DataRowName.EndsWith(sendingInstance.engineSettings.VariableEndMarker)))
{
//reformat and attempt
var reformattedVariable = v_DataRowName.Replace(sendingInstance.engineSettings.VariableStartMarker, "").Replace(sendingInstance.engineSettings.VariableEndMarker, "");
requiredVariable = sendingInstance.VariableList.Where(var => var.VariableName == reformattedVariable).FirstOrDefault();
}

return requiredVariable;
}
public override List<Control> Render(frmCommandEditor editor)
{
base.Render(editor);

RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_DataRowName", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_Option", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_DataValueIndex", this, editor));
RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_UserVariableName", this));
var VariableNameControl = CommandControls.CreateStandardComboboxFor("v_UserVariableName", this).AddVariableNames(editor);
RenderedControls.AddRange(CommandControls.CreateUIHelpersFor("v_UserVariableName", this, new Control[] { VariableNameControl }, editor));
RenderedControls.Add(VariableNameControl);

return RenderedControls;
}



public override string GetDisplayValue()
{
return base.GetDisplayValue() + $" [Get Value '{v_DataValueIndex}' from '{v_DataRowName}', Store In: '{v_UserVariableName}']";
}
}
}
2 changes: 2 additions & 0 deletions taskt/Core/Automation/Commands/ScriptCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace taskt.Core.Automation.Commands
[XmlInclude(typeof(GetDictionaryValueCommand))]
[XmlInclude(typeof(LoadDictionaryCommand))]
[XmlInclude(typeof(AddDataRowCommand))]
[XmlInclude(typeof(GetDataRowCommand))]
[XmlInclude(typeof(GetDataRowValueCommand))]
[XmlInclude(typeof(RemoveDataRowCommand))]
[XmlInclude(typeof(ThickAppClickItemCommand))]
[XmlInclude(typeof(ThickAppGetTextCommand))]
Expand Down
2 changes: 2 additions & 0 deletions taskt/UI/CustomControls/CustomControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ public static Dictionary<string, Image> UIImageDictionary()
uiImages.Add("ExcelActivateSheetCommand", taskt.Properties.Resources.command_spreadsheet);
uiImages.Add("ExcelSplitRangeByColumnCommand", taskt.Properties.Resources.command_spreadsheet);
uiImages.Add("AddDataRowCommand", taskt.Properties.Resources.command_spreadsheet);
uiImages.Add("GetDataRowCommand", taskt.Properties.Resources.command_spreadsheet);
uiImages.Add("GetDataRowValueCommand", taskt.Properties.Resources.command_spreadsheet);
uiImages.Add("CreateDataTableCommand", taskt.Properties.Resources.command_spreadsheet);
uiImages.Add("FilterDataTableCommand", taskt.Properties.Resources.command_spreadsheet);
uiImages.Add("RemoveDataRowCommand", taskt.Properties.Resources.command_spreadsheet);
Expand Down
2 changes: 2 additions & 0 deletions taskt/taskt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@
<Compile Include="Core\Automation\Commands\DeleteFolderCommand.cs" />
<Compile Include="Core\Automation\Commands\ExcelSplitRangeByColumnCommand.cs" />
<Compile Include="Core\Automation\Commands\ExtractFileCommand.cs" />
<Compile Include="Core\Automation\Commands\GetDataRowCommand.cs" />
<Compile Include="Core\Automation\Commands\GetDataRowValueCommand.cs" />
<Compile Include="Core\Automation\Commands\GetFilesCommand.cs" />
<Compile Include="Core\Automation\Commands\GetFoldersCommand.cs" />
<Compile Include="Core\Automation\Commands\MoveFolderCommand.cs" />
Expand Down

0 comments on commit 7ff1808

Please sign in to comment.