diff --git a/src/main/java/NTNU/IDATT1002/controllers/UploadedSingle.java b/src/main/java/NTNU/IDATT1002/controllers/UploadedSingle.java index 68a23d3fa4a847b98ece5ac5dafbc887c780fc43..52ac87342708a859fb86c3491ed52e0a9de1864d 100644 --- a/src/main/java/NTNU/IDATT1002/controllers/UploadedSingle.java +++ b/src/main/java/NTNU/IDATT1002/controllers/UploadedSingle.java @@ -1,6 +1,9 @@ package NTNU.IDATT1002.controllers; import NTNU.IDATT1002.App; +import NTNU.IDATT1002.ApplicationState; +import NTNU.IDATT1002.models.Tag; +import NTNU.IDATT1002.service.ImageService; import javafx.event.ActionEvent; import javafx.fxml.Initializable; import javafx.scene.control.Button; @@ -10,110 +13,142 @@ import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.MouseEvent; +import java.io.File; import java.io.IOException; import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.ResourceBundle; /** * Controls the buttons and changeable elements on upload_single.fxml, * a page where you add descriptions to your selected image + * * @version 1.0 22.03.2020 */ public class UploadedSingle implements Initializable { - - public ImageView tbar_logo; - public TextField tbar_search; - public Button tbar_searchBtn; - public Button tbar_explore; - public Button tbar_map; - public Button tbar_upload; - - public TextField photo_title; - public TextField photo_tag; - public TextArea photo_desc; - public ImageView photo_image; - - public Button acceptBtn; - public Button tbar_albums; - - - /** - * Method that runs when the controller is loaded - * Sets the image url on the page to be the uploaded images url - * @param location - * @param resources - */ - public void initialize(URL location, ResourceBundle resources) { - photo_image.setImage(new Image(App.ex.getUploadedFiles().get(0).toURI().toString())); - } - - /** - * Method that changes scene to Main page - * @param mouseEvent - * @throws IOException - */ - public void switchToMain(MouseEvent mouseEvent) throws IOException { - App.setRoot("main"); - } - - /** - * Method that changes scene to Search page. It reads the value of the search - * field and if not empty it is passed to dataexchange - * @param actionEvent - * @throws IOException - */ - public void switchToSearch(ActionEvent actionEvent) throws IOException { - if (!tbar_search.getText().isEmpty()){ - App.ex.setSearchField(tbar_search.getText()); - } - App.setRoot("search"); - } - - /** - * Method that changes scene to Explore page - * @param actionEvent - * @throws IOException - */ - public void switchToExplore(ActionEvent actionEvent) throws IOException { - App.setRoot("explore"); - } - - /** - * Method that changes scene to Albums page - * @param actionEvent - * @throws IOException - */ - public void switchToAlbums(ActionEvent actionEvent) throws IOException { - App.setRoot("explore_albums"); - } - - /** - * Method that changes scene to Map page - * @param actionEvent - * @throws IOException - */ - public void switchToMap(ActionEvent actionEvent) throws IOException { - App.setRoot("map"); - } - - /** - * Method that changes scene to Upload page - * @param actionEvent - * @throws IOException - */ - public void switchToUpload(ActionEvent actionEvent) throws IOException { - App.setRoot("upload"); - } - - /** - * Method for uploading image to database with title, tags and description - * Image itself is not stored but URL is - * @param actionEvent - * @throws IOException - */ - public void uploadSingle(ActionEvent actionEvent) throws IOException { - //TODO: write method to accept and upload the photo with chosen settings, titles... - App.setRoot("main"); + ImageService imageService; + + public ImageView tbar_logo; + public TextField tbar_search; + public Button tbar_searchBtn; + public Button tbar_explore; + public Button tbar_map; + public Button tbar_upload; + + public TextField photo_title; + public TextField photo_tag; + public TextArea photo_desc; + public ImageView photo_image; + + public Button acceptBtn; + public Button tbar_albums; + + + /** + * Method that runs when the controller is loaded + * Sets the image url on the page to be the uploaded images url + * + * @param location + * @param resources + */ + public void initialize(URL location, ResourceBundle resources) { + photo_image.setImage(new Image(App.ex.getUploadedFiles().get(0).toURI().toString())); + imageService = new ImageService(); + } + + /** + * Method that changes stage to Main page + * + * @param mouseEvent + * @throws IOException + */ + public void switchToMain(MouseEvent mouseEvent) throws IOException { + App.setRoot("main"); + } + + /** + * Method that changes stage to Search page. It reads the value of the search + * field and if not empty it is passed to dataexchange + * + * @param actionEvent + * @throws IOException + */ + public void switchToSearch(ActionEvent actionEvent) throws IOException { + if (!tbar_search.getText().isEmpty()) { + App.ex.setSearchField(tbar_search.getText()); } + App.setRoot("search"); + } + + /** + * Method that changes stage to Explore page + * + * @param actionEvent + * @throws IOException + */ + public void switchToExplore(ActionEvent actionEvent) throws IOException { + App.setRoot("explore"); + } + + /** + * Method that changes stage to Albums page + * + * @param actionEvent + * @throws IOException + */ + public void switchToAlbums(ActionEvent actionEvent) throws IOException { + App.setRoot("explore_albums"); + } + + /** + * Method that changes stage to Map page + * + * @param actionEvent + * @throws IOException + */ + public void switchToMap(ActionEvent actionEvent) throws IOException { + App.setRoot("map"); + } + + /** + * Method that changes stage to Upload page + * + * @param actionEvent + * @throws IOException + */ + public void switchToUpload(ActionEvent actionEvent) throws IOException { + App.setRoot("upload"); + } + + /** + * Method for uploading image to database with tags + * Image itself is not stored but URL is + * + * @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); + + 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("(?=#)")); + + + } } diff --git a/src/main/java/NTNU/IDATT1002/models/GeoLocation.java b/src/main/java/NTNU/IDATT1002/models/GeoLocation.java index 17883cebacd8104b66f59eab7bbc6204491a0e26..39e282c8e1c35e86aeff26124d0c469d8cca7beb 100644 --- a/src/main/java/NTNU/IDATT1002/models/GeoLocation.java +++ b/src/main/java/NTNU/IDATT1002/models/GeoLocation.java @@ -23,8 +23,7 @@ public class GeoLocation { * One to one relation between geolocationId in table Geolocation * Joins column geolocation_id in metadata */ - @OneToOne - @JoinColumn(name = "metadata_id") + @OneToOne(mappedBy = "geolocation") private Metadata metadata; @NotBlank (message = "Altitude may not be blank") @@ -70,4 +69,8 @@ public class GeoLocation { public void setLongitude(String longitude) { this.longitude = longitude; } + + public void setMetadata(Metadata metadata) { + this.metadata = metadata; + } } diff --git a/src/main/java/NTNU/IDATT1002/models/Histogram.java b/src/main/java/NTNU/IDATT1002/models/Histogram.java index dc2680fb90a46f9f1869fabbecf55770253c74cd..c6e74f902a261f81dd9d208b4a1e6635b4424c2d 100644 --- a/src/main/java/NTNU/IDATT1002/models/Histogram.java +++ b/src/main/java/NTNU/IDATT1002/models/Histogram.java @@ -23,25 +23,16 @@ public class Histogram { * One to one relations, joins histogramId * On columns histogramId in metadata */ - @OneToOne - @JoinColumn(name = "metadata_id") + @OneToOne(mappedBy = "histogram") private Metadata metadata; - @NotBlank (message = "Data may not be blank") + @Lob + @NotBlank(message = "Data may not be blank") private String data; public Histogram() { } - /** - * Constrtuctor for Histogram, taking in both histogramId and data - * - * @param data - */ - public Histogram(String data){ - this.data = data; - } - public Long getHistogramId() { return histogramId; } @@ -58,4 +49,7 @@ public class Histogram { this.data = data; } + public void setMetadata(Metadata metadata) { + this.metadata = metadata; + } } diff --git a/src/main/java/NTNU/IDATT1002/models/Image.java b/src/main/java/NTNU/IDATT1002/models/Image.java index 676b4cdca79f712c4317d6df76372ac33f4c0478..4b025ee3e42db5fc7c613556ddcb409b32e0a4bb 100644 --- a/src/main/java/NTNU/IDATT1002/models/Image.java +++ b/src/main/java/NTNU/IDATT1002/models/Image.java @@ -38,7 +38,7 @@ public class Image { @NotEmpty private byte[] rawImage; - @OneToOne + @OneToOne(cascade = CascadeType.ALL) private Metadata metadata; @NotBlank @@ -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/models/Metadata.java b/src/main/java/NTNU/IDATT1002/models/Metadata.java index 7276f0fc124ffb2930296c42786ddae87b1d3b3c..45adbc7280f159fc52edd64f3e0b1621caed7c1d 100644 --- a/src/main/java/NTNU/IDATT1002/models/Metadata.java +++ b/src/main/java/NTNU/IDATT1002/models/Metadata.java @@ -22,14 +22,13 @@ public class Metadata { * One to one relation joining imageId * on image_id column in image */ - @OneToOne - @JoinColumn(name = "image_id") + @OneToOne(mappedBy = "metadata") private Image image; - @OneToOne - private GeoLocation geoLocation; + @OneToOne(cascade = CascadeType.ALL) + private GeoLocation geolocation; - @OneToOne + @OneToOne(cascade = CascadeType.ALL) private Histogram histogram; public Metadata() { @@ -37,7 +36,7 @@ public class Metadata { public Metadata(Image image, GeoLocation geoLocation, Histogram histogram) { this.image = image; - this.geoLocation = geoLocation; + this.geolocation = geolocation; this.histogram = histogram; } @@ -50,7 +49,7 @@ public class Metadata { } public GeoLocation getGeoLocation() { - return geoLocation; + return geolocation; } public Histogram getHistogram() { @@ -61,8 +60,8 @@ public class Metadata { this.image = image; } - public void setGeoLocation(GeoLocation geoLocation) { - this.geoLocation = geoLocation; + public void setGeoLocation(GeoLocation geolocation) { + this.geolocation = geolocation; } public void setHistogram(Histogram histogram) { 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 0000000000000000000000000000000000000000..f5962b0e60c659a92b6c2bcc0b604671c0ad6cfc --- /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 0000000000000000000000000000000000000000..97c009d94a4920f2466a0ac8a6c7803d9fa76dae --- /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 3c18a9fd0a4019fad05035998b1d56e3859ddae5..93b556793dd1032a328368b64a67a681960617c3 100644 --- a/src/main/java/NTNU/IDATT1002/service/ImageService.java +++ b/src/main/java/NTNU/IDATT1002/service/ImageService.java @@ -1,11 +1,6 @@ package NTNU.IDATT1002.service; -import NTNU.IDATT1002.models.Image; -import NTNU.IDATT1002.models.Metadata; -import NTNU.IDATT1002.models.Tag; -import NTNU.IDATT1002.models.User; -import NTNU.IDATT1002.repository.ImageRepository; -import NTNU.IDATT1002.repository.MetadataRepository; -import NTNU.IDATT1002.repository.TagRepository; +import NTNU.IDATT1002.models.*; +import NTNU.IDATT1002.repository.*; import NTNU.IDATT1002.service.filters.ImageFilter; import NTNU.IDATT1002.utils.ImageUtil; import NTNU.IDATT1002.utils.MetaDataExtractor; @@ -14,6 +9,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; @@ -29,6 +25,9 @@ public class ImageService { private ImageRepository imageRepository; private MetadataRepository metadataRepository; private TagRepository tagRepository; + private HistorgramRepository historgramRepository; + private GeoLocatioRepository geoLocatioRepository; + private MetaDataExtractor metaDataExtractor; /** * Inject entity manager instance to the repositories. @@ -40,6 +39,10 @@ public class ImageService { 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(); + } /** @@ -49,18 +52,30 @@ 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) { + + GeoLocation geoLocation = metaDataExtractor.getGeoLocation(file); + Histogram histogram = metaDataExtractor.getHistogram(file); + Image image = new Image(); + Metadata metadata = new Metadata(); + metadata.setImage(image); + image.setMetadata(metadata); + + metadata.setGeoLocation(geoLocation); + geoLocation.setMetadata(metadata); + + metadata.setHistogram(histogram); + histogram.setMetadata(metadata); + + byte[] bFile = ImageUtil.convertToBytes(file.getPath()); - Metadata metadata = MetaDataExtractor.assembleMetaData(file); - metadata = metadataRepository.save(metadata).orElse(null); - //TODO: Unsure what to do with imageAlbum + //TODO: Add image tags and add image to imageAlbum image.setRawImage(bFile); image.setUser(user); - image.setUser(null); - image.setMetadata(metadata); image.setPath(file.getPath()); +// image.addTags(tags); return imageRepository.save(image); } @@ -109,7 +124,7 @@ public class ImageService { */ //This search method is for futureproofing, when we will search using additional parameters than just tags - public List<Image> searchImageAlbums(String query) { + public List<Image> searchImages(String query) { List<Image> allImages = imageRepository.findAll(); return allImages.stream() .filter(ImageFilter.filter(query)) diff --git a/src/main/java/NTNU/IDATT1002/utils/MetaDataExtractor.java b/src/main/java/NTNU/IDATT1002/utils/MetaDataExtractor.java index aade3cb76f3c56214127a2474e432a69b741fd66..04236251eef235d91de98c707b13d61e68c1f7a7 100644 --- a/src/main/java/NTNU/IDATT1002/utils/MetaDataExtractor.java +++ b/src/main/java/NTNU/IDATT1002/utils/MetaDataExtractor.java @@ -2,26 +2,37 @@ package NTNU.IDATT1002.utils; import NTNU.IDATT1002.models.GeoLocation; import NTNU.IDATT1002.models.Histogram; -import NTNU.IDATT1002.models.Image; +import NTNU.IDATT1002.repository.GeoLocatioRepository; +import NTNU.IDATT1002.repository.HistorgramRepository; import com.drew.imaging.ImageMetadataReader; import com.drew.imaging.ImageProcessingException; -import com.drew.metadata.Directory; import com.drew.metadata.Metadata; import com.drew.metadata.MetadataException; -import com.drew.metadata.Tag; -import com.drew.metadata.exif.*; -import com.drew.metadata.jpeg.JpegDirectory; +import com.drew.metadata.exif.GpsDirectory; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; +/** + * Class MetaDataExtractor. Extracts metadata and geolocation and histogram from it. + */ public class MetaDataExtractor { + private GeoLocatioRepository geoLocationRepository; + private HistorgramRepository historgramRepository; + + public MetaDataExtractor() { + EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ImageApplication"); + EntityManager entityManager = entityManagerFactory.createEntityManager(); + + this.historgramRepository = new HistorgramRepository(entityManager); + this.geoLocationRepository = new GeoLocatioRepository(entityManager); + } + /** * Returns a string with the GPS position * @@ -30,7 +41,7 @@ public class MetaDataExtractor { * @throws IOException * @throws MetadataException */ - private static GeoLocation getGPS(File file) throws ImageProcessingException, IOException, MetadataException { + public GeoLocation getGeoLocation(File file) { String gps = ""; String latitude = ""; String longitude = ""; @@ -47,36 +58,42 @@ public class MetaDataExtractor { geoLocation.setLatitude(latitude); geoLocation.setLongitude(longitude); - } catch (NullPointerException e) { + } + catch (NullPointerException | ImageProcessingException | IOException e) { e.printStackTrace(); } return geoLocation; } - public static Histogram getHistorgram(File file) throws ImageProcessingException, IOException { - String text = ""; - Metadata metadata = ImageMetadataReader.readMetadata(file); + /** + * TODO: Decide what data to store. + * + * @param file + * @return + */ + public Histogram getHistogram(File file) { +// Metadata metadata = null; +// +// try { +// metadata = ImageMetadataReader.readMetadata(file); +// } catch (IOException | ImageProcessingException e) { +// e.printStackTrace(); +// } +// +// StringBuilder data = new StringBuilder(); +// assert metadata != null; +// for(Directory d : metadata.getDirectories()) { +// for (Tag t : d.getTags()) { +// data.append(t.toString()).append(" | "); +// } +// } +// histogram.setData(data.toString()); + + Histogram histogram = new Histogram(); + histogram.setData("INSERT DATA HERE"); - for(Directory d : metadata.getDirectories()) { - for (Tag t : d.getTags()) { - text += t.toString() + " | "; - } - } - histogram.setData(text); return histogram; } - public static NTNU.IDATT1002.models.Metadata assembleMetaData(File file) { - NTNU.IDATT1002.models.Metadata metadata = new NTNU.IDATT1002.models.Metadata(); - try { - metadata.setGeoLocation(getGPS(file)); - metadata.setHistogram(getHistorgram(file)); - } - catch (Exception e) { - e.printStackTrace(); - } - return metadata; - } - }