module Server : début de l'implémentation du support des query dans clss SimpleMessageHandler; Ajout clss "ServerQueryException"

This commit is contained in:
Notmoo 2017-07-26 19:53:11 +02:00
parent c411409249
commit a11b85eccd
6 changed files with 125 additions and 12 deletions

View File

@ -6,7 +6,6 @@
<module fileurl="file://$PROJECT_DIR$/client/client.iml" filepath="$PROJECT_DIR$/client/client.iml" /> <module fileurl="file://$PROJECT_DIR$/client/client.iml" filepath="$PROJECT_DIR$/client/client.iml" />
<module fileurl="file://$PROJECT_DIR$/core/core.iml" filepath="$PROJECT_DIR$/core/core.iml" /> <module fileurl="file://$PROJECT_DIR$/core/core.iml" filepath="$PROJECT_DIR$/core/core.iml" />
<module fileurl="file://$PROJECT_DIR$/server/server.iml" filepath="$PROJECT_DIR$/server/server.iml" /> <module fileurl="file://$PROJECT_DIR$/server/server.iml" filepath="$PROJECT_DIR$/server/server.iml" />
<module fileurl="file://$PROJECT_DIR$/server/server.iml" filepath="$PROJECT_DIR$/server/server.iml" />
</modules> </modules>
</component> </component>
</project> </project>

View File

@ -1,7 +1,13 @@
package com.pqt.server.controller; 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.Message;
import com.pqt.core.entities.messages.MessageType; 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.account.AccountService;
import com.pqt.server.module.client.ClientService; import com.pqt.server.module.client.ClientService;
import com.pqt.server.module.sale.SaleService; import com.pqt.server.module.sale.SaleService;
@ -14,12 +20,16 @@ import java.util.Map;
public class SimpleMessageHandler implements IMessageHandler { 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 AccountService accountService;
private SaleService saleService; private SaleService saleService;
private StatisticsService statisticsService; private StatisticsService statisticsService;
private StockService stockService; private StockService stockService;
private ClientService clientService; private ClientService clientService;
private ServerStateService serverStateService; private ServerStateService serverStateService;
private IMessageToolFactory messageToolFactory;
private Map<MessageType, IMessageProcess> queryHandlers; private Map<MessageType, IMessageProcess> queryHandlers;
@ -30,22 +40,105 @@ public class SimpleMessageHandler implements IMessageHandler {
saleService = new SaleService(); saleService = new SaleService();
stockService = new StockService(); stockService = new StockService();
statisticsService = new StatisticsService(stockService, saleService); statisticsService = new StatisticsService(stockService, saleService);
messageToolFactory = new GSonMessageToolFactory();
queryHandlers = new HashMap<>(); queryHandlers = new HashMap<>();
//TODO ajouter callables à la map
queryHandlers.put(MessageType.QUERY_STOCK, (message)->{
Map<String, String> 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<String, String> 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<String, String> 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<String, String> 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 @Override
public Message handleMessage(Message message) { public Message handleMessage(Message message) {
if(queryHandlers.containsKey(message.getType())) Map<String, String> fields = new HashMap<>();
return queryHandlers.get(message.getType()).execute(message);
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 //TODO ajouter Javadoc
private interface IMessageProcess{ private interface IMessageProcess{
Message execute(Message request); 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;
}
} }

View File

@ -0,0 +1,4 @@
package com.pqt.server.exception;
public class ServerQueryException extends Exception {
}

View File

@ -1,6 +1,7 @@
package com.pqt.server.module.sale; package com.pqt.server.module.sale;
import com.pqt.core.entities.sale.Sale; import com.pqt.core.entities.sale.Sale;
import com.pqt.server.exception.ServerQueryException;
//TODO écrire Javadoc //TODO écrire Javadoc
//TODO ajouter logs //TODO ajouter logs
@ -11,11 +12,11 @@ public class SaleService {
public SaleService() { public SaleService() {
} }
public long submitSale(Sale sale) { public long submitSale(Sale sale) throws ServerQueryException {
return dao.submitSale(sale); return dao.submitSale(sale);
} }
public void submitSaleRevert(long id) { public void submitSaleRevert(long id) throws ServerQueryException {
dao.submitSaleRevert(id); dao.submitSaleRevert(id);
} }

View File

@ -33,6 +33,14 @@ public class ServerStateService {
serverState.setPort(port); serverState.setPort(port);
} }
public DataServer getServer() {
return server;
}
public void setServer(DataServer server) {
this.server = server;
}
public ServerState getServerStateCopy() { public ServerState getServerStateCopy() {
return serverState.copy(); return serverState.copy();
} }

View File

@ -1,5 +1,6 @@
package com.pqt.server.module.statistics; package com.pqt.server.module.statistics;
import com.pqt.core.entities.product.LightweightProduct;
import com.pqt.core.entities.product.Product; import com.pqt.core.entities.product.Product;
import com.pqt.server.module.stock.StockService; import com.pqt.server.module.stock.StockService;
import com.pqt.server.module.sale.SaleService; import com.pqt.server.module.sale.SaleService;
@ -31,16 +32,23 @@ public class StatisticsService {
return 0; return 0;
} }
public List<Product> getTopPopularProducts(int amount) { public List<LightweightProduct> getTopPopularProducts(int amount) {
return null; return null;
} }
public int getTotalAmountStaffSales() { public int getStaffSaleAmount() {
return 0; return 0;
} }
public int getTotalAmountGuestSale() { public double getStaffSaleWorth() {
return 0; return 0;
} }
public int getGuestSaleAmount() {
return 0;
}
public double getGuestSaleWorth() {
return 0;
}
} }