Skip to content
Snippets Groups Projects
Commit 84c75015 authored by Eirik Steira's avatar Eirik Steira
Browse files

Merge branch 'refactor/pdf-creation' into 'dev'

Refactor/pdf creation

See merge request !88
parents 88940cbf ece9b708
No related branches found
No related tags found
2 merge requests!104Weekly merge to Master,!88Refactor/pdf creation
Pipeline #77824 passed
......@@ -2,9 +2,9 @@ package NTNU.IDATT1002.controllers;
import NTNU.IDATT1002.App;
import NTNU.IDATT1002.models.ImageAlbum;
import NTNU.IDATT1002.service.ImageAlbumDocument;
import NTNU.IDATT1002.service.ImageAlbumService;
import NTNU.IDATT1002.service.TagService;
import NTNU.IDATT1002.utils.PdfDocument;
import javafx.application.HostServices;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
......@@ -18,8 +18,6 @@ import javafx.scene.layout.Pane;
import javafx.scene.shape.StrokeType;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.EntityManager;
import java.io.File;
......@@ -49,7 +47,7 @@ public class ViewAlbum implements Initializable {
public Text album_descField;
public Pane metadata_pane;
public Button create_album_pdf;
public Button create_album_document;
public ImageView main_picture;
......@@ -70,13 +68,11 @@ public class ViewAlbum implements Initializable {
private ImageAlbumService imageAlbumService;
private Long currentAlbumId;
private Logger logger;
public ViewAlbum() {
EntityManager entityManager = App.ex.getEntityManager();
this.imageAlbumService = new ImageAlbumService(entityManager);
currentAlbumId = App.ex.getChosenAlbumId();
logger = LoggerFactory.getLogger("ImageApplicationLogger");
}
/**
......@@ -372,24 +368,15 @@ public class ViewAlbum implements Initializable {
}
/**
* Create and save album pdf to users downloads directory.
* Retrieve and display album document.
*
* @param actionEvent
*/
public void createPdf(ActionEvent actionEvent) {
public void createDocument(ActionEvent actionEvent) {
Long currentAlbumId = App.ex.getChosenAlbumId();
ImageAlbum imageAlbum = imageAlbumService.getImageAlbumById(currentAlbumId)
.orElseThrow(IllegalArgumentException::new);
String destinationFile = String.format("%s/downloads/%s.pdf",
System.getProperty("user.home"),
imageAlbum.getTitle());
PdfDocument document = new PdfDocument(imageAlbum, destinationFile);
document.createPdfDocument();
logger.info("[x] Saved PDF document to " + destinationFile);
ImageAlbumDocument document = imageAlbumService.getDocument(currentAlbumId);
displayPdfLink(document.getPdfDocument());
displayDocumentLink(document.getDocument());
}
/**
......@@ -397,9 +384,9 @@ public class ViewAlbum implements Initializable {
*
* @param pdfDocument the pdf document to be opened
*/
private void displayPdfLink(File pdfDocument) {
create_album_pdf.setText("Open PDF");
create_album_pdf.setOnAction(actionEvent -> openPdfDocument(actionEvent, pdfDocument));
private void displayDocumentLink(File pdfDocument) {
create_album_document.setText("Open PDF");
create_album_document.setOnAction(actionEvent -> openDocument(actionEvent, pdfDocument));
}
/**
......@@ -408,7 +395,7 @@ public class ViewAlbum implements Initializable {
* @param actionEvent
* @param file the file to open
*/
private void openPdfDocument(ActionEvent actionEvent, File file) {
private void openDocument(ActionEvent actionEvent, File file) {
HostServices hostServices = App.ex.getHostServices();
hostServices.showDocument(file.getAbsolutePath());
}
......
package NTNU.IDATT1002.service;
import java.io.File;
/**
* Image Album Document Interface. Defines operations for getting and creating documents.
*
* @author Eirik Steira
* @version 1.0 30.03.20
*/
public interface ImageAlbumDocument {
File getDocument();
void createDocument();
}
......@@ -7,6 +7,8 @@ import NTNU.IDATT1002.models.Tag;
import NTNU.IDATT1002.models.User;
import NTNU.IDATT1002.repository.ImageAlbumRepository;
import NTNU.IDATT1002.repository.TagRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.EntityManager;
import java.util.ArrayList;
......@@ -30,6 +32,7 @@ public class ImageAlbumService {
private TagRepository tagRepository;
private Logger logger;
/**
* Inject entity manager instance to the repositories.
......@@ -37,6 +40,7 @@ public class ImageAlbumService {
public ImageAlbumService(EntityManager entityManager) {
this.imageAlbumRepository = new ImageAlbumRepository(entityManager);
this.tagRepository = new TagRepository(entityManager);
logger = LoggerFactory.getLogger("ImageApplicationLogger");
}
public Optional<ImageAlbum> getImageAlbumById(Long imageAlbumId) {
......@@ -102,6 +106,27 @@ public class ImageAlbumService {
return createImageAlbum(title, description, user, tags, new ArrayList<>());
}
/**
* Create and return a new document for the image album with the given id.
* The document is saved to the users dowloads folder.
*
* @param albumId the album id to get a document for
* @return the document created
*/
public ImageAlbumDocument getDocument(Long albumId) {
ImageAlbum imageAlbum = getImageAlbumById(albumId)
.orElseThrow(IllegalArgumentException::new);
String destinationFile = String.format("%s/downloads/%s.pdf",
System.getProperty("user.home"),
imageAlbum.getTitle());
ImageAlbumDocument document = new PdfDocument(imageAlbum, destinationFile);
document.createDocument();
logger.info("[x] Saved PDF document to " + destinationFile);
return document;
}
/**
* Retrieves all image albums created by the given user by username.
......
package NTNU.IDATT1002.utils;
package NTNU.IDATT1002.service;
import NTNU.IDATT1002.models.Image;
import NTNU.IDATT1002.models.ImageAlbum;
......@@ -18,7 +18,7 @@ import java.util.Date;
* @author Eirik Steira
* @version 1.0 22.03.20
*/
public class PdfDocument {
public class PdfDocument implements ImageAlbumDocument {
/**
* Height ratio satisfying a 16:9 ratio.
......@@ -48,14 +48,14 @@ public class PdfDocument {
this.document = new Document();
}
public File getPdfDocument() {
public File getDocument() {
return new File(DESTINATION_FILE);
}
/**
* Create a new pdf document.
*/
public void createPdfDocument() {
public void createDocument() {
try {
generatePdfDocument();
} catch (IOException | DocumentException e) {
......
......@@ -104,7 +104,7 @@
</font>
</Text>
<Pane fx:id="metadata_pane" layoutX="76.0" layoutY="371.0" prefHeight="391.0" prefWidth="822.0" style="-fx-background-color: #ffffff;" />
<Button fx:id="create_album_pdf" layoutX="394.0" layoutY="810.0" mnemonicParsing="false" onAction="#createPdf" text="CREATE ALBUM PDF">
<Button fx:id="create_album_document" layoutX="394.0" layoutY="810.0" mnemonicParsing="false" onAction="#createDocument" text="CREATE ALBUM PDF">
<font>
<Font size="18.0" />
</font>
......
......@@ -3,6 +3,7 @@ package NTNU.IDATT1002.utils;
import NTNU.IDATT1002.models.Image;
import NTNU.IDATT1002.models.ImageAlbum;
import NTNU.IDATT1002.models.User;
import NTNU.IDATT1002.service.PdfDocument;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
......@@ -19,13 +20,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
class PdfDocumentTest {
private static final String PATH_TO_PDF = "src/test/java/tmp/generatedImageAlbumPdf.pdf";
private static final String PATH_SEPARATOR = File.pathSeparator;
private ImageAlbum imageAlbum;
private Image image;
private User user;
/**
......@@ -66,9 +62,9 @@ class PdfDocumentTest {
* Test that a pdf document is successfully created.
*/
@Test
void testCreatePdfDocumentCreatesPdfDocument() {
void testCreateDocumentCreatesPdfDocument() {
PdfDocument document = new PdfDocument(imageAlbum, PATH_TO_PDF);
document.createPdfDocument();
document.createDocument();
assertNotNull(document);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment