Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RequiredArgsConstructor
- @Service
- public class CreditSchedulePrintService {
- private static final Map<ReportFormat, String> obipReportFormates = ImmutableMap.of(ReportFormat.PDF, "pdf",
- ReportFormat.XLS, "xlsx",
- ReportFormat.XLSX, "xlsx");
- private static final String UNSUPPORTED_FILE_TYPE_ERROR_CODE = "unsupported_file_type";
- private static final String LOCALE_RU = "ru-RU";
- private final OrganizationGateway organizationGateway;
- private final ReportService reportService;
- private final CreditScheduleReportMapper mapper;
- protected final ReportProperties config;
- private final JAXBContext context = getJaxbContext();
- public FileAttachment getCreditScheduleFile(CreditSchedule creditSchedule,
- UserData userData,
- String organizationId,
- SchedulePrintData printData) {
- return getScheduleFile(creditSchedule,
- userData,
- organizationId,
- printData,
- config.getPaymentSchedule());
- }
- public FileAttachment getGuarantyCreditScheduleFile(CreditSchedule creditSchedule,
- UserData userData,
- String organizationId,
- SchedulePrintData printData) {
- return getScheduleFile(creditSchedule,
- userData,
- organizationId,
- printData,
- config.getGuarantyPaymentSchedule());
- }
- @SneakyThrows
- private FileAttachment getScheduleFile(CreditSchedule creditSchedule,
- UserData userData,
- String organizationId,
- SchedulePrintData printData,
- ReportSetting reportSetting) {
- ReportRequest reportRequest = new ReportRequest();
- ScheduleReportDto dto = getScheduleReportDto(creditSchedule, printData, userData, organizationId);
- setReportData(dto, reportRequest);
- setRequestAttributes(printData.getFileType(), reportRequest, reportSetting);
- byte[] report = reportService.runReport(reportRequest, config.getLogin(), config.getPassword()).getReportBytes();
- return new FileAttachment(reportSetting.getReportName(),
- printData.getFileType().name(),
- ByteBuffer.wrap(report));
- }
- private ScheduleReportDto getScheduleReportDto(CreditSchedule creditSchedule, SchedulePrintData data, UserData userData, String organizationId) {
- Customer organization = getOrganization(userData, organizationId);
- Amount totalSum = getTotalPaymentSum(creditSchedule);
- return mapper.toReport(creditSchedule, data.getDocNumber(), data.getAccountNumber(), totalSum, organization);
- }
- private Amount getTotalPaymentSum(CreditSchedule creditSchedule) {
- return creditSchedule.getLoanPayments()
- .stream()
- .map(LoanPayment::getPaymentAmount)
- .reduce((a, b) -> new Amount(a.getAmount() + b.getAmount(), a.getCurrency()))
- .orElseThrow(() -> new IllegalStateException("schedule is empty"));
- }
- private Customer getOrganization(UserData userData, String organizationId) {
- return organizationGateway.getOrganization(
- userData,
- organizationId,
- new OrganizationsProjection().setWithType(true));
- }
- private void setRequestAttributes(ReportFormat fileType, ReportRequest reportRequest, ReportSetting reportSetting) {
- reportRequest.setAttributeLocale(LOCALE_RU);
- reportRequest.setAttributeTemplate(reportSetting.getScheduleTemplate());
- reportRequest.setReportAbsolutePath(reportSetting.getReportAbsolutePath());
- reportRequest.setAttributeFormat(Optional.ofNullable(obipReportFormates.get(fileType))
- .orElseThrow(() -> new ReportFormatNotSupportedException(UNSUPPORTED_FILE_TYPE_ERROR_CODE,
- config.getUnsupportedFileTypeErrorMessage())));
- reportRequest.setFlattenXML(true);
- reportRequest.setSizeOfDataChunkDownload(-1);
- }
- private void setReportData(ScheduleReportDto reportDto, ReportRequest reportRequest) throws JAXBException {
- StringWriter sw = new StringWriter();
- context.createMarshaller().marshal(reportDto, sw);
- reportRequest.setReportData(sw.toString().getBytes());
- }
- @SneakyThrows
- private JAXBContext getJaxbContext() {
- return JAXBContext.newInstance(ScheduleReportDto.class);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement