Skip to content
Snippets Groups Projects
Commit 99b079ca authored by Eirik Steira's avatar Eirik Steira Committed by Nicolay Schiøll-Johansen
Browse files

Feat/explore albums

parent ec7c1275
No related branches found
No related tags found
1 merge request!104Weekly merge to Master
Showing with 459 additions and 344 deletions
......@@ -2,6 +2,11 @@ package NTNU.IDATT1002;
import NTNU.IDATT1002.models.User;
import NTNU.IDATT1002.repository.UserRepository;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
* Class Application State. Keeps a record of the global application state, such as the current logged in user.
......@@ -13,7 +18,36 @@ public final class ApplicationState {
*/
private static User currentUser;
/**
* Anonymous user for developing purposes.
*/
private static User anonymousUser;
private static UserRepository userRepository;
/**
* Initiate properties and save an anonymous user once.
*/
static {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ImageApplication");
EntityManager entityManager = entityManagerFactory.createEntityManager();
userRepository = new UserRepository(entityManager);
}
public static void setCurrentUser(User currentUser) {
ApplicationState.currentUser = currentUser;
}
/**
* Retrieve the current logged in user if present, or retrieve an anonymous user.
*
* @return the current user.
* @throws IllegalArgumentException if neither the current user nor the anonymous user are present.
*/
public static User getCurrentUser() {
return userRepository.findById(currentUser.getUsername())
.orElseGet(() -> userRepository.findById(anonymousUser.getUsername())
.orElseThrow(IllegalArgumentException::new));
}
}
package NTNU.IDATT1002.controllers;
import NTNU.IDATT1002.App;
import NTNU.IDATT1002.ApplicationState;
import NTNU.IDATT1002.models.User;
import NTNU.IDATT1002.service.ImageAlbumService;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
......@@ -33,6 +36,12 @@ public class CreateAlbum {
public Button tbar_albums;
public Button tbar_searchBtn;
private ImageAlbumService imageAlbumService;
public CreateAlbum() {
imageAlbumService = new ImageAlbumService();
}
/**
* Method that changes stage to Main page
* @param mouseEvent
......@@ -90,4 +99,18 @@ public class CreateAlbum {
public void switchToUpload(ActionEvent actionEvent) throws IOException {
App.setRoot("upload");
}
/**
* Create an empty image album. The user will default to the currently logged in user.
*
* @param actionEvent
*/
public void createEmptyImageAlbum(ActionEvent actionEvent) {
String title = album_title_field.getText();
String description = album_desc_field.getText();
String tags = album_tag_field.getText();
User user = ApplicationState.getCurrentUser();
imageAlbumService.createImageAlbum(title, description, user, tags);
}
}
......@@ -11,6 +11,7 @@ import java.util.List;
public class DataExchange {
private String searchField;
private List<File> uploadedFiles;
private Long albumId;
public DataExchange(){
searchField = "";
......@@ -23,6 +24,10 @@ public class DataExchange {
return searchField;
}
public Long getAlbumId() {
return albumId;
}
public void setUploadedFiles(List<File> uploadedFiles) {
this.uploadedFiles = uploadedFiles;
}
......@@ -30,4 +35,8 @@ public class DataExchange {
public void setSearchField(String searchField) {
this.searchField = searchField;
}
public void setAlbumId(Long albumId) {
this.albumId = albumId;
}
}
package NTNU.IDATT1002.controllers;
import NTNU.IDATT1002.App;
import NTNU.IDATT1002.models.ImageAlbum;
import NTNU.IDATT1002.models.Tag;
import NTNU.IDATT1002.service.ImageAlbumService;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.StrokeType;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;
/**
* Controls the buttons and changeable elements on explore_albums.fxml,
* a page where you explore albums
* @version 1.0 22.03.2020
*/
public class ExploreAlbums {
public class ExploreAlbums implements Initializable {
public ImageView tbar_logo;
public TextField tbar_search;
public Button tbar_map;
public Button tbar_upload;
public Button tbar_albums;
public Button tbar_searchBtn;
public Button tbar_explore;
public ScrollPane scrollpane;
public Button footer_previous_page;
public Button footer_next_page;
public Button tbar_searchBtn;
public Button tbar_explore;
public Text album_amount;
public ChoiceBox sorted_by_choicebox;
public Button create_album_button;
public ImageView album_image;
public Text album_author;
public Text album_title;
public Text album_author;
public Text album_desc;
public Text album_tags;
public Text album_author2;
public Text album_title2;
public Text album_desc2;
public Text album_tags2;
public ImageView album_image2;
public ImageView album_image3;
public Text album_author3;
public Text album_title3;
public Text album_desc3;
public Text album_tags3;
public ImageView album_image4;
public Text album_author4;
public Text album_title4;
public Text album_desc4;
public Text album_tags4;
public ImageView album_image5;
public Text album_author5;
public Text album_title5;
public Text album_desc5;
public Text album_tags5;
public Button tbar_albums;
public Button open_album4;
public Button open_album3;
public Button open_album2;
public Button open_album1;
public Button open_album;
@FXML
private GridPane albums_grid_pane;
private Pane paneContainer;
private ImageAlbumService imageAlbumService;
public ExploreAlbums() {
imageAlbumService = new ImageAlbumService();
}
/**
* Initialize page with all albums. Max 5 per page.
*
* @param url
* @param resourceBundle
*/
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
List<ImageAlbum> albums = imageAlbumService.getAllImageAlbums();
int maxPerPage = Math.min(albums.size(), 5);
for(int i = 0; i<maxPerPage; i++) {
paneContainer = new Pane();
paneContainer.setPrefWidth(200);
paneContainer.setPrefHeight(100);
addSingleAlbumContentToPane(albums.get(i), paneContainer);
albums_grid_pane.add(paneContainer, 0, i);
}
}
/**
* Add all components needed to display an album.
*
* @param album the album to display
* @param pane the pane to add the album to
*/
private void addSingleAlbumContentToPane(ImageAlbum album, Pane pane) {
insertImageViewToPane(album, pane);
insertAlbumTitleLabelToPane(pane);
insertAlbumAuthorLabelToPane(pane);
insertAlbumDescriptionLabelToPane(pane);
insertAlbumAuthorToPane(album, pane);
insertAlbumTitleToPane(album, pane);
insertAlbumDescriptionToPane(album, pane);
insertAlbumTagsLabelToPane(pane);
insertAlbumTagsToPane(album, pane);
insertOpenAlbumButtonToPane(album, pane);
}
/**
* Format and insert the first image in the given album to the given pane.
*
* @param album the album to display image from
* @param pane the pane to add the image to
*/
private void insertImageViewToPane(ImageAlbum album, Pane pane) {
// Set and format image
album_image = new ImageView();
album_image.setFitHeight(307.0);
album_image.setFitWidth(516.0);
album_image.setLayoutX(-2.0);
album_image.setLayoutY(-1.0);
album_image.setPickOnBounds(true);
album_image.setPreserveRatio(true);
NTNU.IDATT1002.models.Image titleImage = album.getImages().get(0);
album_image.setId(titleImage.getId().toString());
Image image = new Image("@../../Images/placeholder-1920x1080.png"); // TODO: display image here
album_image.setImage(image);
pane.getChildren().add(album_image);
}
/**
* Insert album title label to given pane.
*
* @param pane the pane to add the title to
*/
private void insertAlbumTitleLabelToPane(Pane pane) {
// set and format title label
Text text = new Text();
text.setText("ALBUM: ");
text.setFont(Font.font(48.0));
text.setLayoutX(551.0);
text.setLayoutY(63.0);
text.setStrokeType(StrokeType.OUTSIDE);
text.setStrokeWidth(0.0);
text.setWrappingWidth(200);
pane.getChildren().add(text);
}
/**
* Insert author label of the given album to the given pane
*
* @param pane the pane to add the author label to
*/
private void insertAlbumAuthorLabelToPane(Pane pane) {
// set and format author label
Text authorLabel = new Text();
authorLabel.setText("AUTHOR: ");
authorLabel.setFont(Font.font(24));
authorLabel.setLayoutX(551.0);
authorLabel.setLayoutY(97.0);
authorLabel.setStrokeType(StrokeType.OUTSIDE);
authorLabel.setStrokeWidth(0.0);
authorLabel.setWrappingWidth(150.0);
pane.getChildren().add(authorLabel);
}
/**
* Insert description label of the given album to the given pane
*
* @param pane the pane to add the description label to
*/
private void insertAlbumDescriptionLabelToPane(Pane pane) {
// set and format description label
Text descriptionLabel = new Text();
descriptionLabel.setText("DESCRIPTION: ");
descriptionLabel.setFont(Font.font(18.0));
descriptionLabel.setLayoutX(551.0);
descriptionLabel.setLayoutY(157.0);
descriptionLabel.setStrokeType(StrokeType.OUTSIDE);
descriptionLabel.setStrokeWidth(0.0);
descriptionLabel.setWrappingWidth(129.0);
pane.getChildren().add(descriptionLabel);
}
/**
* Insert author of the given album to the given pane
*
* @param album the album which author to display
* @param pane the pane to add the author to
*/
private void insertAlbumAuthorToPane(ImageAlbum album, Pane pane) {
// set and format author
album_author = new Text();
album_author.setId("album_author");
album_author.setText(album.getUser().getUsername());
album_author.setFont(Font.font(24.0));
album_author.setLayoutX(707.0);
album_author.setLayoutY(97.0);
album_author.setStrokeType(StrokeType.OUTSIDE);
album_author.setStrokeWidth(0.0);
album_author.setWrappingWidth(270.0);
pane.getChildren().add(album_author);
}
/**
* Insert title of the given album to the given pane
*
* @param album the album which title to display
* @param pane the pane to add the title to
*/
private void insertAlbumTitleToPane(ImageAlbum album, Pane pane) {
album_title = new Text();
album_title.setId("album_title");
album_title.setText(album.getTitle());
album_title.setFont(Font.font(48.0));
album_title.setLayoutX(751.0);
album_title.setLayoutY(65.0);
album_title.setStrokeType(StrokeType.OUTSIDE);
album_title.setStrokeWidth(0.0);
album_title.setWrappingWidth(653.0);
pane.getChildren().add(album_title);
}
/**
* Insert description of the given album to the given pane
*
* @param album the album which description to display
* @param pane the pane to add the description to
*/
private void insertAlbumDescriptionToPane(ImageAlbum album, Pane pane) {
album_desc = new Text();
album_desc.setId("album_desc");
album_desc.setText(album.getDescription());
album_desc.setFont(Font.font(18.0));
album_desc.setLayoutX(707.0);
album_desc.setLayoutY(157.0);
album_desc.setStrokeType(StrokeType.OUTSIDE);
album_desc.setStrokeWidth(0.0);
album_desc.setWrappingWidth(229.0);
pane.getChildren().add(album_desc);
}
/**
* Insert tags label of the given album to the given pane
*
* @param pane the pane to add the tags label to
*/
private void insertAlbumTagsLabelToPane(Pane pane) {
Text tagsLabel = new Text();
tagsLabel.setText("TAGS: ");
tagsLabel.setFont(Font.font(24));
tagsLabel.setLayoutX(551.0);
tagsLabel.setLayoutY(129.0);
tagsLabel.setStrokeType(StrokeType.OUTSIDE);
tagsLabel.setStrokeWidth(0.0);
tagsLabel.setWrappingWidth(150.0);
pane.getChildren().add(tagsLabel);
}
/**
* Insert tags of the given album to the given pane
*
* @param album the album which tags to display
* @param pane the pane to add the tags to
*/
private void insertAlbumTagsToPane(ImageAlbum album, Pane pane) {
String tagsAsString = album.getTags().stream()
.map(Tag::getName)
.collect(Collectors.joining(", "));
System.out.println(tagsAsString);
album_tags = new Text();
album_tags.setText("tag1, tag2");
album_tags.setFont(Font.font(24.0));
album_tags.setLayoutX(707.0);
album_tags.setLayoutY(129.0);
album_tags.setStrokeType(StrokeType.OUTSIDE);
album_tags.setStrokeWidth(0.0);
album_tags.setWrappingWidth(270.0);
pane.getChildren().add(album_tags);
}
/**
* Insert an 'open album' button to the given album to the given pane.
* If pushed it will take the user to the appropriate album.
*
* @param album the album which the button should take the user to
* @param pane the pane to add the button to
*/
private void insertOpenAlbumButtonToPane(ImageAlbum album, Pane pane) {
// set and format open album button
open_album = new Button();
open_album.setId(album.getId().toString());
open_album.setText("Open Album");
open_album.setFont(Font.font(18.0));
open_album.setLayoutX(551.0);
open_album.setLayoutY(250.0);
open_album.setOnAction(event -> {
try {
switchToViewAlbum(event);
} catch (IOException e) {
e.printStackTrace();
}
});
pane.getChildren().add(open_album);
}
/**
* Method that changes stage to Main page
* @param mouseEvent
......@@ -153,7 +412,11 @@ public class ExploreAlbums {
* @throws IOException
*/
public void switchToViewAlbum(ActionEvent actionEvent) throws IOException {
//TODO: write method to open the specific album chosen
Button source = (Button) actionEvent.getSource();
Long albumId = Long.parseLong(source.getId());
App.ex.setAlbumId(albumId);
App.setRoot("view_album");
}
}
......@@ -12,7 +12,7 @@ import java.util.List;
@Table(name = "user")
public class User {
@Id @NotBlank(message = "Username may not be blank")
@Id
private String username;
@Email
......@@ -55,9 +55,9 @@ public class User {
public User() {
}
public User(String email, String username, String firstName, String lastName, String callingCode, String phoneNumber, Date birthDate) {
this.email = email;
public User(String username, String email, String firstName, String lastName, String callingCode, String phoneNumber, Date birthDate) {
this.username = username;
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
this.callingCode = callingCode;
......@@ -65,7 +65,6 @@ public class User {
this.birthDate = birthDate;
this.isAdmin = false;
this.isActive = true;
this.imageAlbums = imageAlbums;
}
public String getEmail() {
......@@ -108,6 +107,29 @@ public class User {
this.username = username;
}
public void setEmail(String email) {
this.email = email;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setCallingCode(String callingCode) {
this.callingCode = callingCode;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
/**
* Add given image album.
*
......
......@@ -10,7 +10,7 @@ import javax.persistence.EntityManager;
*
* @version 1.0 22.03.20
*/
public class UserRepository extends GenericRepository<User, Long> {
public class UserRepository extends GenericRepository<User, String> {
/**
* Constructor to inject {@link EntityManager} dependency and sets the class type to {@link User}
......
......@@ -11,9 +11,11 @@ import NTNU.IDATT1002.service.filters.ImageAlbumFilter;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
......@@ -42,7 +44,16 @@ public class ImageAlbumService {
}
/**
* Saves a new image album with the given input.
* Retrieves all image albums.
*
* @return list of all image albums.
*/
public List<ImageAlbum> getAllImageAlbums() {
return imageAlbumRepository.findAll();
}
/**
* Create a new image album with all fields populated.
*
* @param title the title of the image album
* @param description the description of the image album
......@@ -67,22 +78,41 @@ public class ImageAlbumService {
}
/**
* Retrieves all image albums created by the given user by username.
* Create and empty image album.
*
* @param user the user to query by
* @return list of all image albums created by the user
* @param title the title of the image album
* @param description the description of the image album
* @param user the user of the image album
* @param tagsAsString the tags of the image album as strings
*/
public List<ImageAlbum> getImageAlbumFromUser(User user) {
return imageAlbumRepository.findAllByUsername(user.getUsername());
public Optional<ImageAlbum> createImageAlbum(String title, String description, User user, String tagsAsString) {
List<Tag> tags = getTagsFromString(tagsAsString);
return createImageAlbum(title, description, user, tags, new ArrayList<>());
}
/**
* Retrieves all image albums.
* Retrieves tags from text field and converts them to a list of tag objects.
*
* @return list of all image albums.
* @return the list of tag objects
*/
public List<ImageAlbum> getAllImageAlbums() {
return imageAlbumRepository.findAll();
private List<Tag> getTagsFromString(String tagsAsString) {
String[] tags = tagsAsString
.trim()
.split("[, ?.@]+");
return Stream.of(tags)
.map(Tag::new)
.collect(Collectors.toList());
}
/**
* Retrieves all image albums created by the given user by username.
*
* @param user the user to query by
* @return list of all image albums created by the user
*/
public List<ImageAlbum> getImageAlbumFromUser(User user) {
return imageAlbumRepository.findAllByUsername(user.getUsername());
}
......
......@@ -83,7 +83,7 @@
<Font name="System Bold" size="18.0" />
</font>
</Button>
<Button fx:id="create_album_button" layoutX="641.0" layoutY="867.0" mnemonicParsing="false" text="CREATE ALBUM">
<Button fx:id="create_album_button" layoutX="641.0" layoutY="867.0" mnemonicParsing="false" onAction="#createEmptyImageAlbum" text="CREATE ALBUM">
<font>
<Font name="System Bold" size="18.0" />
</font>
......
This diff is collapsed.
......@@ -9,8 +9,6 @@ import org.junit.jupiter.api.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
......@@ -30,8 +28,8 @@ class LoginRepositoryTest {
private LoginRepository loginRepository;
private String id1;
private String id2;
private String username1;
private String username2;
private String password;
private String newPassword;
private Date date;
......@@ -49,17 +47,17 @@ class LoginRepositoryTest {
public void setUp() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ImageApplicationTest");
EntityManager entityManager = entityManagerFactory.createEntityManager();
loginRepository = new LoginRepository(entityManager);
id1 = "test1";
id2 = "test2";
username1 = "test1";
username2 = "test2";
password = "Test123";
newPassword = "Test321";
date = new Date(System.currentTimeMillis());
user1 = new User("epost", id1, "fornavn", "etternavn", "test" , "test", date);
user2 = new User("epost2" , id2, "fornavn2", "etternavn2", "test2", "test2", date);
user1 = new User(username1,"email","firstName", "lastName", "test" , "test", date);
user2 = new User(username2, "email2" , "firstName2", "lastName2", "test2", "test2", date);
login1 = new Login(user1, "test", "test");
login2 = new Login(user2, "test2", "test2");
loginRepository = new LoginRepository(entityManager);
}
/**
......@@ -101,10 +99,10 @@ class LoginRepositoryTest {
void testFindByIdReturnsOptionalWithEntityWithId() {
loginRepository.save(login1);
Optional<Login> foundLogins = loginRepository.findById(id1);
Optional<Login> foundLogins = loginRepository.findById(username1);
assertTrue(foundLogins.isPresent());
assertEquals(id1, foundLogins.get().getUser().getUsername());
assertEquals(username1, foundLogins.get().getUser().getUsername());
}
/**
......@@ -113,10 +111,10 @@ class LoginRepositoryTest {
@Test
void testDeleteById() {
loginRepository.save(login1);
Optional<Login> foundLogins = loginRepository.findById(id1);
Optional<Login> foundLogins = loginRepository.findById(username1);
foundLogins.ifPresent(Login -> loginRepository.deleteById(id1));
Optional<Login> deletedLogin = loginRepository.findById(id1);
foundLogins.ifPresent(Login -> loginRepository.deleteById(username1));
Optional<Login> deletedLogin = loginRepository.findById(username1);
assertTrue(deletedLogin.isEmpty());
}
......@@ -145,7 +143,7 @@ class LoginRepositoryTest {
Login login3 = new Login(user1, salt, hash);
loginRepository.save(login3);
assertTrue(loginRepository.logIn(id1, password));
assertTrue(loginRepository.logIn(username1, password));
}
/**
......@@ -159,7 +157,7 @@ class LoginRepositoryTest {
Login login3 = new Login(user1, salt, hash);
loginRepository.save(login3);
assertTrue(loginRepository.changePassword(id1, password, newPassword));
assertTrue(loginRepository.changePassword(username1, password, newPassword));
}
/**
......@@ -173,9 +171,9 @@ class LoginRepositoryTest {
Login login3 = new Login(user1, salt, hash);
loginRepository.save(login3);
assertTrue(loginRepository.logIn(id1, password));
assertTrue(loginRepository.changePassword(id1, password, newPassword));
assertTrue(loginRepository.logIn(id1, newPassword));
assertTrue(loginRepository.logIn(username1, password));
assertTrue(loginRepository.changePassword(username1, password, newPassword));
assertTrue(loginRepository.logIn(username1, newPassword));
}
/**
......@@ -188,7 +186,7 @@ class LoginRepositoryTest {
String hash = credentials.get(1);
Login login3 = new Login(user1, salt, hash);
loginRepository.save(login3);
assertFalse(loginRepository.logIn(id1, newPassword));
assertFalse(loginRepository.logIn(username1, newPassword));
}
/**
......@@ -201,8 +199,8 @@ class LoginRepositoryTest {
String hash = credentials.get(1);
Login login3 = new Login(user1, salt, hash);
loginRepository.save(login3);
assertFalse(loginRepository.changePassword(id1, newPassword, password));
assertTrue(loginRepository.logIn(id1, password));
assertFalse(loginRepository.changePassword(username1, newPassword, password));
assertTrue(loginRepository.logIn(username1, password));
}
/**
......@@ -215,7 +213,7 @@ class LoginRepositoryTest {
String hash = credentials.get(1);
Login login3 = new Login(user1, salt, hash);
loginRepository.save(login3);
assertFalse(loginRepository.logIn(id1, null));
assertFalse(loginRepository.logIn(username1, null));
}
/**
......@@ -228,6 +226,6 @@ class LoginRepositoryTest {
String hash = credentials.get(1);
Login login3 = new Login(user1, salt, hash);
loginRepository.save(login3);
assertFalse(loginRepository.changePassword(id1, null, newPassword));
assertFalse(loginRepository.changePassword(username1, null, newPassword));
}
}
\ No newline at end of file
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