From bb37258deb1e56e51f9a780a0cfd2b35f71f1860 Mon Sep 17 00:00:00 2001 From: Mads Lundegaard <madslun@stud.ntnu.no> Date: Mon, 23 Mar 2020 17:32:50 +0100 Subject: [PATCH] Added geo and histo repos --- .../IDATT1002/controllers/UploadedSingle.java | 6 +++++- src/main/java/NTNU/IDATT1002/models/Image.java | 4 ++++ .../repository/GeoLocatioRepository.java | 18 ++++++++++++++++++ .../repository/HistorgramRepository.java | 18 ++++++++++++++++++ .../NTNU/IDATT1002/service/ImageService.java | 6 ++++-- .../IDATT1002/utils/MetaDataExtractor.java | 6 +++++- 6 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 src/main/java/NTNU/IDATT1002/repository/GeoLocatioRepository.java create mode 100644 src/main/java/NTNU/IDATT1002/repository/HistorgramRepository.java diff --git a/src/main/java/NTNU/IDATT1002/controllers/UploadedSingle.java b/src/main/java/NTNU/IDATT1002/controllers/UploadedSingle.java index 850cc48a..5df00e9b 100644 --- a/src/main/java/NTNU/IDATT1002/controllers/UploadedSingle.java +++ b/src/main/java/NTNU/IDATT1002/controllers/UploadedSingle.java @@ -3,8 +3,10 @@ package NTNU.IDATT1002.controllers; import NTNU.IDATT1002.App; import NTNU.IDATT1002.ApplicationState; +import NTNU.IDATT1002.models.Tag; import NTNU.IDATT1002.service.ImageService; import java.io.File; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javafx.event.ActionEvent; @@ -16,6 +18,7 @@ import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.MouseEvent; +import javax.persistence.Table; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; @@ -137,7 +140,8 @@ public class UploadedSingle implements Initializable { applicationState = new ApplicationState(); List<File> list = App.ex.getUploadedFiles(); File fil = list.get(0); - imageService.createImage(applicationState.getCurrentUser(), fil); + ArrayList<Tag> tags = new ArrayList<>(); + imageService.createImage(applicationState.getCurrentUser(), fil, tags); /* list.stream().forEach(x -> { image = imageService.createImage(applicationState.getCurrentUser(), x).get(); List tags = tagStringSplit(photo_tag); diff --git a/src/main/java/NTNU/IDATT1002/models/Image.java b/src/main/java/NTNU/IDATT1002/models/Image.java index 676b4cdc..3fe27d5d 100644 --- a/src/main/java/NTNU/IDATT1002/models/Image.java +++ b/src/main/java/NTNU/IDATT1002/models/Image.java @@ -91,6 +91,10 @@ public class Image { return imageAlbums; } + public void addTags(ArrayList<Tag> tags) { + tags.addAll(tags); + } + public void addTag(Tag tag){ tags.add(tag); } diff --git a/src/main/java/NTNU/IDATT1002/repository/GeoLocatioRepository.java b/src/main/java/NTNU/IDATT1002/repository/GeoLocatioRepository.java new file mode 100644 index 00000000..f5962b0e --- /dev/null +++ b/src/main/java/NTNU/IDATT1002/repository/GeoLocatioRepository.java @@ -0,0 +1,18 @@ +package NTNU.IDATT1002.repository; + +import NTNU.IDATT1002.models.GeoLocation; + +import javax.persistence.EntityManager; + +public class GeoLocatioRepository extends GenericRepository<GeoLocation, Long> { + + /** + * Constructor to inject {@link EntityManager} dependency. + * + * @param entityManager the entity manager to utilize + */ + public GeoLocatioRepository(EntityManager entityManager) { + super(entityManager); + setClassType(GeoLocation.class); + } +} diff --git a/src/main/java/NTNU/IDATT1002/repository/HistorgramRepository.java b/src/main/java/NTNU/IDATT1002/repository/HistorgramRepository.java new file mode 100644 index 00000000..97c009d9 --- /dev/null +++ b/src/main/java/NTNU/IDATT1002/repository/HistorgramRepository.java @@ -0,0 +1,18 @@ +package NTNU.IDATT1002.repository; + +import NTNU.IDATT1002.models.Histogram; + +import javax.persistence.EntityManager; + +public class HistorgramRepository extends GenericRepository<Histogram, Long> { + + /** + * Constructor to inject {@link EntityManager} dependency. + * + * @param entityManager the entity manager to utilize + */ + public HistorgramRepository(EntityManager entityManager) { + super(entityManager); + setClassType(Histogram.class); + } +} diff --git a/src/main/java/NTNU/IDATT1002/service/ImageService.java b/src/main/java/NTNU/IDATT1002/service/ImageService.java index 3c18a9fd..b7089fd5 100644 --- a/src/main/java/NTNU/IDATT1002/service/ImageService.java +++ b/src/main/java/NTNU/IDATT1002/service/ImageService.java @@ -14,6 +14,7 @@ import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import java.io.File; +import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -49,10 +50,10 @@ public class ImageService { * @param file the file uploaded * @return Optional containing the saved image */ - public Optional<Image> createImage(User user, File file) { + public Optional<Image> createImage(User user, File file, ArrayList<Tag> tags) { Image image = new Image(); byte[] bFile = ImageUtil.convertToBytes(file.getPath()); - Metadata metadata = MetaDataExtractor.assembleMetaData(file); + Metadata metadata = MetaDataExtractor.assembleMetaData(file, image); metadata = metadataRepository.save(metadata).orElse(null); //TODO: Unsure what to do with imageAlbum @@ -61,6 +62,7 @@ public class ImageService { image.setUser(null); image.setMetadata(metadata); image.setPath(file.getPath()); + //image.addTags(tags); return imageRepository.save(image); } diff --git a/src/main/java/NTNU/IDATT1002/utils/MetaDataExtractor.java b/src/main/java/NTNU/IDATT1002/utils/MetaDataExtractor.java index aade3cb7..591f8e20 100644 --- a/src/main/java/NTNU/IDATT1002/utils/MetaDataExtractor.java +++ b/src/main/java/NTNU/IDATT1002/utils/MetaDataExtractor.java @@ -47,6 +47,7 @@ public class MetaDataExtractor { geoLocation.setLatitude(latitude); geoLocation.setLongitude(longitude); + } catch (NullPointerException e) { e.printStackTrace(); } @@ -67,11 +68,14 @@ public class MetaDataExtractor { return histogram; } - public static NTNU.IDATT1002.models.Metadata assembleMetaData(File file) { + public static NTNU.IDATT1002.models.Metadata assembleMetaData(File file, Image image) { + NTNU.IDATT1002.models.Metadata metadata = new NTNU.IDATT1002.models.Metadata(); try { + metadata.setImage(image); metadata.setGeoLocation(getGPS(file)); metadata.setHistogram(getHistorgram(file)); + } catch (Exception e) { e.printStackTrace(); -- GitLab