mirror of
https://github.com/klmp200/PQT_Gestionnaire_vente_stock.git
synced 2024-12-22 23:41:09 +00:00
Module Server, clss FileStockDao : utilisation d'un ISerialFileManager pour gérer les IO du fichier
This commit is contained in:
parent
adf55b68a0
commit
c10c91ec1e
@ -1,16 +1,17 @@
|
|||||||
package com.pqt.server.module.stock;
|
package com.pqt.server.module.stock;
|
||||||
|
|
||||||
import com.pqt.core.entities.product.Product;
|
import com.pqt.core.entities.product.Product;
|
||||||
import com.pqt.server.tools.FileUtil;
|
|
||||||
import com.pqt.server.tools.entities.SaleContent;
|
import com.pqt.server.tools.entities.SaleContent;
|
||||||
|
import com.pqt.server.tools.io.ISerialFileManager;
|
||||||
|
import com.pqt.server.tools.io.SimpleSerialFileManagerFactory;
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
//TODO écrire Javadoc
|
//TODO écrire Javadoc
|
||||||
public class FileStockDao implements IStockDao {
|
public class FileStockDao implements IStockDao {
|
||||||
|
|
||||||
private static final String STOCK_FILE_NAME = "stock.pqt";
|
private static final String STOCK_FILE_NAME = "stock.pqt";
|
||||||
|
private ISerialFileManager<Product> fileManager;
|
||||||
private long nextProductId;
|
private long nextProductId;
|
||||||
private Random random;
|
private Random random;
|
||||||
|
|
||||||
@ -18,6 +19,7 @@ public class FileStockDao implements IStockDao {
|
|||||||
|
|
||||||
public FileStockDao() {
|
public FileStockDao() {
|
||||||
random = new Random();
|
random = new Random();
|
||||||
|
fileManager = SimpleSerialFileManagerFactory.getFileManager(Product.class, STOCK_FILE_NAME);
|
||||||
loadFromFile();
|
loadFromFile();
|
||||||
generateNextProductId();
|
generateNextProductId();
|
||||||
}
|
}
|
||||||
@ -120,66 +122,12 @@ public class FileStockDao implements IStockDao {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void loadFromFile() {
|
private void loadFromFile() {
|
||||||
products = new HashMap<>(load());
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<Long, Product> load(){
|
|
||||||
Map<Long, Product> loadedData = new HashMap<>();
|
Map<Long, Product> loadedData = new HashMap<>();
|
||||||
try{
|
fileManager.loadListFromFile().forEach(product -> loadedData.put(product.getId(), product));
|
||||||
if(FileUtil.createFileIfNotExist(STOCK_FILE_NAME)){
|
products = new HashMap<>(loadedData);
|
||||||
return loadedData;
|
|
||||||
}
|
|
||||||
}catch(IOException e){
|
|
||||||
e.printStackTrace();
|
|
||||||
return loadedData;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 saveToFile() {
|
private void saveToFile() {
|
||||||
save(this.products);
|
fileManager.saveListToFile(new ArrayList<>(products.values()));
|
||||||
}
|
|
||||||
|
|
||||||
private void save(Map<Long, Product> products){
|
|
||||||
try{
|
|
||||||
FileUtil.createFileIfNotExist(STOCK_FILE_NAME);
|
|
||||||
}catch (IOException e){
|
|
||||||
e.printStackTrace();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user