diff --git a/Workspace/client/src/main/java/com/pqt/client/module/account/AccountService.java b/Workspace/client/src/main/java/com/pqt/client/module/account/AccountService.java index 9f6c76a0..1c46f8a6 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/account/AccountService.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/account/AccountService.java @@ -1,48 +1,123 @@ package com.pqt.client.module.account; +import com.pqt.client.module.query.QueryExecutor; +import com.pqt.client.module.query.query_callback.ICollectionItemMessageCallback; +import com.pqt.client.module.query.query_callback.INoItemMessageCallback; +import com.pqt.core.entities.product.Product; import com.pqt.core.entities.user_account.Account; import com.pqt.client.module.account.listeners.IAccountListener; +import javax.swing.event.EventListenerList; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; import java.util.List; -//TODO écrire contenu méthodes //TODO écrire javadoc //TODO add log lines public class AccountService { + private QueryExecutor executor; + private Account currentAccount; + private boolean connected; + private Collection accounts; + private EventListenerList listenerList; + + public AccountService(QueryExecutor executor){ + this.executor = executor; + listenerList = new EventListenerList(); + accounts = new ArrayList<>(); + refreshAccounts(); + } + public Account getCurrentAccount() { - return null; + return currentAccount; } public void setCurrentAccount(Account account) { - + if(connected) + logOutCurrentAccount(); + this.currentAccount = account; } public boolean isCurrentAccountLoggedIn() { - return false; + return connected; } public void logInCurrentAccount(String password) { - + setCurrentAccountState(password, true); } public void logOutCurrentAccount() { - + setCurrentAccountState(null, false); } - public List getRecentAccounts() { - return null; - } + private void setCurrentAccountState(String password, boolean state){ + if(currentAccount!=null && connected!=state) { + Account acc = new Account(currentAccount); + acc.setPassword(password); + executor.executeConnectAccountQuery(acc, state, new INoItemMessageCallback() { + @Override + public void ack() { + if (currentAccount != null + && connected != state + && currentAccount.getUsername().equals(acc.getUsername()) + && currentAccount.getPermissionLevel().equals(acc.getPermissionLevel())) { + connected = state; + Arrays.stream(listenerList.getListeners(IAccountListener.class)) + .forEach(l->l.onAccountStatusChangedEvent(connected)); + }else + Arrays.stream(listenerList.getListeners(IAccountListener.class)) + .forEach(l->l.onAccountStatusNotChangedEvent( + new IllegalStateException("Account service not in the right state") + )); + } + + @Override + public void err(Throwable cause) { + Arrays.stream(listenerList.getListeners(IAccountListener.class)) + .forEach(l->l.onAccountStatusNotChangedEvent(cause)); + } + + @Override + public void ref(Throwable cause) { + Arrays.stream(listenerList.getListeners(IAccountListener.class)) + .forEach(l->l.onAccountStatusNotChangedEvent(cause)); + } + }); + } + } public void addListener(IAccountListener listener) { - + listenerList.add(IAccountListener.class, listener); } public void removeListener(IAccountListener listener) { - + listenerList.remove(IAccountListener.class, listener); } - public List getAllAccounts() { - return null; + public Collection getAllAccounts() { + return accounts; + } + + public void refreshAccounts(){ + executor.executeAccountListQuery(new ICollectionItemMessageCallback() { + @Override + public void err(Throwable cause) { + + } + + @Override + public void ref(Throwable cause) { + + } + + @Override + public void ack(Collection obj) { + accounts = obj; + Arrays.stream(listenerList.getListeners(IAccountListener.class)) + .forEach(IAccountListener::onAccountListChangedEvent); + } + }); } } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/account/listeners/AccountListenerAdapter.java b/Workspace/client/src/main/java/com/pqt/client/module/account/listeners/AccountListenerAdapter.java index 6abd0649..cf29f75a 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/account/listeners/AccountListenerAdapter.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/account/listeners/AccountListenerAdapter.java @@ -8,13 +8,23 @@ public class AccountListenerAdapter implements IAccountListener { /** * @see com.pqt.client.module.account.listeners.IAccountListener#onAccountStatusChangedEvent(boolean) */ + @Override public void onAccountStatusChangedEvent(boolean status) { } + /** + * @see com.pqt.client.module.account.listeners.IAccountListener#onAccountStatusNotChangedEvent(Throwable) + */ + @Override + public void onAccountStatusNotChangedEvent(Throwable cause) { + + } + /** * @see com.pqt.client.module.account.listeners.IAccountListener#onAccountListChangedEvent() */ + @Override public void onAccountListChangedEvent() { } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/account/listeners/IAccountListener.java b/Workspace/client/src/main/java/com/pqt/client/module/account/listeners/IAccountListener.java index 0de33085..821f3798 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/account/listeners/IAccountListener.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/account/listeners/IAccountListener.java @@ -5,5 +5,6 @@ import java.util.EventListener; public interface IAccountListener extends EventListener { void onAccountStatusChangedEvent(boolean status); + void onAccountStatusNotChangedEvent(Throwable cause); void onAccountListChangedEvent(); } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/query/QueryExecutor.java b/Workspace/client/src/main/java/com/pqt/client/module/query/QueryExecutor.java index 63167cee..0472a3a5 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/query/QueryExecutor.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/query/QueryExecutor.java @@ -1,35 +1,170 @@ package com.pqt.client.module.query; -import com.pqt.client.module.query.query_callback.IIdQueryCallback; -import com.pqt.core.entities.query.IQuery; -import com.pqt.client.module.query.query_callback.ISimpleQueryCallback; -import com.pqt.client.module.query.query_callback.IStatQueryCallback; -import com.pqt.client.module.query.query_callback.IStockQueryCallback; +import com.pqt.client.module.connection.ConnectionService; +import com.pqt.client.module.connection.listeners.IConnectionListener; +import com.pqt.client.module.query.exceptions.HeaderNotFoundException; +import com.pqt.client.module.query.exceptions.MessageTimeoutException; +import com.pqt.client.module.query.query_callback.*; +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.Product; +import com.pqt.core.entities.product.ProductUpdate; +import com.pqt.core.entities.sale.Sale; +import com.pqt.core.entities.user_account.Account; + +import java.util.List; -//TODO écrire contenu méthodes //TODO écrire javadoc public class QueryExecutor { - public static final QueryExecutor INSTANCE = new QueryExecutor(); - - private QueryExecutor(){ + private IMessageToolFactory messageToolFactory; + private ConnectionService connectionService; + private QueryMessageFactory messageFactory; + public QueryExecutor(ConnectionService connectionService){ + messageToolFactory = new GSonMessageToolFactory(); + this.connectionService = connectionService; + this.messageFactory = new QueryMessageFactory(messageToolFactory); } - public long execute(IQuery query, ISimpleQueryCallback callback) { - return 0; - } - - public long execute(IQuery query, IStatQueryCallback callback) { - return 0; - } - - public long execute(IQuery query, IStockQueryCallback callback) { - return 0; - } - - public long execute(IQuery query, IIdQueryCallback callback) { - return 0; + public void executeSaleQuery(Sale sale, INoItemMessageCallback callback) { + sendMessage(messageFactory.newSaleMessage(sale), callback, MessageType.ACK_SALE); } + public void executePingQuery(INoItemMessageCallback callback){ + sendMessage(messageFactory.newPingMessage(), callback, MessageType.ACK_PING); + } + + public void executeUpdateQuery(List updates, INoItemMessageCallback callback) { + sendMessage(messageFactory.newUpdateMessage(updates), callback, MessageType.ACK_UPDATE); + } + + public void executeConnectAccountQuery(Account account, boolean desiredState, INoItemMessageCallback callback){ + sendMessage(messageFactory.newConnectAccountMessage(account,desiredState), callback, MessageType.ACK_CONNECT_ACCOUNT); + } + + private void sendMessage(Message message, INoItemMessageCallback callback, MessageType responseType){ + connectionService.sendText(messageToolFactory.getObjectFormatter(Message.class).format(message), new IConnectionListener() { + @Override + public void onMessageReceivedEvent(String msg) { + Message response = messageToolFactory.getObjectParser(Message.class).parse(msg); + if(response.getType().equals(responseType)) + callback.ack(); + else + handleUnexpectedTypeInResponse(response, callback); + } + + @Override + public void onConnectedEvent() { + + } + + @Override + public void onDisconnectedEvent() { + + } + + @Override + public void onTimeOutEvent() { + callback.err(new MessageTimeoutException()); + } + }); + } + + public void executeStockQuery(ICollectionItemMessageCallback callback) { + sendMessage(messageFactory.newStockMessage(), callback, Product.class, MessageType.MSG_STOCK, "stock"); + } + + public void executeAccountListQuery(ICollectionItemMessageCallback callback){ + sendMessage(messageFactory.newAccountListMessage(), callback, Account.class, MessageType.MSG_ACCOUNT_LIST, "accounts"); + } + + private void sendMessage(Message message, ICollectionItemMessageCallback callback, Class clazz, MessageType responseType, String itemHeader){ + connectionService.sendText(messageToolFactory.getObjectFormatter(Message.class).format(message), new IConnectionListener() { + @Override + public void onMessageReceivedEvent(String msg) { + Message response = messageToolFactory.getObjectParser(Message.class).parse(msg); + if(response.getType().equals(responseType)) { + String item = response.getField(itemHeader); + if (item != null) + callback.ack(messageToolFactory.getListParser(clazz).parse(item)); + else + callback.err(new HeaderNotFoundException("Missing expected header \""+ + itemHeader+"\" in response \""+responseType.name()+"\"")); + }else + handleUnexpectedTypeInResponse(response, callback); + } + + @Override + public void onConnectedEvent() { + + } + + @Override + public void onDisconnectedEvent() { + + } + + @Override + public void onTimeOutEvent() { + callback.err(new MessageTimeoutException()); + } + }); + } + + public void executeStatQuery(IMapItemMessageCallback callback) { + sendMessage(messageFactory.newStatMessage(), callback, MessageType.MSG_STAT); + } + + public void executeConfigListQuery(IMapItemMessageCallback callback){ + sendMessage(messageFactory.newConfigListMessage(), callback, MessageType.MSG_CONFIG_LIST); + } + + //TODO à rendre générique pour toute Map au lieu de Map + private void sendMessage(Message message, IMapItemMessageCallback callback, MessageType responseType){ + connectionService.sendText(messageToolFactory.getObjectFormatter(Message.class).format(message), new IConnectionListener() { + @Override + public void onMessageReceivedEvent(String msg) { + Message response = messageToolFactory.getObjectParser(Message.class).parse(msg); + if(response.getType().equals(responseType)){ + callback.ack(response.getFields()); + }else + handleUnexpectedTypeInResponse(response, callback); + } + + @Override + public void onConnectedEvent() { + + } + + @Override + public void onDisconnectedEvent() { + + } + + @Override + public void onTimeOutEvent() { + callback.err(new MessageTimeoutException()); + } + }); + } + + private void handleUnexpectedTypeInResponse(Message response, IMessageCallback callback){ + switch (response.getType()) { + case ERROR_QUERY: + callback.err(messageToolFactory.getObjectParser(Throwable.class).parse(response.getField("Detail_erreur"))); + break; + case REFUSED_QUERY: + callback.ref(messageToolFactory.getObjectParser(Throwable.class).parse(response.getField("Detail_refus"))); + break; + default: + callback.err(new IllegalArgumentException( + "Illegal message type for response : " + + "expected \"ACK_SALE\"`, found \"" + response.getType().name() + "\"" + )); + break; + } + } } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/query/QueryFactory.java b/Workspace/client/src/main/java/com/pqt/client/module/query/QueryFactory.java deleted file mode 100644 index 6335c44d..00000000 --- a/Workspace/client/src/main/java/com/pqt/client/module/query/QueryFactory.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.pqt.client.module.query; - -import com.pqt.core.entities.product.ProductUpdate; -import com.pqt.core.entities.query.IQuery; -import com.pqt.core.entities.sale.Sale; -import com.pqt.core.entities.user_account.Account; - -import java.util.List; - -//TODO écrire contenu méthodes -//TODO écrire javadoc -public class QueryFactory { - - public static IQuery newConnectQuery(String serverAddress) { - return null; - } - - public static IQuery newSaleQuery(Sale sale) { - return null; - } - - public static IQuery newStockQuery() { - return null; - } - - public static IQuery newStatQuery() { - return null; - } - - public static IQuery newLogQuery(Account account, boolean state) { - return null; - } - - public static IQuery newUpdateQuery(List updates) { - return null; - } - -} diff --git a/Workspace/client/src/main/java/com/pqt/client/module/query/QueryMessageFactory.java b/Workspace/client/src/main/java/com/pqt/client/module/query/QueryMessageFactory.java new file mode 100644 index 00000000..52e934c7 --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/module/query/QueryMessageFactory.java @@ -0,0 +1,69 @@ +package com.pqt.client.module.query; + +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.ProductUpdate; +import com.pqt.core.entities.sale.Sale; +import com.pqt.core.entities.user_account.Account; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +//TODO écrire javadoc +class QueryMessageFactory { + + private final IMessageToolFactory messageToolFactory; + + QueryMessageFactory(IMessageToolFactory messageToolFactory) { + this.messageToolFactory = messageToolFactory; + } + + Message newSaleMessage(Sale sale) { + Map fields = new HashMap<>(); + fields.put("sale", messageToolFactory.getObjectFormatter(Sale.class).format(sale)); + return newSimpleMessage(MessageType.QUERY_SALE, fields); + } + + Message newStockMessage() { + return newSimpleMessage(MessageType.QUERY_STOCK); + } + + Message newStatMessage() { + return newSimpleMessage(MessageType.QUERY_STAT); + } + + Message newUpdateMessage(List updates) { + Map fields = new HashMap<>(); + fields.put("updates", messageToolFactory.getListFormatter(ProductUpdate.class).format(updates)); + return newSimpleMessage(MessageType.QUERY_UPDATE, fields); + } + + Message newAccountListMessage(){ + return newSimpleMessage(MessageType.QUERY_ACCOUNT_LIST); + } + + Message newConnectAccountMessage(Account account, boolean desiredState){ + Map fields = new HashMap<>(); + fields.put("account", messageToolFactory.getObjectFormatter(Account.class).format(account)); + fields.put("desired_state", messageToolFactory.getObjectFormatter(Boolean.class).format(desiredState)); + return newSimpleMessage(MessageType.QUERY_CONNECT_ACCOUNT, fields); + } + + Message newPingMessage(){ + return newSimpleMessage(MessageType.QUERY_PING); + } + + Message newConfigListMessage(){ + return newSimpleMessage(MessageType.QUERY_CONFIG_LIST); + } + + private Message newSimpleMessage(MessageType type, Map fields){ + return new Message(type, null, null, null, null, fields); + } + + private Message newSimpleMessage(MessageType type){ + return newSimpleMessage(type, null); + } +} diff --git a/Workspace/client/src/main/java/com/pqt/client/module/query/exceptions/HeaderNotFoundException.java b/Workspace/client/src/main/java/com/pqt/client/module/query/exceptions/HeaderNotFoundException.java new file mode 100644 index 00000000..5ce2a4b1 --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/module/query/exceptions/HeaderNotFoundException.java @@ -0,0 +1,19 @@ +package com.pqt.client.module.query.exceptions; + +public class HeaderNotFoundException extends Exception { + public HeaderNotFoundException() { + super(); + } + + public HeaderNotFoundException(String message) { + super(message); + } + + public HeaderNotFoundException(String message, Throwable cause) { + super(message, cause); + } + + public HeaderNotFoundException(Throwable cause) { + super(cause); + } +} diff --git a/Workspace/client/src/main/java/com/pqt/client/module/query/exceptions/MessageTimeoutException.java b/Workspace/client/src/main/java/com/pqt/client/module/query/exceptions/MessageTimeoutException.java new file mode 100644 index 00000000..c25463af --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/module/query/exceptions/MessageTimeoutException.java @@ -0,0 +1,19 @@ +package com.pqt.client.module.query.exceptions; + +public class MessageTimeoutException extends Exception { + public MessageTimeoutException() { + super(); + } + + public MessageTimeoutException(String message) { + super(message); + } + + public MessageTimeoutException(String message, Throwable cause) { + super(message, cause); + } + + public MessageTimeoutException(Throwable cause) { + super(cause); + } +} diff --git a/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/ICollectionItemMessageCallback.java b/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/ICollectionItemMessageCallback.java new file mode 100644 index 00000000..95b5382b --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/ICollectionItemMessageCallback.java @@ -0,0 +1,6 @@ +package com.pqt.client.module.query.query_callback; + +import java.util.Collection; + +public interface ICollectionItemMessageCallback extends IItemMessageCallback> { +} diff --git a/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IIdQueryCallback.java b/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IIdQueryCallback.java deleted file mode 100644 index 6abe97de..00000000 --- a/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IIdQueryCallback.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.pqt.client.module.query.query_callback; - -public interface IIdQueryCallback { - public void ack(long id); - public void err(long id, Throwable cause); - public void ref(long id, Throwable cause); -} diff --git a/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IItemMessageCallback.java b/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IItemMessageCallback.java new file mode 100644 index 00000000..9aca2954 --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IItemMessageCallback.java @@ -0,0 +1,5 @@ +package com.pqt.client.module.query.query_callback; + +public interface IItemMessageCallback extends IMessageCallback { + void ack(T obj); +} diff --git a/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IMapItemMessageCallback.java b/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IMapItemMessageCallback.java new file mode 100644 index 00000000..c9d4cb1a --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IMapItemMessageCallback.java @@ -0,0 +1,7 @@ +package com.pqt.client.module.query.query_callback; + + +import java.util.Map; + +public interface IMapItemMessageCallback extends IItemMessageCallback> { +} diff --git a/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IMessageCallback.java b/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IMessageCallback.java new file mode 100644 index 00000000..5efcf8ad --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IMessageCallback.java @@ -0,0 +1,6 @@ +package com.pqt.client.module.query.query_callback; + +public interface IMessageCallback { + void err(Throwable cause); + void ref(Throwable cause); +} diff --git a/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/INoItemMessageCallback.java b/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/INoItemMessageCallback.java new file mode 100644 index 00000000..60c242ed --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/INoItemMessageCallback.java @@ -0,0 +1,5 @@ +package com.pqt.client.module.query.query_callback; + +public interface INoItemMessageCallback extends IMessageCallback { + void ack(); +} diff --git a/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/ISimpleQueryCallback.java b/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/ISimpleQueryCallback.java deleted file mode 100644 index 83428624..00000000 --- a/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/ISimpleQueryCallback.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.pqt.client.module.query.query_callback; - -public interface ISimpleQueryCallback { - public void ack(); - public void err(Throwable cause); - public void ref(Throwable cause); -} diff --git a/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IStatQueryCallback.java b/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IStatQueryCallback.java deleted file mode 100644 index 4de628dc..00000000 --- a/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IStatQueryCallback.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.pqt.client.module.query.query_callback; - - -import java.util.Map; - -public interface IStatQueryCallback { - public void ack(Map stats); - public void err(Throwable cause); - public void ref(Throwable cause); -} diff --git a/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IStockQueryCallback.java b/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IStockQueryCallback.java deleted file mode 100644 index f85a7fe5..00000000 --- a/Workspace/client/src/main/java/com/pqt/client/module/query/query_callback/IStockQueryCallback.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.pqt.client.module.query.query_callback; - -import com.pqt.core.entities.product.Product; - -import java.util.List; - -public interface IStockQueryCallback { - public void ack(List products); - public void err(Throwable cause); - public void ref(Throwable cause); -} diff --git a/Workspace/client/src/main/java/com/pqt/client/module/sale/SaleService.java b/Workspace/client/src/main/java/com/pqt/client/module/sale/SaleService.java index 081df440..53eefc4a 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/sale/SaleService.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/sale/SaleService.java @@ -1,8 +1,9 @@ package com.pqt.client.module.sale; import com.pqt.client.module.query.QueryExecutor; -import com.pqt.client.module.query.QueryFactory; -import com.pqt.client.module.query.query_callback.IIdQueryCallback; +import com.pqt.client.module.query.QueryMessageFactory; +import com.pqt.client.module.query.query_callback.ICollectionItemMessageCallback; +import com.pqt.client.module.query.query_callback.INoItemMessageCallback; import com.pqt.client.module.sale.listeners.ISaleFirerer; import com.pqt.client.module.sale.listeners.ISaleListener; import com.pqt.client.module.sale.listeners.SimpleSaleFirerer; @@ -14,10 +15,14 @@ import java.util.List; //TODO add log lines public class SaleService { + private long saleId; private ISaleFirerer eventFirerer; + private QueryExecutor executor; - public SaleService() { + public SaleService(QueryExecutor executor) { + saleId = 0; eventFirerer = new SimpleSaleFirerer(); + this.executor = executor; } public SaleBuilder getNewSaleBuilder() { @@ -25,22 +30,30 @@ public class SaleService { } public long commitSale(SaleBuilder saleBuilder) { - return QueryExecutor.INSTANCE.execute(QueryFactory.newSaleQuery(saleBuilder.build()), new IIdQueryCallback() { + final long currentSaleId = saleId; + if(saleId stats; private IStatFirerer eventFirerer; + private QueryExecutor executor; - public StatDao() { + public StatDao(QueryExecutor executor) { eventFirerer = new SimpleStatFirerer(); stats = new HashMap<>(); lastRefreshTimestamp = null; + this.executor = executor; } public synchronized Map getStats() { @@ -27,14 +29,7 @@ public class StatDao { } public void refreshStats() { - QueryExecutor.INSTANCE.execute(QueryFactory.newStockQuery(), new IStatQueryCallback() { - @Override - public void ack(Map stats) { - replaceStats(stats); - eventFirerer.fireGetStatSuccess(); - //TODO add log line - } - + executor.executeStatQuery(new IMapItemMessageCallback() { @Override public void err(Throwable cause) { eventFirerer.fireGetStatError(cause); @@ -46,6 +41,13 @@ public class StatDao { eventFirerer.fireGetStatRefused(cause); //TODO add log line } + + @Override + public void ack(Map stats) { + replaceStats(stats); + eventFirerer.fireGetStatSuccess(); + //TODO add log line + } }); } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/stat/StatService.java b/Workspace/client/src/main/java/com/pqt/client/module/stat/StatService.java index 8a642b0a..e8278ca3 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/stat/StatService.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/stat/StatService.java @@ -1,5 +1,6 @@ package com.pqt.client.module.stat; +import com.pqt.client.module.query.QueryExecutor; import com.pqt.client.module.stat.listeners.IStatListener; import java.util.Map; @@ -10,8 +11,8 @@ public class StatService { private StatDao dao; - public StatService() { - dao = new StatDao(); + public StatService(QueryExecutor executor) { + dao = new StatDao(executor); } public Map getStats() { diff --git a/Workspace/client/src/main/java/com/pqt/client/module/stock/StockDao.java b/Workspace/client/src/main/java/com/pqt/client/module/stock/StockDao.java index 08439d66..8ea55f44 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/stock/StockDao.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/stock/StockDao.java @@ -1,9 +1,8 @@ package com.pqt.client.module.stock; import com.pqt.client.module.query.QueryExecutor; -import com.pqt.client.module.query.QueryFactory; -import com.pqt.client.module.query.query_callback.IStockQueryCallback; -import com.pqt.client.module.query.query_callback.IIdQueryCallback; +import com.pqt.client.module.query.query_callback.ICollectionItemMessageCallback; +import com.pqt.client.module.query.query_callback.INoItemMessageCallback; import com.pqt.client.module.stock.Listeners.IStockFirerer; import com.pqt.client.module.stock.Listeners.IStockListener; import com.pqt.client.module.stock.Listeners.SimpleStockFirerer; @@ -11,18 +10,22 @@ import com.pqt.core.entities.product.Product; import com.pqt.core.entities.product.ProductUpdate; import java.util.ArrayList; +import java.util.Collection; import java.util.Date; import java.util.List; -import java.util.Optional; //TODO écrire javadoc public class StockDao { + private long updateId; private IStockFirerer eventFirerer; private Date lastRefreshTimestamp; private List products; + private QueryExecutor executor; - public StockDao() { + public StockDao(QueryExecutor executor) { + this.executor = executor; + updateId = 0; eventFirerer = new SimpleStockFirerer(); products = new ArrayList<>(); lastRefreshTimestamp = null; @@ -37,9 +40,9 @@ public class StockDao { } public void refreshProductList() { - QueryExecutor.INSTANCE.execute(QueryFactory.newStockQuery(), new IStockQueryCallback() { + executor.executeStockQuery(new ICollectionItemMessageCallback() { @Override - public void ack(List products) { + public void ack(Collection obj) { replaceProductList(products); eventFirerer.fireGetProductListSuccessEvent(); //TODO add log line @@ -71,27 +74,33 @@ public class StockDao { } public long commitUpdate(List updates){ - return QueryExecutor.INSTANCE.execute(QueryFactory.newUpdateQuery(updates),new IIdQueryCallback(){ - + final long currentUpdateId = updateId; + if(updateId