private void btnRemoveRule_Click(object sender, System.Windows.RoutedEventArgs e) { if (lbxRules.SelectedIndex < 0) { return; } int colindex = -1; string colName = string.Empty; //bool varUsedByAnotherVar = false; applicationViewModel.DeletedVariableInUse = false; for (int i = 0; i < applicationViewModel.EwavSelectedDatasource.AllColumns.Count; i++) { if (((ListBoxItemSource)lbxRules.SelectedItems[0]).DestinationColumn.Equals(applicationViewModel.EwavSelectedDatasource.AllColumns[i].Name)) { colindex = i; colName = applicationViewModel.EwavSelectedDatasource.AllColumns[i].Name; break; } } //for (int i = 0; i < applicationViewModel.ListOfDefinedVarsInUseByAnotherVar.Count; i++) //{ // if (applicationViewModel.ListOfDefinedVarsInUseByAnotherVar[i].Name.ToLower() == colName.ToLower()) // { // varUsedByAnotherVar = true; // } //}varUsedByAnotherVar || //Handles if it is being used by another variable. if (applicationViewModel.EwavSelectedDatasource.AllColumns[colindex].ChildVariableName != null && applicationViewModel.EwavSelectedDatasource.AllColumns[colindex].ChildVariableName.Length > 0) { MessageBox.Show("This variable cannot be deleted since it is being used by another defined variable. Please delete the dependent variables before deleting this variable.", "Warning", MessageBoxButton.OK); return; } //Handles if it being used by another gadget. if (applicationViewModel.EwavSelectedDatasource.AllColumns[colindex].IsInUse) { applicationViewModel.DeletedVariableInUse = true; MessageBoxResult result = MessageBox.Show("This variable is being used by one or more gadgets. The gadgets will have their properties reset if the variable is deleted. Would you like to delete this variable?", "Warning", MessageBoxButton.OKCancel); if (result == MessageBoxResult.Cancel) { return; } } int index = -1; string selectedItem = ((ListBoxItemSource)lbxRules.SelectedItems[0]).DestinationColumn; List <ListBoxItemSource> source = applicationViewModel.ListOfRules; List <EwavRule_Base> listOfRules = new List <EwavRule_Base>(); for (int i = 0; i < applicationViewModel.EwavDefinedVariables.Count; i++) { listOfRules.Add(applicationViewModel.EwavDefinedVariables[i]); } //for (int i = 0; i < lbxRules.Items.Count; i++) //{ // if (listOfRules[i] is EwavRule_Recode) // { // EwavRule_Recode rule = listOfRules[i] as EwavRule_Recode; // if (selectedItem == rule.TxtDestinationField) // { // index = i; // } // } // else if (listOfRules[i] is EwavRule_SimpleAssignment) // { // EwavRule_SimpleAssignment rule = listOfRules[i] as EwavRule_SimpleAssignment; // if (selectedItem == rule.TxtDestinationField) // { // index = i; // } // } // else // { // throw new Exception(" rule error for " + listOfRules[i].GetType().ToString()); // } //} for (int i = 0; i < lbxRules.Items.Count; i++) { if (selectedItem == listOfRules[i].VaraiableName) { index = i; break; } } if (index > -1) { source.RemoveAt(index); listOfRules.RemoveAt(index); } lbxRules.ItemsSource = null; lbxRules.ItemsSource = source; filterCount.Text = string.Format("Defined Variables ({0})", applicationViewModel.ListOfRules.Count); VerticalHeading.Text = string.Format("Defined Variables ({0})", applicationViewModel.ListOfRules.Count); index = -1; EwavRule_Base Rule = new EwavRule_Base(); //for (int i = 0; i < listOfRules.Count; i++) //{ // if (listOfRules[i] is EwavRule_Recode) // { // EwavRule_Recode rule = listOfRules[i] as EwavRule_Recode; // if (selectedItem == rule.TxtDestinationField) // { // index = i; // } // } // else // { // throw new Exception(" rule error for " + listOfRules[i].GetType().ToString()); // } //} //for (int i = 0; i < listOfRules.Count; i++) //{ // if (selectedItem == listOfRules[i].VaraiableName) // { // index = i; // break; // } //} applicationViewModel.InvokePreColumnChangedEvent(); colName = applicationViewModel.EwavSelectedDatasource.AllColumns[colindex].Name; applicationViewModel.RemoveIndicator = true; applicationViewModel.ItemToBeRemoved = applicationViewModel.EwavSelectedDatasource.AllColumns[colindex]; applicationViewModel.EwavSelectedDatasource.AllColumns.RemoveAt(colindex); applicationViewModel.EwavDefinedVariables = listOfRules; applicationViewModel.RemoveIndicator = false; //searches for any reference of deleted column. and removes the dependencies. for (int i = 0; i < applicationViewModel.EwavSelectedDatasource.AllColumns.Count; i++) { if (applicationViewModel.EwavSelectedDatasource.AllColumns[i].ChildVariableName != null && applicationViewModel.EwavSelectedDatasource.AllColumns[i].ChildVariableName == colName) { applicationViewModel.EwavSelectedDatasource.AllColumns[i].ChildVariableName = string.Empty; } } EnableDisableEditRemove(); }
/// <summary> /// Serializes the rules /// </summary> /// <param name="rule"></param> /// <returns></returns> public static XElement Serialize(this EwavRule_Base rule) { XElement element = null; if (rule is EwavRule_Format) { EwavRule_Format ruleFormat = (EwavRule_Format)rule; element = new XElement("rule", new XAttribute("type", "Rule_Format"), new XElement("friendlyRule", ruleFormat.FriendlyLabel), new XElement("sourceColumnName", ruleFormat.CbxFieldName), new XElement("destinationColumnName", ruleFormat.TxtDestinationField), new XElement("formatString", ruleFormat.CbxFormatOptions), new XElement("formatType", ruleFormat.FormatTypes), new XElement("variableDataType", ruleFormat.VaraiableDataType)); } else if (rule is EwavRule_ExpressionAssign) { EwavRule_ExpressionAssign ruleAssign = (EwavRule_ExpressionAssign)rule; element = new XElement("rule", new XAttribute("type", "Rule_ExpressionAssign"), new XElement("friendlyRule", ruleAssign.FriendlyRule), new XElement("expression", ruleAssign.Expression), new XElement("destinationColumnName", ruleAssign.DestinationColumnName), new XElement("destinationColumnType", ruleAssign.DataType), new XElement("variableDataType", ruleAssign.VaraiableDataType)); } else if (rule is EwavRule_ConditionalAssign) { EwavRule_ConditionalAssign ruleCondAssign = (EwavRule_ConditionalAssign)rule; element = new XElement("rule", new XAttribute("type", "Rule_ConditionalAssign"), new XElement("friendlyRule", ruleCondAssign.FriendlyRule.VarName), new XElement("destinationColumnName", ruleCondAssign.TxtDestination), new XElement("destinationColumnType", ruleCondAssign.DestinationColumnType), new XElement("assignValue", ruleCondAssign.AssignValue), new XElement("elseValue", ruleCondAssign.ElseValue), new XElement("cbxFieldType", ruleCondAssign.CbxFieldType), new XElement("variableDataType", ruleCondAssign.VaraiableDataType)); ruleCondAssign.ConditionsList.Serialize(element); } else if (rule is EwavRule_GroupVariable) { EwavRule_GroupVariable ruleGroupVar = (EwavRule_GroupVariable)rule; element = new XElement("rule", new XAttribute("type", "Rule_GroupVariable"), new XElement("friendlyLabel", ruleGroupVar.FriendlyLabel), new XElement("destinationColumnName", ruleGroupVar.VaraiableName), //new XElement("destinationColumnType", ruleGroupVar.VaraiableDataType), new XElement("variableDataType", ruleGroupVar.VaraiableDataType)); XElement items = new XElement("Columns"); for (int i = 0; i < ruleGroupVar.Items.Count; i++) { items.Add(new XElement("column", ruleGroupVar.Items[i].VarName)); } element.Add(items); } else if (rule is EwavRule_Recode) { EwavRule_Recode ruleRecode = (EwavRule_Recode)rule; element = new XElement("rule", new XAttribute("type", "Rule_Recode"), new XElement("friendlyRule", ruleRecode.Friendlyrule), new XElement("sourceColumnName", ruleRecode.SourceColumnName), new XElement("sourceColumnType", ruleRecode.SourceColumnType), new XElement("destinationColumnName", ruleRecode.TxtDestinationField), new XElement("destinationColumnType", ruleRecode.DestinationFieldType), //new XElement("tableColumns",(List<EwavRuleRecodeDataRow>)dataGridViewRecode.ItemsSource.Count.ToString() ), new XElement("elseValue", ruleRecode.TxtElseValue), new XElement("shouldUseWildcards", ruleRecode.CheckboxUseWildcardsIndicator), new XElement("shouldMaintainSortOrder", ruleRecode.CheckboxMaintainSortOrderIndicator), new XElement("variableDataType", ruleRecode.VaraiableDataType)); List <EwavRuleRecodeDataRow> dtb = ruleRecode.RecodeTable; XElement recodeTableElement = new XElement("recodeTable"); XElement recodeTableRowElement = null; for (int i = 0; i < dtb.Count; i++) { if (dtb[i].col1.Length > 0 && dtb[i].col1.Length > 0 && dtb[i].col1.Length > 0) { recodeTableRowElement = new XElement("recodeTableRow"); recodeTableRowElement.Add(new XElement("recodeTableData", dtb[i].col1.Replace("<", "<").Replace(">", ">"))); if (dtb[i].col2 == null) { recodeTableRowElement.Add(new XElement("recodeTableData", null)); } else if (dtb[i].col2.Length > 0) { recodeTableRowElement.Add(new XElement("recodeTableData", dtb[i].col2.Replace("<", "<").Replace(">", ">"))); } recodeTableRowElement.Add(new XElement("recodeTableData", dtb[i].col3.Replace("<", "<").Replace(">", ">"))); recodeTableElement.Add(recodeTableRowElement); } } element.Add(recodeTableElement); } else if (rule is EwavRule_SimpleAssignment) { EwavRule_SimpleAssignment ruleSimple = (EwavRule_SimpleAssignment)rule; element = new XElement("rule", new XAttribute("type", "Rule_SimpleAssign"), new XElement("friendlyRule", ruleSimple.FriendlyLabel), new XElement("assignmentType", ruleSimple.AssignmentType), new XElement("destinationColumnName", ruleSimple.TxtDestinationField), new XElement("variableDataType", ruleSimple.VaraiableDataType)); XElement paramsList = new XElement("parametersList"); for (int i = 0; i < ruleSimple.Parameters.Count; i++) { paramsList.Add(new XElement("parameter", ruleSimple.Parameters[i].VarName)); } element.Add(paramsList); } return(element); }
public List <EwavRule_Base> ConvertXMLToDefinedVariables(XDocument doc) { List <EwavRule_Base> EwavDefinedVariables = new List <EwavRule_Base>(); foreach (var item in doc.Descendants("Rules").Descendants()) { if (item.Name.ToString().ToLower() == "rule") { try { //Type gadgetType = Type.GetType(item.Attribute("type").Value); // item.Attributes["gadgetType"].Value); //EwavRule_Base rule = null; EwavRule_Base baseRule = null; switch (item.Attribute("type").Value.ToLower()) { case "rule_format": EwavRule_Format rule = new EwavRule_Format(); rule.FriendlyLabel = item.Element("friendlyRule").Value.ToString(); rule.CbxFieldName = item.Element("sourceColumnName").Value.ToString(); rule.TxtDestinationField = item.Element("destinationColumnName").Value.ToString(); rule.CbxFormatOptions = item.Element("formatString").Value.ToString(); rule.FormatTypes = (Ewav.Web.EpiDashboard.Rules.FormatTypes)Enum.Parse(typeof(Ewav.Web.EpiDashboard.Rules.FormatTypes), item.Element("formatType").Value.ToString(), true); rule.VaraiableDataType = item.Element("variableDataType").Value.ToString(); baseRule = rule; baseRule.VaraiableName = rule.TxtDestinationField; break; case "rule_expressionassign": EwavRule_ExpressionAssign ruleAssign = new EwavRule_ExpressionAssign(); ruleAssign.FriendlyRule = item.Element("friendlyRule").Value.ToString(); ruleAssign.Expression = item.Element("expression").Value.ToString(); ruleAssign.DestinationColumnName = item.Element("destinationColumnName").Value.ToString(); ruleAssign.DataType = item.Element("destinationColumnType").Value.ToString(); ruleAssign.VaraiableDataType = item.Element("variableDataType").Value.ToString(); baseRule = ruleAssign; baseRule.VaraiableName = ruleAssign.DestinationColumnName; break; case "rule_groupvariable": EwavRule_GroupVariable ruleGroupVar = new EwavRule_GroupVariable(); ruleGroupVar.FriendlyLabel = item.Element("friendlyLabel").Value.ToString(); ruleGroupVar.VaraiableName = item.Element("destinationColumnName").Value.ToString(); ruleGroupVar.VaraiableDataType = item.Element("variableDataType").Value.ToString(); List <MyString> columnList = new List <MyString>(); foreach (var column in item.Descendants("column")) { MyString colVal = new MyString(); colVal.VarName = column.Value.ToString(); columnList.Add(colVal); } ruleGroupVar.Items = columnList; baseRule = ruleGroupVar; baseRule.VaraiableName = ruleGroupVar.VaraiableName; break; case "rule_conditionalassign": EwavRule_ConditionalAssign ruleCondAssign = new EwavRule_ConditionalAssign(); MyString myString = new MyString(); myString.VarName = item.Element("friendlyRule").Value.ToString(); ruleCondAssign.FriendlyRule = myString; ruleCondAssign.TxtDestination = item.Element("destinationColumnName").Value.ToString(); ruleCondAssign.DestinationColumnType = item.Element("destinationColumnType").Value.ToString(); ruleCondAssign.AssignValue = item.Element("assignValue").Value.ToString(); ruleCondAssign.ElseValue = item.Element("elseValue").Value.ToString(); ruleCondAssign.VaraiableDataType = item.Element("variableDataType").Value.ToString(); if (item.Element("cbxFieldType") != null) { ruleCondAssign.CbxFieldType = (cbxFieldTypeEnum)Enum.Parse(typeof(cbxFieldTypeEnum), item.Element("cbxFieldType").Value.ToString(), false); } ruleCondAssign.ConditionsList = new List <EwavDataFilterCondition>(); //ruleCondAssign.ConditionsList = foreach (var condition in item.Descendants("EwavDataFilterCondition").OrderBy(x => (int)x.Attribute("order"))) { EwavDataFilterCondition df = new EwavDataFilterCondition(); if (condition.Attribute("friendlyOperand") != null) { df.FriendlyOperand = ToMyString(condition.Attribute("friendlyOperand").Value); } if (condition.Attribute("friendlyValue") != null) { df.FriendlyValue = ToMyString(condition.Attribute("friendlyValue").Value); } if (condition.Attribute("fieldName") != null) { df.FieldName = ToMyString(condition.Attribute("fieldName").Value); } if (condition.Attribute("joinType") != null) { df.JoinType = ToMyString(condition.Attribute("joinType").Value); } if (condition.Attribute("friendLowValue") != null && condition.Attribute("friendLowValue").Value != "null") { df.FriendLowValue = ToMyString(condition.Attribute("friendLowValue").Value); } if (condition.Attribute("friendHighValue") != null && condition.Attribute("friendHighValue").Value != "null") { df.FriendHighValue = ToMyString(condition.Attribute("friendHighValue").Value); } ruleCondAssign.ConditionsList.Add(df); } baseRule = ruleCondAssign; baseRule.VaraiableName = ruleCondAssign.TxtDestination; break; case "rule_simpleassign": EwavRule_SimpleAssignment ruleSimple = new EwavRule_SimpleAssignment(); ruleSimple.FriendlyLabel = item.Element("friendlyRule").Value.ToString(); ruleSimple.AssignmentType = (Ewav.Web.EpiDashboard.Rules.SimpleAssignType)Enum.Parse(typeof(Ewav.Web.EpiDashboard.Rules.SimpleAssignType), item.Element("assignmentType").Value.ToString(), true); ruleSimple.TxtDestinationField = item.Element("destinationColumnName").Value.ToString(); ruleSimple.Parameters = new List <MyString>(); ruleSimple.VaraiableDataType = item.Element("variableDataType").Value.ToString(); foreach (var item1 in item.Element("parametersList").Descendants()) { MyString mys = new MyString(); mys.VarName = item1.Value; ruleSimple.Parameters.Add(mys); } baseRule = ruleSimple; baseRule.VaraiableName = ruleSimple.TxtDestinationField; break; case "rule_recode": EwavRule_Recode ruleRecode = new EwavRule_Recode(); ruleRecode.Friendlyrule = item.Element("friendlyRule").Value.ToString(); ruleRecode.SourceColumnName = item.Element("sourceColumnName").Value.ToString(); ruleRecode.SourceColumnType = item.Element("sourceColumnType").Value.ToString(); ruleRecode.TxtDestinationField = item.Element("destinationColumnName").Value.ToString(); ruleRecode.DestinationFieldType = (DashboardVariableType)Enum.Parse(typeof(DashboardVariableType), item.Element("destinationColumnType").Value.ToString(), true); ruleRecode.TxtElseValue = item.Element("elseValue").Value.ToString(); ruleRecode.CheckboxUseWildcardsIndicator = bool.Parse(item.Element("shouldUseWildcards").Value.ToString()); ruleRecode.CheckboxMaintainSortOrderIndicator = bool.Parse(item.Element("shouldMaintainSortOrder").Value.ToString()); ruleRecode.VaraiableName = item.Element("destinationColumnName").Value.ToString(); ruleRecode.VaraiableDataType = item.Element("variableDataType").Value.ToString(); //ruleRecode.VaraiableDataType = item.Element("variableDataType").Value.ToString(); List <EwavRuleRecodeDataRow> rows = new List <EwavRuleRecodeDataRow>(); foreach (var item2 in item.Descendants("recodeTable")) { var itemmm = item2; foreach (var item3 in item2.Elements("recodeTableRow")) { EwavRuleRecodeDataRow row = new EwavRuleRecodeDataRow(); IEnumerable <XElement> enumerableList = item3.Elements("recodeTableData"); List <XElement> list = enumerableList.ToList(); if (list.Count == 2) { row.col1 = list[0].Value.ToString().Replace("<", "<").Replace(">", ">"); row.col3 = list[1].Value.ToString().Replace("<", "<").Replace(">", ">"); } else { row.col1 = list[0].Value.ToString().Replace("<", "<").Replace(">", ">"); row.col2 = list[1].Value.ToString().Replace("<", "<").Replace(">", ">"); row.col3 = list[2].Value.ToString().Replace("<", "<").Replace(">", ">"); } rows.Add(row); } } ruleRecode.RecodeTable = rows; baseRule = ruleRecode; baseRule.VaraiableName = ruleRecode.TxtDestinationField; break; default: throw new Exception("This Rule doesn't exists."); } //newColumn.SqlDataTypeAsString = (ColumnDataType)Enum.Parse(typeof(ColumnDataType), item.Element("variableDataType").Value.ToString(), false); EwavDefinedVariables.Add(baseRule); } catch (Exception ex) { //Epi.Windows.MsgBox.ShowError(DashboardSharedStrings.GADGET_LOAD_ERROR); throw new Exception("Exception occured deserializing Rules." + ex.Message); //return; } } } return(EwavDefinedVariables); }