Module Server : fin de l'implémentation du support des query; Suppression des queryLogin et queryConnect

This commit is contained in:
Notmoo 2017-07-26 21:41:09 +02:00
parent a11b85eccd
commit 80ba949505
9 changed files with 40 additions and 82 deletions

View File

@ -0,0 +1 @@
,Notmoo-PC/Guillaume,Notmoo-PC,26.07.2017 21:36,file:///C:/Users/Guillaume/AppData/Roaming/LibreOffice/4;

View File

@ -1,19 +0,0 @@
package com.pqt.core.entities.query;
public class ConnectQuery extends SimpleQuery {
private String serverAddress;
public ConnectQuery(String serverAddress) {
super(QueryType.CONNECT);
this.serverAddress = serverAddress;
}
public String getServerAddress() {
return serverAddress;
}
public void setServerAddress(String serverAddress) {
this.serverAddress = serverAddress;
}
}

View File

@ -1,32 +0,0 @@
package com.pqt.core.entities.query;
import com.pqt.core.entities.user_account.Account;
public class LogQuery extends SimpleQuery {
private Account account;
private boolean newDesiredState;
public LogQuery(Account account, boolean newDesiredState) {
super(QueryType.LOG);
this.account = account;
this.newDesiredState = newDesiredState;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public boolean isNewDesiredState() {
return newDesiredState;
}
public void setNewDesiredState(boolean newDesiredState) {
this.newDesiredState = newDesiredState;
}
}

View File

@ -1,7 +0,0 @@
package com.pqt.server;
public class Server {
public static void main(String[] args){
}
}

View File

@ -2,11 +2,14 @@ package com.pqt.server.controller;
import com.pqt.core.communication.GSonMessageToolFactory; import com.pqt.core.communication.GSonMessageToolFactory;
import com.pqt.core.communication.IMessageToolFactory; import com.pqt.core.communication.IMessageToolFactory;
import com.pqt.core.entities.members.Client;
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.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.sale.Sale; import com.pqt.core.entities.sale.Sale;
import com.pqt.core.entities.user_account.Account;
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;
@ -16,8 +19,10 @@ import com.pqt.server.module.statistics.StatisticsService;
import com.pqt.server.module.stock.StockService; import com.pqt.server.module.stock.StockService;
import java.util.HashMap; import java.util.HashMap;
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
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";
@ -55,7 +60,7 @@ public class SimpleMessageHandler implements IMessageHandler {
long saleId = saleService.submitSale(messageToolFactory.getObjectParser(Sale.class).parse(message.getField("sale"))); long saleId = saleService.submitSale(messageToolFactory.getObjectParser(Sale.class).parse(message.getField("sale")));
fields.put("saleId", Long.toString(saleId)); fields.put("saleId", Long.toString(saleId));
return new Message(MessageType.ACK_SALE, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields); return new Message(MessageType.ACK_SALE, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields);
}catch(ServerQueryException e){ }catch(ServerQueryException | NullPointerException e){
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);
} }
@ -64,7 +69,7 @@ public class SimpleMessageHandler implements IMessageHandler {
try{ try{
saleService.submitSaleRevert(messageToolFactory.getObjectParser(Long.class).parse(message.getField("saleId"))); 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); return new Message(MessageType.ACK_REVERT_SALE, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, null);
}catch(ServerQueryException e){ }catch(ServerQueryException | NullPointerException e){
Map<String, String> fields = new HashMap<>(); Map<String, String> fields = new HashMap<>();
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);
@ -84,16 +89,15 @@ public class SimpleMessageHandler implements IMessageHandler {
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);
}); });
queryHandlers.put(MessageType.QUERY_UPDATE, (message)->{ queryHandlers.put(MessageType.QUERY_UPDATE, (message)->{
//TODO Supporter les query update try{
return null; List<ProductUpdate> updates = messageToolFactory.getListParser(ProductUpdate.class).parse(message.getField("updates"));
}); stockService.applyUpdateList(updates);
queryHandlers.put(MessageType.QUERY_CONNECT, (message)->{ return new Message(MessageType.ACK_UPDATE, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, null);
//TODO Supporter les query client }catch (ServerQueryException | NullPointerException e){
return null; Map<String, String> fields = new HashMap<>();
}); fields.put(header_err_query, e.toString());
queryHandlers.put(MessageType.QUERY_LOGIN, (message)->{ return new Message(MessageType.ERROR_QUERY, serverStateService.getServer(), message.getEmitter(), message.getUser(), message, fields);
//TODO Supporter les query account }
return null;
}); });
} }
@ -102,10 +106,6 @@ public class SimpleMessageHandler implements IMessageHandler {
Map<String, String> fields = new HashMap<>(); Map<String, String> fields = new HashMap<>();
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)){ if(!isAccountRegistered(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);
@ -127,11 +127,6 @@ public class SimpleMessageHandler implements IMessageHandler {
Message execute(Message request); Message execute(Message request);
} }
private boolean checkClient(Message message){
//TODO faire ça
return false;
}
private boolean isAccountRegistered(Message message){ private boolean isAccountRegistered(Message message){
//TODO faire ça //TODO faire ça
return false; return false;

View File

@ -20,4 +20,7 @@ public class AccountService {
return dao.setAccountConnected(acc, connected); return dao.setAccountConnected(acc, connected);
} }
public boolean isAccountRegistered(Account acc){
return dao.isAccountRegistered(acc);
}
} }

View File

@ -10,4 +10,5 @@ public interface IAccountDao {
boolean setAccountConnected(Account acc, boolean connected); boolean setAccountConnected(Account acc, boolean connected);
boolean isAccountRegistered(Account acc);
} }

View File

@ -1,6 +1,8 @@
package com.pqt.server.module.stock; package com.pqt.server.module.stock;
import com.pqt.core.entities.product.Product; import com.pqt.core.entities.product.Product;
import com.pqt.core.entities.product.ProductUpdate;
import com.pqt.server.exception.ServerQueryException;
import java.util.List; import java.util.List;
@ -22,15 +24,29 @@ public class StockService {
return dao.getProduct(id); return dao.getProduct(id);
} }
public void addProduct(Product product) { public void applyUpdateList(List<ProductUpdate> updates) throws ServerQueryException{
for(ProductUpdate upd : updates){
if(upd.getOldVersion()==null){
addProduct(upd.getNewVersion());
}else if(upd.getNewVersion()==null){
removeProduct(upd.getOldVersion().getId());
}else if(upd.getOldVersion()!=null && upd.getNewVersion()!=null){
modifyProduct(upd.getOldVersion().getId(), upd.getNewVersion());
}else{
//TODO écrit le throw d'une ServerQueryException
}
}
}
private void addProduct(Product product) {
dao.addProduct(product); dao.addProduct(product);
} }
public void removeProduct(long id) { private void removeProduct(long id) {
dao.removeProduct(id); dao.removeProduct(id);
} }
public void modifyProduct(long id, Product product) { private void modifyProduct(long id, Product product) {
dao.modifyProduct(id, product); dao.modifyProduct(id, product);
} }
} }