using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Logging; using TTSW.Controllers; using TTSW.EF; using TTSW.Utils; using TTSW.Constant; using TTSW.Common; using TodoAPI2.Models; using System.Data; using Microsoft.Extensions.Configuration; using System.IO; using System.Net; using iTextSharp.text; using iTextSharp.text.pdf; namespace TodoAPI2.Controllers { //[Authorize] [Produces("application/json")] [Route("api/rep_kp7")] public class rep_kp7Controller : BaseController { #region Private Variables private ILogger _logger; private Irep_kp7Service _repository; private IConfiguration Configuration { get; set; } #endregion #region Properties #endregion /// /// Default constructure for dependency injection /// /// /// /// public rep_kp7Controller(ILogger logger, Irep_kp7Service repository, IConfiguration configuration) { _logger = logger; _repository = repository; Configuration = configuration; } /// /// Get Blank Item /// /// /// /// Return a blank item /// Returns the item /// Error Occurred [HttpGet("GetBlankItem")] [ProducesResponseType(typeof(rep_kp7WithSelectionViewModel), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult GetBlankItem() { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); var result = _repository.GetBlankItem(); return Ok(result); } catch (Exception ex) { _logger.LogCritical($"Exception in IActionResult GetBlankItem.", ex); return StatusCode(500, $"{ex.Message}"); } } /// /// Download Report /// /// /// /// Return list of items by specifced keyword /// Returns the item /// Error Occurred [HttpGet("rep_kp7_report")] [ProducesResponseType(typeof(FileStreamResult), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult rep_kp7_report(rep_kp7ReportRequestModel model) { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); var httpclient = new WebClient(); var stream = new MemoryStream(); Document document = new Document(); PdfCopy writer = new PdfCopy(document, stream); document.Open(); // Get Main KP7 report from HR try { var data = GetKP7MainReport(model.employee_id); PdfReader reader = new PdfReader(data); reader.ConsolidateNamedDestinations(); for (int i = 1; i <= reader.NumberOfPages; i++) { PdfImportedPage page = writer.GetImportedPage(reader, i); writer.AddPage(page); } reader.Close(); } catch (Exception ex) { } // Get Jasper Section report string mainurl = MyHelper.GetConfig(Configuration, "JasperReportServer:MainURL"); string reportsite = MyHelper.GetConfig(Configuration, "JasperReportServer:reportsite"); string username = MyHelper.GetConfig(Configuration, "JasperReportServer:username"); string password = MyHelper.GetConfig(Configuration, "JasperReportServer:password"); string url = $"{mainurl}{reportsite}/rep_kp7.{model.filetype}?{MyHelper.GetParameterForJasperReport(model)}&j_username={username}&j_password={password}"; var data2 = httpclient.DownloadData(url); PdfReader reader2 = new PdfReader(data2); reader2.ConsolidateNamedDestinations(); for (int i = 1; i <= reader2.NumberOfPages; i++) { PdfImportedPage page = writer.GetImportedPage(reader2, i); writer.AddPage(page); } reader2.Close(); writer.Close(); document.Close(); var data3 = stream.ToArray(); var stream3 = new MemoryStream(data3); return File(stream3, model.contentType); } catch (Exception ex) { _logger.LogCritical($"Exception while GetReport.", ex); return StatusCode(500, $"{ex.Message}"); } } private byte[] GetKP7MainReport(int? employee_id) { string kp7_main_api = MyHelper.GetConfig(Configuration, "SiteInformation:hr_svc") + "/hrm/employeeReportKP/" + employee_id.ToString(); var httpclient = new WebClient(); return httpclient.DownloadData(kp7_main_api); } } }