set default img if article is added to listed articles
This commit is contained in:
		@ -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<Integer> 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;
 | 
			
		||||
 | 
			
		||||
@ -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<Image, Long> {
 | 
			
		||||
 | 
			
		||||
	@Query("SELECT i.id FROM Image i WHERE i.path = :path")
 | 
			
		||||
	Optional<Integer> findImageByPath(@Param("path") String path);
 | 
			
		||||
 | 
			
		||||
	@Query("SELECT i FROM Image i WHERE i.id = :imageId")
 | 
			
		||||
	Image findImageById(@Param("imageId") long imageId);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							| 
		 Before Width: | Height: | Size: 10 KiB  | 
		Reference in New Issue
	
	Block a user