/Epi Info 7.1.4.0 Release - Web Enter Integration/EpiDashboard/Dialogs/ConditionalAssignDialog.cs

# · C# · 297 lines · 247 code · 39 blank · 11 comment · 41 complexity · 4fb56fdfaf5d9a78c6f25f356e1e26c7 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Windows.Forms.Integration;
  10. using EpiDashboard;
  11. using EpiDashboard.Rules;
  12. namespace EpiDashboard.Dialogs
  13. {
  14. public partial class ConditionalAssignDialog : Form
  15. {
  16. private EpiDashboard.DashboardHelper dashboardHelper;
  17. private Rule_ConditionalAssign assignRule;
  18. private DataFilters DataFilters;
  19. private bool editMode = false;
  20. private delegate void SetAssignValue(Rule_ConditionalAssign conditionalAssignRule);
  21. public ConditionalAssignDialog(DashboardHelper dashboardHelper)
  22. {
  23. InitializeComponent();
  24. this.dashboardHelper = dashboardHelper;
  25. this.DataFilters = new DataFilters(dashboardHelper);
  26. FillComboBoxes();
  27. }
  28. public ConditionalAssignDialog(DashboardHelper dashboardHelper, Rule_ConditionalAssign conditionalAssignRule)
  29. {
  30. InitializeComponent();
  31. this.dashboardHelper = dashboardHelper;
  32. this.DataFilters = conditionalAssignRule.DataFilters;
  33. editMode = true;
  34. FillComboBoxes();
  35. this.txtDestinationField.Text = conditionalAssignRule.DestinationColumnName;
  36. this.txtDestinationField.Enabled = false;
  37. SetAssignValue setAssignValue = new SetAssignValue(SetAssignmentValue);
  38. switch (conditionalAssignRule.DestinationColumnType)
  39. {
  40. case "System.SByte":
  41. case "System.Byte":
  42. case "System.Boolean":
  43. this.cbxFieldType.SelectedItem = "Yes/No";
  44. setAssignValue = new SetAssignValue(SetBooleanAssignmentValue);
  45. break;
  46. case "System.String":
  47. this.cbxFieldType.SelectedItem = "Text";
  48. break;
  49. case "System.Single":
  50. case "System.Double":
  51. case "System.Decimal":
  52. case "System.Int32":
  53. case "System.Int16":
  54. this.cbxFieldType.SelectedItem = "Numeric";
  55. break;
  56. }
  57. cbxFieldType.Enabled = false;
  58. this.txtAssignCondition.Text = DataFilters.GenerateReadableDataFilterString();
  59. //foreach (KeyValuePair<string, object> kvp in conditionalAssignRule.Conditions)
  60. //{
  61. // this.txtAssignValue.Text = kvp.Value.ToString();
  62. // break;
  63. //}
  64. setAssignValue(conditionalAssignRule);
  65. }
  66. private void SetAssignmentValue(Rule_ConditionalAssign conditionalAssignRule)
  67. {
  68. this.txtAssignValue.Text = conditionalAssignRule.AssignValue.ToString();
  69. if (conditionalAssignRule.UseElse == true)
  70. {
  71. this.checkboxUseElse.Checked = true;
  72. this.txtElseValue.Text = conditionalAssignRule.ElseValue.ToString();
  73. }
  74. else
  75. {
  76. this.checkboxUseElse.Checked = false;
  77. this.txtElseValue.Text = string.Empty;
  78. }
  79. }
  80. private void SetBooleanAssignmentValue(Rule_ConditionalAssign conditionalAssignRule)
  81. {
  82. this.txtAssignValue.Text = conditionalAssignRule.AssignValue.ToString();
  83. if (conditionalAssignRule.AssignValue.ToString().ToLower() == "true")
  84. this.cmbAssignValue.SelectedIndex = 0;
  85. else if (conditionalAssignRule.AssignValue.ToString().ToLower() == "false")
  86. this.cmbAssignValue.SelectedIndex = 1;
  87. if (conditionalAssignRule.UseElse == true)
  88. {
  89. this.checkboxUseElse.Checked = true;
  90. this.txtElseValue.Text = conditionalAssignRule.ElseValue.ToString();
  91. if (conditionalAssignRule.ElseValue.ToString().ToLower() == "true")
  92. this.cmbElseValue.SelectedIndex = 0;
  93. else if (conditionalAssignRule.ElseValue.ToString().ToLower() == "false")
  94. this.cmbElseValue.SelectedIndex = 1;
  95. }
  96. else
  97. {
  98. this.checkboxUseElse.Checked = false;
  99. this.txtElseValue.Text = string.Empty;
  100. this.cmbElseValue.SelectedIndex = 0;
  101. }
  102. }
  103. /// <summary>
  104. /// Gets the format rule
  105. /// </summary>
  106. public Rule_ConditionalAssign AssignRule
  107. {
  108. get
  109. {
  110. return this.assignRule;
  111. }
  112. private set
  113. {
  114. this.assignRule = value;
  115. }
  116. }
  117. /// <summary>
  118. /// Fills the combo boxes on this dialog
  119. /// </summary>
  120. private void FillComboBoxes()
  121. {
  122. txtDestinationField.Text = string.Empty;
  123. cbxFieldType.Items.Clear();
  124. List<string> fieldNames = new List<string>();
  125. cbxFieldType.Items.Add("Text");
  126. cbxFieldType.Items.Add("Numeric");
  127. cbxFieldType.Items.Add("Yes/No");
  128. cbxFieldType.SelectedIndex = 0;
  129. cmbAssignValue.Items.Add(dashboardHelper.Config.Settings.RepresentationOfYes);
  130. cmbAssignValue.Items.Add(dashboardHelper.Config.Settings.RepresentationOfNo);
  131. cmbElseValue.Items.Add(dashboardHelper.Config.Settings.RepresentationOfYes);
  132. cmbElseValue.Items.Add(dashboardHelper.Config.Settings.RepresentationOfNo);
  133. }
  134. private void checkboxUseElse_CheckedChanged(object sender, EventArgs e)
  135. {
  136. if (checkboxUseElse.Checked)
  137. {
  138. txtElseValue.Enabled = true;
  139. cmbElseValue.Enabled = true;
  140. }
  141. else
  142. {
  143. txtElseValue.Enabled = false;
  144. cmbElseValue.Enabled = false;
  145. }
  146. }
  147. private void btnIfCondition_Click(object sender, EventArgs e)
  148. {
  149. RowFilterDialog rfd = new RowFilterDialog(this.dashboardHelper, FilterDialogMode.ConditionalMode, DataFilters, true);
  150. DialogResult result = rfd.ShowDialog();
  151. if (result == DialogResult.OK)
  152. {
  153. this.DataFilters = rfd.DataFilters;
  154. this.txtAssignCondition.Text = DataFilters.GenerateReadableDataFilterString();//this.DataFilters.GenerateDataFilterString();
  155. }
  156. }
  157. private void btnOK_Click(object sender, EventArgs e)
  158. {
  159. string destinationColumnType = "System.String";
  160. object elseValue = this.txtElseValue.Text;
  161. object assignValue = this.txtAssignValue.Text;
  162. switch (cbxFieldType.SelectedItem.ToString())
  163. {
  164. case "Yes/No":
  165. destinationColumnType = "System.Boolean";
  166. if (cmbAssignValue.SelectedIndex == 0)
  167. {
  168. assignValue = true;
  169. }
  170. else if (cmbAssignValue.SelectedIndex == 1)
  171. {
  172. assignValue = false;
  173. }
  174. if (cmbElseValue.SelectedIndex == 0)
  175. {
  176. elseValue = true;
  177. }
  178. else if (cmbElseValue.SelectedIndex == 1)
  179. {
  180. elseValue = false;
  181. }
  182. break;
  183. case "Text":
  184. destinationColumnType = "System.String";
  185. elseValue = this.txtElseValue.Text;
  186. assignValue = this.txtAssignValue.Text;
  187. break;
  188. case "Numeric":
  189. destinationColumnType = "System.Decimal";
  190. decimal decElse;
  191. decimal decAssign;
  192. bool success1 = Decimal.TryParse(this.txtElseValue.Text, out decElse);
  193. if (success1) elseValue = decElse;
  194. bool success2 = Decimal.TryParse(this.txtAssignValue.Text, out decAssign);
  195. if (success2) assignValue = decAssign;
  196. if ((!success1 && checkboxUseElse.Checked) || !success2)
  197. {
  198. Epi.Windows.MsgBox.ShowError(DashboardSharedStrings.ERROR_CANNOT_CONDITIONAL_ASSIGN_INVALID_INPUT);
  199. this.DialogResult = DialogResult.None;
  200. return;
  201. }
  202. break;
  203. }
  204. if (!editMode && this.dashboardHelper.TableColumnNames.ContainsKey(txtDestinationField.Text))
  205. {
  206. string columnType = dashboardHelper.GetColumnType(txtDestinationField.Text);
  207. if (columnType != destinationColumnType)
  208. {
  209. Epi.Windows.MsgBox.ShowError(string.Format(DashboardSharedStrings.ERROR_CANNOT_CONDITIONAL_ASSIGN_TYPE_MISMATCH, columnType, destinationColumnType));
  210. this.DialogResult = DialogResult.None;
  211. return;
  212. }
  213. }
  214. string sentencePart = txtAssignCondition.Text;
  215. if (sentencePart.Length > 0)
  216. {
  217. sentencePart = " when " + "t" + txtAssignCondition.Text.Remove(0, 1);
  218. }
  219. string conditionText = "Assign " + txtDestinationField.Text + " the value " + assignValue + sentencePart;
  220. if (!checkboxUseElse.Checked)
  221. {
  222. elseValue = null;
  223. }
  224. else
  225. {
  226. conditionText = conditionText + ". Otherwise, assign " + txtDestinationField.Text + " the value " + elseValue + ".";
  227. }
  228. assignRule = new Rule_ConditionalAssign(this.dashboardHelper, conditionText, txtDestinationField.Text, destinationColumnType, assignValue, elseValue, DataFilters.GenerateDataFilterString());
  229. assignRule.DataFilters = this.DataFilters;
  230. }
  231. private void cbxFieldType_SelectedIndexChanged(object sender, EventArgs e)
  232. {
  233. switch (cbxFieldType.SelectedIndex)
  234. {
  235. case -1:
  236. return;
  237. case 0:
  238. txtAssignValue.Visible = true;
  239. txtElseValue.Visible = true;
  240. cmbAssignValue.Visible = false;
  241. cmbElseValue.Visible = false;
  242. break;
  243. case 1:
  244. txtAssignValue.Visible = true;
  245. txtElseValue.Visible = true;
  246. cmbAssignValue.Visible = false;
  247. cmbElseValue.Visible = false;
  248. break;
  249. case 2:
  250. txtAssignValue.Visible = false;
  251. txtElseValue.Visible = false;
  252. cmbAssignValue.Visible = true;
  253. cmbElseValue.Visible = true;
  254. break;
  255. }
  256. }
  257. }
  258. }