use of BasicCommonClasses.Ingredient in project SmartCity-Market by TechnionYP5777.
the class SQLDatabaseConnectionTest method testCantRemoveNotExistedIngredient.
@Test
public void testCantRemoveNotExistedIngredient() {
SQLDatabaseConnection sqlConnection = new SQLDatabaseConnection();
final String ingredientName = "glotendebug";
Ingredient ingredient = new Ingredient(999, ingredientName);
try {
sqlConnection.removeIngredient(null, ingredient);
fail();
} catch (CriticalError | ClientNotConnected | IngredientStillUsed e) {
fail();
} catch (IngredientNotExist e) {
}
}
use of BasicCommonClasses.Ingredient in project SmartCity-Market by TechnionYP5777.
the class SQLDatabaseConnectionTest method testSimpleAddRemoveProductFromCatalog.
@Test
public void testSimpleAddRemoveProductFromCatalog() {
SQLDatabaseConnection sqlConnection = new SQLDatabaseConnection();
HashSet<Ingredient> ingredients = new HashSet<Ingredient>();
HashSet<Location> locations = new HashSet<Location>();
CatalogProduct newProduct = new CatalogProduct(123L, "name", ingredients, new Manufacturer(1, "תנובה"), "", 20, "", locations);
try {
sqlConnection.addProductToCatalog(null, newProduct);
assertEquals(sqlConnection.getProductFromCatalog(null, newProduct.getBarcode()), new Gson().toJson(newProduct));
sqlConnection.removeProductFromCatalog(null, new SmartCode(newProduct.getBarcode(), null));
} catch (SQLDatabaseException e) {
fail();
}
try {
sqlConnection.getProductFromCatalog(null, newProduct.getBarcode());
fail();
} catch (ProductNotExistInCatalog e) {
} catch (CriticalError | ClientNotConnected e) {
fail();
}
}
use of BasicCommonClasses.Ingredient in project SmartCity-Market by TechnionYP5777.
the class SQLJsonGenerator method CostumerProfileToJson.
/**
* convert customerProfile from ResultSet to Json representation of product
*
* @param customer
* - ResultSet of the customer profile. the
* ResultSet need to point to the username to convert). this
* object will point the next product after returning.
* @param customerIngredients
* - ResultSet of the customer\s ingredients (assuming the
* ResultSet ordered by username column) the ResultSet should
* pointing the product to convert, if it has ingredients. if so,
* this object will point the next customer (or after last line) after returning.
* @return
* @throws CriticalError
*/
static String CostumerProfileToJson(ResultSet customer, ResultSet customerIngredients) throws CriticalError {
HashSet<Ingredient> ingredients;
try {
//get customer username
String customerUsername = getStringFromResultset(customer, CustomersTable.customerusernameCol);
// get all customer ingredients
ingredients = createIngredientsListForCustomer(customerUsername, customerIngredients);
String customeraddress = getStringFromResultset(customer, CustomersTable.customerAddressCol), customerCity = getStringFromResultset(customer, CustomersTable.customerCityCol), customerEmail = getStringFromResultset(customer, CustomersTable.customerEmailCol), customerFirstname = getStringFromResultset(customer, CustomersTable.customerFirstnameCol), customerLastname = getStringFromResultset(customer, CustomersTable.customerLastnameCol), customerPhonenumber = getStringFromResultset(customer, CustomersTable.customerPhonenumberCol);
LocalDate customerBirthdate = customer.getDate(CustomersTable.customerBirthdateCol.getColumnNameSQL()).toLocalDate();
customer.next();
return Serialization.serialize(new CustomerProfile(customerUsername, null, customerFirstname, customerLastname, customerPhonenumber, customerEmail, customerCity, customeraddress, customerBirthdate, ingredients, null));
} catch (SQLException e) {
throw new SQLDatabaseException.CriticalError();
}
}
use of BasicCommonClasses.Ingredient in project SmartCity-Market by TechnionYP5777.
the class SQLJsonGenerator method newIngredientFromResultset.
/**
* Create Ingredient from current row of an resultset
* @param ingredients the result to extract the data from. the resultset must contain the column: ingredientIDCol, ingredientNameCol
* @return new ingredient. on error - return null
* @throws SQLException
* @throws CriticalError
*/
private static Ingredient newIngredientFromResultset(ResultSet ingredients) throws SQLException, CriticalError {
Ingredient result = null;
// extracting the ingredient
int ingredientId = ingredients.getInt(IngredientsTable.ingredientIDCol.getColumnNameSQL());
if (!ingredients.wasNull()) {
String ingdientName = getStringFromResultset(ingredients, IngredientsTable.ingredientNameCol);
// adding the ingredient to set
result = new Ingredient(ingredientId, ingdientName);
}
return result;
}
use of BasicCommonClasses.Ingredient in project SmartCity-Market by TechnionYP5777.
the class SQLDatabaseConnection method addCatalogProduct.
/**
* Add product to the SQL database
*
* @param p
* New product to add
* @throws CriticalError
* @throws SQLException
*/
private void addCatalogProduct(CatalogProduct p) throws CriticalError, SQLException {
// add all ingredients of product
for (Ingredient ¢ : p.getIngredients()) {
String insertToProductQuery = new InsertQuery(ProductsCatalogIngredientsTable.table).addColumn(ProductsCatalogIngredientsTable.barcodeCol, PARAM_MARK).addColumn(ProductsCatalogIngredientsTable.ingredientIDCol, PARAM_MARK).validate() + "";
insertToProductQuery.hashCode();
PreparedStatement statement = getParameterizedQuery(insertToProductQuery, p.getBarcode(), ¢.getId());
statement.executeUpdate();
closeResources(statement);
}
// add all locations of product
for (Location ¢ : p.getLocations()) {
int newID = allocateIDToTable(LocationsTable.table, LocationsTable.locationIDCol);
String insertLocationQuery = new InsertQuery(LocationsTable.table).addColumn(LocationsTable.locationIDCol, PARAM_MARK).addColumn(LocationsTable.placeInStoreCol, PARAM_MARK).addColumn(LocationsTable.pointXCol, PARAM_MARK).addColumn(LocationsTable.pointYCol, PARAM_MARK).validate() + "";
insertLocationQuery.hashCode();
PreparedStatement insertLocationStatement = getParameterizedQuery(insertLocationQuery, newID, ¢.getPlaceInMarket().equals(PlaceInMarket.STORE) ? LOCATIONS_TABLE.VALUE_PLACE_STORE : LOCATIONS_TABLE.VALUE_PLACE_WAREHOUSE, ¢.getX(), ¢.getY());
String insertToProductQuery = new InsertQuery(ProductsCatalogLocationsTable.table).addColumn(ProductsCatalogLocationsTable.barcodeCol, PARAM_MARK).addColumn(ProductsCatalogLocationsTable.locationIDCol, PARAM_MARK).validate() + "";
PreparedStatement statement = getParameterizedQuery(insertToProductQuery, p.getBarcode(), newID);
insertLocationStatement.executeUpdate();
statement.executeUpdate();
closeResources(insertLocationStatement);
closeResources(statement);
}
// add the product itself
String insertQuery = new InsertQuery(ProductsCatalogTable.table).addColumn(ProductsCatalogTable.barcodeCol, PARAM_MARK).addColumn(ProductsCatalogTable.manufacturerIDCol, PARAM_MARK).addColumn(ProductsCatalogTable.productDescriptionCol, PARAM_MARK).addColumn(ProductsCatalogTable.productNameCol, PARAM_MARK).addColumn(ProductsCatalogTable.productPictureCol, PARAM_MARK).addColumn(ProductsCatalogTable.productPriceCol, PARAM_MARK).validate() + "";
PreparedStatement statement = getParameterizedQuery(insertQuery, p.getBarcode(), p.getManufacturer().getId(), p.getDescription(), p.getName(), p.getImageUrl(), p.getPrice());
statement.executeUpdate();
closeResources(statement);
}
Aggregations