Search in sources :

Example 11 with Ingredient

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) {
    }
}
Also used : SQLDatabaseConnection(SQLDatabase.SQLDatabaseConnection) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) Ingredient(BasicCommonClasses.Ingredient) IngredientStillUsed(SQLDatabase.SQLDatabaseException.IngredientStillUsed) IngredientNotExist(SQLDatabase.SQLDatabaseException.IngredientNotExist) Test(org.junit.Test)

Example 12 with Ingredient

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();
    }
}
Also used : SmartCode(BasicCommonClasses.SmartCode) ProductNotExistInCatalog(SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog) SQLDatabaseConnection(SQLDatabase.SQLDatabaseConnection) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) CatalogProduct(BasicCommonClasses.CatalogProduct) Gson(com.google.gson.Gson) Ingredient(BasicCommonClasses.Ingredient) Manufacturer(BasicCommonClasses.Manufacturer) SQLDatabaseException(SQLDatabase.SQLDatabaseException) HashSet(java.util.HashSet) Location(BasicCommonClasses.Location) Test(org.junit.Test)

Example 13 with Ingredient

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();
    }
}
Also used : Ingredient(BasicCommonClasses.Ingredient) SQLException(java.sql.SQLException) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) CustomerProfile(BasicCommonClasses.CustomerProfile) LocalDate(java.time.LocalDate)

Example 14 with Ingredient

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;
}
Also used : Ingredient(BasicCommonClasses.Ingredient)

Example 15 with Ingredient

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);
}
Also used : InsertQuery(com.healthmarketscience.sqlbuilder.InsertQuery) Ingredient(BasicCommonClasses.Ingredient) PreparedStatement(java.sql.PreparedStatement) Location(BasicCommonClasses.Location)

Aggregations

Ingredient (BasicCommonClasses.Ingredient)27 CriticalError (SQLDatabase.SQLDatabaseException.CriticalError)14 ClientNotConnected (SQLDatabase.SQLDatabaseException.ClientNotConnected)11 Test (org.junit.Test)9 SQLDatabaseConnection (SQLDatabase.SQLDatabaseConnection)8 Location (BasicCommonClasses.Location)6 Manufacturer (BasicCommonClasses.Manufacturer)6 CommandWrapper (ClientServerApi.CommandWrapper)6 IngredientNotExist (SQLDatabase.SQLDatabaseException.IngredientNotExist)6 Gson (com.google.gson.Gson)6 CatalogProduct (BasicCommonClasses.CatalogProduct)5 CriticalError (SMExceptions.CommonExceptions.CriticalError)5 HashSet (java.util.HashSet)5 EmployeeNotConnected (EmployeeDefs.AEmployeeException.EmployeeNotConnected)4 IngredientStillUsed (SQLDatabase.SQLDatabaseException.IngredientStillUsed)4 SQLException (java.sql.SQLException)4 ConnectionFailure (EmployeeDefs.AEmployeeException.ConnectionFailure)3 InvalidParameter (EmployeeDefs.AEmployeeException.InvalidParameter)3 ParamIDAlreadyExists (EmployeeDefs.AEmployeeException.ParamIDAlreadyExists)3 ParamIDDoesNotExist (EmployeeDefs.AEmployeeException.ParamIDDoesNotExist)3