diff --git a/Workspace/server/src/main/java/com/pqt/server/controller/SimpleMessageHandler.java b/Workspace/server/src/main/java/com/pqt/server/controller/SimpleMessageHandler.java index e60176bd..7c10ee58 100644 --- a/Workspace/server/src/main/java/com/pqt/server/controller/SimpleMessageHandler.java +++ b/Workspace/server/src/main/java/com/pqt/server/controller/SimpleMessageHandler.java @@ -7,6 +7,7 @@ import com.pqt.core.entities.messages.MessageType; import com.pqt.core.entities.product.LightweightProduct; import com.pqt.core.entities.product.Product; import com.pqt.core.entities.product.ProductUpdate; +import com.pqt.core.entities.sale.LightweightSale; import com.pqt.core.entities.sale.Sale; import com.pqt.core.entities.server_config.ServerConfig; import com.pqt.core.entities.user_account.Account; @@ -87,7 +88,7 @@ public class SimpleMessageHandler implements IMessageHandler { manager.supportForConnectedAccounts(MessageType.QUERY_SALE, (message)->{ Map fields = new HashMap<>(); try { - long saleId = saleService.submitSale(messageToolFactory.getObjectParser(Sale.class).parse(message.getField("sale"))); + long saleId = saleService.submitSale(messageToolFactory.getObjectParser(LightweightSale.class).parse(message.getField("lightweight_sale"))); fields.put("saleId", Long.toString(saleId)); return new Message(MessageType.ACK_SALE, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields); }catch(NullPointerException e){ diff --git a/Workspace/server/src/main/java/com/pqt/server/module/sale/ISaleDao.java b/Workspace/server/src/main/java/com/pqt/server/module/sale/ISaleDao.java index c26d7c86..6573f906 100644 --- a/Workspace/server/src/main/java/com/pqt/server/module/sale/ISaleDao.java +++ b/Workspace/server/src/main/java/com/pqt/server/module/sale/ISaleDao.java @@ -1,5 +1,6 @@ package com.pqt.server.module.sale; +import com.pqt.core.entities.sale.LightweightSale; import com.pqt.core.entities.sale.Sale; /** @@ -18,6 +19,8 @@ public interface ISaleDao { long submitSale(Sale sale); + Sale convert(LightweightSale lwSale); + boolean isSaleRevertSupported(); boolean submitSaleRevert(long id); } diff --git a/Workspace/server/src/main/java/com/pqt/server/module/sale/NoRevertFileSaleDao.java b/Workspace/server/src/main/java/com/pqt/server/module/sale/NoRevertFileSaleDao.java index 96e34d11..9d95de6a 100644 --- a/Workspace/server/src/main/java/com/pqt/server/module/sale/NoRevertFileSaleDao.java +++ b/Workspace/server/src/main/java/com/pqt/server/module/sale/NoRevertFileSaleDao.java @@ -1,6 +1,7 @@ package com.pqt.server.module.sale; import com.pqt.core.entities.product.Product; +import com.pqt.core.entities.sale.LightweightSale; import com.pqt.core.entities.sale.Sale; import com.pqt.server.module.stock.StockService; import com.pqt.server.tools.FileUtil; @@ -10,7 +11,10 @@ import org.apache.commons.io.input.ReversedLinesFileReader; import java.io.*; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; import java.util.Iterator; +import java.util.Map; /** * Implémentation de l'interface {@link ISaleDao} utilisant un fichier comme moyen pour assurer la persistance des @@ -55,11 +59,26 @@ public class NoRevertFileSaleDao implements ISaleDao { long saleId = nextSaleId; stockService.applySale(new SaleContent(sale)); - logSale(sale, saleId); + logSale(sale, new Date(), saleId); generateNextSaleId(); return saleId; } + public Sale convert(LightweightSale lwSale){ + Map products = new HashMap<>(); + + lwSale.getProducts().keySet() + .forEach(pId->{ + Product p = stockService.getProduct(pId); + if(p!=null) + products.put(p, lwSale.getProducts().get(pId)); + }); + if(products.keySet().size()!=lwSale.getProducts().keySet().size()) + return null; + + return new Sale(lwSale.getId(), products, lwSale.getOrderedWith(), lwSale.getOrderedBy(), lwSale.getOrderedFor(), lwSale.getType(), lwSale.getStatus()); + } + private void generateNextSaleId() { nextSaleId++; } @@ -111,24 +130,24 @@ public class NoRevertFileSaleDao implements ISaleDao { throw new UnsupportedOperationException("Le revert de commandes n'est pas supporté"); } - private void logSale(Sale sale, long saleId){ + private void logSale(Sale sale, Date date, long saleId){ try(FileOutputStream fos = new FileOutputStream(SALE_LOG_FILE_NAME); PrintWriter pw = new PrintWriter(fos)){ - pw.append(renderer.render(sale, saleId)); + pw.append(renderer.render(sale, date, saleId)); } catch (IOException e) { e.printStackTrace(); } } private ISaleRenderer getRenderer(){ - return(sale, id)->{ + return(sale, date, id)->{ StringBuffer sb = new StringBuffer("\n#").append(id).append("\n"); String separator = "-----"; DateFormat dateFormat = new SimpleDateFormat("
"); sb.append("type : ").append(sale.getType().name()).append("\n"); - sb.append("at : ").append(dateFormat.format(sale.getOrderedAt())).append("\n"); + sb.append("at : ").append(dateFormat.format(date)).append("\n"); if(sale.getOrderedBy()!=null) sb.append("by : ").append(sale.getOrderedBy().getUsername()).append("(").append(sale.getOrderedBy().getPermissionLevel().name()).append(")").append("\n"); if(sale.getOrderedFor()!=null) @@ -145,6 +164,6 @@ public class NoRevertFileSaleDao implements ISaleDao { } private interface ISaleRenderer{ - String render(Sale sale, long saleId); + String render(Sale sale, Date date, long saleId); } } diff --git a/Workspace/server/src/main/java/com/pqt/server/module/sale/SaleService.java b/Workspace/server/src/main/java/com/pqt/server/module/sale/SaleService.java index 6364ce68..226075c1 100644 --- a/Workspace/server/src/main/java/com/pqt/server/module/sale/SaleService.java +++ b/Workspace/server/src/main/java/com/pqt/server/module/sale/SaleService.java @@ -1,11 +1,16 @@ package com.pqt.server.module.sale; +import com.pqt.core.entities.product.Product; +import com.pqt.core.entities.sale.LightweightSale; import com.pqt.core.entities.sale.Sale; import com.pqt.server.module.sale.listeners.ISaleFirerer; import com.pqt.server.module.sale.listeners.ISaleListener; import com.pqt.server.module.sale.listeners.SimpleSaleFirerer; import com.pqt.server.module.stock.StockService; +import java.util.HashMap; +import java.util.Map; + //TODO ajouter logs /** @@ -47,6 +52,25 @@ public class SaleService { return id; } + /** + * Soumet une commande au service pour validation. Si la commande est validée, les stocks seront débités et + * l'identifiant de la commande sera renvoyé. Si la commande n'est pas validée, la valeur {@value -1} sera renvoyée + * et les stocks resterons inchangés. + *

+ * Les produits de la commande seront récupéré automatiquement via le service de stocks en se basant sur les id + * présent dans {@code sale}. Si un id ne correspond pas à un produit, la totalité de la commande sera refusée. + * + * @param lwSale commande à valider + * @return l'identifiant positif non-nul attribué à la commande si elle est validée, {@value -1} sinon. + */ + public long submitSale(LightweightSale lwSale) { + Sale sale = dao.convert(lwSale); + if(sale!=null) + return submitSale(sale); + + return -1; + } + /** * Détermine si le rollback de commande est supporté par la configuration actuelle du serveur ou non. *