Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

exporting single case to pdf #46

Merged
merged 2 commits into from
Oct 24, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.List;

import org.slf4j.Logger;
Expand All @@ -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{

Expand All @@ -29,12 +30,12 @@ public void exportToPDF(List<String> headers, List<String[]> 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());
Expand All @@ -46,7 +47,7 @@ public void exportToPDF(List<String> headers, List<String[]> cells, File file){

private PdfPTable createTable(List<String> headers, List<String[]> 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");

Expand All @@ -63,4 +64,34 @@ private PdfPTable createTable(List<String> headers, List<String[]> 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().getLastName()+" "+c.getInvestigatingOfficer().getFirstName(), normalFont));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can call .getName() which combines the name

document.add(new Paragraph("Incident date: "+c.getIncident().getDate(), 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");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger.error() can take the Exception as a parameter. It then prints out the stack trace nicely. So these logging lines can be merged into one

logger.error(e.toString());
} catch (FileNotFoundException e) {
logger.error("Error while exporting to PDF");
logger.error(e.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> headers, List<String[]> cells, File file);
void exportCaseToPDF(Case c, File file);
}
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,27 @@ private void updateShownCase(Case c) {
cbxFilterCaseType.setValue("All");
txfFilterCases.setText("");
}

@FXML protected void handleExportCaseToPDF(ActionEvent e){
TableViewSelectionModel<Case> 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<Case> selection = tblCases.getSelectionModel();
Expand Down
1 change: 1 addition & 0 deletions client/src/main/resources/ui/fxml/EditorFrame.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
</Label>
<TextArea fx:id="txaSummaryDetails" editable="false" prefHeight="200.0" prefWidth="200.0" GridPane.columnSpan="2" GridPane.rowIndex="4" />
<ListView fx:id="lstSummaryEvidence" prefHeight="200.0" prefWidth="200.0" GridPane.columnSpan="2" GridPane.rowIndex="6" />
<Button mnemonicParsing="false" onAction="#handleExportCaseToPDF" text="Export to PDF" GridPane.columnIndex="1" />
</children>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.kritsit.casetracker.client.domain.services;

import static org.mockito.Mockito.mock;

import java.time.LocalDate;
import java.util.List;
import java.util.ArrayList;

import java.io.File;
import java.nio.file.Files;

Expand All @@ -12,6 +14,14 @@

import java.io.IOException;

import com.kritsit.casetracker.shared.domain.model.Case;
import com.kritsit.casetracker.shared.domain.model.Defendant;
import com.kritsit.casetracker.shared.domain.model.Evidence;
import com.kritsit.casetracker.shared.domain.model.Incident;
import com.kritsit.casetracker.shared.domain.model.Permission;
import com.kritsit.casetracker.shared.domain.model.Person;
import com.kritsit.casetracker.shared.domain.model.Staff;

public class ExportTest extends TestCase {

public ExportTest(String name){
Expand Down Expand Up @@ -48,4 +58,38 @@ public void testExport(){
assertFalse(file.exists());
}

public void testExportCase(){
IExportService exportService = new Export();

Staff user = mock(Staff.class);
String caseNumber = "2015-02-0001";
String caseName = "Developers vs Testing";
String caseType = "Battle to the death";
String details = "Last man standing survives";
String animalsInvolved = "1 Developer and 1 AI";
LocalDate incidentDate = LocalDate.parse("2014-05-14");
String address = "";
double longitude = -12.9880;
double latitude = 9.82203;
String region = "Outer space";
boolean isReturnVisit = false;
List<Evidence> evidence = new ArrayList<>();
Staff investigatingOfficer = new Staff("inspector", "test", "inspector", "department", "position", Permission.EDITOR);
Person complainant = new Person(-1, "0212202", "test", "complainant", "Somewhere", "0299222", "test@test.com");
Defendant defendant = new Defendant(-1, "0212202", "test", "complainant", "Somewhere", "0299222", "test@test.com", false);
Incident incident = new Incident(-1, longitude, latitude, region, incidentDate, Incident.getDefaultFollowUpDate(incidentDate), false);
Case c = new Case(caseNumber, caseName, details, animalsInvolved, user, incident, defendant, complainant, null, evidence, isReturnVisit, null, caseType, null);

File file = new File("test2.pdf");
exportService.exportCaseToPDF(c, file);
assertTrue(file.exists());
try{
file.delete();
}
catch(Exception e){
fail();
}
assertFalse(file.exists());
}

}