mirror of
https://github.com/klmp200/PQT_Gestionnaire_vente_stock.git
synced 2024-12-23 07:51:08 +00:00
Module Server, clss SimpleMessageHandler : impl vérification compte user émetteur de la query; Ajout javadoc; Association entre query et niveau de permission de compte
This commit is contained in:
parent
713517724e
commit
5a2168c46d
@ -8,6 +8,7 @@ import com.pqt.core.entities.product.LightweightProduct;
|
|||||||
import com.pqt.core.entities.product.Product;
|
import com.pqt.core.entities.product.Product;
|
||||||
import com.pqt.core.entities.product.ProductUpdate;
|
import com.pqt.core.entities.product.ProductUpdate;
|
||||||
import com.pqt.core.entities.sale.Sale;
|
import com.pqt.core.entities.sale.Sale;
|
||||||
|
import com.pqt.core.entities.user_account.AccountLevel;
|
||||||
import com.pqt.server.exception.ServerQueryException;
|
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;
|
||||||
@ -21,11 +22,34 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
//TODO ajouter des messages d'erreur spécifiques pour les NullPointerException si le param du message vaut null
|
//TODO ajouter des messages d'erreur spécifiques pour les NullPointerException si le param du message vaut null
|
||||||
|
//TODO mettre à jour la liste des query supportées lorsque la version du serveur sera proche de la release
|
||||||
|
//TODO ne pas oublier de préciser le niveau de permission requis pour chaque requête
|
||||||
|
//TODO Paramétrer les supports de query et leurs permissions via un meilleur système (config file, etc ...)
|
||||||
|
/**
|
||||||
|
* Implémentation de l'interface {@link IMessageHandler}. Cette classe définit le gestionnaire de message par défaut du
|
||||||
|
* serveur de données du projet PQT.
|
||||||
|
* <p/>
|
||||||
|
* Liste des requêtes supportées :<br/>
|
||||||
|
* <ul>
|
||||||
|
*
|
||||||
|
* </ul>
|
||||||
|
* <p/>
|
||||||
|
* Liste des requêtes non-supportées :<br/>
|
||||||
|
* <ul>
|
||||||
|
*
|
||||||
|
* </ul>
|
||||||
|
* @see IMessageHandler
|
||||||
|
* @version 1.0
|
||||||
|
* @author Guillaume "Cess" Prost
|
||||||
|
*/
|
||||||
public class SimpleMessageHandler implements IMessageHandler {
|
public class SimpleMessageHandler implements IMessageHandler {
|
||||||
|
|
||||||
private final String header_ref_query = "Detail_refus";
|
private final String header_ref_query = "Detail_refus";
|
||||||
private final String header_err_query = "Detail_erreur";
|
private final String header_err_query = "Detail_erreur";
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Liste des services du serveur
|
||||||
|
*/
|
||||||
private AccountService accountService;
|
private AccountService accountService;
|
||||||
private SaleService saleService;
|
private SaleService saleService;
|
||||||
private StatisticsService statisticsService;
|
private StatisticsService statisticsService;
|
||||||
@ -34,7 +58,7 @@ public class SimpleMessageHandler implements IMessageHandler {
|
|||||||
private ServerStateService serverStateService;
|
private ServerStateService serverStateService;
|
||||||
private IMessageToolFactory messageToolFactory;
|
private IMessageToolFactory messageToolFactory;
|
||||||
|
|
||||||
private Map<MessageType, IMessageProcess> queryHandlers;
|
private MessageManager manager;
|
||||||
|
|
||||||
public SimpleMessageHandler() {
|
public SimpleMessageHandler() {
|
||||||
serverStateService = new ServerStateService();
|
serverStateService = new ServerStateService();
|
||||||
@ -45,14 +69,14 @@ public class SimpleMessageHandler implements IMessageHandler {
|
|||||||
statisticsService = new StatisticsService(stockService, saleService);
|
statisticsService = new StatisticsService(stockService, saleService);
|
||||||
messageToolFactory = new GSonMessageToolFactory();
|
messageToolFactory = new GSonMessageToolFactory();
|
||||||
|
|
||||||
queryHandlers = new HashMap<>();
|
manager = new MessageManager();
|
||||||
|
|
||||||
queryHandlers.put(MessageType.QUERY_STOCK, (message)->{
|
manager.support(MessageType.QUERY_STOCK, (message)->{
|
||||||
Map<String, String> fields = new HashMap<>();
|
Map<String, String> fields = new HashMap<>();
|
||||||
fields.put("stock", messageToolFactory.getListFormatter(Product.class).format(stockService.getProductList()));
|
fields.put("stock", messageToolFactory.getListFormatter(Product.class).format(stockService.getProductList()));
|
||||||
return new Message(MessageType.MSG_STOCK, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields);
|
return new Message(MessageType.MSG_STOCK, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields);
|
||||||
});
|
}, AccountLevel.WAITER);
|
||||||
queryHandlers.put(MessageType.QUERY_SALE, (message)->{
|
manager.support(MessageType.QUERY_SALE, (message)->{
|
||||||
Map<String, String> fields = new HashMap<>();
|
Map<String, String> fields = new HashMap<>();
|
||||||
try {
|
try {
|
||||||
long saleId = saleService.submitSale(messageToolFactory.getObjectParser(Sale.class).parse(message.getField("sale")));
|
long saleId = saleService.submitSale(messageToolFactory.getObjectParser(Sale.class).parse(message.getField("sale")));
|
||||||
@ -62,18 +86,8 @@ public class SimpleMessageHandler implements IMessageHandler {
|
|||||||
fields.put(header_ref_query, e.toString());
|
fields.put(header_ref_query, e.toString());
|
||||||
return new Message(MessageType.REFUSED_QUERY, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields);
|
return new Message(MessageType.REFUSED_QUERY, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields);
|
||||||
}
|
}
|
||||||
});
|
}, AccountLevel.WAITER);
|
||||||
queryHandlers.put(MessageType.QUERY_REVERT_SALE, (message)->{
|
manager.support(MessageType.QUERY_STAT, (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 | NullPointerException 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<>();
|
Map<String, String> fields = new HashMap<>();
|
||||||
fields.put("total_sale_worth", Double.toString(statisticsService.getTotalSaleWorth()));
|
fields.put("total_sale_worth", Double.toString(statisticsService.getTotalSaleWorth()));
|
||||||
fields.put("total_sale_amount", Integer.toString(statisticsService.getTotalAmountSale()));
|
fields.put("total_sale_amount", Integer.toString(statisticsService.getTotalAmountSale()));
|
||||||
@ -85,8 +99,8 @@ public class SimpleMessageHandler implements IMessageHandler {
|
|||||||
fields.put("guest_sale_amount",Integer.toString(statisticsService.getGuestSaleAmount()));
|
fields.put("guest_sale_amount",Integer.toString(statisticsService.getGuestSaleAmount()));
|
||||||
|
|
||||||
return new Message(MessageType.MSG_STAT, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields);
|
return new Message(MessageType.MSG_STAT, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields);
|
||||||
});
|
}, AccountLevel.WAITER);
|
||||||
queryHandlers.put(MessageType.QUERY_UPDATE, (message)->{
|
manager.support(MessageType.QUERY_UPDATE, (message)->{
|
||||||
try{
|
try{
|
||||||
List<ProductUpdate> updates = messageToolFactory.getListParser(ProductUpdate.class).parse(message.getField("updates"));
|
List<ProductUpdate> updates = messageToolFactory.getListParser(ProductUpdate.class).parse(message.getField("updates"));
|
||||||
stockService.applyUpdateList(updates);
|
stockService.applyUpdateList(updates);
|
||||||
@ -96,7 +110,20 @@ public class SimpleMessageHandler implements IMessageHandler {
|
|||||||
fields.put(header_err_query, e.toString());
|
fields.put(header_err_query, e.toString());
|
||||||
return new Message(MessageType.ERROR_QUERY, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields);
|
return new Message(MessageType.ERROR_QUERY, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields);
|
||||||
}
|
}
|
||||||
});
|
}, AccountLevel.MASTER);
|
||||||
|
/*
|
||||||
|
manager.support(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 | NullPointerException 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);
|
||||||
|
}
|
||||||
|
}, AccountLevel.MASTER);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -104,7 +131,7 @@ public class SimpleMessageHandler implements IMessageHandler {
|
|||||||
|
|
||||||
Map<String, String> fields = new HashMap<>();
|
Map<String, String> fields = new HashMap<>();
|
||||||
|
|
||||||
if(!isAccountRegistered(message)){
|
if(!isAccountRegisteredAndConnected(message)){
|
||||||
fields.put(header_ref_query, "Compte utilisateur inconnu");
|
fields.put(header_ref_query, "Compte utilisateur inconnu");
|
||||||
return new Message(MessageType.REFUSED_QUERY, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields);
|
return new Message(MessageType.REFUSED_QUERY, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields);
|
||||||
}
|
}
|
||||||
@ -120,18 +147,53 @@ public class SimpleMessageHandler implements IMessageHandler {
|
|||||||
return new Message(MessageType.ERROR_QUERY, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields);
|
return new Message(MessageType.ERROR_QUERY, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO ajouter Javadoc
|
/**
|
||||||
|
* Interface interne utilisé pour encapsuler le traitement des messages dans un objet
|
||||||
|
*/
|
||||||
private interface IMessageProcess{
|
private interface IMessageProcess{
|
||||||
Message execute(Message request);
|
Message execute(Message request);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isAccountRegistered(Message message){
|
private class MessageManager{
|
||||||
//TODO faire ça
|
private Map<MessageType, IMessageProcess> processes;
|
||||||
return false;
|
private Map<MessageType, AccountLevel> levels;
|
||||||
|
|
||||||
|
MessageManager(){
|
||||||
|
processes = new HashMap<>();
|
||||||
|
levels = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void support(MessageType type, IMessageProcess process, AccountLevel permissionLevel){
|
||||||
|
processes.put(type, process);
|
||||||
|
levels.put(type, permissionLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
IMessageProcess getProcess(MessageType messageType){
|
||||||
|
return processes.get(messageType);
|
||||||
|
}
|
||||||
|
|
||||||
|
AccountLevel getLevel(MessageType messageType){
|
||||||
|
return levels.get(messageType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vérifie si le compte utilisé pour émettre la query contenu dans le message est enregistré et connecté.
|
||||||
|
* @param message message dont l'expéditeur doit être vérifié
|
||||||
|
* @return true si le compte est existant et connecté, false si au moins une des conditions est fausse.
|
||||||
|
*/
|
||||||
|
private boolean isAccountRegisteredAndConnected(Message message){
|
||||||
|
return accountService.isAccountRegistered(message.getUser()) && accountService.isAccountConnected(message.getUser());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vérifie si le compte émetteur de la query a les autorisations suffisantes pour effectuer la query.
|
||||||
|
* <p/>
|
||||||
|
* <b>Attention : tenter cette vérification sur un compte inexistant lèvera une {@link NullPointerException}</b>.
|
||||||
|
* @param message message dont le niveau de permission de l'expéditeur doit être vérifié
|
||||||
|
* @return true si l'expéditeur a les autorisations suffisantes, false sinon.
|
||||||
|
*/
|
||||||
private boolean checkAccountPermission(Message message){
|
private boolean checkAccountPermission(Message message){
|
||||||
//TODO faire ça
|
return accountService.getAccountPermissionLevel(message.getUser()).compareTo(manager.getLevel(message.getType()))>=0;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.pqt.server.module.account;
|
package com.pqt.server.module.account;
|
||||||
|
|
||||||
import com.pqt.core.entities.user_account.Account;
|
import com.pqt.core.entities.user_account.Account;
|
||||||
|
import com.pqt.core.entities.user_account.AccountLevel;
|
||||||
|
|
||||||
//TODO écrire Javadoc
|
//TODO écrire Javadoc
|
||||||
//TODO ajouter logs
|
//TODO ajouter logs
|
||||||
@ -23,4 +24,8 @@ public class AccountService {
|
|||||||
public boolean isAccountRegistered(Account account){
|
public boolean isAccountRegistered(Account account){
|
||||||
return dao.isAccountRegistered(account);
|
return dao.isAccountRegistered(account);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AccountLevel getAccountPermissionLevel(Account account){
|
||||||
|
return dao.getAccountPermissionLevel(account);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.pqt.server.module.account;
|
package com.pqt.server.module.account;
|
||||||
|
|
||||||
import com.pqt.core.entities.user_account.Account;
|
import com.pqt.core.entities.user_account.Account;
|
||||||
|
import com.pqt.core.entities.user_account.AccountLevel;
|
||||||
import com.pqt.server.tools.io.ISerialFileManager;
|
import com.pqt.server.tools.io.ISerialFileManager;
|
||||||
import com.pqt.server.tools.io.SimpleSerialFileManagerFactory;
|
import com.pqt.server.tools.io.SimpleSerialFileManagerFactory;
|
||||||
import com.pqt.server.tools.security.IHashTool;
|
import com.pqt.server.tools.security.IHashTool;
|
||||||
@ -81,6 +82,13 @@ public class FileAccountDao implements IAccountDao {
|
|||||||
return lookupMatchingEntry(account, accountEntries)!=null;
|
return lookupMatchingEntry(account, accountEntries)!=null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AccountLevel getAccountPermissionLevel(Account account) {
|
||||||
|
if(isAccountRegistered(account))
|
||||||
|
return lookupMatchingEntry(account, accountEntries).getLevel();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private void saveToFile(){
|
private void saveToFile(){
|
||||||
fileManager.saveSetToFile(accountEntries);
|
fileManager.saveSetToFile(accountEntries);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.pqt.server.module.account;
|
package com.pqt.server.module.account;
|
||||||
|
|
||||||
import com.pqt.core.entities.user_account.Account;
|
import com.pqt.core.entities.user_account.Account;
|
||||||
|
import com.pqt.core.entities.user_account.AccountLevel;
|
||||||
|
|
||||||
//TODO écrire Javadoc
|
//TODO écrire Javadoc
|
||||||
public interface IAccountDao {
|
public interface IAccountDao {
|
||||||
@ -10,4 +11,6 @@ public interface IAccountDao {
|
|||||||
boolean submitAccountCredentials(Account acc, boolean desiredState);
|
boolean submitAccountCredentials(Account acc, boolean desiredState);
|
||||||
|
|
||||||
boolean isAccountRegistered(Account account);
|
boolean isAccountRegistered(Account account);
|
||||||
|
|
||||||
|
AccountLevel getAccountPermissionLevel(Account account);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user