-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathDateCalculationCommand.cs
More file actions
248 lines (213 loc) · 12 KB
/
Copy pathDateCalculationCommand.cs
File metadata and controls
248 lines (213 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.Windows.Forms;
using taskt.UI.Forms;
using taskt.UI.CustomControls;
namespace taskt.Core.Automation.Commands
{
[Serializable]
[Attributes.ClassAttributes.Group("Data Commands")]
[Attributes.ClassAttributes.SubGruop("Other")]
[Attributes.ClassAttributes.Description("This command allows you to build a date and apply it to a variable.")]
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to perform a date calculation.")]
[Attributes.ClassAttributes.ImplementationDescription("This command implements actions against VariableList from the scripting engine.")]
public class DateCalculationCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please supply the date value or variable")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Specify either text or a variable that contains the start date.")]
[Attributes.PropertyAttributes.SampleUsage("**{{{DateTime.Now}}}** or **1/1/2000**")]
[Attributes.PropertyAttributes.Remarks("You can use known text or variables.")]
[Attributes.PropertyAttributes.PropertyShowSampleUsageInDescription(true)]
public string v_InputValue { get; set; }
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please Select a Calculation Method")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Add Seconds")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Add Minutes")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Add Hours")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Add Days")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Add Years")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Subtract Seconds")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Subtract Minutes")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Subtract Hours")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Subtract Days")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Subtract Years")]
[Attributes.PropertyAttributes.InputSpecification("Select the necessary operation")]
[Attributes.PropertyAttributes.SampleUsage("Select From Add Seconds, Add Minutes, Add Hours, Add Days, Add Years, Subtract Seconds, Subtract Minutes, Subtract Hours, Subtract Days, Subtract Years ")]
[Attributes.PropertyAttributes.PropertyRecommendedUIControl(Attributes.PropertyAttributes.PropertyRecommendedUIControl.RecommendeUIControlType.ComboBox)]
public string v_CalculationMethod { get; set; }
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please supply the increment value")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter how many units to increment by")]
[Attributes.PropertyAttributes.SampleUsage("15, {{{vIncrement}}}")]
[Attributes.PropertyAttributes.Remarks("You can use negative numbers which will do the opposite, ex. Subtract Days and an increment of -5 will Add Days.")]
[Attributes.PropertyAttributes.PropertyShowSampleUsageInDescription(true)]
public string v_Increment { get; set; }
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Specify String Format")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Specify if a specific string format is required.")]
[Attributes.PropertyAttributes.SampleUsage("**MM/dd/yy** or **hh:mm** or etc.")]
[Attributes.PropertyAttributes.Remarks("")]
[Attributes.PropertyAttributes.PropertyShowSampleUsageInDescription(true)]
[Attributes.PropertyAttributes.PropertyIsOptional(true)]
public string v_ToStringFormat { get; set; }
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please select the variable to receive the date calculation")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[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.")]
[Attributes.PropertyAttributes.PropertyRecommendedUIControl(Attributes.PropertyAttributes.PropertyRecommendedUIControl.RecommendeUIControlType.ComboBox)]
[Attributes.PropertyAttributes.PropertyIsVariablesList(true)]
public string v_applyToVariableName { get; set; }
public DateCalculationCommand()
{
this.CommandName = "DateCalculationCommand";
this.SelectionName = "Date Calculation";
this.CommandEnabled = true;
this.CustomRendering = true;
this.v_InputValue = "";
this.v_ToStringFormat = "MM/dd/yyyy hh:mm:ss";
}
public override void RunCommand(object sender)
{
//get variablized string
var variableDateTime = v_InputValue.ConvertToUserVariable(sender);
//convert to date time
DateTime requiredDateTime;
if (!DateTime.TryParse(variableDateTime, out requiredDateTime))
{
throw new InvalidDataException("Date was unable to be parsed - " + variableDateTime);
}
//get increment value
double requiredInterval;
var variableIncrement = v_Increment.ConvertToUserVariable(sender);
//convert to double
if (!Double.TryParse(variableIncrement, out requiredInterval))
{
throw new InvalidDataException("Date was unable to be parsed - " + variableIncrement);
}
//perform operation
switch (v_CalculationMethod)
{
case "Add Seconds":
requiredDateTime = requiredDateTime.AddSeconds(requiredInterval);
break;
case "Add Minutes":
requiredDateTime = requiredDateTime.AddMinutes(requiredInterval);
break;
case "Add Hours":
requiredDateTime = requiredDateTime.AddHours(requiredInterval);
break;
case "Add Days":
requiredDateTime = requiredDateTime.AddDays(requiredInterval);
break;
case "Add Years":
requiredDateTime = requiredDateTime.AddYears((int)requiredInterval);
break;
case "Subtract Seconds":
requiredDateTime = requiredDateTime.AddSeconds((requiredInterval * -1));
break;
case "Subtract Minutes":
requiredDateTime = requiredDateTime.AddMinutes((requiredInterval * -1));
break;
case "Subtract Hours":
requiredDateTime = requiredDateTime.AddHours((requiredInterval * -1));
break;
case "Subtract Days":
requiredDateTime = requiredDateTime.AddDays((requiredInterval * -1));
break;
case "Subtract Years":
requiredDateTime = requiredDateTime.AddYears(((int)requiredInterval * -1));
break;
default:
break;
}
//handle if formatter is required
var formatting = v_ToStringFormat.ConvertToUserVariable(sender);
formatting = Regex.Replace(formatting, @"[.,:;\/\\]", x => $"'{x}'");
var stringDateFormatted = requiredDateTime.ToString(formatting);
//store string in variable
stringDateFormatted.StoreInUserVariable(sender, v_applyToVariableName);
}
public override List<Control> Render(frmCommandEditor editor)
{
base.Render(editor);
//create standard group controls
//RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_InputValue", this, editor));
//RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_CalculationMethod", this));
//RenderedControls.Add(CommandControls.CreateDropdownFor("v_CalculationMethod", this));
//RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_Increment", this, editor));
//RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_ToStringFormat", this, editor));
//RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_applyToVariableName", this));
//var VariableNameControl = CommandControls.CreateStandardComboboxFor("v_applyToVariableName", this).AddVariableNames(editor);
//RenderedControls.AddRange(CommandControls.CreateUIHelpersFor("v_applyToVariableName", this, new Control[] { VariableNameControl }, editor));
//RenderedControls.Add(VariableNameControl);
RenderedControls.AddRange(CommandControls.MultiCreateInferenceDefaultControlGroupFor(this, editor));
if (editor.creationMode == frmCommandEditor.CreationMode.Add)
{
this.v_InputValue = editor.ReplaceVariableMaker("{{{DateTime.Now}}}");
}
return RenderedControls;
}
public override string GetDisplayValue()
{
//if calculation method was selected
if (v_CalculationMethod != null)
{
//determine operand and interval
var operand = v_CalculationMethod.Split(' ')[0];
var interval = v_CalculationMethod.Split(' ')[1];
//additional language handling based on selection made
string operandLanguage;
if (operand == "Add")
{
operandLanguage = " to ";
}
else
{
operandLanguage = " from ";
}
//return value
return base.GetDisplayValue() + " [" + operand + " " + v_Increment + " " + interval + operandLanguage + v_InputValue + ", Apply Result to Variable '" + v_applyToVariableName + "']";
}
else
{
return base.GetDisplayValue();
}
}
public override bool IsValidate(frmCommandEditor editor)
{
this.IsValid = true;
this.validationResult = "";
if (String.IsNullOrEmpty(this.v_InputValue))
{
this.validationResult += "Date value is empty.\n";
this.IsValid = false;
}
if (String.IsNullOrEmpty(this.v_CalculationMethod))
{
this.validationResult += "Calculation Method is empty.\n";
this.IsValid = false;
}
if (String.IsNullOrEmpty(this.v_Increment))
{
this.validationResult += "Increment value is empty.\n";
this.IsValid = false;
}
if (String.IsNullOrEmpty(this.v_applyToVariableName))
{
this.validationResult += "Variable is empty.\n";
this.IsValid = false;
}
return this.IsValid;
}
}
}