|
|
|
@ -58,7 +58,10 @@ public class ShoppingListEntry
|
|
|
|
|
PreparedStatement preparedStmt = null; |
|
|
|
|
Connection conn = null; |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
{ |
|
|
|
|
if(amount <= 0 && unit != null && !unit.isDummy) |
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
conn = DatabaseHandler.getInstance().getConnection(); |
|
|
|
|
|
|
|
|
|
if(!combinationExists(this.getParentList(), this.getProduct(), this.getUnit())) |
|
|
|
@ -85,7 +88,7 @@ public class ShoppingListEntry
|
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
updateAmount(); |
|
|
|
|
return updateAmount(false); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch(Exception e) |
|
|
|
@ -112,7 +115,10 @@ public class ShoppingListEntry
|
|
|
|
|
PreparedStatement preparedStmt = null; |
|
|
|
|
Connection conn = null; |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
{ |
|
|
|
|
if(amount <= 0 && unit != null && !unit.isDummy) |
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
conn = DatabaseHandler.getInstance().getConnection(); |
|
|
|
|
|
|
|
|
|
if(this.getAmount() == 0 || this.getUnit().isDummy()) |
|
|
|
@ -127,7 +133,7 @@ public class ShoppingListEntry
|
|
|
|
|
|
|
|
|
|
long numAffectedRows = preparedStmt.executeUpdate(); |
|
|
|
|
Miscellaneous.logEvent("AMOUNT OF UPDATED ROWS: " + numAffectedRows, 5); |
|
|
|
|
ResultSet rs = preparedStmt.getGeneratedKeys(); |
|
|
|
|
// ResultSet rs = preparedStmt.getGeneratedKeys();
|
|
|
|
|
// if (numAffectedRows > 0)
|
|
|
|
|
// {
|
|
|
|
|
preparedStmt.close(); |
|
|
|
@ -137,7 +143,9 @@ public class ShoppingListEntry
|
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
if(combinationExists(this.getParentList(), this.getProduct(), this.getUnit())) |
|
|
|
|
return updateAmount(); |
|
|
|
|
return updateAmount(true); |
|
|
|
|
else |
|
|
|
|
return true; // Combination to be deleted does not exist. So what?
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch(Exception e) |
|
|
|
@ -159,7 +167,7 @@ public class ShoppingListEntry
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected boolean updateAmount() |
|
|
|
|
protected boolean updateAmount(boolean delete) |
|
|
|
|
{ |
|
|
|
|
String query = "SELECT * FROM listEntries WHERE listId=? AND productId=? AND unit=?"; |
|
|
|
|
|
|
|
|
@ -179,14 +187,22 @@ public class ShoppingListEntry
|
|
|
|
|
if(res.next()) |
|
|
|
|
{ |
|
|
|
|
float currentAmount = res.getFloat("amount"); |
|
|
|
|
if(currentAmount + this.getAmount() > 0) |
|
|
|
|
|
|
|
|
|
boolean condition = true; |
|
|
|
|
if(delete) |
|
|
|
|
condition = currentAmount - this.getAmount() > 0; |
|
|
|
|
|
|
|
|
|
if(condition) |
|
|
|
|
{ |
|
|
|
|
// Update the amount
|
|
|
|
|
String updateQuery = "UPDATE listEntries set=? WHERE listId=? AND productId=? AND unit=?"; |
|
|
|
|
String updateQuery = "UPDATE listEntries SET amount=? WHERE listId=? AND productId=? AND unit=?"; |
|
|
|
|
preparedStmt = conn.prepareStatement(updateQuery); |
|
|
|
|
|
|
|
|
|
preparedStmt.setFloat(1, currentAmount + this.getAmount()); |
|
|
|
|
preparedStmt.setLong(2, this.getProduct().getId()); |
|
|
|
|
if(delete) |
|
|
|
|
preparedStmt.setFloat(1, currentAmount - this.getAmount()); |
|
|
|
|
else |
|
|
|
|
preparedStmt.setFloat(1, currentAmount + this.getAmount()); |
|
|
|
|
preparedStmt.setLong(2, this.getParentList().getId()); |
|
|
|
|
preparedStmt.setLong(3, this.getProduct().getId()); |
|
|
|
|
preparedStmt.setLong(4, this.getUnit().getId()); |
|
|
|
|
|
|
|
|
@ -194,11 +210,11 @@ public class ShoppingListEntry
|
|
|
|
|
|
|
|
|
|
long numAffectedRows = preparedStmt.executeUpdate(); |
|
|
|
|
Miscellaneous.logEvent("AMOUNT OF UPDATED ROWS: " + numAffectedRows, 5); |
|
|
|
|
ResultSet rs = preparedStmt.getGeneratedKeys(); |
|
|
|
|
// ResultSet rs = preparedStmt.getGeneratedKeys();
|
|
|
|
|
if (numAffectedRows > 0) |
|
|
|
|
{ |
|
|
|
|
Miscellaneous.logEvent("Amount added to existing entry.", 2); |
|
|
|
|
rs.close(); |
|
|
|
|
// rs.close();
|
|
|
|
|
preparedStmt.close(); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
@ -209,7 +225,7 @@ public class ShoppingListEntry
|
|
|
|
|
String updateQuery = "DELETE FROM listEntries WHERE listId=? AND productId=? AND unit=?"; |
|
|
|
|
preparedStmt = conn.prepareStatement(updateQuery); |
|
|
|
|
|
|
|
|
|
preparedStmt.setLong(1, this.getProduct().getId()); |
|
|
|
|
preparedStmt.setLong(1, this.getParentList().getId()); |
|
|
|
|
preparedStmt.setLong(2, this.getProduct().getId()); |
|
|
|
|
preparedStmt.setLong(3, this.getUnit().getId()); |
|
|
|
|
|
|
|
|
@ -217,14 +233,16 @@ public class ShoppingListEntry
|
|
|
|
|
|
|
|
|
|
long numAffectedRows = preparedStmt.executeUpdate(); |
|
|
|
|
Miscellaneous.logEvent("AMOUNT OF UPDATED ROWS: " + numAffectedRows, 5); |
|
|
|
|
ResultSet rs = preparedStmt.getGeneratedKeys(); |
|
|
|
|
// ResultSet rs = preparedStmt.getGeneratedKeys();
|
|
|
|
|
if (numAffectedRows > 0) |
|
|
|
|
{ |
|
|
|
|
Miscellaneous.logEvent("Resulting amount < 0, entry deleted.", 2); |
|
|
|
|
rs.close(); |
|
|
|
|
// rs.close();
|
|
|
|
|
preparedStmt.close(); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
preparedStmt.close(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -254,8 +272,6 @@ public class ShoppingListEntry
|
|
|
|
|
|
|
|
|
|
ResultSet res = preparedStmt.executeQuery(); |
|
|
|
|
|
|
|
|
|
boolean found = false; |
|
|
|
|
|
|
|
|
|
if(res.next()) |
|
|
|
|
entryAmount = res.getLong("combinationAmount"); |
|
|
|
|
|
|
|
|
@ -272,4 +288,18 @@ public class ShoppingListEntry
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public String toString() |
|
|
|
|
{ |
|
|
|
|
if(this.getUnit() != null) |
|
|
|
|
{ |
|
|
|
|
if(!this.getUnit().isDummy()) |
|
|
|
|
return String.valueOf(getAmount()) + " " + getUnit().getMainName() + " " + getProduct().getName(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return getProduct().getName(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|