Amount/Unit progress

This commit is contained in:
jens 2021-04-27 19:59:01 +02:00
parent 1950db40e0
commit 6ce0f76483
4 changed files with 129 additions and 70 deletions

View File

@ -196,7 +196,12 @@ public class ShoppingList
for(ShoppingListEntry entry : this.entries) for(ShoppingListEntry entry : this.entries)
{ {
if(entry.getProduct().getStoreType().equals(st)) if(entry.getProduct().getStoreType().equals(st))
shoppingListString.append(entry.getProduct().getName() + Diverse.lineSeparator); {
if(entry.getUnit().isDummy)
shoppingListString.append(entry.getProduct().getName() + Diverse.lineSeparator);
else
shoppingListString.append(String.valueOf(entry.getAmount()) + " " + entry.getUnit().getAbbreviation() + " " + entry.getProduct().getName() + Diverse.lineSeparator);
}
} }
shoppingListString.append(Diverse.lineSeparator); shoppingListString.append(Diverse.lineSeparator);

View File

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

View File

@ -9,6 +9,8 @@ public class Start
{ {
public static void main(String[] args) public static void main(String[] args)
{ {
float amount = 0;
String unitName = null;
String productName = null; String productName = null;
String action = null; String action = null;
String filePath = null; String filePath = null;
@ -29,7 +31,11 @@ public class Start
// System.out.println("Found double dash with command " + argstring.substring(2, len) ); // System.out.println("Found double dash with command " + argstring.substring(2, len) );
String argName = argstring.substring(2, len); String argName = argstring.substring(2, len);
doubleOptsList.add(argName); doubleOptsList.add(argName);
if(argName.equalsIgnoreCase("shoppingProduct")) if(argName.equalsIgnoreCase("amount"))
amount = Float.parseFloat(args[i+1].replace("\"", ""));
else if(argName.equalsIgnoreCase("unitName"))
unitName = args[i+1].replace("\"", "");
else if(argName.equalsIgnoreCase("shoppingProduct"))
productName = args[i+1].replace("\"", ""); productName = args[i+1].replace("\"", "");
else if(argName.equalsIgnoreCase("action")) else if(argName.equalsIgnoreCase("action"))
action = args[i+1].replace("\"", ""); action = args[i+1].replace("\"", "");
@ -65,6 +71,8 @@ public class Start
if(!StringUtils.isEmpty(productName)) if(!StringUtils.isEmpty(productName))
{ {
ShoppingList list = ShoppingList.getMostRecentList(); ShoppingList list = ShoppingList.getMostRecentList();
Unit u = Unit.getByName(unitName);
Product p = Product.getByName(productName); Product p = Product.getByName(productName);
if(list == null) if(list == null)
@ -76,6 +84,8 @@ public class Start
ShoppingListEntry entry = new ShoppingListEntry(); ShoppingListEntry entry = new ShoppingListEntry();
entry.setParentList(list); entry.setParentList(list);
entry.setProduct(p); entry.setProduct(p);
entry.setAmount(amount);
entry.setUnit(u);
if(entry.create()) if(entry.create())
{ {

View File

@ -8,6 +8,8 @@ import java.sql.Statement;
import java.sql.Types; import java.sql.Types;
import java.util.ArrayList; import java.util.ArrayList;
import org.apache.commons.lang3.StringUtils;
public class Unit public class Unit
{ {
static ArrayList<Unit> unitCache = null; static ArrayList<Unit> unitCache = null;
@ -16,6 +18,7 @@ static ArrayList<Unit> unitCache = null;
String name; String name;
String abbreviation; String abbreviation;
boolean isDefault; boolean isDefault;
boolean isDummy;
public long getId() public long getId()
{ {
@ -53,6 +56,15 @@ static ArrayList<Unit> unitCache = null;
this.isDefault = isDefault; this.isDefault = isDefault;
} }
public boolean isDummy()
{
return isDummy;
}
public void setDummy(boolean isDummy)
{
this.isDummy = isDummy;
}
public static ArrayList<Unit> readAllUnits() public static ArrayList<Unit> readAllUnits()
{ {
if(unitCache == null) if(unitCache == null)
@ -84,6 +96,7 @@ static ArrayList<Unit> unitCache = null;
u.setName(res.getString("name")); u.setName(res.getString("name"));
u.setAbbreviation(res.getString("abbreviation")); u.setAbbreviation(res.getString("abbreviation"));
u.setDefault(res.getInt("isDefault") == 1); u.setDefault(res.getInt("isDefault") == 1);
u.setDummy(res.getInt("isDummy") == 1);
unitCache.add(u); unitCache.add(u);
} }
@ -108,6 +121,16 @@ static ArrayList<Unit> unitCache = null;
public static Unit getByName(String unitName) public static Unit getByName(String unitName)
{ {
if(StringUtils.isEmpty(unitName))
{
for(Unit u : readAllUnits())
{
if(u.isDummy())
return u;
}
return null;
}
for(Unit u : readAllUnits()) for(Unit u : readAllUnits())
{ {
if(u.getName().equalsIgnoreCase(unitName)) if(u.getName().equalsIgnoreCase(unitName))
@ -138,7 +161,7 @@ static ArrayList<Unit> unitCache = null;
public boolean create() public boolean create()
{ {
String query = "INSERT INTO units (name, abbreviation, isDefault) VALUES (?, ?, ?)"; String query = "INSERT INTO units (name, abbreviation, isDefault, isDummy) VALUES (?, ?, ?, ?)";
Connection conn = DatabaseHandler.getInstance().getConnection(); Connection conn = DatabaseHandler.getInstance().getConnection();
PreparedStatement preparedStmt = null; PreparedStatement preparedStmt = null;
try try
@ -152,6 +175,11 @@ static ArrayList<Unit> unitCache = null;
else else
preparedStmt.setInt(3, 0); preparedStmt.setInt(3, 0);
if(this.isDummy)
preparedStmt.setInt(4, 1);
else
preparedStmt.setInt(4, 0);
preparedStmt.executeUpdate(); preparedStmt.executeUpdate();
ResultSet rs = preparedStmt.getGeneratedKeys(); ResultSet rs = preparedStmt.getGeneratedKeys();
if (rs.next()) if (rs.next())