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

Commit

Permalink
adding export interface
Browse files Browse the repository at this point in the history
  • Loading branch information
pawc committed Oct 23, 2015
1 parent c3587f2 commit 365632e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.kritsit.casetracker.client.domain.services;

import java.io.File;
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);

private 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) {
e.printStackTrace();

This comment has been minimized.

Copy link
@paddatrapper

paddatrapper Oct 23, 2015

Owner

These should rather be logged than printed to the console

} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

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);
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; i<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,10 @@
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);
}

This comment has been minimized.

Copy link
@paddatrapper

paddatrapper Oct 23, 2015

Owner

Extra blank lines here


Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ private void setUpController() {
logger.debug("Setting up controller");
IAdministratorService administratorService = ServiceFactory.getAdministratorService(user);
IMenuService menuService = ServiceFactory.getMenuService(user);
IExportService exportService = ServiceFactory.getExportService(user);
controller.setAdministratorService(administratorService);
controller.setMenuService(menuService);
controller.setStage(stage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

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 @@ -64,6 +65,7 @@ public class AdministratorController implements IController {
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 @@ -74,6 +76,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 @@ -138,7 +144,7 @@ public void initialize(){
});

exportItem.setOnAction(event->{
exportToPDF();
//to be done
});
}

Expand Down Expand Up @@ -373,59 +379,6 @@ private void resetPassword(String username){
}
}

private void exportToPDF(){
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("Users Report.pdf"));
document.open();
document.add(new Paragraph("Case Tracker users:"));
document.add(Chunk.NEWLINE);
document.add(createTable());
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}

}

private PdfPTable createTable(){

PdfPTable table = new PdfPTable(5);

Font boldFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
PdfPCell cellFirstName = new PdfPCell(new Phrase("First name", boldFont));
PdfPCell cellLastName = new PdfPCell(new Phrase("Last name", boldFont));
PdfPCell cellUsername = new PdfPCell(new Phrase("Username", boldFont));
PdfPCell cellDepartment = new PdfPCell(new Phrase("Department", boldFont));
PdfPCell cellPermission = new PdfPCell(new Phrase("Permission", boldFont));
table.addCell(cellFirstName);
table.addCell(cellLastName);
table.addCell(cellUsername);
table.addCell(cellDepartment);
table.addCell(cellPermission);

for(int i=0; i<filteredStaff.size(); i++){
PdfPCell cell1 = new PdfPCell(new Phrase(filteredStaff.get(i).getFirstName()));
PdfPCell cell2 = new PdfPCell(new Phrase(filteredStaff.get(i).getLastName()));
PdfPCell cell3 = new PdfPCell(new Phrase(filteredStaff.get(i).getUsername()));
PdfPCell cell4 = new PdfPCell(new Phrase(filteredStaff.get(i).getDepartment()));
PdfPCell cell5 = new PdfPCell(new Phrase(filteredStaff.get(i).getPermission().toString()));
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);


}

return table;

}

@FXML private TextField txfFilterUsers;
@FXML private ComboBox<String> cbxFilterPermissions;
@FXML private Button btnResetPassword;
Expand Down

0 comments on commit 365632e

Please sign in to comment.