Merge branch 'feature/#2_CORE_Ajout_outils_protocole_communication' into develop

This commit is contained in:
Notmoo 2017-07-24 17:00:06 +02:00
commit 9c2c2a2acd
23 changed files with 495 additions and 1320 deletions

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
#Ignoring Intellij workspace specific folders and files
/**/.idea/workspace.xml
/**/tasks.xml
/**/*.iml
/**/.idea/libraries
#Ignoring target folders
**/target/

File diff suppressed because it is too large Load Diff

View File

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="core" />
</component>
</module>

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

@ -1,15 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

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,39 @@
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();
}
public <T> IObjectFormatter<T> getObjectFormatter(Class<T> clazz){
return (obj)->gson.toJson(obj);
}
public <T> IObjectParser<T> getObjectParser(Class<T> clazz){
return (str)->gson.fromJson(str, clazz);
}
public <T> IObjectFormatter<List<T>> getListFormatter(Class<T> clazz){
return (obj)->gson.toJson(obj);
}
public <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,25 @@
package com.pqt.core.communication;
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;
public interface IMessageToolFactory {
<T> IObjectFormatter<T> getObjectFormatter(Class<T> clazz);
<T> IObjectParser<T> getObjectParser(Class<T> clazz);
<T> IObjectFormatter<List<T>> getListFormatter(Class<T> clazz);
<T> IObjectParser<List<T>> getListParser(Class<T> clazz);
}

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,54 @@
package com.pqt.core.entities.messages;
import com.pqt.core.entities.members.PqtMember;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Message {
private Map<String, String> 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, Map<String, String> fields) {
this.emitter = emitter;
this.receiver = receiver;
this.type = type;
this.fields = new HashMap<>();
if(fields!=null)
for(String key : fields.keySet()){
this.fields.put(key, fields.get(key));
}
}
public Map<String, String> getFields() {
return new HashMap<>(fields);
}
public boolean hasField(String header){
return fields.containsKey(header);
}
public String getField(String header){
return fields.get(header);
}
public PqtMember getEmitter() {
return emitter;
}
public PqtMember getReceiver() {
return receiver;
}
public MessageType getType() {
return type;
}
}

View File

@ -0,0 +1,38 @@
package com.pqt.core.entities.messages;
public enum MessageType {
QUERY_CONNECT,
ACK_CONNECT,
ERR_CONNECT,
REF_CONNECT,
QUERY_SALE,
ACK_SALE,
ERR_SALE,
REF_SALE,
QUERY_REVERT_SALE,
ACK_REVERT_SALE,
ERR_REVERT_SALE,
REF_REVERT_SALE,
QUERY_STAT,
MSG_STAT,
ERR_STAT,
REF_STAT,
QUERY_STOCK,
MSG_STOCK,
ERR_STOCK,
REF_STOCK,
QUERY_LOGIN,
ACK_LOGIN,
ERR_LOGIN,
REF_LOGIN,
QUERY_UPDATE,
ACK_UPDATE,
ERR_UPDATE,
REF_UPDATE
}

View File

