|
|
|
@ -85,71 +85,7 @@ public class ShoppingListEntry
|
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
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(); |
|
|
|
|
|
|
|
|
|
updateAmount(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch(Exception e) |
|
|
|
@ -179,7 +115,7 @@ public class ShoppingListEntry
|
|
|
|
|
{ |
|
|
|
|
conn = DatabaseHandler.getInstance().getConnection(); |
|
|
|
|
|
|
|
|
|
if(this.getAmount() == 0) |
|
|
|
|
if(this.getAmount() == 0 || this.getUnit().isDummy()) |
|
|
|
|
{ |
|
|
|
|
String parentQuery = "DELETE FROM listEntries WHERE listId=? AND productId=?"; |
|
|
|
|
preparedStmt = conn.prepareStatement(parentQuery, Statement.RETURN_GENERATED_KEYS); |
|
|
|
@ -200,7 +136,8 @@ public class ShoppingListEntry
|
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
if(!combinationExists(this.getParentList(), this.getProduct(), this.getUnit())) |
|
|
|
|
if(combinationExists(this.getParentList(), this.getProduct(), this.getUnit())) |
|
|
|
|
return updateAmount(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch(Exception e) |
|
|
|
@ -222,6 +159,85 @@ public class ShoppingListEntry
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected boolean updateAmount() |
|
|
|
|
{ |
|
|
|
|
String query = "SELECT * FROM listEntries WHERE listId=? AND productId=? AND unit=?"; |
|
|
|
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
{ |
|
|
|
|
Miscellaneous.logEvent(Diverse.getStackTraceAsString(e), 2); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean combinationExists(ShoppingList parentList, Product product, Unit unit) |
|
|
|
|
{ |
|
|
|
|
String query = "SELECT count(listId) as combinationAmount FROM listEntries WHERE listId=? AND productId=? AND unit=?"; |
|
|
|
|