mirror of
				https://github.com/klmp200/PQT_Gestionnaire_vente_stock.git
				synced 2025-10-31 09:03:08 +00:00 
			
		
		
		
	Module Server : suppr clss HibernateStockDao, ajout clss FileStockDao
This commit is contained in:
		| @@ -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(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -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) { | ||||
|  | ||||
| 	} | ||||
|  | ||||
| } | ||||
| @@ -13,7 +13,7 @@ public class StockService { | ||||
|     private IStockDao dao; | ||||
|  | ||||
|     public StockService() { | ||||
|         dao = new HibernateStockDao(); | ||||
|         dao = new FileStockDao(); | ||||
|     } | ||||
|  | ||||
|     public List<Product> getProductList() { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user