|
|
|
@ -3,10 +3,16 @@ package org.hso.ecommerce.controller.intern;
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
|
|
|
|
import org.hso.ecommerce.entities.shop.Article;
|
|
|
|
@ -19,9 +25,12 @@ 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;
|
|
|
|
|
import org.springframework.security.crypto.bcrypt.BCrypt;
|
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.ui.Model;
|
|
|
|
|
import org.springframework.util.DigestUtils;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
import org.springframework.web.servlet.view.RedirectView;
|
|
|
|
|
|
|
|
|
|
@Controller
|
|
|
|
@ -80,12 +89,11 @@ public class InternArticleController {
|
|
|
|
|
@RequestParam(value = "price_netto", required = true) String pricenetto,
|
|
|
|
|
@RequestParam(value = "reorderMaxPrice", required = true) String reorderMaxPrice,
|
|
|
|
|
@RequestParam(value = "autobuy", required = true) Boolean shouldReorder,
|
|
|
|
|
@RequestParam(value = "categories", required = true) String categories) {
|
|
|
|
|
@RequestParam(value = "categories", required = true) String categories,
|
|
|
|
|
@RequestParam(value = "img", required = true) MultipartFile imgFile) {
|
|
|
|
|
|
|
|
|
|
Article tmpArticle = articleRepository.findArticleById(id); // get the old article
|
|
|
|
|
|
|
|
|
|
// TODO img
|
|
|
|
|
|
|
|
|
|
String[] separatedCategories = categories.split("\n");
|
|
|
|
|
|
|
|
|
|
tmpArticle.categories.clear();
|
|
|
|
@ -102,6 +110,7 @@ public class InternArticleController {
|
|
|
|
|
tmpArticle.shopPricePerUnitNetCent = (int) (Float.parseFloat(pricenetto) * 100);
|
|
|
|
|
tmpArticle.warehouseUnitsPerSlot = Integer.parseInt(warehouseUnitsPerSlot);
|
|
|
|
|
tmpArticle.title = title;
|
|
|
|
|
updateImage(tmpArticle, imgFile);
|
|
|
|
|
tmpArticle.description = description;
|
|
|
|
|
|
|
|
|
|
articleRepository.save(tmpArticle); // save updated article
|
|
|
|
@ -133,30 +142,56 @@ public class InternArticleController {
|
|
|
|
|
return new RedirectView("../" + articleRepository.findArticleIDByRelatedID(offeredArticleID).get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setDefaultImage(Article tmpArticle) {
|
|
|
|
|
private void setDefaultImage(Article article) {
|
|
|
|
|
String defaultImagePath = "./data/img/no_product_img.jpg"; // path + name of default img
|
|
|
|
|
Optional<Integer> imageID = imageRepository.findImageByPath(defaultImagePath); // get default img
|
|
|
|
|
Optional<Integer> imageID = imageRepository.findImageIDByPath(defaultImagePath); // get default img
|
|
|
|
|
|
|
|
|
|
if (imageID.isPresent()) {
|
|
|
|
|
// default img is in DB
|
|
|
|
|
tmpArticle.image = imageRepository.findImageById(imageID.get()); // set default img to new article
|
|
|
|
|
article.image = imageRepository.findImageById(imageID.get()); // set default img to new article
|
|
|
|
|
} else {
|
|
|
|
|
//default img is not in DB
|
|
|
|
|
// default img is not in DB
|
|
|
|
|
File tmpFile = new File(defaultImagePath);
|
|
|
|
|
//test if default img file exits
|
|
|
|
|
// 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
|
|
|
|
|
// fallback if the file not exists
|
|
|
|
|
// create new file
|
|
|
|
|
BufferedImage bufferedImage = new BufferedImage(422, 428, BufferedImage.TYPE_INT_RGB);
|
|
|
|
|
try {
|
|
|
|
|
ImageIO.write(bufferedImage, "jpg", new File(defaultImagePath)); //save new file on disk
|
|
|
|
|
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
|
|
|
|
|
defaultImage.path = defaultImagePath; // set new file to default img
|
|
|
|
|
imageRepository.save(defaultImage); // save default img
|
|
|
|
|
article.image = defaultImage; // set default img to new article
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateImage(Article article, MultipartFile imgFile) {
|
|
|
|
|
// check if a file is present
|
|
|
|
|
if (imgFile.getSize() > 0) {
|
|
|
|
|
try {
|
|
|
|
|
// get file name based on MD5 hash
|
|
|
|
|
String fileName = DigestUtils.md5DigestAsHex(imgFile.getBytes()) + ".jpg";
|
|
|
|
|
// check if img is already in system
|
|
|
|
|
Optional<Integer> imageID = imageRepository.findImageIDByPath("./data/img/" + fileName);
|
|
|
|
|
if (imageID.isPresent()) {
|
|
|
|
|
article.image = imageRepository.findImageById(imageID.get()); // add existing img to article
|
|
|
|
|
} else {
|
|
|
|
|
// write new img file to disk
|
|
|
|
|
Files.newOutputStream(Paths.get("./data/img/", fileName)).write(imgFile.getBytes());
|
|
|
|
|
// create new img
|
|
|
|
|
Image newImage = new Image();
|
|
|
|
|
newImage.path = "./data/img/" + fileName; // set new file to new img
|
|
|
|
|
imageRepository.save(newImage); // save new img
|
|
|
|
|
article.image = newImage; // set new img to article
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
setDefaultImage(article); // if upload failed, reset to default img
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|