Module Server : suppr clss HibernateStockDao, ajout clss FileStockDao

This commit is contained in:
Notmoo 2017-07-30 13:27:59 +02:00
parent 0b8bb4e183
commit 60935bf45e
4 changed files with 124 additions and 62 deletions

View File

@ -11,17 +11,6 @@
<element id="module-output" name="core" />
</element>
<element id="library" level="project" name="Maven: com.google.code.gson:gson:2.8.1" />
<element id="library" level="project" name="Maven: org.hibernate:hibernate-core:5.2.10.Final" />
<element id="library" level="project" name="Maven: org.jboss.logging:jboss-logging:3.3.0.Final" />
<element id="library" level="project" name="Maven: org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final" />
<element id="library" level="project" name="Maven: org.javassist:javassist:3.20.0-GA" />
<element id="library" level="project" name="Maven: antlr:antlr:2.7.7" />
<element id="library" level="project" name="Maven: org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:1.0.1.Final" />
<element id="library" level="project" name="Maven: org.jboss:jandex:2.0.3.Final" />
<element id="library" level="project" name="Maven: com.fasterxml:classmate:1.3.0" />
<element id="library" level="project" name="Maven: dom4j:dom4j:1.6.1" />
<element id="library" level="project" name="Maven: org.hibernate.common:hibernate-commons-annotations:5.0.1.Final" />
<element id="library" level="project" name="Maven: org.postgresql:postgresql:42.1.3" />
</element>
</element>
<element id="directory" name="META-INF">

View File

@ -0,0 +1,123 @@
package com.pqt.server.module.stock;
import com.pqt.core.entities.product.Product;
import java.io.*;
import java.util.*;
//TODO écrire Javadoc
public class FileStockDao implements IStockDao {
private static final String STOCK_FILE_NAME = "stock.pqt";
private long nextProductId;
private Random random;
private Map<Long, Product> products;
public FileStockDao() {
random = new Random();
products = new HashMap<>();
load();
generateNextProductId();
}
private void generateNextProductId() {
Long newId;
do{
newId = random.nextLong();
}while (products.containsKey(newId));
nextProductId = newId;
}
/**
* @see com.pqt.server.module.stock.IStockDao#getProductList()
*/
public List<Product> getProductList() {
return copyOfProductList();
}
private List<Product> copyOfProductList() {
List<Product> copy = new ArrayList<>();
products.values().stream().forEach(p->copy.add(new Product(p)));
return copy;
}
/**
* @see com.pqt.server.module.stock.IStockDao#getProduct(long)
*/
public Product getProduct(long id) {
return products.get(id);
}
/**
* @see com.pqt.server.module.stock.IStockDao#addProduct(com.pqt.core.entities.product.Product)
*/
public void addProduct(Product product) {
product.setId(nextProductId);
this.products.put(nextProductId, product);
generateNextProductId();
save(this.products);
}
/**
* @see com.pqt.server.module.stock.IStockDao#removeProduct(long)
*/
public void removeProduct(long id) {
Product product = getProduct(id);
if(product!=null){
this.products.remove(product);
save(this.products);
}
}
/**
* @see com.pqt.server.module.stock.IStockDao#modifyProduct(long, com.pqt.core.entities.product.Product)
*/
public void modifyProduct(long id, Product product) {
if(this.products.containsKey(id)){
product.setId(id);
this.products.put(id, product);
}
}
private Map<Long, Product> load(){
Map<Long, Product> loadedData = new HashMap<>();
try(FileInputStream fis = new FileInputStream(STOCK_FILE_NAME);
ObjectInputStream ois = new ObjectInputStream(fis)){
boolean end = false;
do{
try{
Object obj = ois.readObject();
if(Product.class.isInstance(obj)){
Product p = Product.class.cast(obj);
loadedData.put(p.getId(), p);
}
}catch (EOFException e){
end = true;
}catch(ClassNotFoundException | InvalidClassException e){
e.printStackTrace();
}
}while(!end);
}catch( IOException e){
e.printStackTrace();
}
return loadedData;
}
private void save(Map<Long, Product> products){
try(FileOutputStream fos = new FileOutputStream(STOCK_FILE_NAME);
ObjectOutputStream oos = new ObjectOutputStream(fos)){
products.values().stream().forEach(p -> {
try {
oos.writeObject(p);
} catch (IOException e) {
e.printStackTrace();
}
});
}catch(IOException e){
e.printStackTrace();
}
}
}

View File

@ -1,50 +0,0 @@
package com.pqt.server.module.stock;
import com.pqt.core.entities.product.Product;
import java.util.List;
//TODO écrire contenu méthodes
public class HibernateStockDao implements IStockDao {
/**
* @see com.pqt.server.module.stock.IStockDao#getProductList()
*/
public List<Product> getProductList() {
return null;
}
/**
* @see com.pqt.server.module.stock.IStockDao#getProduct(long)
*/
public Product getProduct(long id) {
return null;
}
/**
* @see com.pqt.server.module.stock.IStockDao#addProduct(com.pqt.core.entities.product.Product)
*/
public void addProduct(Product product) {
}
/**
* @see com.pqt.server.module.stock.IStockDao#removeProduct(long)
*/
public void removeProduct(long id) {
}
/**
* @see com.pqt.server.module.stock.IStockDao#modifyProduct(long, com.pqt.core.entities.product.Product)
*/
public void modifyProduct(long id, Product product) {
}
}

View File

@ -13,7 +13,7 @@ public class StockService {
private IStockDao dao;
public StockService() {
dao = new HibernateStockDao();
dao = new FileStockDao();
}
public List<Product> getProductList() {