-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
84 changed files
with
4,034 additions
and
3,469 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
.metadata/.plugins/org.eclipse.core.resources/.history/0/d062a7b996c7001d1642fcfeab6c1cbe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package com.shopme.admin.product; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import com.shopme.admin.FileUploadUtil; | ||
import com.shopme.common.entity.product.Product; | ||
import com.shopme.common.entity.product.ProductImage; | ||
|
||
public class ProductSaveHelper { | ||
|
||
|
||
/* | ||
* delete extra images in file directory that were removed during product | ||
* editing | ||
*/ | ||
private void deleteExtraImagesWeredRemovedOnForm(Product product) { | ||
|
||
String extraImageDir = "../product-images/" + product.getId() + "/extras"; | ||
Path dirPath = Paths.get(extraImageDir); | ||
|
||
try { | ||
Files.list(dirPath).forEach(file -> { | ||
|
||
String fileName = file.toFile().getName(); | ||
|
||
if (!product.containsImageName(fileName)) { | ||
|
||
try { | ||
Files.delete(file); | ||
LOGGER.info("deleted extra image: " + fileName); | ||
} catch (IOException ex) { | ||
LOGGER.error("could not delete extra image: " + fileName); | ||
} | ||
|
||
} | ||
}); | ||
|
||
} catch (IOException ex) { | ||
LOGGER.error("could not list directory: " + dirPath); | ||
} | ||
|
||
} | ||
|
||
private void setExistingExtraImageNames(String[] imageIDs, String[] imageNames, Product product) { | ||
|
||
if (imageIDs == null || imageIDs.length == 0) | ||
return; | ||
|
||
Set<ProductImage> images = new HashSet<>(); | ||
|
||
for (int count = 0; count < imageIDs.length; count++) { | ||
Integer id = Integer.parseInt(imageIDs[count]); | ||
String name = imageNames[count]; | ||
|
||
images.add(new ProductImage(id, name, product)); | ||
} | ||
|
||
product.setImages(images); | ||
} | ||
|
||
private void setProductDetails(String[] detailIDs, String[] detailNames, String[] detailValues, Product product) { | ||
|
||
if (detailNames == null || detailNames.length == 0) | ||
return; | ||
|
||
for (int count = 0; count < detailNames.length; count++) { | ||
String name = detailNames[count]; | ||
String value = detailValues[count]; | ||
Integer id = Integer.parseInt(detailIDs[count]); | ||
|
||
/* check if it is a new ProductDetail object, if yes, add it to product */ | ||
if (id != 0) { | ||
product.addDetail(id, name, value); | ||
|
||
} else if (!name.isEmpty() && !value.isEmpty()) { | ||
product.addDetail(name, value); | ||
} | ||
|
||
} | ||
} | ||
|
||
private void saveUploadedImages(MultipartFile mainImageMultipart, MultipartFile[] extraImageMultiparts, | ||
Product savedProduct) throws IOException { | ||
|
||
if (!mainImageMultipart.isEmpty()) { | ||
String fileName = StringUtils.cleanPath(mainImageMultipart.getOriginalFilename()); | ||
String uploadDir = "../product-images/" + savedProduct.getId(); | ||
|
||
FileUploadUtil.cleanDir(uploadDir); | ||
FileUploadUtil.saveFile(uploadDir, fileName, mainImageMultipart); | ||
} | ||
|
||
if (extraImageMultiparts.length > 0) { | ||
|
||
String uploadDir = "../product-images/" + savedProduct.getId() + "/extras"; | ||
|
||
for (MultipartFile multipartFile : extraImageMultiparts) { | ||
if (multipartFile.isEmpty()) | ||
continue; | ||
|
||
String fileName = StringUtils.cleanPath(multipartFile.getOriginalFilename()); | ||
FileUploadUtil.saveFile(uploadDir, fileName, multipartFile); | ||
} | ||
} | ||
|
||
} | ||
|
||
private void setNewExtraImageNames(MultipartFile[] extraImageMultiparts, Product product) { | ||
if (extraImageMultiparts.length > 0) { | ||
|
||
for (MultipartFile multipartFile : extraImageMultiparts) { | ||
if (!multipartFile.isEmpty()) { | ||
|
||
String fileName = StringUtils.cleanPath(multipartFile.getOriginalFilename()); | ||
|
||
/* check whether the product image exists in product before adding */ | ||
if (!product.containsImageName(fileName)) { | ||
product.addExtraImage(fileName); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void setMainImageName(MultipartFile mainImageMultipart, Product product) { | ||
|
||
if (!mainImageMultipart.isEmpty()) { | ||
String fileName = StringUtils.cleanPath(mainImageMultipart.getOriginalFilename()); | ||
product.setMainImage(fileName); | ||
} | ||
} | ||
|
||
} |
10 changes: 0 additions & 10 deletions
10
.metadata/.plugins/org.eclipse.core.resources/.history/11/d0c99f176cc1001d148ef1787cd440c5
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
.metadata/.plugins/org.eclipse.core.resources/.history/14/a073a9116cc1001d148ef1787cd440c5
This file was deleted.
Oops, something went wrong.
58 changes: 0 additions & 58 deletions
58
.metadata/.plugins/org.eclipse.core.resources/.history/16/60e6ae7886c1001d1e0ea9ad2947c26d
This file was deleted.
Oops, something went wrong.
56 changes: 0 additions & 56 deletions
56
.metadata/.plugins/org.eclipse.core.resources/.history/21/8044170686c1001d1e0ea9ad2947c26d
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.