Browse Source

Amount/Unit progress

development
jens 5 months ago
parent
commit
1950db40e0
  1. 2
      ShoppingList/src/com/jens/rhasspy/shoppinglist/ShoppingList.java
  2. 118
      ShoppingList/src/com/jens/rhasspy/shoppinglist/ShoppingListEntry.java

2
ShoppingList/src/com/jens/rhasspy/shoppinglist/ShoppingList.java

@ -143,6 +143,8 @@ public class ShoppingList
ShoppingListEntry entry = new ShoppingListEntry();
entry.setParentList(resultList);
entry.setProduct(Product.getById(res.getLong("productId")));
entry.setAmount(res.getFloat("amount"));
entry.setUnit(Unit.getById(res.getLong("unit")));
resultList.getEntries().add(entry);
}

118
ShoppingList/src/com/jens/rhasspy/shoppinglist/ShoppingListEntry.java

@ -76,15 +76,80 @@ public class ShoppingListEntry
long numAffectedRows = preparedStmt.executeUpdate();
Miscellaneous.logEvent("AMOUNT OF UPDATED ROWS: " + numAffectedRows, 5);
ResultSet rs = preparedStmt.getGeneratedKeys();
// if (numAffectedRows > 0)
// {
if (numAffectedRows > 0)
{
rs.close();
preparedStmt.close();
return true;
// }
}
}
else
{
continue here.
String query = "SELECT * FROM listEntries WHERE listId=? AND productId=? AND unit=?";
preparedStmt = conn.prepareStatement(query);
preparedStmt.setLong(1, this.getParentList().getId());
preparedStmt.setLong(2, this.getProduct().getId());
preparedStmt.setLong(3, this.getUnit().getId());
Miscellaneous.logEvent(preparedStmt.toString(), 5);
ResultSet res = preparedStmt.executeQuery();
if(res.next())
{
float currentAmount = res.getFloat("amount");
if(currentAmount + this.getAmount() > 0)
{
// Update the amount
String updateQuery = "UPDATE listEntries set=? WHERE listId=? AND productId=? AND unit=?";
preparedStmt = conn.prepareStatement(updateQuery);
preparedStmt.setFloat(1, currentAmount + this.getAmount());
preparedStmt.setLong(2, this.getProduct().getId());
preparedStmt.setLong(3, this.getProduct().getId());
preparedStmt.setLong(4, this.getUnit().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)
{
Miscellaneous.logEvent("Amount added to existing entry.", 2);
rs.close();
preparedStmt.close();
return true;
}
}
else
{
// Resulting sum is < 0 -> Remove the entry completely
String updateQuery = "DELETE FROM listEntries WHERE listId=? AND productId=? AND unit=?";
preparedStmt = conn.prepareStatement(updateQuery);
preparedStmt.setLong(1, this.getProduct().getId());
preparedStmt.setLong(2, this.getProduct().getId());
preparedStmt.setLong(3, this.getUnit().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)
{
Miscellaneous.logEvent("Resulting amount < 0, entry deleted.", 2);
rs.close();
preparedStmt.close();
return true;
}
}
}
res.close();
}
}
catch(Exception e)
@ -107,13 +172,15 @@ public class ShoppingListEntry
}
public boolean delete()
{
PreparedStatement preparedStmt = null;
Connection conn = null;
try
{
PreparedStatement preparedStmt = null;
Connection conn = null;
try
{
conn = DatabaseHandler.getInstance().getConnection();
if(this.getAmount() == 0)
{
conn = DatabaseHandler.getInstance().getConnection();
String parentQuery = "DELETE FROM listEntries WHERE listId=? AND productId=?";
preparedStmt = conn.prepareStatement(parentQuery, Statement.RETURN_GENERATED_KEYS);
@ -131,24 +198,29 @@ public class ShoppingListEntry
return true;
// }
}
catch(Exception e)
else
{
Miscellaneous.logEvent(Diverse.getStackTraceAsString(e), 1);
if(!combinationExists(this.getParentList(), this.getProduct(), this.getUnit()))
}
finally
}
catch(Exception e)
{
Miscellaneous.logEvent(Diverse.getStackTraceAsString(e), 1);
}
finally
{
try
{
if(preparedStmt != null && !preparedStmt.isClosed())
preparedStmt.close();
}
catch (SQLException e)
{
try
{
if(preparedStmt != null && !preparedStmt.isClosed())
preparedStmt.close();
}
catch (SQLException e)
{
}
}
return false;
}
return false;
}
private boolean combinationExists(ShoppingList parentList, Product product, Unit unit)
{

Loading…
Cancel
Save