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

export to PDF #42

Merged
merged 8 commits into from
Oct 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -0,0 +1,66 @@
package com.kritsit.casetracker.client.domain.services;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
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;

public class Export implements IExportService{

private final Logger logger = LoggerFactory.getLogger(Export.class);

public void exportToPDF(List<String> headers, List<String[]> cells, File file){

Document document = new Document();

try {
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");
} catch (DocumentException e) {
logger.error("Error while exporting to PDF");
logger.error(e.toString());
} catch (FileNotFoundException e) {
logger.error("Error while exporting to PDF");
logger.error(e.toString());
}
}

private PdfPTable createTable(List<String> headers, List<String[]> cells){

PdfPTable table = new PdfPTable(headers.size());
Font boldFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
Copy link
Owner

Choose a reason for hiding this comment

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

Indenting in this file is a little hap hazard

logger.info("Filling in the table");

for(int i=0; i<headers.size(); i++){
PdfPCell cell = new PdfPCell(new Phrase(headers.get(i), boldFont));
table.addCell(cell);
}

for(int j=0; j<cells.size(); j++){
for(int k=0; k < cells.get(j).length; k++){
PdfPCell cell = new PdfPCell(new Phrase(cells.get(j)[k]));
table.addCell(cell);
}
}
return table;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.kritsit.casetracker.client.domain.services;

import java.util.List;
import java.io.File;

public interface IExportService {
void exportToPDF(List<String> headers, List<String[]> cells, File file);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
package com.kritsit.casetracker.client.domain.ui.controller;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kritsit.casetracker.client.domain.services.Export;
import com.kritsit.casetracker.client.domain.services.IAdministratorService;
import com.kritsit.casetracker.client.domain.services.IMenuService;
import com.kritsit.casetracker.client.domain.services.IExportService;
import com.kritsit.casetracker.client.domain.services.InputToModelParseResult;
import com.kritsit.casetracker.shared.domain.model.Permission;
import com.kritsit.casetracker.shared.domain.model.Staff;
Expand Down Expand Up @@ -36,7 +48,12 @@
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.SecureRandom;
Expand All @@ -48,8 +65,10 @@
public class AdministratorController implements IController {

private ObservableList<Staff> staffList;
private FilteredList<Staff> filteredStaff;
private IAdministratorService administratorService;
private IMenuService menuService;
private IExportService exportService;
private Stage stage;
private final Logger logger = LoggerFactory.getLogger(AdministratorController.class);

Expand All @@ -60,6 +79,10 @@ public void setAdministratorService(IAdministratorService administratorService)
public void setMenuService(IMenuService menuService){
this.menuService = menuService;
}

public void setExportService(IExportService exportService){
this.exportService = exportService;
}

public void initFrame(){
logger.info("Initiating frame");
Expand Down Expand Up @@ -122,6 +145,40 @@ public void initialize(){
btnEdit.setOnAction(event->{
editUser();
});

exportItem.setOnAction(event->{
export();
});
}

private void export() {
exportService = new Export();

List<String> headers = new ArrayList<String>();
headers.add("First name");
headers.add("Last name");
headers.add("Username");
headers.add("Department");
headers.add("Position");
headers.add("Permissions");

List<String[]> cells = new ArrayList<String[]>();
for(Staff s : filteredStaff){
String[] row = new String[6];
row[0] = s.getFirstName();
row[1] = s.getLastName();
row[2] = s.getUsername();
row[3] = s.getDepartment();
row[4] = s.getPosition();
row[5] = s.getPermission().toString();
cells.add(row);
}

FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save report");
File file = fileChooser.showSaveDialog(stage);

exportService.exportToPDF(headers, cells, file);
}

private void addUser() {
Expand Down Expand Up @@ -234,7 +291,7 @@ protected void initStaffTable(){
logger.info("Initiating staff list table");
staffList = FXCollections.observableArrayList(administratorService.getStaff());

FilteredList<Staff> filteredStaff = new FilteredList<>(staffList, p -> true);
filteredStaff = new FilteredList<>(staffList, p -> true);
txfFilterUsers.textProperty().addListener((observable, oldValue, newValue) -> {
filteredStaff.setPredicate(s -> {
if (newValue == null || newValue.isEmpty() || newValue.equals("All")) {
Expand Down Expand Up @@ -374,6 +431,7 @@ private void resetPassword(String username){
@FXML private TableColumn<Staff, String> colDepartment;
@FXML private TableColumn<Staff, String> colPermission;
@FXML private MenuItem changePasswordItem;
@FXML private MenuItem exportItem;
@FXML private MenuItem logoutItem;
@FXML private MenuItem exitItem;
@FXML private MenuItem newUserItem;
Expand Down
1 change: 1 addition & 0 deletions client/src/main/resources/ui/fxml/AdministratorFrame.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="changePasswordItem" mnemonicParsing="false" text="Change password" />
<MenuItem fx:id="exportItem" mnemonicParsing="false" text="Export to PDF" />
<MenuItem fx:id="logoutItem" mnemonicParsing="false" text="Logout" />
<MenuItem fx:id="exitItem" mnemonicParsing="false" text="Exit" />
</items>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.kritsit.casetracker.client.domain.services;

import java.util.List;
import java.util.ArrayList;

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

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import java.io.IOException;

public class ExportTest extends TestCase {

public ExportTest(String name){
super(name);
}

public static Test suite(){
return new TestSuite(ExportTest.class);
}

public void testExport(){
IExportService exportService = new Export();
List<String> headers = new ArrayList<String>();
headers.add("first column");
headers.add("second column");
List<String[]> rows = new ArrayList<String[]>();
String[] firstRow = new String[2];
firstRow[0] = "first row, first column";
firstRow[1] = "first row, second column";
rows.add(firstRow);
String[] secondRow = new String[2];
secondRow[0] = "second row, first column";
secondRow[1] = "second row, second column";
rows.add(secondRow);
File file = new File("test.pdf");
exportService.exportToPDF(headers, rows, file);
assertTrue(file.exists());
try{
file.delete();
}
catch(Exception e){
fail();
}
assertFalse(file.exists());
}

}
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.7</version>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
Expand Down