/// <summary> /// Creates a new Cell object with the InlineString data type. /// </summary> private static Cell CreateTextCell( object cellValue, uint?styleIndex, bool isRelationshipValue = false) { var cell = new Cell { DataType = CellValues.InlineString }; //apply the cell style if supplied if (styleIndex.HasValue) { cell.StyleIndex = styleIndex.Value; } var inlineString = new InlineString( ); var t = new DocumentFormat.OpenXml.Spreadsheet.Text( ); if (cellValue != null) { t.Text = isRelationshipValue ? DatabaseTypeHelper.GetEntityXmlName(cellValue.ToString( )) : cellValue.ToString( ); } inlineString.AppendChild(t); cell.AppendChild(inlineString); return(cell); }
/// <summary> /// Get the formatted cell value for the different data types. /// </summary> private static string GetFormattedCellValue(object cellValue, DatabaseType type, bool isRelationshipType = false) { if (cellValue == null || cellValue is DBNull) { return(""); } if (type is BoolType) { string result = ExportDataHelper.GetBooleanCellValue(( bool )cellValue); return(result); } if (type is ChoiceRelationshipType || type is InlineRelationshipType || isRelationshipType) { string result = DatabaseTypeHelper.GetEntityXmlName(( string )cellValue); return(result); } else { // TODO: UTC ??!! string formatString = DatabaseTypeHelper.GetDisplayFormatString(type); string result = string.Format(formatString, cellValue); return(result); } }
private static object ReformatResult(object result, TestData testData) { if (result == null) { return("<null>"); } var list = result as IEnumerable <IEntity>; if (list != null) { var names = list.Select(e => e == null ? "<null>" : e.As <Resource>().Name ?? e.Alias); if (testData.Unsorted) { names = names.OrderBy(name => name); } return(string.Join(",", names)); } if (result is string) { string sResult = (string)result; if (sResult.StartsWith("<e ")) { sResult = DatabaseTypeHelper.GetEntityXmlName(sResult); } return(sResult); } var list2 = result as IEnumerable; if (list2 != null) { var names = list2.OfType <object>().Select(e => e == null ? "<null>" : e.ToString()).Select(f => f.StartsWith("<e ") ? DatabaseTypeHelper.GetEntityXmlName(f) : f); if (testData.Unsorted) { names = names.OrderBy(name => name); } return(string.Join(",", names)); } decimal?dResult = result as decimal?; if (dResult != null && testData.Expected.ExprType.DecimalPlaces != null) { result = decimal.Round(dResult.Value, testData.Expected.ExprType.DecimalPlaces.Value); } return(result); }
/// <summary> /// Generate CSV document byte stream /// </summary> /// <param name="queryResult">QueryResult</param> /// <returns>byte[]</returns> public static byte[] CreateCsvDocument(QueryResult queryResult) { var currentCulure = Thread.CurrentThread.CurrentCulture; Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; try { var sb = new StringBuilder( ); int headerIndx = 1; foreach (ResultColumn col in queryResult.Columns) { //add separator if (!col.IsHidden) { sb.Append(col.DisplayName); if (headerIndx != queryResult.Columns.Count) { sb.Append(','); } } headerIndx++; } //append new line sb.Append("\r\n"); foreach (DataRow row in queryResult.DataTable.Rows) { int colIndx = 0; bool first = true; foreach (ResultColumn col in queryResult.Columns) { if (!col.IsHidden) { if (first) { first = false; } else { sb.Append(','); } object cellValue = row[colIndx]; string csvValue; if (cellValue == null || cellValue is DBNull) { csvValue = string.Empty; } else if (col.ColumnType is StructureLevelsType) { csvValue = GetStructureLevelCellValue(( string )cellValue, false); } else if (col.ColumnType is ChoiceRelationshipType || col.ColumnType is InlineRelationshipType) { csvValue = DatabaseTypeHelper.GetEntityXmlName(( string )cellValue); } else if (col.ColumnType is AutoIncrementType) { string displayPattern = null; if (row.Table.Columns[colIndx].ExtendedProperties.ContainsKey("DisplayPattern")) { displayPattern = ( string )row.Table.Columns[colIndx].ExtendedProperties["DisplayPattern"]; } var intStringVal = cellValue as string; int temp; if (intStringVal != null) { temp = int.Parse(intStringVal); } else { temp = ( int )cellValue; } csvValue = DatabaseTypeHelper.ConvertToString(col.ColumnType, temp, displayPattern); } else if (col.ColumnType is BoolType) { csvValue = GetBooleanCellValue(( bool )cellValue); } else if (col.ColumnType is DateType) { csvValue = (( DateTime )cellValue).ToString("d/MM/yyyy"); } else if (col.ColumnType is TimeType) { csvValue = (( DateTime )cellValue).ToString("h:mm tt"); } else if (col.ColumnType is DateTimeType) { csvValue = (( DateTime )cellValue).ToString("d/MM/yyyy h:mm tt"); } else { //The cell value is string type. bool isRelationshipValue = col.RequestColumn.Expression is AggregateExpression; // Use ConvertToString, which is the network format, because it is invariant. csvValue = isRelationshipValue ? DatabaseTypeHelper.GetEntityXmlName(cellValue.ToString( )) : DatabaseTypeHelper.ConvertToString(col.ColumnType, cellValue); csvValue = csvValue.Replace("\r", string.Empty).Replace("\n", string.Empty); } sb.Append(CsvEscape(csvValue)); } colIndx++; } //append new line sb.Append("\r\n"); } byte[] bytesInStream = Encoding.UTF8.GetBytes(sb.ToString( )); return(bytesInStream); } finally { Thread.CurrentThread.CurrentCulture = currentCulure; } }
/// <summary> /// Get Boolean Cell Value /// </summary> public static string GetResourceCellValue(string cellValue) { string result = DatabaseTypeHelper.GetEntityXmlName(cellValue); return(result); }
/// <summary> /// Predicates for rule. /// </summary> /// <param name="condition">The condition.</param> /// <param name="isResourceCondition">if set to <c>true</c> [is resource condition].</param> /// <param name="gridResultColumn">The result column.</param> /// <param name="conditionalFormatter">The conditional formatter column.</param> /// <returns>Predicate{System.Object}.</returns> private static Predicate <object> PredicateForRule(Condition condition, bool isResourceCondition, ResultColumn gridResultColumn, ConditionalFormatter conditionalFormatter) { try { Predicate <object> valueFilterPredicate = BuildValueFilterPredicateNoNulls(condition, gridResultColumn, conditionalFormatter); return(value => { try { // Handle nulls // for now ANY type of filter on a column will reject null values if (value == null || value is DBNull) { switch (condition.Operator) { case ConditionType.NotEqual: value = ""; break; case ConditionType.IsNull: return true; case ConditionType.IsNotNull: return false; case ConditionType.AnyExcept: return true; default: return false; } } // Handle transforms if (isResourceCondition) { var xml = (string)value; if (condition.Operator == ConditionType.AnyExcept || condition.Operator == ConditionType.AnyOf || condition.Operator == ConditionType.CurrentUser) { value = DatabaseTypeHelper.GetEntityXmlId(xml); } else { value = DatabaseTypeHelper.GetEntityXmlName(xml); } } // Evaluate the specific predicate return valueFilterPredicate(value); } catch { return false; //throw new Exception( string.Format( "Predicate failed. Data={0} {1}. Expected={2}", value == null ? "null" : value.GetType( ).Name, value, condition.ColumnType ), ex ); } }); } catch (ArgumentException) { return(null); // argument does not exist .. return null } }