From 60935bf45e237ce490c256137c9c8bc98713e5d4 Mon Sep 17 00:00:00 2001 From: Notmoo Date: Sun, 30 Jul 2017 13:27:59 +0200 Subject: [PATCH] Module Server : suppr clss HibernateStockDao, ajout clss FileStockDao --- .../.idea/artifacts/server_war_exploded.xml | 11 -- .../pqt/server/module/stock/FileStockDao.java | 123 ++++++++++++++++++ .../module/stock/HibernateStockDao.java | 50 ------- .../pqt/server/module/stock/StockService.java | 2 +- 4 files changed, 124 insertions(+), 62 deletions(-) create mode 100644 Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/FileStockDao.java delete mode 100644 Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/HibernateStockDao.java diff --git a/Workspace/.idea/artifacts/server_war_exploded.xml b/Workspace/.idea/artifacts/server_war_exploded.xml index c41d4bfa..b6a9ebaa 100644 --- a/Workspace/.idea/artifacts/server_war_exploded.xml +++ b/Workspace/.idea/artifacts/server_war_exploded.xml @@ -11,17 +11,6 @@ - - - - - - - - - - - diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/FileStockDao.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/FileStockDao.java new file mode 100644 index 00000000..6f8c42a1 --- /dev/null +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/FileStockDao.java @@ -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 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 getProductList() { + return copyOfProductList(); + } + + private List copyOfProductList() { + List 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 load(){ + Map 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 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(); + } + } +} diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/HibernateStockDao.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/HibernateStockDao.java deleted file mode 100644 index cbe9ba2f..00000000 --- a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/HibernateStockDao.java +++ /dev/null @@ -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 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) { - - } - -} diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/StockService.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/StockService.java index a5e08643..867ad057 100644 --- a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/StockService.java +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/StockService.java @@ -13,7 +13,7 @@ public class StockService { private IStockDao dao; public StockService() { - dao = new HibernateStockDao(); + dao = new FileStockDao(); } public List getProductList() {