Ajout outils de génération/lecture de messages dans package core

This commit is contained in:
Notmoo 2017-07-23 18:47:18 +02:00
parent 3b30621075
commit cc44f2ee8e
14 changed files with 357 additions and 16 deletions

View File

@ -1,6 +1,6 @@
package com.pqt.client.module.sale; 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.product.Product;
import com.pqt.core.entities.sale.Sale; import com.pqt.core.entities.sale.Sale;
import com.pqt.core.entities.sale.SaleStatus; import com.pqt.core.entities.sale.SaleStatus;

View File

@ -12,4 +12,14 @@
<artifactId>core</artifactId> <artifactId>core</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
</project> </project>

View File

@ -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<Message> getMessageFormatter() {
return getObjectFormatter(Message.class);
}
@Override
public IObjectParser<Message> getMessageParser() {
return (str)->gson.fromJson(str, Message.class);
}
@Override
public IObjectFormatter<Product> getProductFormatter() {
return getObjectFormatter(Product.class);
}
@Override
public IObjectParser<Product> getProductParser() {
return getObjectParser(Product.class);
}
@Override
public IObjectFormatter<List<Product>> getProductListFormatter() {
return getListFormatter(Product.class);
}
@Override
public IObjectParser<List<Product>> getProductListParser() {
return getListParser(Product.class);
}
@Override
public IObjectFormatter<Sale> getSaleFormatter() {
return getObjectFormatter(Sale.class);
}
@Override
public IObjectParser<Sale> getSaleParser() {
return null;
}
@Override
public IObjectFormatter<PqtMember> getPqtMemberFormatter() {
return getObjectFormatter(PqtMember.class);
}
@Override
public IObjectParser<PqtMember> getPqtMemberParser() {
return getObjectParser(PqtMember.class);
}
@Override
public IObjectFormatter<ProductUpdate> getProductUpdateFormatter() {
return getObjectFormatter(ProductUpdate.class);
}
@Override
public IObjectParser<ProductUpdate> getProductUpdateParser() {
return getObjectParser(ProductUpdate.class);
}
@Override
public IObjectFormatter<MessageType> getMessageTypeFormatter() {
return getObjectFormatter(MessageType.class);
}
@Override
public IObjectParser<MessageType> getMessageTypeParser() {
return getObjectParser(MessageType.class);
}
@Override
public IObjectFormatter<PqtMemberType> getPqtMemberTypeFormatter() {
return getObjectFormatter(PqtMemberType.class);
}
@Override
public IObjectParser<PqtMemberType> getPqtMemberTypeParser() {
return getObjectParser(PqtMemberType.class);
}
private <T> IObjectFormatter<T> getObjectFormatter(Class<T> clazz){
return (obj)->gson.toJson(obj);
}
private <T> IObjectParser<T> getObjectParser(Class<T> clazz){
return (str)->gson.fromJson(str, clazz);
}
private <T> IObjectFormatter<List<T>> getListFormatter(Class<T> clazz){
return (obj)->gson.toJson(obj);
}
private <T> IObjectParser<List<T>> getListParser(Class<T> clazz){
Type listType = new TypeToken<ArrayList<T>>(){}.getType();
return (str)->gson.fromJson(str, listType);
}
}

View File

@ -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<Message> getMessageFormatter();
IObjectParser<Message> getMessageParser();
IObjectFormatter<Product> getProductFormatter();
IObjectParser<Product> getProductParser();
IObjectFormatter<List<Product>> getProductListFormatter();
IObjectParser<List<Product>> getProductListParser();
IObjectFormatter<Sale> getSaleFormatter();
IObjectParser<Sale> getSaleParser();
IObjectFormatter<PqtMember> getPqtMemberFormatter();
IObjectParser<PqtMember> getPqtMemberParser();
IObjectFormatter<ProductUpdate> getProductUpdateFormatter();
IObjectParser<ProductUpdate> getProductUpdateParser();
IObjectFormatter<MessageType> getMessageTypeFormatter();
IObjectParser<MessageType> getMessageTypeParser();
IObjectFormatter<PqtMemberType> getPqtMemberTypeFormatter();
IObjectParser<PqtMemberType> getPqtMemberTypeParser();
}

View File

@ -0,0 +1,7 @@
package com.pqt.core.communication;
public interface IObjectFormatter<T> {
String format(T obj);
}

View File

@ -0,0 +1,6 @@
package com.pqt.core.communication;
public interface IObjectParser<T> {
T parse(String str);
}

View File

@ -1,6 +1,4 @@
package com.pqt.core.entities.client; package com.pqt.core.entities.members;
import com.pqt.core.entities.log.ILoggable;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
@ -8,29 +6,27 @@ import java.util.Date;
/** /**
* Created by Notmoo on 18/07/2017. * 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 String address;
private Date lastUpdate; private Date lastUpdate;
public Client() { 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) { public Client(int id, String address, Date lastUpdate) {
this.id = id; super(id, PqtMemberType.CLIENT);
this.address = address; this.address = address;
this.lastUpdate = lastUpdate; this.lastUpdate = lastUpdate;
} }
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() { public String getAddress() {
return address; return address;
} }

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,7 @@
package com.pqt.core.entities.members;
public enum PqtMemberType {
CLIENT, DATA_SERVER;
}

View File

@ -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;
}
}

View File

@ -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<Field> 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<Field> fields) {
this.emitter = emitter;
this.receiver = receiver;
this.type = type;
this.fields = new ArrayList<>();
if(fields!=null)
this.fields.addAll(fields);
}
public List<Field> getFields() {
return new ArrayList<>(fields);
}
public PqtMember getEmitter() {
return emitter;
}
public PqtMember getReceiver() {
return receiver;
}
public MessageType getType() {
return type;
}
}

View File

@ -0,0 +1,6 @@
package com.pqt.core.entities.messages;
public enum MessageType {
//TODO ajouter types messages
TEST_MESSAGE
}

View File

@ -1,7 +1,7 @@
package com.pqt.core.entities.sale; package com.pqt.core.entities.sale;
import com.pqt.core.entities.log.ILoggable; 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.product.Product;
import com.pqt.core.entities.user_account.Account; import com.pqt.core.entities.user_account.Account;