Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1.  
  2. @RequiredArgsConstructor
  3. @Service
  4. public class CreditSchedulePrintService {
  5. private static final Map<ReportFormat, String> obipReportFormates = ImmutableMap.of(ReportFormat.PDF, "pdf",
  6. ReportFormat.XLS, "xlsx",
  7. ReportFormat.XLSX, "xlsx");
  8.  
  9. private static final String UNSUPPORTED_FILE_TYPE_ERROR_CODE = "unsupported_file_type";
  10.  
  11. private static final String LOCALE_RU = "ru-RU";
  12.  
  13. private final OrganizationGateway organizationGateway;
  14.  
  15. private final ReportService reportService;
  16.  
  17. private final CreditScheduleReportMapper mapper;
  18.  
  19. protected final ReportProperties config;
  20.  
  21. private final JAXBContext context = getJaxbContext();
  22.  
  23.  
  24. public FileAttachment getCreditScheduleFile(CreditSchedule creditSchedule,
  25. UserData userData,
  26. String organizationId,
  27. SchedulePrintData printData) {
  28. return getScheduleFile(creditSchedule,
  29. userData,
  30. organizationId,
  31. printData,
  32. config.getPaymentSchedule());
  33. }
  34.  
  35. public FileAttachment getGuarantyCreditScheduleFile(CreditSchedule creditSchedule,
  36. UserData userData,
  37. String organizationId,
  38. SchedulePrintData printData) {
  39. return getScheduleFile(creditSchedule,
  40. userData,
  41. organizationId,
  42. printData,
  43. config.getGuarantyPaymentSchedule());
  44. }
  45.  
  46.  
  47. @SneakyThrows
  48. private FileAttachment getScheduleFile(CreditSchedule creditSchedule,
  49. UserData userData,
  50. String organizationId,
  51. SchedulePrintData printData,
  52. ReportSetting reportSetting) {
  53. ReportRequest reportRequest = new ReportRequest();
  54.  
  55. ScheduleReportDto dto = getScheduleReportDto(creditSchedule, printData, userData, organizationId);
  56.  
  57. setReportData(dto, reportRequest);
  58. setRequestAttributes(printData.getFileType(), reportRequest, reportSetting);
  59.  
  60. byte[] report = reportService.runReport(reportRequest, config.getLogin(), config.getPassword()).getReportBytes();
  61. return new FileAttachment(reportSetting.getReportName(),
  62. printData.getFileType().name(),
  63. ByteBuffer.wrap(report));
  64. }
  65.  
  66. private ScheduleReportDto getScheduleReportDto(CreditSchedule creditSchedule, SchedulePrintData data, UserData userData, String organizationId) {
  67. Customer organization = getOrganization(userData, organizationId);
  68. Amount totalSum = getTotalPaymentSum(creditSchedule);
  69. return mapper.toReport(creditSchedule, data.getDocNumber(), data.getAccountNumber(), totalSum, organization);
  70. }
  71.  
  72. private Amount getTotalPaymentSum(CreditSchedule creditSchedule) {
  73. return creditSchedule.getLoanPayments()
  74. .stream()
  75. .map(LoanPayment::getPaymentAmount)
  76. .reduce((a, b) -> new Amount(a.getAmount() + b.getAmount(), a.getCurrency()))
  77. .orElseThrow(() -> new IllegalStateException("schedule is empty"));
  78. }
  79.  
  80. private Customer getOrganization(UserData userData, String organizationId) {
  81. return organizationGateway.getOrganization(
  82. userData,
  83. organizationId,
  84. new OrganizationsProjection().setWithType(true));
  85. }
  86.  
  87. private void setRequestAttributes(ReportFormat fileType, ReportRequest reportRequest, ReportSetting reportSetting) {
  88. reportRequest.setAttributeLocale(LOCALE_RU);
  89. reportRequest.setAttributeTemplate(reportSetting.getScheduleTemplate());
  90. reportRequest.setReportAbsolutePath(reportSetting.getReportAbsolutePath());
  91. reportRequest.setAttributeFormat(Optional.ofNullable(obipReportFormates.get(fileType))
  92. .orElseThrow(() -> new ReportFormatNotSupportedException(UNSUPPORTED_FILE_TYPE_ERROR_CODE,
  93. config.getUnsupportedFileTypeErrorMessage())));
  94. reportRequest.setFlattenXML(true);
  95. reportRequest.setSizeOfDataChunkDownload(-1);
  96. }
  97.  
  98. private void setReportData(ScheduleReportDto reportDto, ReportRequest reportRequest) throws JAXBException {
  99. StringWriter sw = new StringWriter();
  100. context.createMarshaller().marshal(reportDto, sw);
  101. reportRequest.setReportData(sw.toString().getBytes());
  102. }
  103.  
  104. @SneakyThrows
  105. private JAXBContext getJaxbContext() {
  106. return JAXBContext.newInstance(ScheduleReportDto.class);
  107. }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement