Amount/Unit progress
This commit is contained in:
parent
3d79c8447c
commit
1950db40e0
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
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,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
|
||||
{
|
||||
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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user