Skip to content
Snippets Groups Projects
Commit d7c8a7fa authored by Lars Brodin Østby's avatar Lars Brodin Østby
Browse files

Merge branch 'create-album' into 'dev'

Create album

See merge request !118
parents ea596bc7 8300b931
No related branches found
No related tags found
2 merge requests!165Weekly merge to Master,!118Create album
Pipeline #78903 passed
......@@ -2,29 +2,45 @@ package NTNU.IDATT1002.controllers;
import NTNU.IDATT1002.App;
import NTNU.IDATT1002.ApplicationState;
import NTNU.IDATT1002.models.Album;
import NTNU.IDATT1002.models.Image;
import NTNU.IDATT1002.models.Tag;
import NTNU.IDATT1002.models.User;
import NTNU.IDATT1002.service.AlbumService;
import NTNU.IDATT1002.service.ImageService;
import NTNU.IDATT1002.service.TagService;
import NTNU.IDATT1002.utils.ImageUtil;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import org.hibernate.boot.jaxb.internal.stax.HbmEventReader;
import javax.persistence.EntityManager;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.stream.Collectors;
/**
* Controls the buttons and changeable elements on create_album.fxml,
* a page where you create albums
* @version 1.0 22.03.2020
*/
public class CreateAlbum {
public class CreateAlbum implements Initializable {
public TextField tbar_search;
public ImageView tbar_logo;
public Button tbar_explore;
......@@ -39,12 +55,36 @@ public class CreateAlbum {
public Button create_album_button;
public Button tbar_albums;
public Button tbar_searchBtn;
public VBox fileContainer;
private AlbumService albumService;
private ImageService imageService;
public CreateAlbum() {
EntityManager entityManager = App.ex.getEntityManager();
albumService = new AlbumService(entityManager);
imageService = new ImageService(entityManager);
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
List<Image> allImages = imageService.getImageFromUser(ApplicationState.getCurrentUser());
for (Image image : allImages){
javafx.scene.image.Image convertedImage = ImageUtil.convertToFXImage(image);
HBox container = new HBox();
container.setPrefWidth(450);
container.setAlignment(Pos.TOP_CENTER);
ImageView imageView = new ImageView();
imageView.setFitHeight(200);
imageView.setFitWidth(350);
imageView.setPickOnBounds(true);
imageView.setPreserveRatio(true);
imageView.setImage(convertedImage);
CheckBox checkBox = new CheckBox();
checkBox.setId(image.getId().toString());
container.getChildren().addAll(imageView, checkBox);
fileContainer.getChildren().add(container);
}
}
/**
......@@ -106,17 +146,51 @@ public class CreateAlbum {
}
/**
* Create an empty album. The user will default to the currently logged in user.
*
* Method that creates album based on the user input and checked images
* @param actionEvent
*/
public void createEmptyAlbum(ActionEvent actionEvent) {
public void createAlbum(ActionEvent actionEvent){
String title = album_title_field.getText();
String description = album_desc_field.getText();
String tags = album_tag_field.getText();
List<Tag> tagsToAdd = TagService.getTagsFromString(tags);
User user = ApplicationState.getCurrentUser();
albumService.createEmptyAlbum(title, description, user, tagsToAdd);
List<Node> imageContainers = new ArrayList<>(fileContainer.getChildren());
List<String> checkedImagesId = new ArrayList<>();
//Each image and checkbox has an hbox container
imageContainers.forEach(hbox ->
((HBox) hbox).getChildren().stream()
//Filter children that is a checked checkbox
.filter(child -> child instanceof CheckBox && ((CheckBox) child).isSelected())
//Adds all checked image id
.forEach(checked -> checkedImagesId.add(checked.getId()))
);
//Find the users images and makes a filter on the checked images
List<Image> albumImages = imageService.getImageFromUser(user).stream().filter(image -> checkedImagesId.contains(image.getId().toString())).collect(Collectors.toList());
if (albumImages.size() > 0){
Optional<Album> createdAlbum = albumService.createAlbum(title, description, user, tagsToAdd, albumImages);
createdAlbum.ifPresent(album -> {
App.ex.setChosenAlbumId(album.getId());
try {
App.setRoot("view_album");
} catch (IOException e) {
e.printStackTrace();
}
});
}
else {
Optional<Album> createdAlbum = albumService.createEmptyAlbum(title, description, user, tagsToAdd);
createdAlbum.ifPresent(album -> {
App.ex.setChosenAlbumId(album.getId());
try {
App.setRoot("view_album");
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
}
......@@ -22,7 +22,7 @@ public class ImageRepository extends AbstractRepository<Image, Long> {
/**
* Mapping to @NamedQuery 'find all albums by users username' defined in {@link Image}
*/
public static final String IMAGE_FIND_BY_USERNAME = "Album.findAllByUsername";
public static final String IMAGE_FIND_BY_USERNAME = "Image.findAllByUsername";
/**
* Constructor to inject {@link EntityManager} dependency.
......
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<VBox xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="NTNU.IDATT1002.controllers.CreateAlbum">
<VBox xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="NTNU.IDATT1002.controllers.CreateAlbum">
<children>
<HBox alignment="CENTER" minHeight="100.0" prefHeight="100.0" prefWidth="1920.0" spacing="20.0" style="-fx-background-color: #0c0c0c;">
<children>
......@@ -65,12 +58,7 @@
<Pane fx:id="metadata_pane" prefHeight="373.0" prefWidth="739.0" style="-fx-background-color: #ffff;" />
<HBox alignment="CENTER" prefHeight="41.0" prefWidth="662.0" spacing="20.0">
<children>
<Button fx:id="add_images_button" mnemonicParsing="false" text="ADD IMAGES">
<font>
<Font name="System Bold" size="18.0" />
</font>
</Button>
<Button fx:id="create_album_button" mnemonicParsing="false" onAction="#createEmptyAlbum" text="CREATE ALBUM">
<Button fx:id="create_album_button" mnemonicParsing="false" onAction="#createAlbum" text="CREATE ALBUM">
<font>
<Font name="System Bold" size="18.0" />
</font>
......@@ -83,21 +71,10 @@
</VBox>
<ScrollPane hbarPolicy="NEVER" prefHeight="920.0" prefWidth="450.0">
<content>
<VBox alignment="TOP_CENTER" prefWidth="450.0" spacing="10.0">
<children>
<ImageView fitHeight="218.0" fitWidth="328.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../Images/placeholder-1920x1080.png" />
</image>
</ImageView>
</children>
<padding>
<Insets top="65.0" />
</padding>
<VBox fx:id="fileContainer" alignment="TOP_CENTER" prefWidth="450.0" spacing="10.0">
</VBox>
</content>
</ScrollPane>
<VBox prefHeight="200.0" prefWidth="250.0" HBox.hgrow="ALWAYS" />
</children>
</HBox>
</children>
......
......@@ -18,7 +18,7 @@
<?import javafx.scene.text.Text?>
<?import javafx.scene.control.ProgressBar?>
<VBox fx:id="root" prefHeight="1080.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="NTNU.IDATT1002.controllers.ExploreAlbums">
<VBox fx:id="pageRootContainer" prefHeight="1080.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="NTNU.IDATT1002.controllers.ExploreAlbums">
<children>
<HBox fx:id="progressBarContainer" alignment="CENTER" minHeight="15.0" prefHeight="15.0" prefWidth="1920.0" spacing="20.0" style="-fx-background-color: #6d6d6d;">
<children>
......
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