예제 #1
0
        internal static string FlatModelToCsv(FlatModel FlatModel)
        {
            StringBuilder sbCsv = new StringBuilder();

            #region create header

            string csvHeader = string.Join(",", FlatModel.Headers);
            sbCsv.AppendLine(csvHeader);

            #endregion

            #region Create Lines

            foreach (var line in FlatModel.Lines)
            {
                StringBuilder sbCsvLine = new StringBuilder();

                for (int fieldIndex = 0; fieldIndex < line.Count(); fieldIndex++)
                {
                    //add the value to the string builder
                    string fieldValue = line[fieldIndex];
                    string CsvValue   = FieldValueToCsvValue(fieldValue);
                    sbCsvLine.Append(CsvValue);

                    //add seperator before the next field to the string builder
                    if (fieldIndex != line.Count() - 1)
                    {
                        sbCsvLine.Append(',');
                    }
                }

                string csvLine = sbCsvLine.ToString();
                sbCsv.AppendLine(csvLine);
            }

            #endregion

            string result = sbCsv.ToString();
            return(result);
        }
예제 #2
0
        /// <summary>
        /// converts data to flat text representation
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="data"></param>
        /// <param name="PropertiesToInclude"></param>
        /// <param name="lineValueForNullValue"></param>
        /// <returns></returns>
        /// <remarks>
        /// 1. null values in the data are mapped to lineValueForNullValue
        /// 2. datetime values are mapped to utc date time
        /// </remarks>
        internal static FlatModel MapDataToFlatModel <TModel>(IEnumerable <TModel> data, IEnumerable <string> PropertiesToInclude, string lineValueForNullValue)
        {
            Type modelType       = data.GetType().GetGenericArguments().Single();
            var  modelProperties = modelType.GetProperties();

            #region validate input: Reference,References Properties can not be included

            foreach (PropertyInfo property in modelProperties)
            {
                //exculde Reference properties
                if (PropertiesToInclude.Contains(property.Name) && property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Reference <>))
                {
                    throw new PepperiException("Operation failed. Reason: Reference Properties can not be included. (" + property.Name + ")");
                }

                //exculde References properties
                if (PropertiesToInclude.Contains(property.Name) && property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(References <>))
                {
                    throw new PepperiException("Operation failed. Reason: References Properties can not be included. (" + property.Name + ")");
                }
            }

            #endregion

            #region caculate relevantProperties

            List <PropertyInfo> relevantProperties = new List <PropertyInfo>();

            foreach (PropertyInfo property in modelProperties)
            {
                if (PropertiesToInclude != null && PropertiesToInclude.Contains(property.Name))
                {
                    relevantProperties.Add(property);
                }
            }

            #endregion

            #region calculate flat model Headers  by relevantProperties

            List <string> Headers = new List <string>();

            foreach (var relevantProperty in relevantProperties)
            {
                Headers.Add(relevantProperty.Name);
            }

            #endregion

            #region calculate flat model Lines

            List <FlatLine> FlatLines = new List <FlatLine>();

            foreach (var dataItem in data)
            {
                FlatLine FlatLine = new FlatLine();

                foreach (var flatModelProperty in relevantProperties)
                {
                    object fieldValueAsObject = flatModelProperty.GetValue(dataItem);

                    //handle null
                    if (fieldValueAsObject == null)
                    {
                        FlatLine.Add(lineValueForNullValue);
                        continue;
                    }

                    //handle date
                    if (flatModelProperty.PropertyType == typeof(Nullable <DateTime>))
                    {
                        DateTime?fieldValueAsDateTime = fieldValueAsObject as DateTime?;
                        string   fieldValueAsString   = (fieldValueAsDateTime == null) ? lineValueForNullValue : fieldValueAsDateTime.Value.ToString("yyyy-MM-ddTHH:mm:ssZ");
                        FlatLine.Add(fieldValueAsString);
                        continue;
                    }

                    //handle Numbers - uncomment if you want to change format
                    //if (flatModelProperty.PropertyType == typeof(Nullable<Double>))
                    //{
                    //    Double? fieldValueAsDouble = fieldValueAsObject as Double?;
                    //    string fieldValueAsString = (fieldValueAsDouble == null) ? lineValueForNullValue : fieldValueAsDouble.Value.ToString("0,00");
                    //    FlatLine.Add(fieldValueAsString);
                    //    continue;
                    //}

                    //handle list
                    if (flatModelProperty.PropertyType == typeof(List <string>))
                    {
                        List <string> fieldListValues  = fieldValueAsObject as List <string>;
                        string        fieldListAsArray = "";
                        if (fieldListValues != null && fieldListValues.Count > 0)
                        {
                            fieldListAsArray = string.Join("~", fieldListValues);
                        }
                        FlatLine.Add(fieldListAsArray);
                        continue;
                    }
                    //default
                    string fieldValue = fieldValueAsObject.ToString();
                    FlatLine.Add(fieldValue);
                }

                FlatLines.Add(FlatLine);
            }

            #endregion

            FlatModel FlatModel = new FlatModel(Headers, FlatLines);

            return(FlatModel);
        }