Units/amount complete.

This commit is contained in:
jens 2021-04-29 15:42:59 +02:00
parent 6ce0f76483
commit 5b81de7484
4 changed files with 164 additions and 48 deletions

View File

@ -173,7 +173,7 @@ public class ShoppingList
return resultList;
}
public boolean send()
public boolean sendViaEmail()
{
StringBuilder shoppingListString = new StringBuilder();

View File

@ -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();
}
}

View File

@ -31,9 +31,9 @@ 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("amount"))
if(argName.equalsIgnoreCase("shoppingAmount"))
amount = Float.parseFloat(args[i+1].replace("\"", ""));
else if(argName.equalsIgnoreCase("unitName"))
else if(argName.equalsIgnoreCase("shoppingUnit"))
unitName = args[i+1].replace("\"", "");
else if(argName.equalsIgnoreCase("shoppingProduct"))
productName = args[i+1].replace("\"", "");
@ -75,25 +75,28 @@ public class Start
Unit u = Unit.getByName(unitName);
Product p = Product.getByName(productName);
if(list == null)
exitWithError(Settings.languageBlock.get("couldNotCreateList"));
if(p == null)
exitWithError(Settings.languageBlock.get("productNotFound") + " " + productName);
ShoppingListEntry entry = new ShoppingListEntry();
entry.setParentList(list);
entry.setProduct(p);
entry.setAmount(amount);
entry.setUnit(u);
if(amount > 0 && (u == null || u.isDummy))
entry.setUnit(Unit.getPieceUnit());
if(list == null)
exitWithError(Settings.languageBlock.get("couldNotCreateList"));
if(p == null)
exitWithError(Settings.languageBlock.get("productNotFound") + " " + entry.toString());
if(entry.create())
{
DatabaseHandler.getInstance().disconnect();
System.exit(0);
}
else
exitWithError(Settings.languageBlock.get("couldNotAddProdToList") + " " + productName);
exitWithError(Settings.languageBlock.get("couldNotAddProdToList") + " " + entry.toString());
}
else
System.out.println(Settings.languageBlock.get("noProdSpecified"));
@ -103,26 +106,57 @@ public class Start
{
ShoppingList list = ShoppingList.getMostRecentList();
Product p = Product.getByName(productName);
Unit u = Unit.getByName(unitName);
ShoppingListEntry entry = new ShoppingListEntry();
entry.setParentList(list);
entry.setProduct(p);
entry.setAmount(amount);
entry.setUnit(u);
if(amount > 0 && (u == null || u.isDummy))
entry.setUnit(Unit.getPieceUnit());
if(list == null)
exitWithError(Settings.languageBlock.get("couldNotCreateList"));
if(p == null)
exitWithError(Settings.languageBlock.get("productNotFound") + " " + productName);
for(ShoppingListEntry entry : list.getEntries())
exitWithError(Settings.languageBlock.get("productNotFound") + " " + entry.toString());
if(entry.delete())
{
if(entry.getProduct().equals(p))
{
if(entry.delete())
{
DatabaseHandler.getInstance().disconnect();
System.exit(0);
}
else
Miscellaneous.logEvent(Settings.languageBlock.get("couldNotRemoveProdFromList") + " " + productName, 2);
}
DatabaseHandler.getInstance().disconnect();
System.exit(0);
}
else
exitWithError(Settings.languageBlock.get("couldNotRemoveProdFromList") + " " + entry.toString());
// for(ShoppingListEntry entry : list.getEntries())
// {
// if(entry.getProduct().equals(p))
// {
// if(amount > 0 && u != null && !u.isDummy())
// {
// /*
// * Amount and unit specified. Adjust entries that may exist.
// */
// }
// else
// {
// /*
// * No amount unit or unit specified. Delete all existing entries
// * of that product from the list.
// */
// if(entry.delete())
// {
// DatabaseHandler.getInstance().disconnect();
// System.exit(0);
// }
// else
// Miscellaneous.logEvent(Settings.languageBlock.get("couldNotRemoveProdFromList") + " " + productName, 2);
// }
// }
// }
// If it wasn't on the list - why care?
DatabaseHandler.getInstance().disconnect();
System.exit(0);
@ -131,7 +165,7 @@ public class Start
System.out.println(Settings.languageBlock.get("noProdSpecified"));
break;
case "sendList":
if(ShoppingList.getMostRecentList().send())
if(ShoppingList.getMostRecentList().sendViaEmail())
{
// System.out.println("Liste wurde verschickt.");
DatabaseHandler.getInstance().disconnect();

View File

@ -19,6 +19,7 @@ static ArrayList<Unit> unitCache = null;
String abbreviation;
boolean isDefault;
boolean isDummy;
boolean isPiece;
public long getId()
{
@ -29,6 +30,14 @@ static ArrayList<Unit> unitCache = null;
this.id = id;
}
public String getMainName()
{
if(name.contains(";"))
return name.split(";")[0];
else
return name;
}
public String getName()
{
return name;
@ -65,6 +74,15 @@ static ArrayList<Unit> unitCache = null;
this.isDummy = isDummy;
}
public boolean isPiece()
{
return isPiece;
}
public void setPiece(boolean isPiece)
{
this.isPiece = isPiece;
}
public static ArrayList<Unit> readAllUnits()
{
if(unitCache == null)
@ -97,6 +115,7 @@ static ArrayList<Unit> unitCache = null;
u.setAbbreviation(res.getString("abbreviation"));
u.setDefault(res.getInt("isDefault") == 1);
u.setDummy(res.getInt("isDummy") == 1);
u.setPiece(res.getInt("isPiece") == 1);
unitCache.add(u);
}
@ -133,10 +152,13 @@ static ArrayList<Unit> unitCache = null;
for(Unit u : readAllUnits())
{
if(u.getName().equalsIgnoreCase(unitName))
return u;
else if(u.getName().equalsIgnoreCase(unitName.replace("-", " ")))
return u;
for(String unitSynonym : u.getName().split(";"))
{
if(unitSynonym.equalsIgnoreCase(unitName))
return u;
else if(unitSynonym.equalsIgnoreCase(unitName.replace("-", " ")))
return u;
}
}
return null;
@ -145,7 +167,10 @@ static ArrayList<Unit> unitCache = null;
@Override
public String toString()
{
return getName();
if(getName().contains(";"))
return getName().split(";")[0];
else
return getName();
}
public static Unit getById(long id)
@ -161,7 +186,7 @@ static ArrayList<Unit> unitCache = null;
public boolean create()
{
String query = "INSERT INTO units (name, abbreviation, isDefault, isDummy) VALUES (?, ?, ?, ?)";
String query = "INSERT INTO units (name, abbreviation, isDefault, isDummy, isPiece) VALUES (?, ?, ?, ?, ?)";
Connection conn = DatabaseHandler.getInstance().getConnection();
PreparedStatement preparedStmt = null;
try
@ -179,6 +204,11 @@ static ArrayList<Unit> unitCache = null;
preparedStmt.setInt(4, 1);
else
preparedStmt.setInt(4, 0);
if(this.isPiece)
preparedStmt.setInt(5, 1);
else
preparedStmt.setInt(5, 0);
preparedStmt.executeUpdate();
ResultSet rs = preparedStmt.getGeneratedKeys();
@ -219,4 +249,26 @@ static ArrayList<Unit> unitCache = null;
return false;
}
public static Unit getDefaultUnit()
{
for(Unit u : readAllUnits())
{
if(u.isDefault())
return u;
}
return null;
}
public static Unit getPieceUnit()
{
for(Unit u : readAllUnits())
{
if(u.isPiece())
return u;
}
return null;
}
}