88fde14c185d87acb9f105df28aa6430d9f09e27
FAQ/ListReports.md
| ... | ... | @@ -13,30 +13,45 @@ These examples are basic customized methods of obtaining a list of reports done |
| 13 | 13 | ###[[DatabaseAdHocConfig|http://wiki.izenda.us/FAQ/Storing-Reports#Database-Mode]] |
| 14 | 14 | |
| 15 | 15 | ```csharp |
| 16 | -public override Izenda.AdHoc.ReportInfo[] ListReports() { |
|
| 17 | - ArrayList reportNames = new ArrayList(); |
|
| 18 | - string sql = String.Format("SELECT Name, CreatedDate, ModifiedDate FROM {0} ORDER BY Name", |
|
| 19 | - AdHocSettings.SavedReportsTable); |
|
| 20 | - System.Data.IDbCommand command = null; |
|
| 21 | - try { |
|
| 22 | - command = Izenda.AdHoc.AdHocContext.Driver.CreateCommand(sql); |
|
| 23 | - System.Data.IDataReader reader = command.ExecuteReader(); |
|
| 24 | - |
|
| 25 | - while (reader.Read()) { |
|
| 26 | - string reportName = reader["Name"].ToString(); |
|
| 27 | - DateTime createdDate = (DateTime)reader["CreatedDate"]; |
|
| 28 | - DateTime modifiedDate = (DateTime)reader["ModifiedDate"]; |
|
| 29 | - if (reportName != "") |
|
| 30 | - reportNames.Add(new Izenda.AdHoc.ReportInfo(reportName, false, createdDate, modifiedDate)); |
|
| 16 | +public override ReportInfo[] ListReports() |
|
| 17 | + { |
|
| 18 | + try |
|
| 19 | + { |
|
| 20 | + string connectionString = @"Persist Security Info=False;Initial Catalog=Northwind;Data Source=LESHA-PC\SQL2012;User ID=dataLogin;Password=dataPassword;Integrated Security=false;"; |
|
| 21 | + string sql = String.Format("SELECT Name, CreatedDate, ModifiedDate FROM {0} ORDER BY Name", AdHocSettings.SavedReportsTable); |
|
| 22 | + ArrayList reportNames = new ArrayList(); |
|
| 23 | + using (System.Data.IDbConnection connection = new System.Data.SqlClient.SqlConnection(connectionString)) |
|
| 24 | + { |
|
| 25 | + connection.Open(); |
|
| 26 | + using (System.Data.IDbCommand command = connection.CreateCommand()) |
|
| 27 | + { |
|
| 28 | + command.CommandText = sql; |
|
| 29 | + using (System.Data.IDataReader reader = command.ExecuteReader()) |
|
| 30 | + { |
|
| 31 | + while (reader.Read()) |
|
| 32 | + { |
|
| 33 | + try |
|
| 34 | + { |
|
| 35 | + string reportName = reader["Name"].ToString(); |
|
| 36 | + DateTime createdDate = (DateTime) reader["CreatedDate"]; |
|
| 37 | + DateTime modifiedDate = (DateTime) reader["ModifiedDate"]; |
|
| 38 | + if (reportName != "") |
|
| 39 | + reportNames.Add(new ReportInfo(reportName, false, createdDate, modifiedDate, "_global_")); |
|
| 40 | + } |
|
| 41 | + catch |
|
| 42 | + { |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | + return (ReportInfo[]) reportNames.ToArray(typeof(ReportInfo)); |
|
| 49 | + } |
|
| 50 | + catch |
|
| 51 | + { |
|
| 52 | + } |
|
| 53 | + return new ReportInfo[0]; |
|
| 31 | 54 | } |
| 32 | - } |
|
| 33 | - catch (Exception) { } |
|
| 34 | - |
|
| 35 | - if (command.Connection.State == System.Data.ConnectionState.Open) |
|
| 36 | - command.Connection.Close(); |
|
| 37 | - return (Izenda.AdHoc.ReportInfo[])reportNames.ToArray(typeof(Izenda.AdHoc.ReportInfo)); |
|
| 38 | -} |
|
| 39 | - |
|
| 40 | 55 | ``` |
| 41 | 56 | |
| 42 | 57 | ####Controlling report access |
| ... | ... | @@ -50,59 +65,66 @@ In [[Database Mode|http://wiki.izenda.us/FAQ/Storing-Reports#Database-Mode]], yo |
| 50 | 65 | ### [[FileSystemAdHocConfig|http://wiki.izenda.us/FAQ/Storing-Reports#File-System-Mode]] |
| 51 | 66 | |
| 52 | 67 | ```csharp |
| 53 | -public override ReportInfo[] ListReports() { |
|
| 54 | - ArrayList reports = new ArrayList(); |
|
| 55 | - |
|
| 56 | - if (AdHocSettings.ReportsPath != null) { |
|
| 57 | - DirectoryInfo dir = new DirectoryInfo(AdHocSettings.ReportsPath); |
|
| 58 | - if (dir.Exists) { |
|
| 59 | - string dirName = Path.GetFullPath(dir.FullName); |
|
| 60 | - ArrayList fileArray = new ArrayList(); |
|
| 61 | - GetAllXmlFiles(dir, fileArray); //your method for obtaining XML files from your file server |
|
| 62 | - FileInfo[] files = (FileInfo[])fileArray.ToArray(typeof(FileInfo)); |
|
| 63 | - foreach (FileInfo file in files) { |
|
| 64 | - if (file.Extension == ".xml") { |
|
| 65 | - try { |
|
| 66 | - StreamReader reader = file.OpenText(); |
|
| 67 | - reader.Close(); |
|
| 68 | - } |
|
| 69 | - catch (Exception) { |
|
| 70 | - continue; |
|
| 71 | - } |
|
| 72 | - bool readOnly = false; |
|
| 73 | - try { |
|
| 74 | - StreamWriter writer = file.AppendText(); |
|
| 75 | - writer.Close(); |
|
| 76 | - } |
|
| 77 | - catch (Exception) { |
|
| 78 | - readOnly = true; |
|
| 68 | +public override ReportInfo[] ListReports() |
|
| 69 | + { |
|
| 70 | + ArrayList reportNames = new ArrayList(); |
|
| 71 | + if (AdHocSettings.ReportsPath != null) |
|
| 72 | + { |
|
| 73 | + System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(AdHocSettings.ReportsPath); |
|
| 74 | + if (dir.Exists) |
|
| 75 | + { |
|
| 76 | + string dirName = System.IO.Path.GetFullPath(dir.FullName); |
|
| 77 | + ArrayList fileArray = new ArrayList(); |
|
| 78 | + GetAllXmlFiles(dir, fileArray); |
|
| 79 | + System.IO.FileInfo[] files = (System.IO.FileInfo[]) fileArray.ToArray(typeof(System.IO.FileInfo)); |
|
| 80 | + foreach (System.IO.FileInfo file in files) |
|
| 81 | + { |
|
| 82 | + if (file.Extension == ".xml") |
|
| 83 | + { |
|
| 84 | + try |
|
| 85 | + { |
|
| 86 | + System.IO.StreamReader reader = file.OpenText(); |
|
| 87 | + reader.Close(); |
|
| 88 | + } |
|
| 89 | + catch (Exception) |
|
| 90 | + { |
|
| 91 | + continue; |
|
| 92 | + } |
|
| 93 | + bool readOnly = false; |
|
| 94 | + try |
|
| 95 | + { |
|
| 96 | + System.IO.StreamWriter writer = file.AppendText(); |
|
| 97 | + writer.Close(); |
|
| 98 | + } |
|
| 99 | + catch (Exception) |
|
| 100 | + { |
|
| 101 | + readOnly = true; |
|
| 102 | + } |
|
| 103 | + string fileName = System.IO.Path.GetFullPath(file.FullName); |
|
| 104 | + string reportName = fileName.Substring(dirName.Length + (dirName.EndsWith("\\") ? 0 : 1)); |
|
| 105 | + reportName = StringHelper.TrimEnd(reportName, ".xml"); |
|
| 106 | + reportNames.Add(new ReportInfo(reportName, readOnly, file.CreationTime, file.LastWriteTime, "_global_")); |
|
| 107 | + } |
|
| 79 | 108 | } |
| 80 | - string fileName = Path.GetFullPath(file.FullName); |
|
| 81 | - string reportName = fileName.Substring(dirName.Length + (dirName.EndsWith("\\") ? 0 : 1)); |
|
| 82 | - reportName = StringHelper.TrimEnd(reportName, ".xml"); |
|
| 83 | - reports.Add(new ReportInfo(reportName, readOnly, file.CreationTime, |
|
| 84 | - file.LastWriteTime)); |
|
| 85 | - }//end if |
|
| 86 | - }//end foreach |
|
| 87 | - }//end if |
|
| 88 | - }//end if |
|
| 89 | - return (ReportInfo[])reports.ToArray(); |
|
| 90 | -}//end method |
|
| 91 | - |
|
| 92 | -private static void GetAllXmlFiles(System.IO.DirectoryInfo dir, ArrayList files) |
|
| 93 | -{ |
|
| 94 | - try |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + return (ReportInfo[]) reportNames.ToArray(typeof(ReportInfo)); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + private static void GetAllXmlFiles(System.IO.DirectoryInfo dir, ArrayList files) |
|
| 95 | 115 | { |
| 116 | + try |
|
| 117 | + { |
|
| 96 | 118 | files.AddRange(dir.GetFiles("*.xml")); |
| 97 | 119 | System.IO.DirectoryInfo[] subDirs = dir.GetDirectories(); |
| 98 | 120 | foreach (System.IO.DirectoryInfo subDir in subDirs) |
| 99 | - if (!subDir.Name.StartsWith("~")) |
|
| 100 | - GetAllXmlFiles(subDir, files); |
|
| 101 | - } |
|
| 102 | - catch (UnauthorizedAccessException) |
|
| 103 | - { |
|
| 121 | + if (!subDir.Name.StartsWith("~")) |
|
| 122 | + GetAllXmlFiles(subDir, files); |
|
| 123 | + } |
|
| 124 | + catch |
|
| 125 | + { |
|
| 126 | + } |
|
| 104 | 127 | } |
| 105 | -} |
|
| 106 | 128 | ``` |
| 107 | 129 | |
| 108 | 130 | ##Sample VB.NET Methods |
| ... | ... | @@ -110,83 +132,91 @@ private static void GetAllXmlFiles(System.IO.DirectoryInfo dir, ArrayList files) |
| 110 | 132 | ###DatabaseAdHocConfig |
| 111 | 133 | |
| 112 | 134 | ```visualbasic |
| 113 | -Public Class CustomAdHocConfig |
|
| 114 | - Inherits Izenda.AdHoc.DatabaseAdHocConfig |
|
| 115 | - |
|
| 116 | - Protected Overrides Function ListReports() As Izenda.AdHoc.ReportInfo() |
|
| 117 | - Dim reportNames As New ArrayList() |
|
| 118 | - Dim sql As String = String.Format("SELECT Name, CreatedDate, ModifiedDate FROM {0} ORDER BY Name WHERE UserID='{1}'", AdHocSettings.SavedReportsTable, AdHocSettings.CurrentUserName) |
|
| 119 | - |
|
| 120 | - Dim command As IDbCommand = Nothing |
|
| 121 | - Try |
|
| 122 | - command = Izenda.AdHoc.AdHocContext.Driver.CreateCommand(sql) |
|
| 123 | - Dim reader As IDataReader = command.ExecuteReader() |
|
| 124 | - |
|
| 125 | - While reader.Read() |
|
| 126 | - Dim reportName As String = reader("Name").ToString() |
|
| 127 | - Dim createdDate As DateTime = Convert.ToDateTime(reader("CreatedDate").ToString()) |
|
| 128 | - Dim modifiedDate As DateTime = Convert.ToDateTime(reader("ModifiedDate").ToString()) |
|
| 129 | - If reportName.Equals("") Then |
|
| 130 | - reportNames.Add(New Izenda.AdHoc.ReportInfo(reportName, False, createdDate, modifiedDate)) |
|
| 131 | - End If |
|
| 132 | - End While |
|
| 133 | - Catch |
|
| 134 | - End Try |
|
| 135 | - |
|
| 136 | - If command.Connection.State = System.Data.ConnectionState.Open Then command.Connection.Close() |
|
| 137 | - Return DirectCast(reportNames.ToArray(GetType(Izenda.AdHoc.ReportInfo)), Izenda.AdHoc.ReportInfo()) |
|
| 138 | - End Sub |
|
| 139 | -End Class |
|
| 135 | +Public Overrides Function ListReports() As ReportInfo() |
|
| 136 | + Try |
|
| 137 | + Dim connectionString As String = "Persist Security Info=False;Initial Catalog=Northwind;Data Source=LESHA-PC\SQL2012;User ID=dataLogin;Password=dataPassword;Integrated Security=false;" |
|
| 138 | + Dim sql As String = String.Format("SELECT Name, CreatedDate, ModifiedDate FROM {0} ORDER BY Name", AdHocSettings.SavedReportsTable) |
|
| 139 | + Dim reportNames As ArrayList = New ArrayList() |
|
| 140 | + Using connection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString) |
|
| 141 | + connection.Open() |
|
| 142 | + Using command As System.Data.IDbCommand = connection.CreateCommand() |
|
| 143 | + command.CommandText = sql |
|
| 144 | + Using reader As System.Data.IDataReader = command.ExecuteReader() |
|
| 145 | + While reader.Read() |
|
| 146 | + Try |
|
| 147 | + Dim reportName As String = reader("Name").ToString() |
|
| 148 | + Dim createdDate As DateTime = DirectCast(reader("CreatedDate"), DateTime) |
|
| 149 | + Dim modifiedDate As DateTime = DirectCast(reader("ModifiedDate"), DateTime) |
|
| 150 | + If reportName <> "" Then |
|
| 151 | + reportNames.Add(New ReportInfo(reportName, False, createdDate, modifiedDate, "_global_")) |
|
| 152 | + End If |
|
| 153 | + Catch ex As Exception |
|
| 154 | + End Try |
|
| 155 | + End While |
|
| 156 | + End Using |
|
| 157 | + End Using |
|
| 158 | + End Using |
|
| 159 | + Return DirectCast(reportNames.ToArray(GetType(ReportInfo)), ReportInfo()) |
|
| 160 | + Catch ex2 As Exception |
|
| 161 | + End Try |
|
| 162 | + Dim res(0) As ReportInfo |
|
| 163 | + Return res |
|
| 164 | + End Function |
|
| 140 | 165 | ``` |
| 141 | 166 | |
| 142 | 167 | ###FileSystemAdHocConfig |
| 143 | 168 | |
| 144 | 169 | ```visualbasic |
| 145 | 170 | Public Overrides Function ListReports() As ReportInfo() |
| 146 | - Dim reports As New ArrayList() |
|
| 147 | - |
|
| 148 | - If AdHocSettings.ReportsPath IsNot Nothing Then |
|
| 149 | - Dim dir As DirectoryInfo = New DirectoryInfo(AdHocSettings.ReportsPath) |
|
| 150 | - If dir.Exists Then |
|
| 151 | - Dim dirName As String = Path.GetFullPath(dir.FullName) |
|
| 152 | - Dim fileArray As New ArrayList() |
|
| 153 | - GetAllXmlFiles(dir, fileArray) |
|
| 154 | - Dim files As FileInfo() = DirectCast(fileArray.ToArray(typeof(FileInfo)), FileInfo()) |
|
| 155 | - For Each file As FileInfo In files |
|
| 156 | - If file.Extension = ".xml" Then |
|
| 157 | - Try |
|
| 158 | - Dim reader As StreamReader = file.OpenText() |
|
| 159 | - reader.Close() |
|
| 160 | - Catch e As Exception |
|
| 161 | - Continue For |
|
| 162 | - End Try |
|
| 163 | - Dim readOnly As Boolean = False |
|
| 164 | - Try |
|
| 165 | - Dim writer As StreamWriter = file.AppendText() |
|
| 166 | - writer.Close() |
|
| 167 | - catch e As Exception |
|
| 168 | - readOnly = True |
|
| 169 | - End Try |
|
| 170 | - Dim fileName As String = Path.GetFullPath(file.FullName) |
|
| 171 | - Dim reportName As String = fileName.Substring(dirName.Length + CStr(IIf((dirName.EndsWith("\\"), 0, 1)))) |
|
| 172 | - reportName = StringHelper.TrimEnd(reportName, ".xml") |
|
| 173 | - reports.Add(New ReportInfo(reportName, readOnly, file.CreationTime, _ |
|
| 174 | - file.LastWriteTime)) |
|
| 171 | + Dim reportNames As ArrayList = New ArrayList() |
|
| 172 | + If Not (AdHocSettings.ReportsPath Is Nothing) Then |
|
| 173 | + Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(AdHocSettings.ReportsPath) |
|
| 174 | + If dir.Exists Then |
|
| 175 | + Dim dirName As String = System.IO.Path.GetFullPath(dir.FullName) |
|
| 176 | + Dim fileArray As ArrayList = New ArrayList() |
|
| 177 | + GetAllXmlFiles(dir, fileArray) |
|
| 178 | + Dim files As System.IO.FileInfo() = DirectCast(fileArray.ToArray(GetType(System.IO.FileInfo)), System.IO.FileInfo()) |
|
| 179 | + For Each file As System.IO.FileInfo In files |
|
| 180 | + If file.Extension = ".xml" Then |
|
| 181 | + Try |
|
| 182 | + Dim reader As System.IO.StreamReader = file.OpenText() |
|
| 183 | + reader.Close() |
|
| 184 | + Catch ex As Exception |
|
| 185 | + Continue For |
|
| 186 | + End Try |
|
| 187 | + Dim ro As Boolean = False |
|
| 188 | + Try |
|
| 189 | + Dim writer As System.IO.StreamWriter = file.AppendText() |
|
| 190 | + writer.Close() |
|
| 191 | + Catch ex2 As Exception |
|
| 192 | + ro = True |
|
| 193 | + End Try |
|
| 194 | + Dim fileName As String = System.IO.Path.GetFullPath(file.FullName) |
|
| 195 | + Dim reportName As String |
|
| 196 | + If dirName.EndsWith("\\") Then |
|
| 197 | + reportName = fileName.Substring(dirName.Length) |
|
| 198 | + Else |
|
| 199 | + reportName = fileName.Substring(dirName.Length + 1) |
|
| 200 | + End If |
|
| 201 | + reportName = StringHelper.TrimEnd(reportName, ".xml") |
|
| 202 | + reportNames.Add(New ReportInfo(reportName, ro, file.CreationTime, file.LastWriteTime, "_global_")) |
|
| 203 | + End If |
|
| 204 | + Next |
|
| 175 | 205 | End If |
| 176 | - Next |
|
| 177 | - End If |
|
| 178 | - End If |
|
| 179 | - Return DirectCast(reports.ToArray(), ReportInfo()) |
|
| 180 | -End Function |
|
| 181 | - |
|
| 182 | -Private Shared Sub GetAllXmlFiles(ByVal dir As System.IO.DirectoryInfo, ByRef files As ArrayList) |
|
| 183 | - Try |
|
| 206 | + End If |
|
| 207 | + Return DirectCast(reportNames.ToArray(GetType(ReportInfo)), ReportInfo()) |
|
| 208 | + End Function |
|
| 209 | + |
|
| 210 | + Public Sub GetAllXmlFiles(dir As System.IO.DirectoryInfo, files As ArrayList) |
|
| 211 | + Try |
|
| 184 | 212 | files.AddRange(dir.GetFiles("*.xml")) |
| 185 | 213 | Dim subDirs As System.IO.DirectoryInfo() = dir.GetDirectories() |
| 186 | 214 | For Each subDir As System.IO.DirectoryInfo In subDirs |
| 187 | - If Not subDir.Name.StartsWith("~")) |
|
| 188 | - GetAllXmlFiles(subDir, files) |
|
| 189 | - Catch uae As UnauthorizedAccessException |
|
| 190 | - End Try |
|
| 191 | -End Sub |
|
| 215 | + If Not (subDir.Name.StartsWith("~")) Then |
|
| 216 | + GetAllXmlFiles(subDir, files) |
|
| 217 | + End If |
|
| 218 | + Next |
|
| 219 | + Catch ex As Exception |
|
| 220 | + End Try |
|
| 221 | + End Sub |
|
| 192 | 222 | ``` |
| ... | ... | \ No newline at end of file |