Skip to content
Snippets Groups Projects
Commit 2267c248 authored by Mads Lundegaard's avatar Mads Lundegaard
Browse files

Merge branch 'tagsAtUpload' into 'dev'

Tags at upload

See merge request !99
parents 5e6d7d01 5576e2d1
No related branches found
No related tags found
2 merge requests!104Weekly merge to Master,!99Tags at upload
Pipeline #78368 passed
......@@ -3,7 +3,9 @@ package NTNU.IDATT1002.controllers;
import NTNU.IDATT1002.App;
import NTNU.IDATT1002.ApplicationState;
import NTNU.IDATT1002.models.Tag;
import NTNU.IDATT1002.repository.TagRepository;
import NTNU.IDATT1002.service.ImageService;
import NTNU.IDATT1002.service.TagService;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
......@@ -133,28 +135,18 @@ public class UploadedSingle implements Initializable {
* Method for uploading image to database with tags
* Image itself is not stored but URL is
*
* @author Lars ØStby
* @param actionEvent
* @throws IOException
*/
public void uploadSingle(ActionEvent actionEvent) throws IOException {
List<File> list = App.ex.getUploadedFiles();
File fil = list.get(0);
ArrayList<Tag> tags = new ArrayList<>();
imageService.createImage(ApplicationState.getCurrentUser(), fil, tags);
File file = list.get(0);
List<Tag> tags = TagService.getTagsFromString(photo_tag.getText());
imageService.createImage(ApplicationState.getCurrentUser(), file, tags);
App.setRoot("main");
}
/***
* Method for splitting the tag textField into tags in a list
* @param photo_tag
* @return list of string
*/
public List<String> tagStringSplit(TextField photo_tag) {
String tagTekst = photo_tag.getText();
return Arrays.asList(tagTekst.split("(?=#)"));
}
}
......@@ -6,6 +6,12 @@ import NTNU.IDATT1002.models.Tag;
import NTNU.IDATT1002.service.AlbumDocument;
import NTNU.IDATT1002.service.AlbumService;
import NTNU.IDATT1002.utils.ImageUtil;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.logging.Logger;
import javafx.application.HostServices;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
......@@ -23,6 +29,8 @@ import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javax.persistence.EntityManager;
import org.slf4j.LoggerFactory;
import javax.persistence.EntityManager;
import java.io.File;
......@@ -31,6 +39,7 @@ import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.stream.Collectors;
import org.slf4j.LoggerFactory;
/**
* Controls the buttons and changeable elements on view_album.fxml,
......@@ -45,12 +54,9 @@ public class ViewAlbum implements Initializable {
public Button tbar_upload;
public Button tbar_searchBtn;
public Button tbar_albums;
public Pane metadataPane;
public Button createAlbumPdf;
public ImageView mainPicture;
public Text pictureTitleField;
public Text pictureTagsField;
......@@ -61,6 +67,7 @@ public class ViewAlbum implements Initializable {
private AlbumService albumService;
private Album currentAlbum;
/**
* Initialize view with real album data.
......
......@@ -32,7 +32,7 @@ public class TagRepository extends GenericRepository<Tag, Long> {
* @return the tag if found, else the newly created one.
*/
public Optional<Tag> findOrCreate(Tag tag) {
try {
try {
Optional<Tag> foundTag = findById(tag.getTagId());
if (foundTag.isPresent())
......
......@@ -5,6 +5,8 @@ import NTNU.IDATT1002.repository.*;
import NTNU.IDATT1002.utils.ImageUtil;
import NTNU.IDATT1002.utils.MetaDataExtractor;
import java.util.Arrays;
import javafx.scene.control.TextField;
import javax.persistence.EntityManager;
import java.io.File;
import java.util.ArrayList;
......@@ -21,22 +23,16 @@ import java.util.stream.Collectors;
public class ImageService {
private ImageRepository imageRepository;
private MetadataRepository metadataRepository;
private TagRepository tagRepository;
private HistorgramRepository historgramRepository;
private GeoLocatioRepository geoLocatioRepository;
private MetaDataExtractor metaDataExtractor;
private TagService tagService;
/**
* Inject entity manager instance to the repositories.
*/
public ImageService(EntityManager entityManager) {
this.imageRepository = new ImageRepository(entityManager);
this.metadataRepository = new MetadataRepository(entityManager);
this.tagRepository = new TagRepository(entityManager);
this.historgramRepository = new HistorgramRepository(entityManager);
this.geoLocatioRepository = new GeoLocatioRepository(entityManager);
this.metaDataExtractor = new MetaDataExtractor();
this.tagService = new TagService(entityManager);
}
/**
......@@ -46,7 +42,7 @@ public class ImageService {
* @param file the file uploaded
* @return Optional containing the saved image
*/
public Optional<Image> createImage(User user, File file, ArrayList<Tag> tags) {
public Optional<Image> createImage(User user, File file, List<Tag> tags) {
GeoLocation geoLocation = metaDataExtractor.getGeoLocation(file);
Histogram histogram = metaDataExtractor.getHistogram(file);
......@@ -61,18 +57,17 @@ public class ImageService {
metadata.setHistogram(histogram);
histogram.setMetadata(metadata);
byte[] bFile = ImageUtil.convertToBytes(file.getPath());
//TODO: Add image tags and add image to album
image.setRawImage(bFile);
image.setUser(user);
image.setPath(file.getPath());
image.addTags(tags);
image.addTags((ArrayList<Tag>) tagService.getOrCreateTags(tags));
return imageRepository.save(image);
}
/**
* Finds each picture belonging to a specific user
* @param user
......@@ -91,25 +86,6 @@ public class ImageService {
return imageRepository.findAll();
}
/**
* Adds the given tag to the given album.
*
* @param image the album to add the tag to
* @param tag the tag to add
* @return the updated album
*/
public Optional<Image> addTagToImage(Image image, Tag tag) {
Image foundImage = imageRepository.findById(image.getId())
.orElseThrow(IllegalArgumentException::new);
Tag foundTag = tagRepository.findOrCreate(tag)
.orElseThrow(IllegalArgumentException::new);
foundImage.addTag(foundTag);
return imageRepository.save(foundImage);
}
/**
* Search all images by tags specified in {@link ImageFilter#filter(String)}.
*
......@@ -124,4 +100,7 @@ public class ImageService {
.filter(ImageFilter.filter(query))
.collect(Collectors.toList());
}
}
package NTNU.IDATT1002.service;
import NTNU.IDATT1002.models.Image;
import NTNU.IDATT1002.models.Tag;
import NTNU.IDATT1002.repository.ImageRepository;
import NTNU.IDATT1002.repository.TagRepository;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.persistence.EntityManager;
/**
......@@ -16,6 +22,15 @@ import java.util.stream.Stream;
*/
public class TagService {
TagRepository tagRepository;
ImageRepository imageRepository;
public TagService(EntityManager entityManager){
this.tagRepository = new TagRepository(entityManager);
this.imageRepository = new ImageRepository(entityManager);
}
/**
* Retrieves tags from text field and converts them to a list of tag objects.
*
......@@ -42,4 +57,35 @@ public class TagService {
.map(Tag::getName)
.collect(Collectors.joining(", "));
}
/**
* Gets or creates given tags in given ArrayList.
*
* @@author Lars Østby
* @param tags the list of tags
* @return an ArrayList of persisted tags
*/
public List<Tag> getOrCreateTags(List<Tag> tags) {
return tags.stream().map(tag -> tagRepository.findOrCreate(tag).orElse(null)).collect(Collectors.toList());
}
/**
* Adds the given tag to the given album.
*
* @param image the album to add the tag to
* @param tag the tag to add
* @return the updated album
*/
public Optional<Image> addTagToImage(Image image, Tag tag) {
Image foundImage = imageRepository.findById(image.getId())
.orElseThrow(IllegalArgumentException::new);
Tag foundTag = tagRepository.findOrCreate(tag)
.orElseThrow(IllegalArgumentException::new);
foundImage.addTag(foundTag);
return imageRepository.save(foundImage);
}
}
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