/// <summary> /// Sets the appropriate properties for a given field, based on the specified column conversion information /// </summary> /// <param name="field">The Epi Info 7 field</param> /// <param name="cci">The column conversion information</param> private void SetFieldProperties(Field field, ColumnConversionInfo cci) { if (cci.Prompt == null) { cci.Prompt = cci.DestinationColumnName; } switch (field.FieldType) { case MetaFieldType.Checkbox: CheckBoxField checkboxField = (CheckBoxField)field; checkboxField.TabIndex = cci.TabIndex; checkboxField.IsReadOnly = cci.IsReadOnly; checkboxField.IsRequired = cci.IsRequired; checkboxField.ShouldRepeatLast = cci.IsRepeatLast; break; case MetaFieldType.YesNo: YesNoField yesNoField = (YesNoField)field; yesNoField.TabIndex = cci.TabIndex; yesNoField.IsReadOnly = cci.IsReadOnly; yesNoField.IsRequired = cci.IsRequired; yesNoField.ShouldRepeatLast = cci.IsRepeatLast; break; case MetaFieldType.Text: SingleLineTextField textField = (SingleLineTextField)field; textField.TabIndex = cci.TabIndex; textField.IsReadOnly = cci.IsReadOnly; textField.IsRequired = cci.IsRequired; textField.ShouldRepeatLast = cci.IsRepeatLast; if (cci.UpperBound is int) { textField.MaxLength = (int)cci.UpperBound; } break; case MetaFieldType.Multiline: MultilineTextField multilineTextField = (MultilineTextField)field; multilineTextField.TabIndex = cci.TabIndex; multilineTextField.IsReadOnly = cci.IsReadOnly; multilineTextField.IsRequired = cci.IsRequired; multilineTextField.ShouldRepeatLast = cci.IsRepeatLast; break; case MetaFieldType.Date: DateField dateField = (DateField)field; dateField.TabIndex = cci.TabIndex; dateField.IsReadOnly = cci.IsReadOnly; dateField.IsRequired = cci.IsRequired; dateField.ShouldRepeatLast = cci.IsRepeatLast; break; case MetaFieldType.DateTime: DateTimeField dateTimeField = (DateTimeField)field; dateTimeField.TabIndex = cci.TabIndex; dateTimeField.IsReadOnly = cci.IsReadOnly; dateTimeField.IsRequired = cci.IsRequired; dateTimeField.ShouldRepeatLast = cci.IsRepeatLast; break; case MetaFieldType.Time: TimeField timeField = (TimeField)field; timeField.TabIndex = cci.TabIndex; timeField.IsReadOnly = cci.IsReadOnly; timeField.IsRequired = cci.IsRequired; timeField.ShouldRepeatLast = cci.IsRepeatLast; break; case MetaFieldType.Number: NumberField numberField = (NumberField)field; numberField.TabIndex = cci.TabIndex; numberField.IsReadOnly = cci.IsReadOnly; numberField.IsRequired = cci.IsRequired; numberField.ShouldRepeatLast = cci.IsRepeatLast; break; case MetaFieldType.LegalValues: DDLFieldOfLegalValues legalValuesField = (DDLFieldOfLegalValues)field; legalValuesField.TabIndex = cci.TabIndex; legalValuesField.IsReadOnly = cci.IsReadOnly; legalValuesField.IsRequired = cci.IsRequired; legalValuesField.ShouldRepeatLast = cci.IsRepeatLast; if (string.IsNullOrEmpty(cci.ListSourceTableName)) { DataTable dt = new DataTable(cci.SourceColumnName); dt.Columns.Add(new DataColumn(cci.SourceColumnName, typeof(string))); // table is blank, so assume user wants to use a SELECT DISTINCT as the value source Query selectDistinctQuery = sourceDriver.CreateQuery("SELECT DISTINCT [" + cci.SourceColumnName + "] FROM [" + tableName + "]"); IDataReader distinctReader = sourceDriver.ExecuteReader(selectDistinctQuery); while (distinctReader.Read()) { dt.Rows.Add(distinctReader[0].ToString()); } cci.ListSourceTable = dt; cci.ListSourceTableName = cci.SourceColumnName; cci.ListSourceTextColumnName = cci.SourceColumnName; IDbDriver db = project.CollectedData.GetDatabase(); if (!db.TableExists(cci.ListSourceTableName)) { project.CreateCodeTable(cci.ListSourceTableName, cci.ListSourceTextColumnName); project.SaveCodeTableData(cci.ListSourceTable, cci.ListSourceTableName, cci.ListSourceTextColumnName); } legalValuesField.SourceTableName = cci.ListSourceTableName; legalValuesField.TextColumnName = cci.ListSourceTextColumnName; legalValuesField.CodeColumnName = cci.ListSourceTextColumnName; } else { IDbDriver db = project.CollectedData.GetDatabase(); if (!db.TableExists(cci.ListSourceTableName)) { project.CreateCodeTable(cci.ListSourceTableName, cci.ListSourceTextColumnName); string[] columns = new string[1]; columns[0] = cci.ListSourceTextColumnName; project.InsertCodeTableData(cci.ListSourceTable, cci.ListSourceTableName, columns); } legalValuesField.SourceTableName = cci.ListSourceTableName; legalValuesField.TextColumnName = cci.ListSourceTextColumnName; legalValuesField.CodeColumnName = cci.ListSourceTextColumnName; } break; default: throw new ApplicationException("Invalid field type"); //break; } double ControlHeightPercentage = 0.0; double ControlWidthPercentage = 0.0; if (field is FieldWithSeparatePrompt) { FieldWithSeparatePrompt fieldWithPrompt; fieldWithPrompt = (FieldWithSeparatePrompt)field; fieldWithPrompt.PromptText = cci.Prompt; fieldWithPrompt.PromptFont = cci.PromptFont; fieldWithPrompt.ControlFont = cci.ControlFont; fieldWithPrompt.PromptLeftPositionPercentage = cci.ControlLeftPosition / 100; fieldWithPrompt.PromptTopPositionPercentage = cci.ControlTopPosition / 100; fieldWithPrompt.Name = cci.DestinationColumnName; fieldWithPrompt.ControlHeightPercentage = ControlHeightPercentage / 100; fieldWithPrompt.ControlWidthPercentage = ControlWidthPercentage / 100; fieldWithPrompt.ControlTopPositionPercentage = cci.ControlTopPosition / 100; fieldWithPrompt.ControlLeftPositionPercentage = (cci.ControlLeftPosition / 100) + 0.090702947845805; fieldWithPrompt.UpdatePromptPosition(); fieldWithPrompt.UpdateControlPosition(); } else { FieldWithoutSeparatePrompt fieldWithoutPrompt; fieldWithoutPrompt = (FieldWithoutSeparatePrompt)field; fieldWithoutPrompt.PromptText = cci.Prompt; fieldWithoutPrompt.PromptFont = cci.PromptFont; fieldWithoutPrompt.Name = cci.DestinationColumnName; fieldWithoutPrompt.ControlHeightPercentage = ControlHeightPercentage / 100; fieldWithoutPrompt.ControlWidthPercentage = ControlWidthPercentage / 100; fieldWithoutPrompt.ControlTopPositionPercentage = cci.ControlTopPosition / 100; fieldWithoutPrompt.ControlLeftPositionPercentage = (cci.ControlLeftPosition / 100) + 0.090702947845805; fieldWithoutPrompt.UpdateControlPosition(); } }
/// <summary> /// Gets the appropriate QueryParameter for a given column and value. /// </summary> /// <param name="cci">ColumnConversionInfo</param> /// <param name="value">Raw value</param> /// <returns>QueryParameter</returns> private QueryParameter GetQueryParameterForField(ColumnConversionInfo cci, object value) { String fieldName = cci.DestinationColumnName; // Scenario 1: // Source column is read as text // User elects to convert this column to numeric // Must specify numeric query param... // Thus, do some conversion based on allowable conversions. switch (cci.FieldType) { // User can elect to use checkbox fields for boolean and number columns case MetaFieldType.Checkbox: if (value is bool) { return(new QueryParameter("@" + fieldName, DbType.Boolean, value)); } else if (value.ToString() == "1" || value.ToString() == "-1") { return(new QueryParameter("@" + fieldName, DbType.Boolean, true)); } else { return(new QueryParameter("@" + fieldName, DbType.Boolean, false)); } // User can elect to use Yes/No fields for boolean and number columns case MetaFieldType.YesNo: if (value.ToString() == "1" || value.ToString() == "-1" || value.ToString().ToLowerInvariant() == "true") { return(new QueryParameter("@" + fieldName, DbType.Byte, 1)); } else if (value.ToString() == "0" || value.ToString().ToLowerInvariant() == "false") { return(new QueryParameter("@" + fieldName, DbType.Byte, 0)); } else { return(new QueryParameter("@" + fieldName, DbType.Byte, DBNull.Value)); } // User can elect to use Text fields for boolean, number, DateTime and text columns case MetaFieldType.Text: if (value is double || value is int || value is DateTime || value is bool) { return(new QueryParameter("@" + fieldName, DbType.String, value.ToString())); } else { return(new QueryParameter("@" + fieldName, DbType.String, value)); } case MetaFieldType.Multiline: return(new QueryParameter("@" + fieldName, DbType.String, value)); case MetaFieldType.Date: case MetaFieldType.DateTime: case MetaFieldType.Time: return(new QueryParameter("@" + fieldName, DbType.DateTime, value)); case MetaFieldType.LegalValues: return(new QueryParameter("@" + fieldName, DbType.String, value)); case MetaFieldType.Number: if (value is string) { double dValue; bool success = double.TryParse(value.ToString(), out dValue); if (success) { return(new QueryParameter("@" + fieldName, DbType.Double, dValue)); } else { return(new QueryParameter("@" + fieldName, DbType.Double, DBNull.Value)); } } else { return(new QueryParameter("@" + fieldName, DbType.Double, value)); } default: throw new ApplicationException("Invalid field type"); //break; } //switch (cci.SourceColumnType.ToString()) //{ // case "System.String": // return new QueryParameter("@" + fieldName, DbType.String, value); // case "System.Byte": // return new QueryParameter("@" + fieldName, DbType.Byte, value); // case "System.DateTime": // return new QueryParameter("@" + fieldName, DbType.DateTime, value); // case "System.Int16": // return new QueryParameter("@" + fieldName, DbType.Int16, value); // case "System.Int32": // return new QueryParameter("@" + fieldName, DbType.Int32, value); // case "System.Int64": // return new QueryParameter("@" + fieldName, DbType.Int64, value); // case "System.Single": // return new QueryParameter("@" + fieldName, DbType.Single, value); // case "System.Double": // return new QueryParameter("@" + fieldName, DbType.Double, value); // case "System.Decimal": // return new QueryParameter("@" + fieldName, DbType.Decimal, value); // case "System.Boolean": // if (destinationView.Fields.Contains(fieldName) && destinationView.Fields[fieldName] is Epi.Fields.YesNoField) // { // Epi.Fields.Field field = destinationView.Fields[fieldName] as Epi.Fields.YesNoField; // if ((bool)value == true) // { // return new QueryParameter("@" + fieldName, DbType.Byte, 1); // } // else if ((bool)value == false) // { // return new QueryParameter("@" + fieldName, DbType.Byte, 0); // } // else // { // return null; // } // } // else // return new QueryParameter("@" + fieldName, DbType.Boolean, value); // //default: // //throw new ApplicationException("Not a supported field type"); //} //return null; }
/// <summary> /// Gets the appropriate QueryParameter for a given column and value. /// </summary> /// <param name="cci">ColumnConversionInfo</param> /// <param name="value">Raw value</param> /// <returns>QueryParameter</returns> private QueryParameter GetQueryParameterForField(ColumnConversionInfo cci, object value) { String fieldName = cci.DestinationColumnName; // Scenario 1: // Source column is read as text // User elects to convert this column to numeric // Must specify numeric query param... // Thus, do some conversion based on allowable conversions. switch (cci.FieldType) { // User can elect to use checkbox fields for boolean and number columns case MetaFieldType.Checkbox: if (value is bool) { return new QueryParameter("@" + fieldName, DbType.Boolean, value); } else if (value.ToString() == "1" || value.ToString() == "-1") { return new QueryParameter("@" + fieldName, DbType.Boolean, true); } else { return new QueryParameter("@" + fieldName, DbType.Boolean, false); } // User can elect to use Yes/No fields for boolean and number columns case MetaFieldType.YesNo: if (value.ToString() == "1" || value.ToString() == "-1" || value.ToString().ToLower() == "true") { return new QueryParameter("@" + fieldName, DbType.Byte, 1); } else if (value.ToString() == "0" || value.ToString().ToLower() == "false") { return new QueryParameter("@" + fieldName, DbType.Byte, 0); } else { return new QueryParameter("@" + fieldName, DbType.Byte, DBNull.Value); } // User can elect to use Text fields for boolean, number, DateTime and text columns case MetaFieldType.Text: if (value is double || value is int || value is DateTime || value is bool) { return new QueryParameter("@" + fieldName, DbType.String, value.ToString()); } else { return new QueryParameter("@" + fieldName, DbType.String, value); } case MetaFieldType.Multiline: return new QueryParameter("@" + fieldName, DbType.String, value); case MetaFieldType.Date: case MetaFieldType.DateTime: case MetaFieldType.Time: return new QueryParameter("@" + fieldName, DbType.DateTime, value); case MetaFieldType.LegalValues: return new QueryParameter("@" + fieldName, DbType.String, value); case MetaFieldType.Number: if (value is string) { double dValue; bool success = double.TryParse(value.ToString(), out dValue); if (success) { return new QueryParameter("@" + fieldName, DbType.Single, dValue); } else { return new QueryParameter("@" + fieldName, DbType.Single, DBNull.Value); } } else { return new QueryParameter("@" + fieldName, DbType.Single, value); } default: throw new ApplicationException("Invalid field type"); //break; } //switch (cci.SourceColumnType.ToString()) //{ // case "System.String": // return new QueryParameter("@" + fieldName, DbType.String, value); // case "System.Byte": // return new QueryParameter("@" + fieldName, DbType.Byte, value); // case "System.DateTime": // return new QueryParameter("@" + fieldName, DbType.DateTime, value); // case "System.Int16": // return new QueryParameter("@" + fieldName, DbType.Int16, value); // case "System.Int32": // return new QueryParameter("@" + fieldName, DbType.Int32, value); // case "System.Int64": // return new QueryParameter("@" + fieldName, DbType.Int64, value); // case "System.Single": // return new QueryParameter("@" + fieldName, DbType.Single, value); // case "System.Double": // return new QueryParameter("@" + fieldName, DbType.Double, value); // case "System.Decimal": // return new QueryParameter("@" + fieldName, DbType.Decimal, value); // case "System.Boolean": // if (destinationView.Fields.Contains(fieldName) && destinationView.Fields[fieldName] is Epi.Fields.YesNoField) // { // Epi.Fields.Field field = destinationView.Fields[fieldName] as Epi.Fields.YesNoField; // if ((bool)value == true) // { // return new QueryParameter("@" + fieldName, DbType.Byte, 1); // } // else if ((bool)value == false) // { // return new QueryParameter("@" + fieldName, DbType.Byte, 0); // } // else // { // return null; // } // } // else // return new QueryParameter("@" + fieldName, DbType.Boolean, value); // //default: // //throw new ApplicationException("Not a supported field type"); //} //return null; }