Compare commits
17 Commits
38547ed9dd
...
0971e3cfaf
Author | SHA1 | Date | |
---|---|---|---|
0971e3cfaf | |||
84e73a9ec5 | |||
89f51dab28 | |||
39db9470cc | |||
3c752a9ef1 | |||
07dd5f9c7e | |||
9b9c1c2323 | |||
4ddd2563b4 | |||
7bd9c4d16f | |||
db41fb525f | |||
34eb2ba9bf | |||
5b81de7484 | |||
d75cb0a32f | |||
26f6e4b7c7 | |||
6ce0f76483 | |||
1950db40e0 | |||
3d79c8447c |
Binary file not shown.
196
Database schema/shoppingList.sql
Normal file
196
Database schema/shoppingList.sql
Normal file
@ -0,0 +1,196 @@
|
||||
-- phpMyAdmin SQL Dump
|
||||
-- version 5.0.4
|
||||
-- https://www.phpmyadmin.net/
|
||||
--
|
||||
|
||||
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
|
||||
START TRANSACTION;
|
||||
SET time_zone = "+00:00";
|
||||
|
||||
|
||||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
|
||||
/*!40101 SET NAMES utf8mb4 */;
|
||||
|
||||
--
|
||||
-- Database: `shoppingList`
|
||||
--
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Table structure for table `listEntries`
|
||||
--
|
||||
|
||||
CREATE TABLE `listEntries` (
|
||||
`listId` int(11) NOT NULL,
|
||||
`productId` int(11) NOT NULL,
|
||||
`amount` float NOT NULL DEFAULT 1,
|
||||
`unit` int(11) NOT NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Table structure for table `lists`
|
||||
--
|
||||
|
||||
CREATE TABLE `lists` (
|
||||
`id` int(11) NOT NULL,
|
||||
`creationTime` bigint(20) NOT NULL,
|
||||
`comment` varchar(200) DEFAULT NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Table structure for table `products`
|
||||
--
|
||||
|
||||
CREATE TABLE `products` (
|
||||
`id` int(11) NOT NULL,
|
||||
`name` varchar(100) NOT NULL,
|
||||
`synonyms` varchar(500) DEFAULT NULL COMMENT 'Separate entries with semicolons.',
|
||||
`storeTypeId` int(11) NOT NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Table structure for table `settings`
|
||||
--
|
||||
|
||||
CREATE TABLE `settings` (
|
||||
`settingName` varchar(100) NOT NULL,
|
||||
`settingValue` varchar(100) NOT NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
--
|
||||
-- Dumping data for table `settings`
|
||||
--
|
||||
|
||||
INSERT INTO `settings` (`settingName`, `settingValue`) VALUES
|
||||
('databaseVersion', '3');
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Table structure for table `storeTypes`
|
||||
--
|
||||
|
||||
CREATE TABLE `storeTypes` (
|
||||
`id` int(11) NOT NULL,
|
||||
`name` varchar(200) NOT NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Table structure for table `units`
|
||||
--
|
||||
|
||||
CREATE TABLE `units` (
|
||||
`id` int(11) NOT NULL,
|
||||
`name` varchar(100) NOT NULL,
|
||||
`abbreviation` varchar(10) NOT NULL,
|
||||
`isDefault` tinyint(2) NOT NULL DEFAULT 0,
|
||||
`isDummy` tinyint(2) NOT NULL DEFAULT 0 COMMENT 'This unit will not actually be displayed. Instead an item will just be shown without amount or unit.',
|
||||
`isPiece` tinyint(2) NOT NULL DEFAULT 0
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
--
|
||||
-- Indexes for dumped tables
|
||||
--
|
||||
|
||||
--
|
||||
-- Indexes for table `listEntries`
|
||||
--
|
||||
ALTER TABLE `listEntries`
|
||||
ADD PRIMARY KEY (`listId`,`productId`,`unit`),
|
||||
ADD KEY `listEntries_ibfk_3` (`unit`),
|
||||
ADD KEY `listEntries_ibfk_2` (`productId`);
|
||||
|
||||
--
|
||||
-- Indexes for table `lists`
|
||||
--
|
||||
ALTER TABLE `lists`
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
--
|
||||
-- Indexes for table `products`
|
||||
--
|
||||
ALTER TABLE `products`
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD UNIQUE KEY `name` (`name`),
|
||||
ADD KEY `storeTypeId` (`storeTypeId`);
|
||||
|
||||
--
|
||||
-- Indexes for table `settings`
|
||||
--
|
||||
ALTER TABLE `settings`
|
||||
ADD PRIMARY KEY (`settingName`);
|
||||
|
||||
--
|
||||
-- Indexes for table `storeTypes`
|
||||
--
|
||||
ALTER TABLE `storeTypes`
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD UNIQUE KEY `name` (`name`);
|
||||
|
||||
--
|
||||
-- Indexes for table `units`
|
||||
--
|
||||
ALTER TABLE `units`
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
--
|
||||
-- AUTO_INCREMENT for dumped tables
|
||||
--
|
||||
|
||||
--
|
||||
-- AUTO_INCREMENT for table `lists`
|
||||
--
|
||||
ALTER TABLE `lists`
|
||||
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
|
||||
|
||||
--
|
||||
-- AUTO_INCREMENT for table `products`
|
||||
--
|
||||
ALTER TABLE `products`
|
||||
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
|
||||
|
||||
--
|
||||
-- AUTO_INCREMENT for table `storeTypes`
|
||||
--
|
||||
ALTER TABLE `storeTypes`
|
||||
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
|
||||
|
||||
--
|
||||
-- AUTO_INCREMENT for table `units`
|
||||
--
|
||||
ALTER TABLE `units`
|
||||
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
|
||||
|
||||
--
|
||||
-- Constraints for dumped tables
|
||||
--
|
||||
|
||||
--
|
||||
-- Constraints for table `listEntries`
|
||||
--
|
||||
ALTER TABLE `listEntries`
|
||||
ADD CONSTRAINT `listEntries_ibfk_1` FOREIGN KEY (`listId`) REFERENCES `lists` (`id`),
|
||||
ADD CONSTRAINT `listEntries_ibfk_2` FOREIGN KEY (`productId`) REFERENCES `products` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
ADD CONSTRAINT `listEntries_ibfk_3` FOREIGN KEY (`unit`) REFERENCES `units` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
--
|
||||
-- Constraints for table `products`
|
||||
--
|
||||
ALTER TABLE `products`
|
||||
ADD CONSTRAINT `products_ibfk_1` FOREIGN KEY (`storeTypeId`) REFERENCES `storeTypes` (`id`);
|
||||
COMMIT;
|
||||
|
||||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
@ -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)
|
||||
|
@ -143,6 +143,8 @@ public class ShoppingList
|
||||
ShoppingListEntry entry = new ShoppingListEntry();
|
||||
entry.setParentList(resultList);
|
||||
entry.setProduct(Product.getById(res.getLong("productId")));
|
||||
entry.setAmount(res.getFloat("amount"));
|
||||
entry.setUnit(Unit.getById(res.getLong("unit")));
|
||||
resultList.getEntries().add(entry);
|
||||
}
|
||||
|
||||
@ -171,7 +173,7 @@ public class ShoppingList
|
||||
return resultList;
|
||||
}
|
||||
|
||||
public boolean send()
|
||||
public boolean sendViaEmail()
|
||||
{
|
||||
StringBuilder shoppingListString = new StringBuilder();
|
||||
|
||||
@ -194,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);
|
||||
|
@ -10,6 +10,8 @@ public class ShoppingListEntry
|
||||
{
|
||||
ShoppingList parentList;
|
||||
Product product;
|
||||
float amount;
|
||||
Unit unit;
|
||||
|
||||
public ShoppingList getParentList()
|
||||
{
|
||||
@ -31,30 +33,63 @@ 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;
|
||||
Connection conn = null;
|
||||
try
|
||||
{
|
||||
if(amount <= 0 && unit != null && !unit.isDummy)
|
||||
return false;
|
||||
|
||||
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)
|
||||
{
|
||||
rs.close();
|
||||
preparedStmt.close();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return updateAmount(false);
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
@ -81,24 +116,37 @@ public class ShoppingListEntry
|
||||
Connection conn = null;
|
||||
try
|
||||
{
|
||||
if(amount <= 0 && unit != null && !unit.isDummy)
|
||||
return false;
|
||||
|
||||
conn = DatabaseHandler.getInstance().getConnection();
|
||||
|
||||
String parentQuery = "DELETE FROM listEntries WHERE listId=? AND productId=?";
|
||||
preparedStmt = conn.prepareStatement(parentQuery, Statement.RETURN_GENERATED_KEYS);
|
||||
if(this.getAmount() == 0 || this.getUnit().isDummy())
|
||||
{
|
||||
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());
|
||||
preparedStmt.setLong(1, this.getParentList().getId());
|
||||
preparedStmt.setLong(2, this.getProduct().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
|
||||
{
|
||||
if(combinationExists(this.getParentList(), this.getProduct(), this.getUnit()))
|
||||
return updateAmount(true);
|
||||
else
|
||||
return true; // Combination to be deleted does not exist. So what?
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
@ -118,4 +166,141 @@ public class ShoppingListEntry
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected boolean updateAmount(boolean delete)
|
||||
{
|
||||
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");
|
||||
|
||||
boolean condition = true;
|
||||
if(delete)
|
||||
condition = currentAmount - this.getAmount() > 0;
|
||||
|
||||
if(condition)
|
||||
{
|
||||
// Update the amount
|
||||
String updateQuery = "UPDATE listEntries set amount=? WHERE listId=? AND productId=? AND unit=?";
|
||||
preparedStmt = conn.prepareStatement(updateQuery);
|
||||
|
||||
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());
|
||||
|
||||
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.getParentList().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;
|
||||
}
|
||||
else
|
||||
preparedStmt.close();
|
||||
}
|
||||
}
|
||||
|
||||
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=?";
|
||||
long entryAmount = 0;
|
||||
|
||||
try
|
||||
{
|
||||
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);
|
||||
|
||||
ResultSet res = preparedStmt.executeQuery();
|
||||
|
||||
if(res.next())
|
||||
entryAmount = res.getLong("combinationAmount");
|
||||
|
||||
res.close();
|
||||
preparedStmt.close();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Miscellaneous.logEvent(Diverse.getStackTraceAsString(e), 1);
|
||||
}
|
||||
|
||||
if(entryAmount > 0)
|
||||
return true;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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("shoppingAmount"))
|
||||
amount = Float.parseFloat(args[i+1].replace("\"", ""));
|
||||
else if(argName.equalsIgnoreCase("shoppingUnit"))
|
||||
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,17 +71,24 @@ public class Start
|
||||
if(!StringUtils.isEmpty(productName))
|
||||
{
|
||||
ShoppingList list = ShoppingList.getMostRecentList();
|
||||
|
||||
Unit u = Unit.getByName(unitName);
|
||||
Product p = Product.getByName(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") + " " + productName);
|
||||
|
||||
ShoppingListEntry entry = new ShoppingListEntry();
|
||||
entry.setParentList(list);
|
||||
entry.setProduct(p);
|
||||
exitWithError(Settings.languageBlock.get("productNotFound") + " " + entry.toString());
|
||||
|
||||
if(entry.create())
|
||||
{
|
||||
@ -83,7 +96,7 @@ public class Start
|
||||
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"));
|
||||
@ -93,26 +106,31 @@ 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);
|
||||
exitWithError(Settings.languageBlock.get("productNotFound") + " " + entry.toString());
|
||||
|
||||
for(ShoppingListEntry entry : list.getEntries())
|
||||
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());
|
||||
|
||||
// If it wasn't on the list - why care?
|
||||
DatabaseHandler.getInstance().disconnect();
|
||||
System.exit(0);
|
||||
@ -121,7 +139,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();
|
||||
|
274
ShoppingList/src/com/jens/rhasspy/shoppinglist/Unit.java
Normal file
274
ShoppingList/src/com/jens/rhasspy/shoppinglist/Unit.java
Normal file
@ -0,0 +1,274 @@
|
||||
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;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class Unit
|
||||
{
|
||||
static ArrayList<Unit> unitCache = null;
|
||||
|
||||
long id;
|
||||
String name;
|
||||
String abbreviation;
|
||||
boolean isDefault;
|
||||
boolean isDummy;
|
||||
boolean isPiece;
|
||||
|
||||
public long getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
public void setId(long id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMainName()
|
||||
{
|
||||
if(name.contains(";"))
|
||||
return name.split(";")[0];
|
||||
else
|
||||
return name;
|
||||
}
|
||||
|
||||
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 boolean isDummy()
|
||||
{
|
||||
return isDummy;
|
||||
}
|
||||
public void setDummy(boolean isDummy)
|
||||
{
|
||||
this.isDummy = isDummy;
|
||||
}
|
||||
|
||||
public boolean isPiece()
|
||||
{
|
||||
return isPiece;
|
||||
}
|
||||
public void setPiece(boolean isPiece)
|
||||
{
|
||||
this.isPiece = isPiece;
|
||||
}
|
||||
|
||||
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);
|
||||
u.setDummy(res.getInt("isDummy") == 1);
|
||||
u.setPiece(res.getInt("isPiece") == 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)
|
||||
{
|
||||
if(StringUtils.isEmpty(unitName))
|
||||
{
|
||||
for(Unit u : readAllUnits())
|
||||
{
|
||||
if(u.isDummy())
|
||||
return u;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
for(Unit u : readAllUnits())
|
||||
{
|
||||
for(String unitSynonym : u.getName().split(";"))
|
||||
{
|
||||
if(unitSynonym.equalsIgnoreCase(unitName))
|
||||
return u;
|
||||
else if(unitSynonym.equalsIgnoreCase(unitName.replace("-", " ")))
|
||||
return u;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
if(getName().contains(";"))
|
||||
return getName().split(";")[0];
|
||||
else
|
||||
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, isDummy, isPiece) 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);
|
||||
|
||||
if(this.isDummy)
|
||||
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();
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -7,5 +7,6 @@
|
||||
public static $mysqlpw = 'somePassword'; //Password
|
||||
public static $mysqldb = 'shoppingList'; //Database
|
||||
public static $mysqlPort = 3306;
|
||||
public static $rhasspyMasterUrl = "http://rhasspy-master:12101"; // URL of your Rhasspy master webpage WITHOUT ending slash. It is required if you want to trigger a retraining.
|
||||
}
|
||||
?>
|
@ -1,12 +1,32 @@
|
||||
#!/bin/bash
|
||||
|
||||
SHOPPINGITEM=""
|
||||
AMOUNT=""
|
||||
UNIT=""
|
||||
SITEID=""
|
||||
PARAMS=""
|
||||
SESSIONID=""
|
||||
|
||||
while (( "$#" )); do
|
||||
case "$1" in
|
||||
--shoppingAmount)
|
||||
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
|
||||
AMOUNT=$2
|
||||
shift 2
|
||||
else
|
||||
echo "Error: Argument for $1 is missing" >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
--shoppingUnit)
|
||||
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
|
||||
UNIT=$2
|
||||
shift 2
|
||||
else
|
||||
echo "Error: Argument for $1 is missing" >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
--shoppingProduct)
|
||||
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
|
||||
SHOPPINGITEM=$2
|
||||
@ -53,7 +73,21 @@ then
|
||||
echo "Was soll ich auf die Liste setzen?"
|
||||
exit 1
|
||||
else
|
||||
java -jar /home/pi/hc_scripts/ShoppingList.jar --action addToList --shoppingProduct $SHOPPINGITEM
|
||||
lengthAmount=${#AMOUNT}
|
||||
lengthUnit=${#UNIT}
|
||||
|
||||
if [ $lengthAmount -gt 0 ]
|
||||
then
|
||||
if [ $lengthUnit -gt 0 ]
|
||||
then
|
||||
java -jar /home/pi/hc_scripts/ShoppingList.jar --action addToList --shoppingAmount $AMOUNT --shoppingUnit $UNIT --shoppingProduct $SHOPPINGITEM
|
||||
else
|
||||
java -jar /home/pi/hc_scripts/ShoppingList.jar --action addToList --shoppingAmount $AMOUNT --shoppingProduct $SHOPPINGITEM
|
||||
fi
|
||||
else
|
||||
java -jar /home/pi/hc_scripts/ShoppingList.jar --action addToList --shoppingProduct $SHOPPINGITEM
|
||||
fi
|
||||
|
||||
if [ "$?" -eq "0" ]
|
||||
then
|
||||
exit 0
|
||||
|
@ -1,12 +1,32 @@
|
||||
#!/bin/bash
|
||||
|
||||
SHOPPINGITEM=""
|
||||
AMOUNT=""
|
||||
UNIT=""
|
||||
SITEID=""
|
||||
PARAMS=""
|
||||
SESSIONID=""
|
||||
|
||||
while (( "$#" )); do
|
||||
case "$1" in
|
||||
--shoppingAmount)
|
||||
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
|
||||
AMOUNT=$2
|
||||
shift 2
|
||||
else
|
||||
echo "Error: Argument for $1 is missing" >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
--shoppingUnit)
|
||||
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
|
||||
UNIT=$2
|
||||
shift 2
|
||||
else
|
||||
echo "Error: Argument for $1 is missing" >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
--shoppingProduct)
|
||||
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
|
||||
SHOPPINGITEM=$2
|
||||
@ -53,11 +73,25 @@ then
|
||||
echo "Was soll ich von der Liste streichen?"
|
||||
exit 1
|
||||
else
|
||||
java -jar /home/pi/hc_scripts/ShoppingList.jar --action removeFromList --shoppingProduct $SHOPPINGITEM
|
||||
if [ "$?" -eq "0" ]
|
||||
then
|
||||
exit 0
|
||||
else
|
||||
exit 1
|
||||
fi
|
||||
lengthAmount=${#AMOUNT}
|
||||
lengthUnit=${#UNIT}
|
||||
|
||||
if [ $lengthAmount -gt 0 ]
|
||||
then
|
||||
if [ $lengthUnit -gt 0 ]
|
||||
then
|
||||
java -jar /home/pi/hc_scripts/ShoppingList.jar --action removeFromList --shoppingAmount $AMOUNT --shoppingUnit $UNIT --shoppingProduct $SHOPPINGITEM
|
||||
else
|
||||
java -jar /home/pi/hc_scripts/ShoppingList.jar --action removeFromList --shoppingAmount $AMOUNT --shoppingProduct $SHOPPINGITEM
|
||||
fi
|
||||
else
|
||||
java -jar /home/pi/hc_scripts/ShoppingList.jar --action removeFromList --shoppingProduct $SHOPPINGITEM
|
||||
fi
|
||||
|
||||
if [ "$?" -eq "0" ]
|
||||
then
|
||||
exit 0
|
||||
else
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
5
slotPrograms/shoppingProduct
Normal file
5
slotPrograms/shoppingProduct
Normal file
@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Adjust the URL to match where your php interface is.
|
||||
|
||||
curl http://yourWebserver/rhasspy/shoppingList.php?command=printProductList
|
5
slotPrograms/shoppingUnit
Normal file
5
slotPrograms/shoppingUnit
Normal file
@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Adjust the URL to match where your php interface is.
|
||||
|
||||
curl http://yourWebserver/rhasspy/shoppingList.php?command=printUnitList
|
Loading…
Reference in New Issue
Block a user