Module Server : Ajout dép mvn Apache commons io, ajout clss NoRevertFileSaleDao, ajout méthd applySale() aux clss StockService et IStockDao + impl

This commit is contained in:
Notmoo 2017-07-30 16:03:34 +02:00
parent 57757fe10b
commit e3c0e510bf
8 changed files with 161 additions and 3 deletions

View File

@ -11,6 +11,7 @@
<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: commons-io:commons-io:2.4" />
</element>
</element>
<element id="directory" name="META-INF">

View File

@ -25,6 +25,13 @@
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,132 @@
package com.pqt.server.module.sale;
import com.pqt.core.entities.product.Product;
import com.pqt.core.entities.sale.Sale;
import com.pqt.server.module.stock.StockService;
import com.pqt.server.utils.FileUtil;
import org.apache.commons.io.input.ReversedLinesFileReader;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
public class NoRevertFileSaleDao implements ISaleDao {
private static final String SALE_LOG_FILE_NAME = "sale_log.txt";
private StockService stockService;
private long nextSaleId;
private ISaleRenderer renderer;
NoRevertFileSaleDao(StockService stockService) {
this.stockService = stockService;
this.renderer = getRenderer();
nextSaleId = readLastSaleIdFromFile()+1;
}
@Override
public long submitSale(Sale sale) {
boolean valid = true;
Iterator<Product> it = sale.getProducts().keySet().iterator();
while(valid && it.hasNext()){
Product p = it.next();
Product product = stockService.getProduct(p.getId());
valid = product!=null
&& p.equals(product)
&& product.isSellable()
&& product.getAmountRemaining()>=sale.getProducts().get(p);
}
if(!valid)
return -1;
long saleId = nextSaleId;
stockService.applySale(sale.getProducts());
logSale(sale, saleId);
generateNextSaleId();
return saleId;
}
private void generateNextSaleId() {
nextSaleId++;
}
/**
* Read the last sale id written in the log file with title {@link #SALE_LOG_FILE_NAME} or a default value if such id has not been found.
* <p/>
* Different reasons why this method may not find any id :<br/>
* - file does not exist<br/>
* - file is empty<br/>
* - file does not respect the expected syntax for writing sales' data<br/>
* <p/>
* The log file with title {@link #SALE_LOG_FILE_NAME} is not created by this method if it doesn't exist yet.
* @return last sale id used in the log file, or -1 if none was found.
*/
private long readLastSaleIdFromFile(){
long id = -1;
if(FileUtil.exist(SALE_LOG_FILE_NAME)){
try(ReversedLinesFileReader rlfr = new ReversedLinesFileReader(new File("SALE_LOG_FILE_NAME"))){
boolean stop = false;
do{
try {
String line = rlfr.readLine();
if(line.matches("^[0-9]+$")){
id = Long.parseLong(line.substring(1));
stop = true;
}
}catch (EOFException e){
stop = true;
}
}while(!stop);
} catch (IOException e) {
e.printStackTrace();
}
return id;
}else{
return id;
}
}
@Override
public void submitSaleRevert(long id) {
//TODO Créer un nouveau dao qui supporte le revert
throw new UnsupportedOperationException("Le revert de commandes n'est pas supporté");
}
private void logSale(Sale sale, long saleId){
try(FileOutputStream fos = new FileOutputStream(SALE_LOG_FILE_NAME);
PrintWriter pw = new PrintWriter(fos)){
pw.append(renderer.render(sale, saleId));
} catch (IOException e) {
e.printStackTrace();
}
}
private ISaleRenderer getRenderer(){
return(sale, id)->{
StringBuffer sb = new StringBuffer("\n#").append(id).append("\n");
String separator = "-----";
DateFormat dateFormat = new SimpleDateFormat("<dd/ww/yyyy - HH:mm:ss>");
sb.append("type : ").append(sale.getType().name()).append("\n");
sb.append("at : ").append(dateFormat.format(sale.getOrderedAt())).append("\n");
if(sale.getOrderedBy()!=null)
sb.append("by : ").append(sale.getOrderedBy().getUsername()).append("(").append(sale.getOrderedBy().getPermissionLevel().name()).append(")").append("\n");
if(sale.getOrderedFor()!=null)
sb.append("for : ").append(sale.getOrderedFor().getUsername()).append("(").append(sale.getOrderedFor().getPermissionLevel().name()).append(")").append("\n");
sb.append(separator).append("\n");
sb.append("Products : \n");
sale.getProducts().keySet().forEach(p->{
int productAmount = sale.getProducts().get(p);
sb.append(String.format(" * %s (%du, %f€) : %d remaining in stock", p.getName(), productAmount, p.getPrice()*(double)productAmount, p.getAmountRemaining()-productAmount)).append("\n");
});
sb.append(separator);
return sb.toString();
};
}
private interface ISaleRenderer{
String render(Sale sale, long saleId);
}
}

View File

@ -2,6 +2,7 @@ package com.pqt.server.module.sale;
import com.pqt.core.entities.sale.Sale;
import com.pqt.server.exception.ServerQueryException;
import com.pqt.server.module.stock.StockService;
//TODO écrire Javadoc
//TODO ajouter logs
@ -9,7 +10,8 @@ public class SaleService {
private ISaleDao dao;
public SaleService() {
public SaleService(StockService stockService) {
dao = new NoRevertFileSaleDao(stockService);
}
public long submitSale(Sale sale) throws ServerQueryException {

View File

@ -81,6 +81,11 @@ public class FileStockDao implements IStockDao {
}
}
@Override
public void applySale(Map<Product, Integer> productAmounts) {
//TODO faire ça
}
private Map<Long, Product> load(){
Map<Long, Product> loadedData = new HashMap<>();
try{

View File

@ -3,6 +3,7 @@ package com.pqt.server.module.stock;
import com.pqt.core.entities.product.Product;
import java.util.List;
import java.util.Map;
//TODO écrire javadoc
public interface IStockDao {
@ -17,4 +18,5 @@ public interface IStockDao {
public void modifyProduct(long id, Product product);
void applySale(Map<Product, Integer> productAmounts);
}

View File

@ -5,6 +5,7 @@ import com.pqt.core.entities.product.ProductUpdate;
import com.pqt.server.exception.ServerQueryException;
import java.util.List;
import java.util.Map;
//TODO écrire Javadoc
//TODO ajouter logs
@ -24,6 +25,10 @@ public class StockService {
return dao.getProduct(id);
}
public void applySale(Map<Product, Integer> productAmounts) {
dao.applySale(productAmounts);
}
public void applyUpdateList(List<ProductUpdate> updates) throws ServerQueryException{
for(ProductUpdate upd : updates){
if(upd.getOldVersion()==null){

View File

@ -16,11 +16,15 @@ public class FileUtil {
* @throws IOException if any IOException happend during this method's execution.
*/
public static boolean createFileIfNotExist(String filePath) throws IOException {
Path path = Paths.get(filePath);
if(!Files.exists(path)){
if(FileUtil.exist(filePath)){
Path path = Paths.get(filePath);
Files.createFile(path);
return true;
}
return false;
}
public static boolean exist(String filePath) {
return Files.exists(Paths.get(filePath));
}
}