Started amount and unit work.

This commit is contained in:
jens 2021-04-26 02:35:11 +02:00
parent 38547ed9dd
commit 3d79c8447c
4 changed files with 309 additions and 47 deletions

View File

@ -24,6 +24,7 @@ public class Product
{
this.id = id;
}
public String getName()
{
return name;
@ -41,6 +42,7 @@ public class Product
{
this.synonyms = synonyms;
}
public static ArrayList<Product> readAllProducts()
{
if(productCache == null)

View File

@ -10,6 +10,8 @@ public class ShoppingListEntry
{
ShoppingList parentList;
Product product;
float amount;
Unit unit;
public ShoppingList getParentList()
{
@ -31,6 +33,26 @@ public class ShoppingListEntry
this.product = product;
}
public float getAmount()
{
return amount;
}
public void setAmount(float amount)
{
this.amount = amount;
}
public Unit getUnit()
{
return unit;
}
public void setUnit(Unit unit)
{
this.unit = unit;
}
public boolean create()
{
PreparedStatement preparedStmt = null;
@ -39,22 +61,31 @@ public class ShoppingListEntry
{
conn = DatabaseHandler.getInstance().getConnection();
String parentQuery = "INSERT IGNORE INTO listEntries (listId, productId) VALUES (?, ?)";
preparedStmt = conn.prepareStatement(parentQuery, Statement.RETURN_GENERATED_KEYS);
if(!combinationExists(this.getParentList(), this.getProduct(), this.getUnit()))
{
String parentQuery = "INSERT INTO listEntries (listId, productId, amount, unit) VALUES (?, ?, ?, ?)";
preparedStmt = conn.prepareStatement(parentQuery, Statement.RETURN_GENERATED_KEYS);
preparedStmt.setLong(1, this.getParentList().getId());
preparedStmt.setLong(2, this.getProduct().getId());
preparedStmt.setLong(1, this.getParentList().getId());
preparedStmt.setLong(2, this.getProduct().getId());
preparedStmt.setFloat(3, this.getAmount());
preparedStmt.setLong(4, this.getUnit().getId());
Miscellaneous.logEvent(preparedStmt.toString(), 5);
Miscellaneous.logEvent(preparedStmt.toString(), 5);
long numAffectedRows = preparedStmt.executeUpdate();
Miscellaneous.logEvent("AMOUNT OF UPDATED ROWS: " + numAffectedRows, 5);
ResultSet rs = preparedStmt.getGeneratedKeys();
// if (numAffectedRows > 0)
// {
preparedStmt.close();
return true;
// }
long numAffectedRows = preparedStmt.executeUpdate();
Miscellaneous.logEvent("AMOUNT OF UPDATED ROWS: " + numAffectedRows, 5);
ResultSet rs = preparedStmt.getGeneratedKeys();
// if (numAffectedRows > 0)
// {
preparedStmt.close();
return true;
// }
}
else
{
continue here.
}
}
catch(Exception e)
{
@ -76,45 +107,80 @@ public class ShoppingListEntry
}
public boolean delete()
{
PreparedStatement preparedStmt = null;
Connection conn = null;
try
{
conn = DatabaseHandler.getInstance().getConnection();
String parentQuery = "DELETE FROM listEntries WHERE listId=? AND productId=?";
preparedStmt = conn.prepareStatement(parentQuery, Statement.RETURN_GENERATED_KEYS);
preparedStmt.setLong(1, this.getParentList().getId());
preparedStmt.setLong(2, this.getProduct().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)
// {
preparedStmt.close();
return true;
// }
}
catch(Exception e)
{
Miscellaneous.logEvent(Diverse.getStackTraceAsString(e), 1);
}
finally
{
try
{
if(preparedStmt != null && !preparedStmt.isClosed())
preparedStmt.close();
}
catch (SQLException e)
{
}
}
return false;
}
private boolean combinationExists(ShoppingList parentList, Product product, Unit unit)
{
PreparedStatement preparedStmt = null;
Connection conn = null;
String query = "SELECT count(listId) as combinationAmount FROM listEntries WHERE listId=? AND productId=? AND unit=?";
long entryAmount = 0;
try
{
conn = DatabaseHandler.getInstance().getConnection();
String parentQuery = "DELETE FROM listEntries WHERE listId=? AND productId=?";
preparedStmt = conn.prepareStatement(parentQuery, Statement.RETURN_GENERATED_KEYS);
preparedStmt.setLong(1, this.getParentList().getId());
preparedStmt.setLong(2, this.getProduct().getId());
PreparedStatement preparedStmt = DatabaseHandler.getInstance().getConnection().prepareStatement(query);
preparedStmt.setLong(1, parentList.getId());
preparedStmt.setLong(2, product.getId());
preparedStmt.setLong(3, unit.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)
// {
preparedStmt.close();
return true;
// }
ResultSet res = preparedStmt.executeQuery();
boolean found = false;
if(res.next())
entryAmount = res.getLong("combinationAmount");
res.close();
preparedStmt.close();
}
catch(Exception e)
{
Miscellaneous.logEvent(Diverse.getStackTraceAsString(e), 1);
}
finally
{
try
{
if(preparedStmt != null && !preparedStmt.isClosed())
preparedStmt.close();
}
catch (SQLException e)
{
}
}
if(entryAmount > 0)
return true;
return false;
}

View File

@ -0,0 +1,194 @@
package com.jens.rhasspy.shoppinglist;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
public class Unit
{
static ArrayList<Unit> unitCache = null;
long id;
String name;
String abbreviation;
boolean isDefault;
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAbbreviation()
{
return abbreviation;
}
public void setAbbreviation(String abbreviation)
{
this.abbreviation = abbreviation;
}
public boolean isDefault()
{
return isDefault;
}
public void setDefault(boolean isDefault)
{
this.isDefault = isDefault;
}
public static ArrayList<Unit> readAllUnits()
{
if(unitCache == null)
{
try
{
Connection conn = DatabaseHandler.getInstance().getConnection();
if(conn == null)
Start.exitWithError(Settings.languageBlock.get("dbCouldNotConnect"));
PreparedStatement preparedStmt = null;
String query = "SELECT * FROM units";
preparedStmt = conn.prepareStatement(query);
Miscellaneous.logEvent(preparedStmt.toString(), 5);
ResultSet res = preparedStmt.executeQuery();
unitCache = new ArrayList<Unit>();
while (res.next())
{
Unit u = new Unit();
u.setId(res.getLong("id"));
u.setName(res.getString("name"));
u.setAbbreviation(res.getString("abbreviation"));
u.setDefault(res.getInt("isDefault") == 1);
unitCache.add(u);
}
res.close();
preparedStmt.close();
}
catch(Exception e)
{
Start.exitWithError(Settings.languageBlock.get("dbCouldNotConnect"));
}
}
return unitCache;
}
@Override
public boolean equals(Object obj)
{
return ((Unit)obj).getName().equalsIgnoreCase(getName());
}
public static Unit getByName(String unitName)
{
for(Unit u : readAllUnits())
{
if(u.getName().equalsIgnoreCase(unitName))
return u;
else if(u.getName().equalsIgnoreCase(unitName.replace("-", " ")))
return u;
}
return null;
}
@Override
public String toString()
{
return getName();
}
public static Unit getById(long id)
{
for(Unit u : readAllUnits())
{
if(u.getId() == id)
return u;
}
return null;
}
public boolean create()
{
String query = "INSERT INTO units (name, abbreviation, isDefault) VALUES (?, ?, ?)";
Connection conn = DatabaseHandler.getInstance().getConnection();
PreparedStatement preparedStmt = null;
try
{
preparedStmt = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
preparedStmt.setString(1, this.getName());
preparedStmt.setString(2, this.getAbbreviation());
if(this.isDefault)
preparedStmt.setInt(3, 1);
else
preparedStmt.setInt(3, 0);
preparedStmt.executeUpdate();
ResultSet rs = preparedStmt.getGeneratedKeys();
if (rs.next())
{
this.setId(rs.getInt(1));
Miscellaneous.logEvent("INSERT-ID: " + String.valueOf(this.getId()), 5);
rs.close();
preparedStmt.close();
Miscellaneous.logEvent("Unit has been successfully created.", 2);
return true;
}
else
{
rs.close();
String error = "Insert new unit failed.";
Miscellaneous.logEvent(error, 1);
}
}
catch (SQLException e)
{
Miscellaneous.logEvent(Diverse.getStackTraceAsString(e), 1);
}
finally
{
try
{
if(preparedStmt != null && !preparedStmt.isClosed())
preparedStmt.close();
}
catch (SQLException e)
{
}
}
return false;
}
}