diff --git a/client/src/main/java/com/kritsit/casetracker/client/domain/services/Export.java b/client/src/main/java/com/kritsit/casetracker/client/domain/services/Export.java index 8a6b3e6..8fce042 100644 --- a/client/src/main/java/com/kritsit/casetracker/client/domain/services/Export.java +++ b/client/src/main/java/com/kritsit/casetracker/client/domain/services/Export.java @@ -4,7 +4,6 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; - import java.util.List; import org.slf4j.Logger; @@ -14,11 +13,13 @@ import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; +import com.itextpdf.text.ListItem; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; +import com.kritsit.casetracker.shared.domain.model.Case; public class Export implements IExportService{ @@ -29,24 +30,22 @@ public void exportToPDF(List headers, List cells, File file){ Document document = new Document(); try { - logger.info("Creating file output"); + logger.info("Creating file output"); PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); document.add(createTable(headers, cells)); document.close(); - logger.info("export to PDF: success"); + logger.info("export to PDF: success"); } catch (DocumentException e) { - logger.error("Error while exporting to PDF"); - logger.error(e.toString()); + logger.error("Error while exporting to PDF", e); } catch (FileNotFoundException e) { - logger.error("Error while exporting to PDF"); - logger.error(e.toString()); + logger.error("Error while exporting to PDF", e); } } private PdfPTable createTable(List headers, List cells){ - PdfPTable table = new PdfPTable(headers.size()); + PdfPTable table = new PdfPTable(headers.size()); Font boldFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); logger.info("Filling in the table"); @@ -63,4 +62,34 @@ private PdfPTable createTable(List headers, List cells){ } return table; } + + public void exportCaseToPDF(Case c, File file) { + Document document = new Document(); + + try { + logger.info("Creating file output"); + PdfWriter.getInstance(document, new FileOutputStream(file)); + document.open(); + Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD); + Font normalFont = new Font(Font.FontFamily.TIMES_ROMAN, 12); + + document.add(new Paragraph("Case no. "+c.getNumber(), titleFont)); + document.add(new Paragraph("Investigating officer: "+c.getInvestigatingOfficer().getName(), normalFont)); + document.add(new Paragraph("Incident date: "+c.getIncident().getDate(), normalFont)); + document.add(new Paragraph("Defendant: "+c.getDefendant().getName(), normalFont)); + document.add(new Paragraph("Complainant: "+c.getComplainant().getName(), normalFont)); + document.add(new Paragraph(c.getDescription(), normalFont)); + document.add(new Paragraph("Evidence: ", normalFont)); + com.itextpdf.text.List list = new com.itextpdf.text.List(); + for(int i=0; i < c.getEvidence().size(); i++){ + list.add(new ListItem(c.getEvidence().get(i).getDescription())); + } + document.close(); + logger.info("export to PDF: success"); + } catch (DocumentException e) { + logger.error("Error while exporting to PDF", e); + } catch (FileNotFoundException e) { + logger.error("Error while exporting to PDF", e); + } + } } diff --git a/client/src/main/java/com/kritsit/casetracker/client/domain/services/IExportService.java b/client/src/main/java/com/kritsit/casetracker/client/domain/services/IExportService.java index b65336f..0c08c89 100644 --- a/client/src/main/java/com/kritsit/casetracker/client/domain/services/IExportService.java +++ b/client/src/main/java/com/kritsit/casetracker/client/domain/services/IExportService.java @@ -2,7 +2,9 @@ import java.util.List; import java.io.File; +import com.kritsit.casetracker.shared.domain.model.Case; public interface IExportService { void exportToPDF(List headers, List cells, File file); + void exportCaseToPDF(Case c, File file); } \ No newline at end of file diff --git a/client/src/main/java/com/kritsit/casetracker/client/domain/ui/controller/EditorController.java b/client/src/main/java/com/kritsit/casetracker/client/domain/ui/controller/EditorController.java index 573355a..018ed78 100644 --- a/client/src/main/java/com/kritsit/casetracker/client/domain/ui/controller/EditorController.java +++ b/client/src/main/java/com/kritsit/casetracker/client/domain/ui/controller/EditorController.java @@ -420,6 +420,27 @@ private void updateShownCase(Case c) { cbxFilterCaseType.setValue("All"); txfFilterCases.setText(""); } + + @FXML protected void handleExportCaseToPDF(ActionEvent e){ + TableViewSelectionModel selection = tblCases.getSelectionModel(); + if(selection.getSelectedItem()==null){ + Alert alert = new Alert(AlertType.INFORMATION); + alert.setTitle("Exporting case"); + alert.setHeaderText("Information"); + alert.setContentText("Select the case you want to export"); + alert.showAndWait(); + return; + } + + exportService = new Export(); + + FileChooser fileChooser = new FileChooser(); + fileChooser.setTitle("Export a case"); + File file = fileChooser.showSaveDialog(stage); + + exportService.exportCaseToPDF(selection.getSelectedItem(), file); + + } @FXML protected void handleEditCaseAction(ActionEvent e) { TableViewSelectionModel selection = tblCases.getSelectionModel(); diff --git a/client/src/main/resources/ui/fxml/EditorFrame.fxml b/client/src/main/resources/ui/fxml/EditorFrame.fxml index 00967f4..3834972 100644 --- a/client/src/main/resources/ui/fxml/EditorFrame.fxml +++ b/client/src/main/resources/ui/fxml/EditorFrame.fxml @@ -221,6 +221,7 @@