Amount/Unit progress

This commit is contained in:
jens 2021-04-27 19:59:01 +02:00
parent 1950db40e0
commit 6ce0f76483
4 changed files with 129 additions and 70 deletions

View File

@ -196,7 +196,12 @@ public class ShoppingList
for(ShoppingListEntry entry : this.entries)
{
if(entry.getProduct().getStoreType().equals(st))
{
if(entry.getUnit().isDummy)
shoppingListString.append(entry.getProduct().getName() + Diverse.lineSeparator);
else
shoppingListString.append(String.valueOf(entry.getAmount()) + " " + entry.getUnit().getAbbreviation() + " " + entry.getProduct().getName() + Diverse.lineSeparator);
}
}
shoppingListString.append(Diverse.lineSeparator);

View File

@ -84,10 +84,90 @@ public class ShoppingListEntry
}
}
else
{
updateAmount();
}
}
catch(Exception e)
{
Miscellaneous.logEvent(Diverse.getStackTraceAsString(e), 1);
}
finally
{
try
{
if(preparedStmt != null && !preparedStmt.isClosed())
preparedStmt.close();
}
catch (SQLException e)
{
}
}
return false;
}
public boolean delete()
{
PreparedStatement preparedStmt = null;
Connection conn = null;
try
{
conn = DatabaseHandler.getInstance().getConnection();
if(this.getAmount() == 0 || this.getUnit().isDummy())
{
String parentQuery = "DELETE FROM listEntries WHERE listId=? AND productId=?";
preparedStmt = conn.prepareStatement(parentQuery, Statement.RETURN_GENERATED_KEYS);
preparedStmt.setLong(1, this.getParentList().getId());
preparedStmt.setLong(2, this.getProduct().getId());
Miscellaneous.logEvent(preparedStmt.toString(), 5);
long numAffectedRows = preparedStmt.executeUpdate();
Miscellaneous.logEvent("AMOUNT OF UPDATED ROWS: " + numAffectedRows, 5);
ResultSet rs = preparedStmt.getGeneratedKeys();
// if (numAffectedRows > 0)
// {
preparedStmt.close();
return true;
// }
}
else
{
if(combinationExists(this.getParentList(), this.getProduct(), this.getUnit()))
return updateAmount();
}
}
catch(Exception e)
{
Miscellaneous.logEvent(Diverse.getStackTraceAsString(e), 1);
}
finally
{
try
{
if(preparedStmt != null && !preparedStmt.isClosed())
preparedStmt.close();
}
catch (SQLException e)
{
}
}
return false;
}
protected boolean updateAmount()
{
String query = "SELECT * FROM listEntries WHERE listId=? AND productId=? AND unit=?";
preparedStmt = conn.prepareStatement(query);
try
{
Connection conn = DatabaseHandler.getInstance().getConnection();
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setLong(1, this.getParentList().getId());
preparedStmt.setLong(2, this.getProduct().getId());
preparedStmt.setLong(3, this.getUnit().getId());
@ -149,74 +229,10 @@ public class ShoppingListEntry
}
res.close();
}
}
catch(Exception e)
{
Miscellaneous.logEvent(Diverse.getStackTraceAsString(e), 1);
}
finally
{
try
{
if(preparedStmt != null && !preparedStmt.isClosed())
preparedStmt.close();
}
catch (SQLException e)
{
}
}
return false;
}
public boolean delete()
{
PreparedStatement preparedStmt = null;
Connection conn = null;
try
{
conn = DatabaseHandler.getInstance().getConnection();
if(this.getAmount() == 0)
{
String parentQuery = "DELETE FROM listEntries WHERE listId=? AND productId=?";
preparedStmt = conn.prepareStatement(parentQuery, Statement.RETURN_GENERATED_KEYS);
preparedStmt.setLong(1, this.getParentList().getId());
preparedStmt.setLong(2, this.getProduct().getId());
Miscellaneous.logEvent(preparedStmt.toString(), 5);
long numAffectedRows = preparedStmt.executeUpdate();
Miscellaneous.logEvent("AMOUNT OF UPDATED ROWS: " + numAffectedRows, 5);
ResultSet rs = preparedStmt.getGeneratedKeys();
// if (numAffectedRows > 0)
// {
preparedStmt.close();
return true;
// }
}
else
{
if(!combinationExists(this.getParentList(), this.getProduct(), this.getUnit()))
}
}
catch(Exception e)
{
Miscellaneous.logEvent(Diverse.getStackTraceAsString(e), 1);
}
finally
{
try
{
if(preparedStmt != null && !preparedStmt.isClosed())
preparedStmt.close();
}
catch (SQLException e)
{
}
Miscellaneous.logEvent(Diverse.getStackTraceAsString(e), 2);
}
return false;

View File

@ -9,6 +9,8 @@ public class Start
{
public static void main(String[] args)
{
float amount = 0;
String unitName = null;
String productName = null;
String action = null;
String filePath = null;
@ -29,7 +31,11 @@ public class Start
// System.out.println("Found double dash with command " + argstring.substring(2, len) );
String argName = argstring.substring(2, len);
doubleOptsList.add(argName);
if(argName.equalsIgnoreCase("shoppingProduct"))
if(argName.equalsIgnoreCase("amount"))
amount = Float.parseFloat(args[i+1].replace("\"", ""));
else if(argName.equalsIgnoreCase("unitName"))
unitName = args[i+1].replace("\"", "");
else if(argName.equalsIgnoreCase("shoppingProduct"))
productName = args[i+1].replace("\"", "");
else if(argName.equalsIgnoreCase("action"))
action = args[i+1].replace("\"", "");
@ -65,6 +71,8 @@ public class Start
if(!StringUtils.isEmpty(productName))
{
ShoppingList list = ShoppingList.getMostRecentList();
Unit u = Unit.getByName(unitName);
Product p = Product.getByName(productName);
if(list == null)
@ -76,6 +84,8 @@ public class Start
ShoppingListEntry entry = new ShoppingListEntry();
entry.setParentList(list);
entry.setProduct(p);
entry.setAmount(amount);
entry.setUnit(u);
if(entry.create())
{

View File

@ -8,6 +8,8 @@ import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import org.apache.commons.lang3.StringUtils;
public class Unit
{
static ArrayList<Unit> unitCache = null;
@ -16,6 +18,7 @@ static ArrayList<Unit> unitCache = null;
String name;
String abbreviation;
boolean isDefault;
boolean isDummy;
public long getId()
{
@ -53,6 +56,15 @@ static ArrayList<Unit> unitCache = null;
this.isDefault = isDefault;
}
public boolean isDummy()
{
return isDummy;
}
public void setDummy(boolean isDummy)
{
this.isDummy = isDummy;
}
public static ArrayList<Unit> readAllUnits()
{
if(unitCache == null)
@ -84,6 +96,7 @@ static ArrayList<Unit> unitCache = null;
u.setName(res.getString("name"));
u.setAbbreviation(res.getString("abbreviation"));
u.setDefault(res.getInt("isDefault") == 1);
u.setDummy(res.getInt("isDummy") == 1);
unitCache.add(u);
}
@ -108,6 +121,16 @@ static ArrayList<Unit> unitCache = null;
public static Unit getByName(String unitName)
{
if(StringUtils.isEmpty(unitName))
{
for(Unit u : readAllUnits())
{
if(u.isDummy())
return u;
}
return null;
}
for(Unit u : readAllUnits())
{
if(u.getName().equalsIgnoreCase(unitName))
@ -138,7 +161,7 @@ static ArrayList<Unit> unitCache = null;
public boolean create()
{
String query = "INSERT INTO units (name, abbreviation, isDefault) VALUES (?, ?, ?)";
String query = "INSERT INTO units (name, abbreviation, isDefault, isDummy) VALUES (?, ?, ?, ?)";
Connection conn = DatabaseHandler.getInstance().getConnection();
PreparedStatement preparedStmt = null;
try
@ -152,6 +175,11 @@ static ArrayList<Unit> unitCache = null;
else
preparedStmt.setInt(3, 0);
if(this.isDummy)
preparedStmt.setInt(4, 1);
else
preparedStmt.setInt(4, 0);
preparedStmt.executeUpdate();
ResultSet rs = preparedStmt.getGeneratedKeys();
if (rs.next())