From cc44f2ee8efbb6a25884ddddad1d8f2a8b872371 Mon Sep 17 00:00:00 2001 From: Notmoo Date: Sun, 23 Jul 2017 18:47:18 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20outils=20de=20g=C3=A9n=C3=A9ration/lect?= =?UTF-8?q?ure=20de=20messages=20dans=20package=20core?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pqt/client/module/sale/SaleBuilder.java | 2 +- Workspace/core/pom.xml | 10 ++ .../communication/GSonMessageToolFactory.java | 122 ++++++++++++++++++ .../communication/IMessageToolFactory.java | 46 +++++++ .../core/communication/IObjectFormatter.java | 7 + .../pqt/core/communication/IObjectParser.java | 6 + .../entities/{client => members}/Client.java | 24 ++-- .../pqt/core/entities/members/DataServer.java | 42 ++++++ .../pqt/core/entities/members/PqtMember.java | 35 +++++ .../core/entities/members/PqtMemberType.java | 7 + .../com/pqt/core/entities/messages/Field.java | 22 ++++ .../pqt/core/entities/messages/Message.java | 42 ++++++ .../core/entities/messages/MessageType.java | 6 + .../java/com/pqt/core/entities/sale/Sale.java | 2 +- 14 files changed, 357 insertions(+), 16 deletions(-) create mode 100644 Workspace/core/src/main/java/com/pqt/core/communication/GSonMessageToolFactory.java create mode 100644 Workspace/core/src/main/java/com/pqt/core/communication/IMessageToolFactory.java create mode 100644 Workspace/core/src/main/java/com/pqt/core/communication/IObjectFormatter.java create mode 100644 Workspace/core/src/main/java/com/pqt/core/communication/IObjectParser.java rename Workspace/core/src/main/java/com/pqt/core/entities/{client => members}/Client.java (67%) create mode 100644 Workspace/core/src/main/java/com/pqt/core/entities/members/DataServer.java create mode 100644 Workspace/core/src/main/java/com/pqt/core/entities/members/PqtMember.java create mode 100644 Workspace/core/src/main/java/com/pqt/core/entities/members/PqtMemberType.java create mode 100644 Workspace/core/src/main/java/com/pqt/core/entities/messages/Field.java create mode 100644 Workspace/core/src/main/java/com/pqt/core/entities/messages/Message.java create mode 100644 Workspace/core/src/main/java/com/pqt/core/entities/messages/MessageType.java diff --git a/Workspace/client/src/main/java/com/pqt/client/module/sale/SaleBuilder.java b/Workspace/client/src/main/java/com/pqt/client/module/sale/SaleBuilder.java index 9d205d64..4c550960 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/sale/SaleBuilder.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/sale/SaleBuilder.java @@ -1,6 +1,6 @@ package com.pqt.client.module.sale; -import com.pqt.core.entities.client.Client; +import com.pqt.core.entities.members.Client; import com.pqt.core.entities.product.Product; import com.pqt.core.entities.sale.Sale; import com.pqt.core.entities.sale.SaleStatus; diff --git a/Workspace/core/pom.xml b/Workspace/core/pom.xml index ab69cb6e..3837197a 100644 --- a/Workspace/core/pom.xml +++ b/Workspace/core/pom.xml @@ -12,4 +12,14 @@ core + + + + + com.google.code.gson + gson + 2.8.1 + + + \ No newline at end of file diff --git a/Workspace/core/src/main/java/com/pqt/core/communication/GSonMessageToolFactory.java b/Workspace/core/src/main/java/com/pqt/core/communication/GSonMessageToolFactory.java new file mode 100644 index 00000000..0534273f --- /dev/null +++ b/Workspace/core/src/main/java/com/pqt/core/communication/GSonMessageToolFactory.java @@ -0,0 +1,122 @@ +package com.pqt.core.communication; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import com.pqt.core.entities.members.PqtMember; +import com.pqt.core.entities.members.PqtMemberType; +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 java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; + +//TODO écrire Javadoc +public class GSonMessageToolFactory implements IMessageToolFactory { + private Gson gson; + + public GSonMessageToolFactory() { + gson = new GsonBuilder().create(); + } + + @Override + public IObjectFormatter getMessageFormatter() { + return getObjectFormatter(Message.class); + } + + @Override + public IObjectParser getMessageParser() { + return (str)->gson.fromJson(str, Message.class); + } + + @Override + public IObjectFormatter getProductFormatter() { + return getObjectFormatter(Product.class); + } + + @Override + public IObjectParser getProductParser() { + return getObjectParser(Product.class); + } + + @Override + public IObjectFormatter> getProductListFormatter() { + return getListFormatter(Product.class); + } + + @Override + public IObjectParser> getProductListParser() { + return getListParser(Product.class); + } + + @Override + public IObjectFormatter getSaleFormatter() { + return getObjectFormatter(Sale.class); + } + + @Override + public IObjectParser getSaleParser() { + return null; + } + + @Override + public IObjectFormatter getPqtMemberFormatter() { + return getObjectFormatter(PqtMember.class); + } + + @Override + public IObjectParser getPqtMemberParser() { + return getObjectParser(PqtMember.class); + } + + @Override + public IObjectFormatter getProductUpdateFormatter() { + return getObjectFormatter(ProductUpdate.class); + } + + @Override + public IObjectParser getProductUpdateParser() { + return getObjectParser(ProductUpdate.class); + } + + @Override + public IObjectFormatter getMessageTypeFormatter() { + return getObjectFormatter(MessageType.class); + } + + @Override + public IObjectParser getMessageTypeParser() { + return getObjectParser(MessageType.class); + } + + @Override + public IObjectFormatter getPqtMemberTypeFormatter() { + return getObjectFormatter(PqtMemberType.class); + } + + @Override + public IObjectParser getPqtMemberTypeParser() { + return getObjectParser(PqtMemberType.class); + } + + private IObjectFormatter getObjectFormatter(Class clazz){ + return (obj)->gson.toJson(obj); + } + + private IObjectParser getObjectParser(Class clazz){ + return (str)->gson.fromJson(str, clazz); + } + + private IObjectFormatter> getListFormatter(Class clazz){ + return (obj)->gson.toJson(obj); + } + + private IObjectParser> getListParser(Class clazz){ + Type listType = new TypeToken>(){}.getType(); + return (str)->gson.fromJson(str, listType); + } +} diff --git a/Workspace/core/src/main/java/com/pqt/core/communication/IMessageToolFactory.java b/Workspace/core/src/main/java/com/pqt/core/communication/IMessageToolFactory.java new file mode 100644 index 00000000..b2ca97c5 --- /dev/null +++ b/Workspace/core/src/main/java/com/pqt/core/communication/IMessageToolFactory.java @@ -0,0 +1,46 @@ +package com.pqt.core.communication; + +import com.pqt.core.entities.members.PqtMember; +import com.pqt.core.entities.members.PqtMemberType; +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 java.util.List; + +public interface IMessageToolFactory { + + IObjectFormatter getMessageFormatter(); + + IObjectParser getMessageParser(); + + IObjectFormatter getProductFormatter(); + + IObjectParser getProductParser(); + + IObjectFormatter> getProductListFormatter(); + + IObjectParser> getProductListParser(); + + IObjectFormatter getSaleFormatter(); + + IObjectParser getSaleParser(); + + IObjectFormatter getPqtMemberFormatter(); + + IObjectParser getPqtMemberParser(); + + IObjectFormatter getProductUpdateFormatter(); + + IObjectParser getProductUpdateParser(); + + IObjectFormatter getMessageTypeFormatter(); + + IObjectParser getMessageTypeParser(); + + IObjectFormatter getPqtMemberTypeFormatter(); + + IObjectParser getPqtMemberTypeParser(); +} diff --git a/Workspace/core/src/main/java/com/pqt/core/communication/IObjectFormatter.java b/Workspace/core/src/main/java/com/pqt/core/communication/IObjectFormatter.java new file mode 100644 index 00000000..df4b05bf --- /dev/null +++ b/Workspace/core/src/main/java/com/pqt/core/communication/IObjectFormatter.java @@ -0,0 +1,7 @@ +package com.pqt.core.communication; + +public interface IObjectFormatter { + + String format(T obj); + +} diff --git a/Workspace/core/src/main/java/com/pqt/core/communication/IObjectParser.java b/Workspace/core/src/main/java/com/pqt/core/communication/IObjectParser.java new file mode 100644 index 00000000..6a354eef --- /dev/null +++ b/Workspace/core/src/main/java/com/pqt/core/communication/IObjectParser.java @@ -0,0 +1,6 @@ +package com.pqt.core.communication; + +public interface IObjectParser { + + T parse(String str); +} diff --git a/Workspace/core/src/main/java/com/pqt/core/entities/client/Client.java b/Workspace/core/src/main/java/com/pqt/core/entities/members/Client.java similarity index 67% rename from Workspace/core/src/main/java/com/pqt/core/entities/client/Client.java rename to Workspace/core/src/main/java/com/pqt/core/entities/members/Client.java index b8306b55..0d667489 100644 --- a/Workspace/core/src/main/java/com/pqt/core/entities/client/Client.java +++ b/Workspace/core/src/main/java/com/pqt/core/entities/members/Client.java @@ -1,6 +1,4 @@ -package com.pqt.core.entities.client; - -import com.pqt.core.entities.log.ILoggable; +package com.pqt.core.entities.members; import java.io.Serializable; import java.util.Date; @@ -8,29 +6,27 @@ import java.util.Date; /** * Created by Notmoo on 18/07/2017. */ -public class Client implements ILoggable, Serializable { +public class Client extends PqtMember{ - private int id; private String address; private Date lastUpdate; public Client() { + super(-1, PqtMemberType.CLIENT); + } + + public Client(int id, String address) { + super(id, PqtMemberType.CLIENT); + this.address = address; + this.lastUpdate = new Date(); } public Client(int id, String address, Date lastUpdate) { - this.id = id; + super(id, PqtMemberType.CLIENT); this.address = address; this.lastUpdate = lastUpdate; } - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - public String getAddress() { return address; } diff --git a/Workspace/core/src/main/java/com/pqt/core/entities/members/DataServer.java b/Workspace/core/src/main/java/com/pqt/core/entities/members/DataServer.java new file mode 100644 index 00000000..9804f487 --- /dev/null +++ b/Workspace/core/src/main/java/com/pqt/core/entities/members/DataServer.java @@ -0,0 +1,42 @@ +package com.pqt.core.entities.members; + +import java.io.Serializable; +import java.util.Date; + +public class DataServer extends PqtMember{ + + private String address; + private Date lastUpdate; + + public DataServer() { + super(-1, PqtMemberType.DATA_SERVER); + } + + public DataServer(long id, String address) { + super(id, PqtMemberType.DATA_SERVER); + this.address = address; + this.lastUpdate = new Date(); + } + + public DataServer(long id, String address, Date lastUpdate) { + super(id, PqtMemberType.DATA_SERVER); + this.address = address; + this.lastUpdate = lastUpdate; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public Date getLastUpdate() { + return lastUpdate; + } + + public void setLastUpdate(Date lastUpdate) { + this.lastUpdate = lastUpdate; + } +} diff --git a/Workspace/core/src/main/java/com/pqt/core/entities/members/PqtMember.java b/Workspace/core/src/main/java/com/pqt/core/entities/members/PqtMember.java new file mode 100644 index 00000000..94399c6b --- /dev/null +++ b/Workspace/core/src/main/java/com/pqt/core/entities/members/PqtMember.java @@ -0,0 +1,35 @@ +package com.pqt.core.entities.members; + +import com.pqt.core.entities.log.ILoggable; + +import java.io.Serializable; + +public class PqtMember implements ILoggable, Serializable { + + private long id; + private PqtMemberType type; + + public PqtMember() { + } + + public PqtMember(long id, PqtMemberType type) { + this.id = id; + this.type = type; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public PqtMemberType getType() { + return type; + } + + public void setType(PqtMemberType type) { + this.type = type; + } +} diff --git a/Workspace/core/src/main/java/com/pqt/core/entities/members/PqtMemberType.java b/Workspace/core/src/main/java/com/pqt/core/entities/members/PqtMemberType.java new file mode 100644 index 00000000..2c8e22de --- /dev/null +++ b/Workspace/core/src/main/java/com/pqt/core/entities/members/PqtMemberType.java @@ -0,0 +1,7 @@ +package com.pqt.core.entities.members; + +public enum PqtMemberType { + + CLIENT, DATA_SERVER; + +} diff --git a/Workspace/core/src/main/java/com/pqt/core/entities/messages/Field.java b/Workspace/core/src/main/java/com/pqt/core/entities/messages/Field.java new file mode 100644 index 00000000..a40f74cb --- /dev/null +++ b/Workspace/core/src/main/java/com/pqt/core/entities/messages/Field.java @@ -0,0 +1,22 @@ +package com.pqt.core.entities.messages; + +import java.io.Serializable; + +public class Field implements Serializable{ + + private String header; + private String data; + + public Field(String header, String data) { + this.header = header; + this.data = data; + } + + public String getHeader() { + return header; + } + + public String getData() { + return data; + } +} diff --git a/Workspace/core/src/main/java/com/pqt/core/entities/messages/Message.java b/Workspace/core/src/main/java/com/pqt/core/entities/messages/Message.java new file mode 100644 index 00000000..26ce58f4 --- /dev/null +++ b/Workspace/core/src/main/java/com/pqt/core/entities/messages/Message.java @@ -0,0 +1,42 @@ +package com.pqt.core.entities.messages; + +import com.pqt.core.entities.members.PqtMember; + +import java.util.ArrayList; +import java.util.List; + +public class Message { + + private List fields; + private MessageType type; + private PqtMember emitter, receiver; + + public Message(MessageType type, PqtMember emitter, PqtMember receiver) { + this(type, emitter, receiver, null); + } + + public Message(MessageType type, PqtMember emitter, PqtMember receiver, List fields) { + this.emitter = emitter; + this.receiver = receiver; + this.type = type; + this.fields = new ArrayList<>(); + if(fields!=null) + this.fields.addAll(fields); + } + + public List getFields() { + return new ArrayList<>(fields); + } + + public PqtMember getEmitter() { + return emitter; + } + + public PqtMember getReceiver() { + return receiver; + } + + public MessageType getType() { + return type; + } +} diff --git a/Workspace/core/src/main/java/com/pqt/core/entities/messages/MessageType.java b/Workspace/core/src/main/java/com/pqt/core/entities/messages/MessageType.java new file mode 100644 index 00000000..e6f052f6 --- /dev/null +++ b/Workspace/core/src/main/java/com/pqt/core/entities/messages/MessageType.java @@ -0,0 +1,6 @@ +package com.pqt.core.entities.messages; + +public enum MessageType { + //TODO ajouter types messages + TEST_MESSAGE +} diff --git a/Workspace/core/src/main/java/com/pqt/core/entities/sale/Sale.java b/Workspace/core/src/main/java/com/pqt/core/entities/sale/Sale.java index 3a805f5c..1da9bd55 100644 --- a/Workspace/core/src/main/java/com/pqt/core/entities/sale/Sale.java +++ b/Workspace/core/src/main/java/com/pqt/core/entities/sale/Sale.java @@ -1,7 +1,7 @@ package com.pqt.core.entities.sale; import com.pqt.core.entities.log.ILoggable; -import com.pqt.core.entities.client.Client; +import com.pqt.core.entities.members.Client; import com.pqt.core.entities.product.Product; import com.pqt.core.entities.user_account.Account;