Skip to content

Commit

Permalink
Fixed #2475
Browse files Browse the repository at this point in the history
  • Loading branch information
vertigo17 committed Sep 10, 2023
1 parent eaab4a5 commit f23d1fa
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
import java.security.Principal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -141,6 +141,8 @@ public ResponseEntity<InputStreamResource> findCampaignExecutionPdfById(
Date today = Calendar.getInstance().getTime();
DateFormat df = new SimpleDateFormat(DateUtil.DATE_FORMAT_REPORT_FILE);

String filePostName = campaignExecutionId + "_" + String.valueOf(df.format(today));

String rootPath = "";
if (System.getProperty("java.io.tmpdir") != null) {
rootPath = System.getProperty("java.io.tmpdir");
Expand All @@ -158,19 +160,29 @@ public ResponseEntity<InputStreamResource> findCampaignExecutionPdfById(
File folderPath = new File(tmpFolderPath);
folderPath.mkdirs();

// Summary PDF
String pdfFilenameOri = this.pdfService.generatePdf(campaignExeIdTag, today, tmpFolderPath);
String pdfFilename = this.pdfService.addHeaderAndFooter(pdfFilenameOri, tmpFolderPath + "Campaign Execution.pdf", campaignExeIdTag, today);
Path filePath = Paths.get(pdfFilename);
String pdfFilename = this.pdfService.addHeaderAndFooter(pdfFilenameOri, tmpFolderPath + "Campaign Execution-" + filePostName + ".pdf", campaignExeIdTag, today, true);

// Appendix PDFs
List<String> pdfFilenameOriAppendix = this.pdfService.generatePdfAppendix(campaignExeIdTag, today, tmpFolderPath);
int i = 0;
List<String> pdfFilenameAppendixList = new ArrayList<>();
for (String filenameAppendix : pdfFilenameOriAppendix) {
i++;
pdfFilenameAppendixList.add(this.pdfService.addHeaderAndFooter(filenameAppendix, tmpFolderPath + "Campaign Execution-" + filePostName + " - Appendix " + i + ".pdf", campaignExeIdTag, today, false));

String pdfFilenameOriAppendix = this.pdfService.generatePdfAppendix(campaignExeIdTag, today, tmpFolderPath);
String pdfFilenameAppendix = this.pdfService.addHeaderAndFooter(pdfFilenameOriAppendix, tmpFolderPath + "Campaign Execution - Appendix.pdf", campaignExeIdTag, today);
Path filePathAppendix = Paths.get(pdfFilenameAppendix);
}

// Creating a PdfWriter
String zipPath = rootPath + File.separatorChar + "campaignExecutionReport-" + fileUUID.toString().substring(0, 17) + ".zip";
Path zipFilePath = Paths.get(zipPath);

List<String> filePaths = Arrays.asList(pdfFilename, pdfFilenameAppendix);
List<String> filePaths = new ArrayList<>();
filePaths.add(pdfFilename);
for (String filenam : pdfFilenameAppendixList) {
filePaths.add(filenam);
}

try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipPath))) {
for (String filePath1 : filePaths) {
Expand All @@ -184,7 +196,7 @@ public ResponseEntity<InputStreamResource> findCampaignExecutionPdfById(
return ResponseEntity
.status(HttpStatus.OK)
.contentLength(zipFilePath.toFile().length())
.header("Content-Disposition", "attachment; filename=CampaignReport-" + campaignExecutionId + "_" + String.valueOf(df.format(today)) + ".zip")
.header("Content-Disposition", "attachment; filename=CampaignReport-" + filePostName + ".zip")
.body(new InputStreamResource(Files.newInputStream(zipFilePath)));
} catch (EntityNotFoundException exception) {
return ResponseEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.FileNotFoundException;
import java.util.Date;
import java.util.List;
import org.cerberus.core.crud.entity.Tag;

/**
Expand All @@ -44,17 +45,18 @@ public interface IPDFService {
* @return
* @throws java.io.FileNotFoundException
*/
String generatePdfAppendix(Tag tag, Date today, String folder) throws FileNotFoundException;
List<String> generatePdfAppendix(Tag tag, Date today, String folder) throws FileNotFoundException;

/**
*
* @param pdfFilePathSrc
* @param pdfFilePathDst
* @param tag
* @param today
* @param withLogo
* @return
* @throws FileNotFoundException
*/
String addHeaderAndFooter(String pdfFilePathSrc, String pdfFilePathDst, Tag tag, Date today) throws FileNotFoundException;
String addHeaderAndFooter(String pdfFilePathSrc, String pdfFilePathDst, Tag tag, Date today, boolean withLogo) throws FileNotFoundException;

}
Loading

0 comments on commit f23d1fa

Please sign in to comment.