This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
export to PDF #42
Merged
Merged
export to PDF #42
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bc65b47
admin view: added export menuitem
c3587f2
basic users report in admin view
365632e
adding export interface
00c0c58
implementing export method in admin view
13a3980
file chooser in pdf exporting
66764e5
fixing details
ec85a03
added export test
0c94688
export test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
client/src/main/java/com/kritsit/casetracker/client/domain/services/Export.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
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; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
client/src/main/java/com/kritsit/casetracker/client/domain/services/IExportService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
client/src/test/java/com/kritsit/casetracker/client/domain/services/ExportTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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