public bool PopulateStaticItems(DataField field, FieldValue[] contextValues) { if (field.SupportsStaticItems() && (String.IsNullOrEmpty(field.ContextFields) || ((contextValues != null) || (field.ItemsStyle == "CheckBoxList")))) { if (PopulatingStaticItems) { return(true); } PopulatingStaticItems = true; try { string[] filter = null; if (!(String.IsNullOrEmpty(field.ContextFields))) { List <string> contextFilter = new List <string>(); Match m = Regex.Match(field.ContextFields, "(\\w+)=(.+?)($|,)"); while (m.Success) { Match vm = Regex.Match(m.Groups[2].Value, "^\'(.+?)\'$"); if (vm.Success) { contextFilter.Add(String.Format("{0}:={1}", m.Groups[1].Value, vm.Groups[1].Value)); } else if (contextValues != null) { foreach (FieldValue cv in contextValues) { if (cv.Name == m.Groups[2].Value) { contextFilter.Add(String.Format("{0}:={1}", m.Groups[1].Value, cv.NewValue)); break; } } } m = m.NextMatch(); } filter = contextFilter.ToArray(); } PageRequest request = new PageRequest(-1, 1000, field.ItemsDataTextField, filter); ViewPage page = ControllerFactory.CreateDataController().GetPage(field.ItemsDataController, field.ItemsDataView, request); int dataValueFieldIndex = page.Fields.IndexOf(page.FindField(field.ItemsDataValueField)); if (dataValueFieldIndex == -1) { foreach (DataField aField in page.Fields) { if (aField.IsPrimaryKey) { dataValueFieldIndex = page.Fields.IndexOf(aField); break; } } } int dataTextFieldIndex = page.Fields.IndexOf(page.FindField(field.ItemsDataTextField)); if (dataTextFieldIndex == -1) { int i = 0; while ((dataTextFieldIndex == -1) && (i < page.Fields.Count)) { DataField f = page.Fields[i]; if (!(f.Hidden) && (f.Type == "String")) { dataTextFieldIndex = i; } i++; } if (dataTextFieldIndex == -1) { dataTextFieldIndex = 0; } } List <int> fieldIndexes = new List <int>(); fieldIndexes.Add(dataValueFieldIndex); fieldIndexes.Add(dataTextFieldIndex); if (!(String.IsNullOrEmpty(field.Copy))) { Match m = Regex.Match(field.Copy, "(\\w+)=(\\w+)"); while (m.Success) { int copyFieldIndex = page.Fields.IndexOf(page.FindField(m.Groups[2].Value)); if (copyFieldIndex >= 0) { fieldIndexes.Add(copyFieldIndex); } m = m.NextMatch(); } } foreach (object[] row in page.Rows) { object[] values = new object[fieldIndexes.Count]; for (int i = 0; (i < fieldIndexes.Count); i++) { int copyFieldIndex = fieldIndexes[i]; if (copyFieldIndex >= 0) { values[i] = row[copyFieldIndex]; } } field.Items.Add(values); } return(true); } finally { PopulatingStaticItems = false; } } return(false); }
private void ExportDataAsRss(ViewPage page, DbDataReader reader, StreamWriter writer) { string appPath = Regex.Replace(HttpContext.Current.Request.Url.AbsoluteUri, "^(.+)Export.ashx.+$", "$1", RegexOptions.IgnoreCase); XmlWriterSettings settings = new XmlWriterSettings(); settings.CloseOutput = false; XmlWriter output = XmlWriter.Create(writer, settings); output.WriteStartDocument(); output.WriteStartElement("rss"); output.WriteAttributeString("version", "2.0"); output.WriteStartElement("channel"); output.WriteElementString("title", ((string)(_view.Evaluate("string(concat(/c:dataController/@label, \' | \', @label))", Resolver)))); output.WriteElementString("lastBuildDate", DateTime.Now.ToString("r")); output.WriteElementString("language", System.Threading.Thread.CurrentThread.CurrentCulture.Name.ToLower()); int rowCount = 0; while ((rowCount < MaximumRssItems) && reader.Read()) { output.WriteStartElement("item"); bool hasTitle = false; bool hasPubDate = false; StringBuilder desc = new StringBuilder(); for (int i = 0; (i < page.Fields.Count); i++) { DataField field = page.Fields[i]; if (!(field.Hidden)) { if (rowCount == 0) { field.NormalizeDataFormatString(); } if (!(String.IsNullOrEmpty(field.AliasName))) { field = page.FindField(field.AliasName); } string text = String.Empty; object v = reader[field.Name]; if (!(DBNull.Value.Equals(v))) { if (!(String.IsNullOrEmpty(field.DataFormatString))) { text = String.Format(field.DataFormatString, v); } else { text = Convert.ToString(v); } } if (!(hasPubDate) && (field.Type == "DateTime")) { hasPubDate = true; if (!(String.IsNullOrEmpty(text))) { output.WriteElementString("pubDate", ((DateTime)(reader[field.Name])).ToString("r")); } } if (!(hasTitle)) { hasTitle = true; output.WriteElementString("title", text); StringBuilder link = new StringBuilder(); link.Append(_config.Evaluate("string(/c:dataController/@name)")); foreach (DataField pkf in page.Fields) { if (pkf.IsPrimaryKey) { link.Append(String.Format("&{0}={1}", pkf.Name, reader[pkf.Name])); } } string itemGuid = String.Format("{0}Details.aspx?l={1}", appPath, HttpUtility.UrlEncode(Convert.ToBase64String(Encoding.Default.GetBytes(link.ToString())))); output.WriteElementString("link", itemGuid); output.WriteElementString("guid", itemGuid); } else if (!(String.IsNullOrEmpty(field.OnDemandHandler)) && (field.OnDemandStyle == OnDemandDisplayStyle.Thumbnail)) { if (text.Equals("1")) { desc.AppendFormat("{0}:<br /><img src=\"{1}Blob.ashx?{2}=t", HttpUtility.HtmlEncode(field.Label), appPath, field.OnDemandHandler); foreach (DataField f in page.Fields) { if (f.IsPrimaryKey) { desc.Append("|"); desc.Append(reader[f.Name]); } } desc.Append("\" style=\"width:92px;height:71px;\"/><br />"); } } else { desc.AppendFormat("{0}: {1}<br />", HttpUtility.HtmlEncode(field.Label), HttpUtility.HtmlEncode(text)); } } } output.WriteStartElement("description"); output.WriteCData(String.Format("<span style=\\\"font-size:small;\\\">{0}</span>", desc.ToString())); output.WriteEndElement(); output.WriteEndElement(); rowCount++; } output.WriteEndElement(); output.WriteEndElement(); output.WriteEndDocument(); output.Close(); }
private void ExportDataAsCsv(ViewPage page, DbDataReader reader, StreamWriter writer) { bool firstField = true; for (int i = 0; (i < page.Fields.Count); i++) { DataField field = page.Fields[i]; if (!(field.Hidden)) { if (firstField) { firstField = false; } else { writer.Write(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator); } if (!(String.IsNullOrEmpty(field.AliasName))) { field = page.FindField(field.AliasName); } writer.Write("\"{0}\"", field.Label.Replace("\"", "\"\"")); } field.NormalizeDataFormatString(); } writer.WriteLine(); while (reader.Read()) { firstField = true; for (int j = 0; (j < page.Fields.Count); j++) { DataField field = page.Fields[j]; if (!(field.Hidden)) { if (firstField) { firstField = false; } else { writer.Write(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator); } if (!(String.IsNullOrEmpty(field.AliasName))) { field = page.FindField(field.AliasName); } string text = String.Empty; object v = reader[field.Name]; if (!(DBNull.Value.Equals(v))) { if (!(String.IsNullOrEmpty(field.DataFormatString))) { text = String.Format(field.DataFormatString, v); } else { text = Convert.ToString(v); } writer.Write("\"{0}\"", text.Replace("\"", "\"\"")); } else { writer.Write("\"\""); } } } writer.WriteLine(); } }
private void ExportDataAsRowset(ViewPage page, DbDataReader reader, StreamWriter writer) { string s = "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"; string dt = "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"; string rs = "urn:schemas-microsoft-com:rowset"; string z = "#RowsetSchema"; XmlWriterSettings settings = new XmlWriterSettings(); settings.CloseOutput = false; XmlWriter output = XmlWriter.Create(writer, settings); output.WriteStartDocument(); output.WriteStartElement("xml"); output.WriteAttributeString("xmlns", "s", null, s); output.WriteAttributeString("xmlns", "dt", null, dt); output.WriteAttributeString("xmlns", "rs", null, rs); output.WriteAttributeString("xmlns", "z", null, z); // declare rowset schema output.WriteStartElement("Schema", s); output.WriteAttributeString("id", "RowsetSchema"); output.WriteStartElement("ElementType", s); output.WriteAttributeString("name", "row"); output.WriteAttributeString("content", "eltOnly"); output.WriteAttributeString("CommandTimeout", rs, "60"); List <DataField> fields = new List <DataField>(); foreach (DataField field in page.Fields) { if (!((field.Hidden || field.OnDemand)) && !(fields.Contains(field))) { DataField aliasField = field; if (!(String.IsNullOrEmpty(field.AliasName))) { aliasField = page.FindField(field.AliasName); } fields.Add(aliasField); } } int number = 1; foreach (DataField field in fields) { field.NormalizeDataFormatString(); output.WriteStartElement("AttributeType", s); output.WriteAttributeString("name", field.Name); output.WriteAttributeString("number", rs, number.ToString()); output.WriteAttributeString("nullable", rs, "true"); output.WriteAttributeString("name", rs, field.Label); output.WriteStartElement("datatype", s); output.WriteAttributeString("type", dt, RowsetTypeMap[field.Type]); if (field.DataFormatString == "{0:c}") { output.WriteAttributeString("dbtype", rs, "currency"); } output.WriteEndElement(); output.WriteEndElement(); number++; } output.WriteStartElement("extends", s); output.WriteAttributeString("type", "rs:rowbase"); output.WriteEndElement(); output.WriteEndElement(); output.WriteEndElement(); // output rowset data output.WriteStartElement("data", rs); while (reader.Read()) { output.WriteStartElement("row", z); foreach (DataField field in fields) { object v = reader[field.Name]; if (!(DBNull.Value.Equals(v))) { if (!(String.IsNullOrEmpty(field.DataFormatString)) && !(((field.DataFormatString == "{0:d}") || (field.DataFormatString == "{0:c}")))) { output.WriteAttributeString(field.Name, String.Format(field.DataFormatString, v)); } else if (field.Type == "DateTime") { output.WriteAttributeString(field.Name, ((DateTime)(v)).ToString("s")); } else { output.WriteAttributeString(field.Name, v.ToString()); } } } output.WriteEndElement(); } output.WriteEndElement(); output.WriteEndElement(); output.WriteEndDocument(); output.Close(); }