2020-04-29 22:44:16 +02:00
package org.hso.ecommerce.controller.intern ;
2020-04-28 22:41:29 +02:00
2020-05-09 23:01:25 +02:00
import org.hso.ecommerce.entities.shop.Article ;
2020-05-15 18:13:12 +02:00
import org.hso.ecommerce.entities.shop.Category ;
2020-05-23 10:45:14 +02:00
import org.hso.ecommerce.entities.shop.Image ;
2020-05-19 16:30:27 +02:00
import org.hso.ecommerce.entities.supplier.ArticleOffer ;
2020-05-09 23:01:25 +02:00
import org.hso.ecommerce.repos.shop.ArticleRepository ;
2020-05-15 18:13:12 +02:00
import org.hso.ecommerce.repos.shop.CategoryRepository ;
2020-05-23 10:45:14 +02:00
import org.hso.ecommerce.repos.shop.ImageRepository ;
2020-05-19 16:30:27 +02:00
import org.hso.ecommerce.repos.shop.OffersRepository ;
2020-05-10 15:26:50 +02:00
import org.hso.ecommerce.repos.warehouse.WarehouseBookingPositionSlotEntryRepository ;
2020-05-09 23:01:25 +02:00
import org.springframework.beans.factory.annotation.Autowired ;
2020-04-28 22:41:29 +02:00
import org.springframework.stereotype.Controller ;
2020-05-09 23:01:25 +02:00
import org.springframework.ui.Model ;
2020-05-23 14:18:33 +02:00
import org.springframework.util.DigestUtils ;
2020-05-09 23:01:25 +02:00
import org.springframework.web.bind.annotation.* ;
2020-05-23 14:18:33 +02:00
import org.springframework.web.multipart.MultipartFile ;
2020-05-11 15:32:39 +02:00
import org.springframework.web.servlet.view.RedirectView ;
2020-05-09 23:01:25 +02:00
2020-06-01 20:46:00 +02:00
import javax.imageio.ImageIO ;
import java.awt.image.BufferedImage ;
import java.io.File ;
import java.io.IOException ;
import java.nio.file.Files ;
2020-06-16 23:38:35 +02:00
import java.nio.file.Path ;
2020-06-01 20:46:00 +02:00
import java.nio.file.Paths ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.Optional ;
2020-05-10 15:26:50 +02:00
@Controller
2020-05-11 14:32:07 +02:00
@RequestMapping ( " intern/articles " )
2020-05-11 11:58:26 +02:00
public class InternArticleController {
2020-05-10 15:26:50 +02:00
@Autowired
private final ArticleRepository articleRepository = null ;
2020-05-09 23:01:25 +02:00
2020-05-10 15:26:50 +02:00
@Autowired
private final WarehouseBookingPositionSlotEntryRepository warehouseEntryRepository = null ;
2020-05-09 23:01:25 +02:00
2020-05-15 18:13:12 +02:00
@Autowired
private final CategoryRepository categoryRepository = null ;
2020-05-19 16:30:27 +02:00
@Autowired
private final OffersRepository offersRepository = null ;
2020-05-23 10:45:14 +02:00
@Autowired
private final ImageRepository imageRepository = null ;
2020-05-10 15:26:50 +02:00
@GetMapping ( " / " )
2020-05-11 11:58:26 +02:00
public String internListedArticles ( Model model ) {
2020-05-10 15:26:50 +02:00
2020-05-14 22:16:06 +02:00
List < UImodelArticles > totals = new ArrayList < UImodelArticles > ( ) ;
2020-05-10 15:26:50 +02:00
for ( Article article : articleRepository . findAll ( ) ) {
2020-05-14 22:16:06 +02:00
UImodelArticles tmp = new UImodelArticles ( ) ;
2020-06-20 23:24:06 +02:00
tmp . addListedArticle ( article , warehouseEntryRepository
. getByArticle ( article . id )
. stream ( )
. mapToInt ( e - > e . newSumSlot )
. sum ( ) ) ;
2020-05-10 15:26:50 +02:00
totals . add ( tmp ) ;
}
model . addAttribute ( " ListedArticles " , totals ) ;
return " intern/listedArticles/index " ;
}
@GetMapping ( " /{id} " )
2020-05-11 11:58:26 +02:00
public String internListedArticlesId ( Model model , @PathVariable String id ) {
2020-06-20 23:24:06 +02:00
long articleId = Long . parseLong ( id ) ;
2020-05-14 22:16:06 +02:00
UImodelArticle total = new UImodelArticle ( ) ;
2020-06-20 23:24:06 +02:00
total . addArticle (
articleRepository . findById ( articleId ) . get ( ) ,
warehouseEntryRepository . getByArticle ( articleId )
. stream ( )
. mapToInt ( e - > e . newSumSlot )
. sum ( ) ) ;
2020-05-12 22:52:01 +02:00
2020-05-11 11:58:26 +02:00
model . addAttribute ( " ArticleID " , total ) ;
2020-05-10 15:26:50 +02:00
return " intern/listedArticles/id " ;
}
2020-05-12 22:52:01 +02:00
2020-05-11 15:32:39 +02:00
@PostMapping ( " /{id}/saveChanges " )
2020-05-19 16:30:27 +02:00
public RedirectView saveChanges ( @PathVariable ( required = true ) int id ,
@RequestParam ( value = " title " , required = true ) String title ,
@RequestParam ( value = " description " , required = true ) String description ,
@RequestParam ( value = " units-per-slot " , required = true ) String warehouseUnitsPerSlot ,
2020-06-05 18:05:10 +02:00
@RequestParam ( value = " priceNet " , required = true ) String pricenetto ,
2020-05-19 16:30:27 +02:00
@RequestParam ( value = " reorderMaxPrice " , required = true ) String reorderMaxPrice ,
@RequestParam ( value = " autobuy " , required = true ) Boolean shouldReorder ,
2020-05-27 21:43:26 +02:00
@RequestParam ( value = " categorie " , required = true ) String categories ,
2020-05-23 14:18:33 +02:00
@RequestParam ( value = " img " , required = true ) MultipartFile imgFile ) {
2020-05-19 16:30:27 +02:00
Article tmpArticle = articleRepository . findArticleById ( id ) ; // get the old article
2020-05-12 22:52:01 +02:00
2020-05-15 19:45:10 +02:00
String [ ] separatedCategories = categories . split ( " \ n " ) ;
2020-05-15 18:13:12 +02:00
2020-05-19 16:30:27 +02:00
tmpArticle . categories . clear ( ) ;
2020-05-15 18:13:12 +02:00
2020-05-19 16:30:27 +02:00
// loop through all categories strings and create a new category if a new one;
2020-05-27 21:43:26 +02:00
// also adds the categories to the article
2020-05-15 18:13:12 +02:00
for ( String category : separatedCategories ) {
2020-05-19 16:30:27 +02:00
tmpArticle . categories . add ( categoryRepository . findCategoryByName ( category . trim ( ) )
2020-05-15 18:13:12 +02:00
. orElseGet ( ( ) - > new Category ( category . trim ( ) ) ) ) ;
}
2020-05-19 16:30:27 +02:00
tmpArticle . shouldReorder = shouldReorder ;
tmpArticle . reorderMaxPrice = ( int ) ( Float . parseFloat ( reorderMaxPrice ) * 100 ) ;
tmpArticle . shopPricePerUnitNetCent = ( int ) ( Float . parseFloat ( pricenetto ) * 100 ) ;
tmpArticle . warehouseUnitsPerSlot = Integer . parseInt ( warehouseUnitsPerSlot ) ;
tmpArticle . title = title ;
2020-05-23 14:18:33 +02:00
updateImage ( tmpArticle , imgFile ) ;
2020-05-19 16:30:27 +02:00
tmpArticle . description = description ;
2020-05-12 22:52:01 +02:00
2020-05-19 16:30:27 +02:00
articleRepository . save ( tmpArticle ) ; // save updated article
2020-05-15 18:13:12 +02:00
return new RedirectView ( " ../ " ) ; // return to overview page
2020-05-19 16:30:27 +02:00
}
@PostMapping ( " /addArticle/{id} " )
public RedirectView addArticle ( @PathVariable ( required = true ) String id ) {
// article is not already listed, create new one
2020-06-21 00:58:53 +02:00
long offeredArticleID = Long . parseLong ( id ) ;
2020-05-19 16:30:27 +02:00
Article tmpArticle = new Article ( ) ;
2020-06-21 00:58:53 +02:00
ArticleOffer offeredArticle = offersRepository . findById ( offeredArticleID ) . get ( ) ;
// Check for duplicates
Optional < Article > related = articleRepository . findArticleByArticleOffer ( offeredArticle ) ;
if ( related . isPresent ( ) ) {
return new RedirectView ( " ../ " + related . get ( ) . id ) ;
}
2020-05-19 16:30:27 +02:00
// set default values
tmpArticle . description = " " ;
tmpArticle . reorderMaxPrice = 0 ;
tmpArticle . shopPricePerUnitNetCent = offeredArticle . pricePerUnitNet ;
tmpArticle . shouldReorder = false ;
tmpArticle . title = offeredArticle . title ;
tmpArticle . warehouseUnitsPerSlot = 1 ;
2020-05-23 10:45:14 +02:00
setDefaultImage ( tmpArticle ) ;
2020-05-19 16:30:27 +02:00
tmpArticle . related = offeredArticle ;
articleRepository . save ( tmpArticle ) ; // save new article
// return to edit article page
return new RedirectView ( " ../ " + articleRepository . findArticleIDByRelatedID ( offeredArticleID ) . get ( ) ) ;
2020-05-12 22:52:01 +02:00
}
2020-05-10 15:26:50 +02:00
2020-05-23 14:18:33 +02:00
private void setDefaultImage ( Article article ) {
2020-05-23 10:45:14 +02:00
String defaultImagePath = " ./data/img/no_product_img.jpg " ; // path + name of default img
2020-05-23 14:18:33 +02:00
Optional < Integer > imageID = imageRepository . findImageIDByPath ( defaultImagePath ) ; // get default img
2020-05-23 10:45:14 +02:00
if ( imageID . isPresent ( ) ) {
// default img is in DB
2020-05-23 14:18:33 +02:00
article . image = imageRepository . findImageById ( imageID . get ( ) ) ; // set default img to new article
2020-05-23 10:45:14 +02:00
} else {
2020-05-23 14:18:33 +02:00
// default img is not in DB
2020-05-23 10:45:14 +02:00
File tmpFile = new File ( defaultImagePath ) ;
2020-05-23 14:18:33 +02:00
// test if default img file exits
2020-05-23 10:45:14 +02:00
if ( ! tmpFile . exists ( ) ) {
2020-06-01 10:53:10 +02:00
// fallback if the file not exists
2020-05-23 14:18:33 +02:00
// create new file
2020-06-01 10:53:10 +02:00
BufferedImage bufferedImage = new BufferedImage ( 422 , 428 , BufferedImage . TYPE_INT_RGB ) ;
2020-05-23 10:45:14 +02:00
try {
2020-05-23 14:18:33 +02:00
ImageIO . write ( bufferedImage , " jpg " , new File ( defaultImagePath ) ) ; // save new file on disk
2020-05-23 10:45:14 +02:00
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
2020-05-23 14:18:33 +02:00
Image defaultImage = new Image ( ) ;
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 {
2020-06-16 23:38:35 +02:00
Path targetPath = Paths . get ( " ./data/img/ " ) ;
if ( Files . notExists ( targetPath ) ) {
Files . createDirectories ( targetPath ) ;
}
2020-05-23 14:18:33 +02:00
// write new img file to disk
2020-06-16 23:38:35 +02:00
Files . newOutputStream ( Paths . get ( targetPath . toString ( ) , fileName ) ) . write ( imgFile . getBytes ( ) ) ;
2020-05-23 14:18:33 +02:00
// 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
}
2020-06-16 23:38:35 +02:00
} catch ( IOException e ) {
e . printStackTrace ( ) ;
2020-05-23 14:18:33 +02:00
setDefaultImage ( article ) ; // if upload failed, reset to default img
}
2020-05-23 10:45:14 +02:00
}
}
2020-05-14 22:16:06 +02:00
public static class UImodelArticles {
2020-05-10 15:26:50 +02:00
public String imgPath ;
public String title ;
public String price ;
2020-06-05 18:05:10 +02:00
public String priceNet ;
2020-05-10 15:26:50 +02:00
public String categorie ;
public int stock ;
2020-06-05 18:05:10 +02:00
public long offerID ;
2020-05-10 15:26:50 +02:00
public long id ;
2020-05-11 11:58:26 +02:00
void addListedArticle ( Article article , int stock ) {
2020-06-01 10:53:10 +02:00
if ( article . image ! = null ) {
this . imgPath = article . image . path ;
}
2020-05-11 11:58:26 +02:00
this . title = article . title ;
2020-06-05 18:05:10 +02:00
this . priceNet = String . format ( " %.2f " , ( ( float ) article . shopPricePerUnitNetCent / 100 ) ) ;
2020-05-11 11:58:26 +02:00
this . price = String . format ( " %.2f " , ( ( float ) article . getPriceGross ( ) / 100 ) ) ;
2020-05-19 16:30:27 +02:00
2020-05-15 18:13:12 +02:00
StringBuilder result = new StringBuilder ( ) ;
for ( Category temp : article . categories ) {
result . append ( temp . name + " " ) ;
}
2020-05-19 16:30:27 +02:00
this . categorie = result . toString ( ) ;
2020-05-15 18:13:12 +02:00
2020-05-11 11:58:26 +02:00
this . stock = stock ;
2020-06-05 18:05:10 +02:00
this . offerID = article . related . id ;
2020-05-11 11:58:26 +02:00
this . id = article . id ;
}
}
2020-05-14 22:16:06 +02:00
public static class UImodelArticle {
2020-05-11 11:58:26 +02:00
2020-05-15 19:47:26 +02:00
public String imgPath ;
public String title ;
public String price ;
2020-06-05 18:05:10 +02:00
public String priceNet ;
2020-05-15 19:47:26 +02:00
public String reorderMaxPrice ;
public String categorie ;
public int stock ;
2020-06-05 18:05:10 +02:00
public long offerID ;
2020-05-15 19:47:26 +02:00
public long id ;
public boolean shouldReorder ;
public String warehouseUnitsPerSlot ;
public String description ;
2020-06-01 20:46:00 +02:00
public int vatPercent ;
2020-05-19 16:30:27 +02:00
2020-05-12 22:52:01 +02:00
public String getCategorie ( ) {
return categorie ;
}
public String getDescription ( ) {
return description ;
}
2020-05-11 11:58:26 +02:00
void addArticle ( Article article , int stock ) {
2020-06-01 10:53:10 +02:00
if ( article . image ! = null ) {
this . imgPath = article . image . path ;
}
2020-05-10 15:26:50 +02:00
this . title = article . title ;
2020-06-05 18:05:10 +02:00
this . priceNet = String . format ( " %.2f " , ( ( float ) article . shopPricePerUnitNetCent / 100 ) ) ;
2020-05-11 11:58:26 +02:00
this . price = String . format ( " %.2f " , ( ( float ) article . getPriceGross ( ) / 100 ) ) ;
2020-05-19 16:30:27 +02:00
2020-05-15 18:13:12 +02:00
StringBuilder result = new StringBuilder ( ) ;
for ( Category temp : article . categories ) {
result . append ( temp . name ) ;
result . append ( " \ n " ) ;
}
2020-05-19 16:30:27 +02:00
this . categorie = result . toString ( ) ;
2020-05-10 15:26:50 +02:00
this . stock = stock ;
2020-06-05 18:05:10 +02:00
this . offerID = article . related . id ;
2020-05-10 15:26:50 +02:00
this . id = article . id ;
2020-05-14 22:16:06 +02:00
this . reorderMaxPrice = String . format ( " %.2f " , ( ( float ) article . reorderMaxPrice / 100 ) ) ;
2020-05-11 11:58:26 +02:00
this . shouldReorder = article . shouldReorder ;
2020-05-12 22:52:01 +02:00
this . warehouseUnitsPerSlot = String . valueOf ( article . warehouseUnitsPerSlot ) ;
2020-05-11 11:58:26 +02:00
this . description = article . description ;
2020-06-01 20:46:00 +02:00
this . vatPercent = article . related . vatPercent ;
2020-05-10 15:26:50 +02:00
}
}
2020-04-28 22:41:29 +02:00
}