-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathReportDesignerWebApiController.cs
277 lines (245 loc) · 10 KB
/
ReportDesignerWebApiController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using BoldReports.Web;
using BoldReports.Web.ReportDesigner;
using BoldReports.Web.ReportViewer;
using Newtonsoft.Json;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using System.Data;
using Microsoft.Extensions.Caching.Memory;
using System.Net;
using Samples.Core.Logger;
using System.Reflection;
namespace ReportsCoreSamples.Controllers
{
[Microsoft.AspNetCore.Cors.EnableCors("AllowAllOrigins")]
public class ReportDesignerWebApiController : Controller, IReportDesignerController, IReportLogger, IReportHelperSettings
{
private Microsoft.Extensions.Caching.Memory.IMemoryCache _cache;
#if NETCOREAPP2_1
private IHostingEnvironment _hostingEnvironment;
#else
private IWebHostEnvironment _hostingEnvironment;
#endif
internal ReportHelperSettings _helperSettings = null;
internal ExternalServer Server
{
get;
set;
}
internal string ServerURL
{
get;
set;
}
internal ReportHelperSettings HelperSettings
{
get { return this._helperSettings; }
set { this._helperSettings = value; }
}
#if NETCOREAPP2_1
public ReportDesignerWebApiController(Microsoft.Extensions.Caching.Memory.IMemoryCache memoryCache, IHostingEnvironment hostingEnvironment)
#else
public ReportDesignerWebApiController(Microsoft.Extensions.Caching.Memory.IMemoryCache memoryCache, IWebHostEnvironment hostingEnvironment)
#endif
{
_cache = memoryCache;
_hostingEnvironment = hostingEnvironment;
ExternalServer externalServer = new ExternalServer(_hostingEnvironment);
this.Server = externalServer;
this.ServerURL = "Sample";
externalServer.ReportServerUrl = this.ServerURL;
}
public void InitializeSettings(ReportHelperSettings helperSettings)
{
helperSettings.ReportingServer = Server;
HelperSettings = helperSettings;
}
[ActionName("GetImage")]
[AcceptVerbs("GET")]
public object GetImage(string key, string image)
{
return ReportDesignerHelper.GetImage(key, image, this);
}
[HttpPost]
public bool DisposeObjects()
{
try
{
string targetFolder = this._hostingEnvironment.WebRootPath + "\\";
targetFolder += "Cache";
if (Directory.Exists(targetFolder))
{
string[] dirs = Directory.GetDirectories(targetFolder);
for (var index = 0; index < dirs.Length; index++)
{
string[] files = Directory.GetFiles(dirs[index]);
var fileCount = 0;
for (var fileIndex = 0; fileIndex < files.Length; fileIndex++)
{
FileInfo fi = new FileInfo(files[fileIndex]);
if (fi.LastAccessTimeUtc < DateTime.UtcNow.AddDays(-2))
{
fileCount++;
}
}
if (files.Length == 0 || (files.Length == fileCount))
{
Directory.Delete(dirs[index], true);
}
}
}
return true;
}
catch (Exception ex)
{
LogExtension.LogError(ex.Message, ex, MethodBase.GetCurrentMethod());
}
return false;
}
[ActionName("GetResource")]
[AcceptVerbs("GET")]
public object GetResource(ReportResource resource)
{
return ReportHelper.GetResource(resource, this, _cache);
}
public void OnInitReportOptions(ReportViewerOptions reportOption)
{
string reportName = reportOption.ReportModel.ReportPath;
reportOption.ReportModel.ReportingServer = this.Server;
reportOption.ReportModel.ReportServerUrl = this.ServerURL;
reportOption.ReportModel.ReportServerCredential = new NetworkCredential("Sample", "Passwprd");
if (reportName == "load-large-data.rdlc")
{
Models.SqlQuery.getJson(this._cache);
reportOption.ReportModel.ProcessingMode = ProcessingMode.Remote;
reportOption.ReportModel.DataSources.Add(new ReportDataSource("SalesOrderDetail", _cache.Get("SalesOrderDetail") as DataTable));
}
}
public void OnReportLoaded(ReportViewerOptions reportOption)
{
}
[HttpPost]
public object PostDesignerAction([FromBody] Dictionary<string, object> jsonResult)
{
this.UpdateReportType(jsonResult);
return ReportDesignerHelper.ProcessDesigner(jsonResult, this, null, this._cache);
}
[HttpPost]
public object PostFormDesignerAction()
{
return ReportDesignerHelper.ProcessDesigner(null, this, null, this._cache);
}
[HttpPost]
public object PostFormReportAction()
{
return ReportHelper.ProcessReport(null, this, this._cache);
}
[HttpPost]
public object PostReportAction([FromBody] Dictionary<string, object> jsonResult)
{
this.UpdateReportType(jsonResult);
return ReportHelper.ProcessReport(jsonResult, this, this._cache);
}
[HttpPost]
public void UploadReportAction()
{
ReportDesignerHelper.ProcessDesigner(null, this, this.Request.Form.Files[0], this._cache);
}
private string GetFilePath(string itemName, string key)
{
string dirPath = Path.Combine(this._hostingEnvironment.WebRootPath,"Cache",key);
if (!System.IO.Directory.Exists(dirPath))
{
System.IO.Directory.CreateDirectory(dirPath);
}
return Path.Combine(dirPath, itemName);
}
public bool SetData(string key, string itemId, ItemInfo itemData, out string errMsg)
{
errMsg = string.Empty;
try
{
if (itemData.Data != null)
{
System.IO.File.WriteAllBytes(this.GetFilePath(itemId, key), itemData.Data);
}
else if (itemData.PostedFile != null)
{
var fileName = itemId;
if (string.IsNullOrEmpty(itemId))
{
fileName = System.IO.Path.GetFileName(itemData.PostedFile.FileName);
}
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
itemData.PostedFile.OpenReadStream().CopyTo(stream);
byte[] bytes = stream.ToArray();
var writePath = this.GetFilePath(fileName, key);
if (System.IO.File.Exists(writePath))
{
System.IO.File.Delete(writePath);
}
System.IO.File.WriteAllBytes(writePath, bytes);
stream.Close();
stream.Dispose();
}
}
}
catch (Exception ex)
{
LogExtension.LogError(ex.Message, ex, MethodBase.GetCurrentMethod());
errMsg = ex.Message;
return false;
}
return true;
}
public ResourceInfo GetData(string key, string itemId)
{
var resource = new ResourceInfo();
try
{
var filePath = this.GetFilePath(itemId, key);
if (itemId.Equals(Path.GetFileName(filePath), StringComparison.InvariantCultureIgnoreCase) && System.IO.File.Exists(filePath))
{
resource.Data = System.IO.File.ReadAllBytes(filePath);
LogExtension.LogInfo(string.Format("Method Name: {0}; Class Name: {1}; Message: {2};", "GetData", "CacheHelper", string.Format("File data retrieved from the path: {0}", filePath)), null);
}
else
{
resource.ErrorMessage = "File not found from the specified path";
LogExtension.LogInfo(string.Format("Method Name: {0}; Class Name: {1}; Message: {2};", "GetData", "CacheHelper", string.Format("File not found from the specified path: {0}", filePath)), null);
}
}
catch (Exception ex)
{
LogExtension.LogError(ex.Message, ex, MethodBase.GetCurrentMethod());
resource.ErrorMessage = ex.Message;
}
return resource;
}
public void LogError(string message, Exception exception, MethodBase methodType, ErrorType errorType)
{
LogExtension.LogError(message, exception, methodType, errorType == ErrorType.Error ? "Error" : "Info");
}
public void LogError(string errorCode, string message, Exception exception, string errorDetail, string methodName, string className)
{
LogExtension.LogError(message, exception, System.Reflection.MethodBase.GetCurrentMethod(), errorCode + "-" + errorDetail);
}
public void UpdateReportType(Dictionary<string, object> jsonResult)
{
string reportType = "";
if (jsonResult.ContainsKey("customData"))
{
string customData = jsonResult["customData"].ToString();
reportType = (string)(JsonConvert.DeserializeObject(customData) as dynamic).reportType;
}
this.Server.reportType = String.IsNullOrEmpty(reportType) ? "RDL" : reportType;
}
}
}