Amount/Unit progress

This commit is contained in:
jens 2021-04-27 16:29:38 +02:00
parent 3d79c8447c
commit 1950db40e0
2 changed files with 99 additions and 25 deletions

View File

@ -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);
}

View File

@ -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)
@ -114,6 +179,8 @@ public class ShoppingListEntry
{
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);
@ -131,6 +198,11 @@ public class ShoppingListEntry
return true;
// }
}
else
{
if(!combinationExists(this.getParentList(), this.getProduct(), this.getUnit()))
}
}
catch(Exception e)
{
Miscellaneous.logEvent(Diverse.getStackTraceAsString(e), 1);