From ac51f3952d5ef350cc9d1732f934a22d303e6880 Mon Sep 17 00:00:00 2001 From: Notmoo Date: Sun, 30 Jul 2017 17:44:31 +0200 Subject: [PATCH] Module Server : utilisation clss SaleContent dans process d'application d'une sale. Impl de ce process dans le StockService --- .../module/sale/NoRevertFileSaleDao.java | 5 +-- .../pqt/server/module/stock/FileStockDao.java | 32 ++++++++++++++++--- .../pqt/server/module/stock/IStockDao.java | 3 +- .../pqt/server/module/stock/StockService.java | 3 +- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/sale/NoRevertFileSaleDao.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/sale/NoRevertFileSaleDao.java index 62578ea3..ef6d6db4 100644 --- a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/sale/NoRevertFileSaleDao.java +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/sale/NoRevertFileSaleDao.java @@ -3,7 +3,8 @@ package com.pqt.server.module.sale; import com.pqt.core.entities.product.Product; import com.pqt.core.entities.sale.Sale; import com.pqt.server.module.stock.StockService; -import com.pqt.server.utils.FileUtil; +import com.pqt.server.tools.FileUtil; +import com.pqt.server.tools.entities.SaleContent; import org.apache.commons.io.input.ReversedLinesFileReader; import java.io.*; @@ -41,7 +42,7 @@ public class NoRevertFileSaleDao implements ISaleDao { return -1; long saleId = nextSaleId; - stockService.applySale(sale.getProducts()); + stockService.applySale(new SaleContent(sale)); logSale(sale, saleId); generateNextSaleId(); return saleId; diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/FileStockDao.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/FileStockDao.java index 658f9c24..d4c5f599 100644 --- a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/FileStockDao.java +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/FileStockDao.java @@ -1,7 +1,8 @@ package com.pqt.server.module.stock; import com.pqt.core.entities.product.Product; -import com.pqt.server.utils.FileUtil; +import com.pqt.server.tools.FileUtil; +import com.pqt.server.tools.entities.SaleContent; import java.io.*; import java.util.*; @@ -17,8 +18,7 @@ public class FileStockDao implements IStockDao { public FileStockDao() { random = new Random(); - products = new HashMap<>(); - load(); + loadFromFile(); generateNextProductId(); } @@ -82,8 +82,30 @@ public class FileStockDao implements IStockDao { } @Override - public void applySale(Map productAmounts) { - //TODO faire ça + public void applySale(SaleContent saleContent) throws IllegalArgumentException { + try { + saleContent.getProductList().forEach(product -> applyRecursiveStockRemoval(product, saleContent.getProductAmount(product))); + }catch (IllegalStateException e){ + loadFromFile(); + throw new IllegalArgumentException(e); + } + } + + private void applyRecursiveStockRemoval(Product product, int amount)throws IllegalStateException{ + Product correspondingProduct = getProduct(product.getId()); + if(correspondingProduct!=null) { + correspondingProduct.setAmountSold(correspondingProduct.getAmountSold() + amount); + correspondingProduct.setAmountRemaining(correspondingProduct.getAmountRemaining() - amount); + correspondingProduct.getComponents().forEach(component -> applyRecursiveStockRemoval(component, amount)); + }else{ + StringBuffer sb = new StringBuffer("StockService>StockDao : Un produit vendu ne correspond pas à un produit connu : "); + sb.append(product.getId()).append(" - ").append(product.getName()).append("(").append(product.getCategory()).append(")"); + throw new IllegalStateException(sb.toString()); + } + } + + private void loadFromFile() { + products = new HashMap<>(load()); } private Map load(){ diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/IStockDao.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/IStockDao.java index 3a8f7698..0cc9e1f8 100644 --- a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/IStockDao.java +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/IStockDao.java @@ -1,6 +1,7 @@ package com.pqt.server.module.stock; import com.pqt.core.entities.product.Product; +import com.pqt.server.tools.entities.SaleContent; import java.util.List; import java.util.Map; @@ -18,5 +19,5 @@ public interface IStockDao { public void modifyProduct(long id, Product product); - void applySale(Map productAmounts); + void applySale(SaleContent productAmounts) throws IllegalArgumentException; } diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/StockService.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/StockService.java index 3f538db2..03ebefa1 100644 --- a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/StockService.java +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/StockService.java @@ -3,6 +3,7 @@ package com.pqt.server.module.stock; import com.pqt.core.entities.product.Product; import com.pqt.core.entities.product.ProductUpdate; import com.pqt.server.exception.ServerQueryException; +import com.pqt.server.tools.entities.SaleContent; import java.util.List; import java.util.Map; @@ -25,7 +26,7 @@ public class StockService { return dao.getProduct(id); } - public void applySale(Map productAmounts) { + public void applySale(SaleContent productAmounts) { dao.applySale(productAmounts); }