@ -0,0 +1,97 @@
package com.pqt.core.entities.product;
import java.util.ArrayList;
import java.util.List;
public class LightweightProduct {
private long id;
private String name;
private int amountRemaining;
private int amountSold;
private boolean sellable;
private double price;
private List<Long> componentIds;
private Category category;
public LightweightProduct() {
componentIds = new ArrayList<>();
}
public LightweightProduct(Product product) {
this.id = product.getId();
this.name = product.getName();
this.amountRemaining = product.getAmountRemaining();
this.amountSold = product.getAmountSold();
this.sellable = product.isSellable();
this.price = product.getPrice();
this.category = product.getCategory();
this.componentIds = new ArrayList<>();
if(componentIds!=null){
product.getComponents().stream().forEach((component -> componentIds.add(component.getId())));
}
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAmountRemaining() {
return amountRemaining;
}
public void setAmountRemaining(int amountRemaining) {
this.amountRemaining = amountRemaining;
}
public int getAmountSold() {
return amountSold;
}
public void setAmountSold(int amountSold) {
this.amountSold = amountSold;
}
public boolean isSellable() {
return sellable;
}
public void setSellable(boolean sellable) {
this.sellable = sellable;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public List<Long> getComponentIds() {
return componentIds;
}
public void setComponentIds(List<Long> componentIds) {
this.componentIds = componentIds;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}

View File

@ -10,12 +10,12 @@ import java.util.List;
* Created by Notmoo on 18/07/2017. * Created by Notmoo on 18/07/2017.
*/ */
public class Product implements ILoggable, Serializable{ public class Product implements ILoggable, Serializable{
private int id; private long id;
private String name; private String name;
private int amountRemaining; private int amountRemaining;
private int amountSold; private int amountSold;
private boolean sellable; private boolean sellable;
private boolean price; private double price;
private List<Product> components; private List<Product> components;
private Category category; private Category category;
@ -23,22 +23,25 @@ public class Product implements ILoggable, Serializable{
components = new ArrayList<>(); components = new ArrayList<>();
} }
public Product(int id, String name, int amountRemaining, int amountSold, boolean sellable, boolean price, List<Product> components, Category category) { public Product(long id, String name, int amountRemaining, int amountSold, boolean sellable, double price, List<Product> components, Category category) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.amountRemaining = amountRemaining; this.amountRemaining = amountRemaining;
this.amountSold = amountSold; this.amountSold = amountSold;
this.sellable = sellable; this.sellable = sellable;
this.price = price; this.price = price;
this.components = components;
this.category = category; this.category = category;
this.components = new ArrayList<>();
if(components!=null){
this.components.addAll(components);
}
} }
public int getId() { public long getId() {
return id; return id;
} }
public void setId(int id) { public void setId(long id) {
this.id = id; this.id = id;
} }
@ -74,11 +77,11 @@ public class Product implements ILoggable, Serializable{
this.sellable = sellable; this.sellable = sellable;
} }
public boolean isPrice() { public double getPrice() {
return price; return price;
} }
public void setPrice(boolean price) { public void setPrice(double price) {
this.price = price; this.price = price;
} }

View File

@ -2,5 +2,5 @@ package com.pqt.core.entities.query;
public enum QueryType { public enum QueryType {
CONNECT, SALE, STOCL, STAT, LOG, UPDATE CONNECT, SALE, STOCK, STAT, LOG, UPDATE
} }

View File

@ -0,0 +1,102 @@
package com.pqt.core.entities.sale;
import com.pqt.core.entities.members.Client;
import com.pqt.core.entities.product.Product;
import com.pqt.core.entities.user_account.Account;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class LightweightSale {
private int id;
private Map<Long, Integer> products;
private Date orderedAt;
private Client orderedWith;
private Account orderedBy;
private Account orderedFor;
private SaleType type;
private SaleStatus status;
public LightweightSale() {
}
public LightweightSale(Sale sale) {
this.id = sale.getId();
this.orderedAt = sale.getOrderedAt();
this.orderedWith = sale.getOrderedWith();
this.orderedBy = sale.getOrderedBy();
this.orderedFor = sale.getOrderedFor();
this.type = sale.getType();
this.status = sale.getStatus();
products = new HashMap<>();
for(Product product : sale.getProducts().keySet()){
products.put(product.getId(), sale.getProducts().get(product));
}
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Map<Long, Integer> getProducts() {
return products;
}
public void setProducts(Map<Long, Integer> products) {
this.products = products;
}
public Date getOrderedAt() {
return orderedAt;
}
public void setOrderedAt(Date orderedAt) {
this.orderedAt = orderedAt;
}
public Client getOrderedWith() {
return orderedWith;
}
public void setOrderedWith(Client orderedWith) {
this.orderedWith = orderedWith;
}
public Account getOrderedBy() {
return orderedBy;
}
public void setOrderedBy(Account orderedBy) {
this.orderedBy = orderedBy;
}
public Account getOrderedFor() {
return orderedFor;
}
public void setOrderedFor(Account orderedFor) {
this.orderedFor = orderedFor;
}
public SaleType getType() {
return type;
}
public void setType(SaleType type) {
this.type = type;
}
public SaleStatus getStatus() {
return status;
}
public void setStatus(SaleStatus status) {
this.status = status;
}
}

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;