Browse Source

Amount/Unit progress

development
jens 5 months ago
parent
commit
6ce0f76483
  1. 7
      ShoppingList/src/com/jens/rhasspy/shoppinglist/ShoppingList.java
  2. 150
      ShoppingList/src/com/jens/rhasspy/shoppinglist/ShoppingListEntry.java
  3. 12
      ShoppingList/src/com/jens/rhasspy/shoppinglist/Start.java
  4. 30
      ShoppingList/src/com/jens/rhasspy/shoppinglist/Unit.java

7
ShoppingList/src/com/jens/rhasspy/shoppinglist/ShoppingList.java

@ -196,7 +196,12 @@ public class ShoppingList
for(ShoppingListEntry entry : this.entries)
{
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);

150
ShoppingList/src/com/jens/rhasspy/shoppinglist/ShoppingListEntry.java

@ -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=?";

12
ShoppingList/src/com/jens/rhasspy/shoppinglist/Start.java

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

30
ShoppingList/src/com/jens/rhasspy/shoppinglist/Unit.java

@ -8,6 +8,8 @@ import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import org.apache.commons.lang3.StringUtils;
public class Unit
{
static ArrayList<Unit> unitCache = null;
@ -16,6 +18,7 @@ static ArrayList<Unit> unitCache = null;
String name;
String abbreviation;
boolean isDefault;
boolean isDummy;
public long getId()
{
@ -53,6 +56,15 @@ static ArrayList<Unit> unitCache = null;
this.isDefault = isDefault;
}
public boolean isDummy()
{
return isDummy;
}
public void setDummy(boolean isDummy)
{
this.isDummy = isDummy;
}
public static ArrayList<Unit> readAllUnits()
{
if(unitCache == null)
@ -84,6 +96,7 @@ static ArrayList<Unit> unitCache = null;
u.setName(res.getString("name"));
u.setAbbreviation(res.getString("abbreviation"));
u.setDefault(res.getInt("isDefault") == 1);
u.setDummy(res.getInt("isDummy") == 1);
unitCache.add(u);
}
@ -108,6 +121,16 @@ static ArrayList<Unit> unitCache = null;
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())
{
if(u.getName().equalsIgnoreCase(unitName))
@ -138,7 +161,7 @@ static ArrayList<Unit> unitCache = null;
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();
PreparedStatement preparedStmt = null;
try
@ -151,6 +174,11 @@ static ArrayList<Unit> unitCache = null;
preparedStmt.setInt(3, 1);
else
preparedStmt.setInt(3, 0);
if(this.isDummy)
preparedStmt.setInt(4, 1);
else
preparedStmt.setInt(4, 0);
preparedStmt.executeUpdate();
ResultSet rs = preparedStmt.getGeneratedKeys();

Loading…
Cancel
Save