using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sleis.Models ; using System.Reflection; using System.IO; using Sleis.ViewModels; namespace Sleis.Export { public abstract class ExportBase : IExport { public virtual object Entity { get; set; } public Dictionary Fields { get; set; } public Dictionary FieldPropertyMapping { get; set; } public Dictionary> EntityCustomFields { get; set; } public ExportBase() { sb = new StringBuilder(); Fields = new Dictionary(); FieldPropertyMapping = new Dictionary(); EntityCustomFields = new Dictionary>(); } public virtual string FileName { get { return string.Format("{0}{1}", m_ReportYear, m_FileName); } } public byte[] Content { get { return m_Content; } } public virtual void GenerateReport() { CreateColumns(); PopulateData(); m_Content = ConvertStringToByteArray(sb.ToString()); } public int ReportingYear { set { m_ReportYear = value; } } public string ContentType { get { return "text/comma-separated-values"; } } protected virtual void CreateColumns() { CreateCustomFieldsColumn(); RemoveLastComma(); } private void CreateCustomFieldsColumn() { List customFields = new List(); foreach (List custfields in EntityCustomFields.Values) { if (custfields.Count > customFields.Count) { customFields = custfields; } } foreach (CustomFieldView fieldView in customFields) { sb.AppendFormat("{0},", fieldView.Field.Label); } } protected virtual void PopulateData() { RemoveLastComma(); } protected void PopulateCustomFieldValue(int key) { List customFields = EntityCustomFields[key]; foreach (CustomFieldView cfv in customFields) { if (cfv.Field.PublicStyle != CustomFieldDataStyleType.Hidden) { sb.AppendFormat("{0},", cfv.HasValue? cfv.Value.FieldValue.Value.ToString():string.Empty); } } } protected void RemoveLastComma() { sb.Remove(sb.Length - 2, 2); //remove last comma sb.AppendLine(); } protected string GetFieldKey(FieldPropertyMapping fpm) { foreach (string key in FieldPropertyMapping.Keys) { if (FieldPropertyMapping[key].ColumnName.Equals(fpm.ColumnName)) { return key; } } return null; } protected virtual void PopulateData(object root, string propertyName, StringBuilder sb) { //check property name //if it has child object, the format is child object.child property //if it has more property names, they are sperated by : object parent = null; if (propertyName.Contains(".")) { string[] propNames = propertyName.Split('.'); string childPropertyName = string.Empty; for (int i = 1; i < propNames.Length; i++) { childPropertyName = string.Format("{0}.{1}", childPropertyName, propNames[i]); } //remove first dot childPropertyName = childPropertyName.Remove(0, 1); if (root.GetType().GetProperty(propNames[0]).CanRead) { parent = root.GetType().GetProperty(propNames[0]).GetValue(root, null); PopulateData(parent, childPropertyName, sb); } } else { //need retrieve multiple value if (propertyName.Contains(":")) { WriteMultipleObjectValue(root, propertyName, sb); } else { object obj = root.GetType().GetProperty(propertyName).GetValue(root, null); //if (obj != null) //{ // Type t = obj.GetType(); // if (t.GetProperty("Count")!=null && t.GetProperty("Count").PropertyType == typeof(System.Int32)) // { // WriteListObjectValue(obj, propertyName, sb); // } // else // { WriteObjectValue(root, propertyName, sb); // } //} } } } protected virtual void WriteObjectValue(object root, string propertyName, StringBuilder sb) { if (root.GetType().GetProperty(propertyName).CanRead) { object obj = root.GetType().GetProperty(propertyName).GetValue(root, null); if (obj!=null && obj.GetType().GetProperty("Count") != null && obj.GetType().GetProperty("Count").PropertyType == typeof(System.Int32)) { string childPropertyName = string.Empty; foreach (FieldPropertyMapping p in FieldPropertyMapping.Values) { if (p.PropertyName == propertyName) { childPropertyName = p.ChildPropertyName; break; } } Int32 count = (Int32)obj.GetType().GetProperty("Count").GetValue(obj, null); PropertyInfo pinfo = obj.GetType().GetProperty("Item"); for (int i = 0; i < count; i++) { object process = pinfo.GetValue(obj, new object[] { i }); object child = process.GetType().GetProperty(childPropertyName).GetValue(process, null); sb.AppendFormat("{0} ", child == null ? string.Empty : child.ToString()); } sb.Append(","); } else { sb.AppendFormat("{0}, ", obj == null ? string.Empty : obj.ToString()); } } } protected virtual void WriteListObjectValue(object root, string propertyName, StringBuilder sb) { //find the child propertyname string childPropertyName = string.Empty; foreach (FieldPropertyMapping p in FieldPropertyMapping.Values) { if (p.PropertyName == propertyName) { childPropertyName = p.ChildPropertyName; break; } } //get count Int32 count = (Int32)root.GetType().GetProperty("Count").GetValue(root, null); PropertyInfo pinfo = root.GetType().GetProperty("Item"); for (int i = 0; i < count; i++) { object obj = pinfo.GetValue(root, new object[] { i }); object child = obj.GetType().GetProperty(childPropertyName).GetValue(obj, null); sb.AppendFormat("{0} ", child == null ? string.Empty : child.ToString()); } sb.Append(","); } protected virtual void WriteMultipleObjectValue(object root, string propertyName, StringBuilder sb) { } protected byte[] ConvertStringToByteArray(string input) { MemoryStream stream = new MemoryStream(); using (StreamWriter writer = new StreamWriter(stream)) { writer.Write(input); writer.Flush(); } return stream.ToArray(); } protected string m_FileName = string.Empty; protected byte[] m_Content = new byte[1024]; protected int m_ReportYear; protected StringBuilder sb = null; protected PropertyInfo[] propertyInfos = null; } }