diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/intern/InternArticleController.java b/prototype/src/main/java/org/hso/ecommerce/controller/intern/InternArticleController.java index 7a7e7fe..f47f6ca 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/intern/InternArticleController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/intern/InternArticleController.java @@ -1,13 +1,21 @@ package org.hso.ecommerce.controller.intern; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Optional; + +import javax.imageio.ImageIO; import org.hso.ecommerce.entities.shop.Article; import org.hso.ecommerce.entities.shop.Category; +import org.hso.ecommerce.entities.shop.Image; import org.hso.ecommerce.entities.supplier.ArticleOffer; import org.hso.ecommerce.repos.shop.ArticleRepository; import org.hso.ecommerce.repos.shop.CategoryRepository; +import org.hso.ecommerce.repos.shop.ImageRepository; import org.hso.ecommerce.repos.shop.OffersRepository; import org.hso.ecommerce.repos.warehouse.WarehouseBookingPositionSlotEntryRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -31,6 +39,9 @@ public class InternArticleController { @Autowired private final OffersRepository offersRepository = null; + @Autowired + private final ImageRepository imageRepository = null; + @GetMapping("/") public String internListedArticles(Model model) { @@ -114,15 +125,41 @@ public class InternArticleController { tmpArticle.shouldReorder = false; tmpArticle.title = offeredArticle.title; tmpArticle.warehouseUnitsPerSlot = 1; - tmpArticle.image = articleRepository.findAll().get(0).image; // TODO set any static default image + setDefaultImage(tmpArticle); tmpArticle.related = offeredArticle; - articleRepository.save(tmpArticle); // save new article // return to edit article page return new RedirectView("../" + articleRepository.findArticleIDByRelatedID(offeredArticleID).get()); } + private void setDefaultImage(Article tmpArticle) { + String defaultImagePath = "./data/img/no_product_img.jpg"; // path + name of default img + Optional imageID = imageRepository.findImageByPath(defaultImagePath); // get default img + + if (imageID.isPresent()) { + // default img is in DB + tmpArticle.image = imageRepository.findImageById(imageID.get()); // set default img to new article + } else { + //default img is not in DB + File tmpFile = new File(defaultImagePath); + //test if default img file exits + if (!tmpFile.exists()) { + //fallback if the file not exists + BufferedImage bufferedImage = new BufferedImage(422, 428, BufferedImage.TYPE_INT_RGB); //create new file + try { + ImageIO.write(bufferedImage, "jpg", new File(defaultImagePath)); //save new file on disk + } catch (IOException e) { + e.printStackTrace(); + } + } + Image defaultImage = new Image(); + defaultImage.path = defaultImagePath; //set new file to default img + imageRepository.save(defaultImage); //save default img + tmpArticle.image = defaultImage; //set default img to new article + } + } + public static class UImodelArticles { public String imgPath; diff --git a/prototype/src/main/java/org/hso/ecommerce/repos/shop/ImageRepository.java b/prototype/src/main/java/org/hso/ecommerce/repos/shop/ImageRepository.java new file mode 100644 index 0000000..bcef6be --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/repos/shop/ImageRepository.java @@ -0,0 +1,20 @@ +package org.hso.ecommerce.repos.shop; + +import org.hso.ecommerce.entities.shop.Image; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface ImageRepository extends JpaRepository { + + @Query("SELECT i.id FROM Image i WHERE i.path = :path") + Optional findImageByPath(@Param("path") String path); + + @Query("SELECT i FROM Image i WHERE i.id = :imageId") + Image findImageById(@Param("imageId") long imageId); + +} diff --git a/prototype/src/main/resources/static/img/no_product_img.jpg b/prototype/src/main/resources/static/img/no_product_img.jpg deleted file mode 100644 index f898b51..0000000 Binary files a/prototype/src/main/resources/static/img/no_product_img.jpg and /dev/null differ