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(); ShoppingListEntry entry = new ShoppingListEntry();
entry.setParentList(resultList); entry.setParentList(resultList);
entry.setProduct(Product.getById(res.getLong("productId"))); entry.setProduct(Product.getById(res.getLong("productId")));
entry.setAmount(res.getFloat("amount"));
entry.setUnit(Unit.getById(res.getLong("unit")));
resultList.getEntries().add(entry); resultList.getEntries().add(entry);
} }

View File

@ -76,15 +76,80 @@ public class ShoppingListEntry
long numAffectedRows = preparedStmt.executeUpdate(); long numAffectedRows = preparedStmt.executeUpdate();
Miscellaneous.logEvent("AMOUNT OF UPDATED ROWS: " + numAffectedRows, 5); Miscellaneous.logEvent("AMOUNT OF UPDATED ROWS: " + numAffectedRows, 5);
ResultSet rs = preparedStmt.getGeneratedKeys(); ResultSet rs = preparedStmt.getGeneratedKeys();
// if (numAffectedRows > 0) if (numAffectedRows > 0)
// { {
rs.close();
preparedStmt.close(); preparedStmt.close();
return true; return true;
// } }
} }
else 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) catch(Exception e)
@ -107,13 +172,15 @@ public class ShoppingListEntry
} }
public boolean delete() public boolean delete()
{ {
PreparedStatement preparedStmt = null; PreparedStatement preparedStmt = null;
Connection conn = null; Connection conn = null;
try try
{
conn = DatabaseHandler.getInstance().getConnection();
if(this.getAmount() == 0)
{ {
conn = DatabaseHandler.getInstance().getConnection();
String parentQuery = "DELETE FROM listEntries WHERE listId=? AND productId=?"; String parentQuery = "DELETE FROM listEntries WHERE listId=? AND productId=?";
preparedStmt = conn.prepareStatement(parentQuery, Statement.RETURN_GENERATED_KEYS); preparedStmt = conn.prepareStatement(parentQuery, Statement.RETURN_GENERATED_KEYS);
@ -131,24 +198,29 @@ public class ShoppingListEntry
return true; return true;
// } // }
} }
catch(Exception e) else
{ {
Miscellaneous.logEvent(Diverse.getStackTraceAsString(e), 1); if(!combinationExists(this.getParentList(), this.getProduct(), this.getUnit()))
} }
finally
{
try
{
if(preparedStmt != null && !preparedStmt.isClosed())
preparedStmt.close();
}
catch (SQLException e)
{
}
}
return false;
} }
catch(Exception e)
{
Miscellaneous.logEvent(Diverse.getStackTraceAsString(e), 1);
}
finally
{
try
{
if(preparedStmt != null && !preparedStmt.isClosed())
preparedStmt.close();
}
catch (SQLException e)
{
}
}
return false;
}
private boolean combinationExists(ShoppingList parentList, Product product, Unit unit) private boolean combinationExists(ShoppingList parentList, Product product, Unit unit)
{ {