diff --git a/Workspace/client/src/main/java/com/pqt/client/gui/modules/account_screen/AccountScreenModel.java b/Workspace/client/src/main/java/com/pqt/client/gui/modules/account_screen/AccountScreenModel.java index d682efbc..7bc60966 100644 --- a/Workspace/client/src/main/java/com/pqt/client/gui/modules/account_screen/AccountScreenModel.java +++ b/Workspace/client/src/main/java/com/pqt/client/gui/modules/account_screen/AccountScreenModel.java @@ -2,6 +2,7 @@ package com.pqt.client.gui.modules.account_screen; import com.pqt.client.gui.modules.account_screen.listeners.IAccountScreenModelListener; import com.pqt.client.module.account.AccountService; +import com.pqt.client.module.account.listeners.AccountUpdateBuilder; import com.pqt.client.module.account.listeners.IAccountListener; import com.pqt.core.entities.user_account.Account; import com.pqt.core.entities.user_account.AccountLevel; @@ -40,15 +41,15 @@ class AccountScreenModel { } void modifyAccount(Account oldVal, Account newVal) { - //accountService.submitAccountUpdate(oldVal, newVal); + accountService.submitAccountUpdate(new AccountUpdateBuilder().modifyAccount(oldVal,newVal)); } void addAccount(Account newVal) { - //accountService.submitAccountUpdate(null, newVal); + accountService.submitAccountUpdate(new AccountUpdateBuilder().addAccount(newVal)); } void removeAccount(Account oldVal) { - //accountService.submitAccountUpdate(oldVal, null); + accountService.submitAccountUpdate(new AccountUpdateBuilder().removeAccount(oldVal)); } Collection getAccountCollection() { 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 d9f9884c..341dd1b6 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,10 +1,10 @@ package com.pqt.client.module.account; import com.pqt.client.module.account.listeners.AccountListenerAdapter; +import com.pqt.client.module.account.listeners.AccountUpdateBuilder; 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; @@ -12,7 +12,6 @@ import javax.swing.event.EventListenerList; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.List; import java.util.concurrent.CountDownLatch; //TODO écrire javadoc @@ -155,4 +154,24 @@ public class AccountService { //Nothing to do } } + + public void submitAccountUpdate(AccountUpdateBuilder builder) { + executor.executeAccountUpdateQuery(builder.build(), new INoItemMessageCallback() { + @Override + public void ack() { + //TODO Issue #6 : add log line + refreshAccounts(); + } + + @Override + public void err(Throwable cause) { + //TODO Issue #6 : add log line + } + + @Override + public void ref(Throwable cause) { + //TODO Issue #6 : add log line + } + }); + } } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/account/listeners/AccountUpdateBuilder.java b/Workspace/client/src/main/java/com/pqt/client/module/account/listeners/AccountUpdateBuilder.java new file mode 100644 index 00000000..37a82b8d --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/module/account/listeners/AccountUpdateBuilder.java @@ -0,0 +1,52 @@ +package com.pqt.client.module.account.listeners; + +import com.pqt.core.entities.user_account.Account; +import com.pqt.core.entities.user_account.AccountUpdate; + +import java.util.*; + +public class AccountUpdateBuilder { + + private Set toAdd, toRemove; + private Map toModify; + + public AccountUpdateBuilder() { + toAdd = new HashSet<>(); + toRemove = new HashSet<>(); + toModify = new HashMap<>(); + } + + public AccountUpdateBuilder addAccount(Account account) { + if(!toAdd.contains(account)){ + toAdd.add(account); + } + return this; + } + + public AccountUpdateBuilder removeAccount(Account account) { + if(!toRemove.contains(account)){ + toRemove.add(account); + } + return this; + } + + public AccountUpdateBuilder modifyAccount(Account oldVersion, Account newVersion) { + toModify.put(oldVersion, newVersion); + return this; + } + + public List build() { + List reply = new ArrayList<>(); + for(Account account : toAdd){ + reply.add(new AccountUpdate(null, account)); + } + for(Account account : toRemove){ + reply.add(new AccountUpdate(account, null)); + } + for(Account account : toModify.keySet()){ + reply.add(new AccountUpdate(account, toModify.get(account))); + } + + return reply; + } +} 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 e8f7d3f8..0c5c0307 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 @@ -13,8 +13,8 @@ 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.LightweightSale; -import com.pqt.core.entities.sale.Sale; import com.pqt.core.entities.user_account.Account; +import com.pqt.core.entities.user_account.AccountUpdate; import java.util.List; @@ -47,6 +47,10 @@ public class QueryExecutor { sendMessage(messageFactory.newStockUpdateMessage(updates), callback, MessageType.ACK_STOCK_UPDATE); } + public void executeAccountUpdateQuery(List updates, INoItemMessageCallback callback) { + sendMessage(messageFactory.newAccountUpdateMessage(updates), callback, MessageType.ACK_ACCOUNT_UPDATE); + } + public void executeConnectAccountQuery(Account account, boolean desiredState, INoItemMessageCallback callback){ sendMessage(messageFactory.newConnectAccountMessage(account,desiredState), callback, MessageType.ACK_CONNECT_ACCOUNT); } 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 index 4a7ea260..6f4c7ded 100644 --- 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 @@ -8,6 +8,7 @@ import com.pqt.core.entities.product.ProductUpdate; import com.pqt.core.entities.sale.LightweightSale; import com.pqt.core.entities.sale.Sale; import com.pqt.core.entities.user_account.Account; +import com.pqt.core.entities.user_account.AccountUpdate; import java.util.HashMap; import java.util.List; @@ -48,6 +49,12 @@ class QueryMessageFactory { return newSimpleMessage(MessageType.QUERY_STOCK_UPDATE, fields); } + Message newAccountUpdateMessage(List updates) { + Map fields = new HashMap<>(); + fields.put("updates", messageToolFactory.getListFormatter(AccountUpdate.class).format(updates)); + return newSimpleMessage(MessageType.QUERY_ACCOUNT_UPDATE, fields); + } + Message newAccountListMessage(){ return newSimpleMessage(MessageType.QUERY_ACCOUNT_LIST); }