Skip to content

Commit

Permalink
good work
Browse files Browse the repository at this point in the history
  • Loading branch information
fung-csf committed Mar 21, 2023
1 parent dedfb50 commit afa3a12
Show file tree
Hide file tree
Showing 84 changed files with 4,034 additions and 3,469 deletions.
217 changes: 217 additions & 0 deletions .metadata/.log

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
#MyEclipse Usage Data
#Sun Mar 19 22:35:06 SGT 2023
#Tue Mar 21 11:34:38 SGT 2023
misc/arch=x86_64
view/org.eclipse.pde.runtime.LogView=3
misc/eclipseVersion/4.17.2.202301310704=1
bundle/com.genuitec.eclipse.startup.workspace=553
bundle/com.genuitec.eclipse.theming.ui=553
bundle/com.genuitec.eclipse.startup.workspace=556
bundle/com.genuitec.eclipse.theming.ui=556
bundle/com.genuitec.eclipse.theming.elevation.win32=-1
view/org.eclipse.ui.console.ConsoleView=236
bundle/com.genuitec.eclipse.theming.css=-1
editor/org.eclipse.wst.css.core.csssource.source=25
perspective/org.eclipse.egit.ui.GitRepositoryExploring=1
view/org.eclipse.ui.navigator.ProjectExplorer=1
misc/timestamp=Mar 19, 2023, 2\:35\:06 PM
misc/timestamp=Mar 21, 2023, 3\:34\:38 AM
editor/org.eclipse.ui.genericeditor.GenericEditor=225
editor/org.eclipse.jdt.ui.CompilationUnitEditor=1856
bundle/com.genuitec.eclipse.theming.scrollbar=553
bundle/com.genuitec.eclipse.theming.base=553
editor/org.eclipse.jdt.ui.CompilationUnitEditor=1866
bundle/com.genuitec.eclipse.theming.scrollbar=556
bundle/com.genuitec.eclipse.theming.base=556
devstyle/state/iconsColor/primary=1
bundle/com.genuitec.eclipse.inlinesearch=553
bundle/com.genuitec.eclipse.monitor=553
bundle/com.genuitec.eclipse.inlinesearch=556
bundle/com.genuitec.eclipse.monitor=556
devstyle/state/enabled=1
editor/org.eclipse.jdt.ui.ClassFileEditor=42
bundle/com.genuitec.eclipse.webclipse.evergreen=553
editor/SpringBootPropertyEditor=75
bundle/com.genuitec.eclipse.webclipse.evergreen=556
editor/SpringBootPropertyEditor=76
misc/product/org.springframework.boot.ide.branding.sts4=1
bundle/com.genuitec.eclipse.news=-1
bundle/com.genuitec.eclipse.theming.scrollbar.win=-1
misc/period=1294406742
misc/period=1299123366
bundle/com.genuitec.eclipse.theming.icon.designer=-1
devstyle/state/inlinesearch=1
bundle/com.genuitec.eclipse.ui.common.platform=623
misc/count=623
bundle/com.genuitec.eclipse.ui.common.platform=627
misc/count=627
view/org.eclipse.ui.views.ProblemView=5
bundle/com.genuitec.eclipse.meexplorer=553
bundle/com.genuitec.eclipse.meexplorer=556
misc/productType/devstyle=1
misc/os=win32
bundle/com.genuitec.eclipse.meexplorer.jdt=553
editor/org.eclipse.wst.html.core.htmlsource.source=1389
bundle/com.genuitec.eclipse.meexplorer.jdt=556
editor/org.eclipse.wst.html.core.htmlsource.source=1390
devstyle/state/workbenchColor/Dark_Gray=1
view/org.eclipse.jdt.ui.PackageExplorer=750
bundle/com.genuitec.eclipse.webicons=623
view/org.eclipse.jdt.ui.PackageExplorer=752
bundle/com.genuitec.eclipse.webicons=627
misc/core_version=<unknown>
view/org.eclipse.help.ui.HelpView=1
view/org.eclipse.ui.views.ContentOutline=2
Expand All @@ -51,19 +51,19 @@ editor/org.eclipse.m2e.editor.MavenPomEditor=251
misc/ws=win32
view/org.eclipse.jdt.junit.ResultView=34
view/org.eclipse.egit.ui.RepositoriesView=1
bundle/com.genuitec.eclipse.patches=623
bundle/com.genuitec.eclipse.startup=553
bundle/com.genuitec.eclipse.patches=627
bundle/com.genuitec.eclipse.startup=556
view/org.eclipse.search.ui.views.SearchView=4
devstyle/state/editorColor/IntelliJ_IDEA_Dark=1
bundle/com.genuitec.eclipse.theming.base.win=-1
misc/installmode/standalone=0
misc/locale=en_US
misc/workspace_hash=-2114249626
view/org.springframework.ide.eclipse.boot.dash.views.BootDashView=179
perspective/org.eclipse.jdt.ui.JavaPerspective=73
view/org.springframework.ide.eclipse.boot.dash.views.BootDashView=180
perspective/org.eclipse.jdt.ui.JavaPerspective=74
bundle/com.genuitec.eclipse.theming.epl=-1
bundle/com.genuitec.eclipsecolortheme.api=623
bundle/com.genuitec.eclipsecolortheme.api=627
view/org.eclipse.wst.server.ui.ServersView=1
bundle/com.genuitec.eclipse.theming.core=553
bundle/com.genuitec.eclipse.core.common.platform=623
bundle/com.genuitec.eclipse.theming.core=556
bundle/com.genuitec.eclipse.core.common.platform=627
view/org.eclipse.debug.ui.DebugView=3
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);
}
}

}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit afa3a12

Please sign in to comment.