From a11b85eccd2ab32fb33003661200ce22145de851 Mon Sep 17 00:00:00 2001 From: Notmoo Date: Wed, 26 Jul 2017 19:53:11 +0200 Subject: [PATCH] =?UTF-8?q?module=20Server=20:=20d=C3=A9but=20de=20l'impl?= =?UTF-8?q?=C3=A9mentation=20du=20support=20des=20query=20dans=20clss=20Si?= =?UTF-8?q?mpleMessageHandler;=20Ajout=20clss=20"ServerQueryException"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Workspace/.idea/modules.xml | 1 - .../controller/SimpleMessageHandler.java | 101 +++++++++++++++++- .../exception/ServerQueryException.java | 4 + .../pqt/server/module/sale/SaleService.java | 5 +- .../module/state/ServerStateService.java | 8 ++ .../module/statistics/StatisticsService.java | 18 +++- 6 files changed, 125 insertions(+), 12 deletions(-) create mode 100644 Workspace/server/src/main/WEB-INF/classes/com/pqt/server/exception/ServerQueryException.java diff --git a/Workspace/.idea/modules.xml b/Workspace/.idea/modules.xml index 6af297d4..1796f0ba 100644 --- a/Workspace/.idea/modules.xml +++ b/Workspace/.idea/modules.xml @@ -6,7 +6,6 @@ - \ No newline at end of file diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/controller/SimpleMessageHandler.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/controller/SimpleMessageHandler.java index 67de608e..008640c3 100644 --- a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/controller/SimpleMessageHandler.java +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/controller/SimpleMessageHandler.java @@ -1,7 +1,13 @@ package com.pqt.server.controller; +import com.pqt.core.communication.GSonMessageToolFactory; +import com.pqt.core.communication.IMessageToolFactory; import com.pqt.core.entities.messages.Message; 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.sale.Sale; +import com.pqt.server.exception.ServerQueryException; import com.pqt.server.module.account.AccountService; import com.pqt.server.module.client.ClientService; import com.pqt.server.module.sale.SaleService; @@ -14,12 +20,16 @@ import java.util.Map; public class SimpleMessageHandler implements IMessageHandler { + private final String header_ref_query = "Detail_refus"; + private final String header_err_query = "Detail_erreur"; + private AccountService accountService; private SaleService saleService; private StatisticsService statisticsService; private StockService stockService; private ClientService clientService; private ServerStateService serverStateService; + private IMessageToolFactory messageToolFactory; private Map queryHandlers; @@ -30,22 +40,105 @@ public class SimpleMessageHandler implements IMessageHandler { saleService = new SaleService(); stockService = new StockService(); statisticsService = new StatisticsService(stockService, saleService); + messageToolFactory = new GSonMessageToolFactory(); queryHandlers = new HashMap<>(); - //TODO ajouter callables à la map + + queryHandlers.put(MessageType.QUERY_STOCK, (message)->{ + Map fields = new HashMap<>(); + fields.put("stock", messageToolFactory.getListFormatter(Product.class).format(stockService.getProductList())); + return new Message(MessageType.MSG_STOCK, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields); + }); + queryHandlers.put(MessageType.QUERY_SALE, (message)->{ + Map fields = new HashMap<>(); + try { + long saleId = saleService.submitSale(messageToolFactory.getObjectParser(Sale.class).parse(message.getField("sale"))); + fields.put("saleId", Long.toString(saleId)); + return new Message(MessageType.ACK_SALE, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields); + }catch(ServerQueryException e){ + fields.put(header_ref_query, e.toString()); + return new Message(MessageType.REFUSED_QUERY, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields); + } + }); + queryHandlers.put(MessageType.QUERY_REVERT_SALE, (message)->{ + try{ + saleService.submitSaleRevert(messageToolFactory.getObjectParser(Long.class).parse(message.getField("saleId"))); + return new Message(MessageType.ACK_REVERT_SALE, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, null); + }catch(ServerQueryException e){ + Map fields = new HashMap<>(); + fields.put(header_err_query, e.toString()); + return new Message(MessageType.ERROR_QUERY, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields); + } + }); + queryHandlers.put(MessageType.QUERY_STAT, (message)->{ + Map fields = new HashMap<>(); + fields.put("total_sale_worth", Double.toString(statisticsService.getTotalSaleWorth())); + fields.put("total_sale_amount", Integer.toString(statisticsService.getTotalAmountSale())); + fields.put("total_money_made", Double.toString(statisticsService.getTotalMoneyMade())); + fields.put("top_popular_products", messageToolFactory.getListFormatter(LightweightProduct.class).format(statisticsService.getTopPopularProducts(5))); + fields.put("staff_sale_worth",Double.toString(statisticsService.getStaffSaleWorth())); + fields.put("staff_sale_amount",Integer.toString(statisticsService.getStaffSaleAmount())); + fields.put("guest_sale_worth",Double.toString(statisticsService.getGuestSaleAmount())); + fields.put("guest_sale_amount",Integer.toString(statisticsService.getStaffSaleAmount())); + + return new Message(MessageType.MSG_STAT, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields); + }); + queryHandlers.put(MessageType.QUERY_UPDATE, (message)->{ + //TODO Supporter les query update + return null; + }); + queryHandlers.put(MessageType.QUERY_CONNECT, (message)->{ + //TODO Supporter les query client + return null; + }); + queryHandlers.put(MessageType.QUERY_LOGIN, (message)->{ + //TODO Supporter les query account + return null; + }); } @Override public Message handleMessage(Message message) { - if(queryHandlers.containsKey(message.getType())) - return queryHandlers.get(message.getType()).execute(message); + Map fields = new HashMap<>(); - return null; + if(!checkClient(message)){ + fields.put(header_ref_query, "client non enregistré sur le serveur"); + return new Message(MessageType.REFUSED_QUERY, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields); + } + if(!isAccountRegistered(message)){ + fields.put(header_ref_query, "Compte utilisateur inconnu"); + return new Message(MessageType.REFUSED_QUERY, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields); + } + if(!checkAccountPermission(message)){ + fields.put(header_ref_query, "Compte utilisateur avec permission trop faible"); + return new Message(MessageType.REFUSED_QUERY, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields); + } + if(queryHandlers.containsKey(message.getType())){ + return queryHandlers.get(message.getType()).execute(message); + } + + fields.put(header_err_query, "Type requête non pris en charge par ce serveur"); + return new Message(MessageType.ERROR_QUERY, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields); } //TODO ajouter Javadoc private interface IMessageProcess{ Message execute(Message request); } + + private boolean checkClient(Message message){ + //TODO faire ça + return false; + } + + private boolean isAccountRegistered(Message message){ + //TODO faire ça + return false; + } + + private boolean checkAccountPermission(Message message){ + //TODO faire ça + return false; + } } diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/exception/ServerQueryException.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/exception/ServerQueryException.java new file mode 100644 index 00000000..c3fb5f6d --- /dev/null +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/exception/ServerQueryException.java @@ -0,0 +1,4 @@ +package com.pqt.server.exception; + +public class ServerQueryException extends Exception { +} diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/sale/SaleService.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/sale/SaleService.java index f6fd3c56..4057c88f 100644 --- a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/sale/SaleService.java +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/sale/SaleService.java @@ -1,6 +1,7 @@ package com.pqt.server.module.sale; import com.pqt.core.entities.sale.Sale; +import com.pqt.server.exception.ServerQueryException; //TODO écrire Javadoc //TODO ajouter logs @@ -11,11 +12,11 @@ public class SaleService { public SaleService() { } - public long submitSale(Sale sale) { + public long submitSale(Sale sale) throws ServerQueryException { return dao.submitSale(sale); } - public void submitSaleRevert(long id) { + public void submitSaleRevert(long id) throws ServerQueryException { dao.submitSaleRevert(id); } diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/state/ServerStateService.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/state/ServerStateService.java index 1d6f8878..4e2d63c7 100644 --- a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/state/ServerStateService.java +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/state/ServerStateService.java @@ -33,6 +33,14 @@ public class ServerStateService { serverState.setPort(port); } + public DataServer getServer() { + return server; + } + + public void setServer(DataServer server) { + this.server = server; + } + public ServerState getServerStateCopy() { return serverState.copy(); } diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/statistics/StatisticsService.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/statistics/StatisticsService.java index 262792fa..5c7cfef9 100644 --- a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/statistics/StatisticsService.java +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/statistics/StatisticsService.java @@ -1,5 +1,6 @@ package com.pqt.server.module.statistics; +import com.pqt.core.entities.product.LightweightProduct; import com.pqt.core.entities.product.Product; import com.pqt.server.module.stock.StockService; import com.pqt.server.module.sale.SaleService; @@ -31,16 +32,23 @@ public class StatisticsService { return 0; } - public List getTopPopularProducts(int amount) { + public List getTopPopularProducts(int amount) { return null; } - public int getTotalAmountStaffSales() { + public int getStaffSaleAmount() { return 0; } - public int getTotalAmountGuestSale() { - return 0; - } + public double getStaffSaleWorth() { + return 0; + } + public int getGuestSaleAmount() { + return 0; + } + + public double getGuestSaleWorth() { + return 0; + } }