Search in sources :

Example 16 with CatalogProduct

use of BasicCommonClasses.CatalogProduct in project SmartCity-Market by TechnionYP5777.

the class CommandExecuter method addProductToCatalogCommand.

private void addProductToCatalogCommand(SQLDatabaseConnection c) {
    CatalogProduct catalogProduct = null;
    log.info("Add Product To Catalog command called");
    try {
        catalogProduct = Serialization.deserialize(inCommandWrapper.getData(), CatalogProduct.class);
    } catch (java.lang.RuntimeException e) {
        log.fatal("Failed to parse data for Add Product To Catalog command");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
        return;
    }
    if (!catalogProduct.isValid()) {
        log.info("Add Product To Catalog command failed, product is invalid");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_INVALID_PARAMETER);
    } else {
        try {
            c.addProductToCatalog(inCommandWrapper.getSenderID(), catalogProduct);
            outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_OK);
        } catch (CriticalError e) {
            log.fatal("Add Product To Catalog command failed, critical error occured from SQL Database connection");
            outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
        } catch (ClientNotConnected e) {
            log.info("Add Product To Catalog command failed, username dosen't login to the system");
            outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_SENDER_IS_NOT_CONNECTED);
        } catch (ProductAlreadyExistInCatalog e) {
            log.info("Add Product To Catalog command failed, product already exist in the system");
            outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_CATALOG_PRODUCT_ALREADY_EXISTS);
        } catch (IngredientNotExist e) {
            log.info("Add Product To Catalog command failed, ingredient in the product dosen't exist in the system");
            outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_INVALID_PARAMETER);
        } catch (ManufacturerNotExist e) {
            log.info("Add Product To Catalog command failed, manufacturer in the product dosen't exist in the system");
            outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_INVALID_PARAMETER);
        }
        log.info("Add Product To Catalog with product " + catalogProduct + " finished");
    }
}
Also used : ManufacturerNotExist(SQLDatabase.SQLDatabaseException.ManufacturerNotExist) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) CatalogProduct(BasicCommonClasses.CatalogProduct) CommandWrapper(ClientServerApi.CommandWrapper) IngredientNotExist(SQLDatabase.SQLDatabaseException.IngredientNotExist) ProductAlreadyExistInCatalog(SQLDatabase.SQLDatabaseException.ProductAlreadyExistInCatalog)

Example 17 with CatalogProduct

use of BasicCommonClasses.CatalogProduct in project SmartCity-Market by TechnionYP5777.

the class EditProductFromCatalogTest method editCatalogProductNotExistInCatalogTest.

@Test
public void editCatalogProductNotExistInCatalogTest() {
    int senderID = 1;
    CatalogProduct catalogProduct = new CatalogProduct(0, "Shoko", null, null, null, 4, null, null);
    String command = new CommandWrapper(senderID, CommandDescriptor.EDIT_PRODUCT_FROM_CATALOG, new Gson().toJson(catalogProduct, CatalogProduct.class)).serialize();
    CommandExecuter commandExecuter = new CommandExecuter(command);
    CommandWrapper out;
    try {
        Mockito.doThrow(new ProductNotExistInCatalog()).when(sqlDatabaseConnection).editProductInCatalog(senderID, catalogProduct);
    } catch (CriticalError | ClientNotConnected | IngredientNotExist | ManufacturerNotExist e1) {
        fail();
    } catch (ProductNotExistInCatalog __) {
    /* Success */
    }
    out = commandExecuter.execute(sqlDatabaseConnection);
    assertEquals(ResultDescriptor.SM_CATALOG_PRODUCT_DOES_NOT_EXIST, out.getResultDescriptor());
}
Also used : ProductNotExistInCatalog(SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog) ManufacturerNotExist(SQLDatabase.SQLDatabaseException.ManufacturerNotExist) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) CatalogProduct(BasicCommonClasses.CatalogProduct) Gson(com.google.gson.Gson) CommandWrapper(ClientServerApi.CommandWrapper) IngredientNotExist(SQLDatabase.SQLDatabaseException.IngredientNotExist) CommandExecuter(CommandHandler.CommandExecuter) Test(org.junit.Test)

Example 18 with CatalogProduct

use of BasicCommonClasses.CatalogProduct in project SmartCity-Market by TechnionYP5777.

the class SQLJsonGenerator method ProductToJson.

/**
	 * convert product from ResultSet to Json representation of product
	 * 
	 * @param product
	 *            - ResultSet of the product\s joined with manufactures table
	 *            (assuming the ResultSet ordered by product barcode) the
	 *            ResultSet need to point to the product to convert). this
	 *            object will point the next product after returning.
	 * @param productIngredients
	 *            - ResultSet of the product\s ingredients (assuming the
	 *            ResultSet ordered by product barcode) the ResultSet should
	 *            pointing the product to convert, if it has ingredients. if so,
	 *            this object will point the next product after returning.
	 * @param productLocations
	 *            - ResultSet of the product\s locations (assuming the ResultSet
	 *            ordered by product barcode) the ResultSet should pointing the
	 *            product to convert, if it has ingredients. if so, this object
	 *            will point the next product after returning.
	 * @return
	 * @throws CriticalError
	 */
static String ProductToJson(ResultSet product, ResultSet productIngredients, ResultSet productLocations) throws CriticalError {
    HashSet<Location> locations;
    HashSet<Ingredient> ingredients;
    try {
        long productBarcode = product.getLong(ProductsCatalogTable.barcodeCol.getColumnNameSQL());
        // adding all ingredients
        ingredients = createIngredientsListByBarcode(productBarcode, productIngredients);
        // adding all locations
        locations = createLocationsList(productBarcode, productLocations);
        // get product other details
        String productManufacturerName = getStringFromResultset(product, ManufacturerTable.manufacturerNameCol);
        int productManufacturerID = product.getInt(ManufacturerTable.manufacturerIDCol.getColumnNameSQL());
        String productDescription = getStringFromResultset(product, ProductsCatalogTable.productDescriptionCol), productName = getStringFromResultset(product, ProductsCatalogTable.productNameCol), productPicture = getStringFromResultset(product, ProductsCatalogTable.productPictureCol);
        double productPrice = product.getDouble(ProductsCatalogTable.productPriceCol.getColumnNameSQL());
        product.next();
        return Serialization.serialize(new CatalogProduct(productBarcode, productName, ingredients, new Manufacturer(productManufacturerID, productManufacturerName), productDescription, productPrice, productPicture, locations));
    } catch (SQLException e) {
        throw new SQLDatabaseException.CriticalError();
    }
}
Also used : SQLException(java.sql.SQLException) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) CatalogProduct(BasicCommonClasses.CatalogProduct) Ingredient(BasicCommonClasses.Ingredient) Manufacturer(BasicCommonClasses.Manufacturer) Location(BasicCommonClasses.Location)

Example 19 with CatalogProduct

use of BasicCommonClasses.CatalogProduct in project SmartCity-Market by TechnionYP5777.

the class AddProductToCatalogTest method addCatalogProductCriticalErrorTest.

@Test
public void addCatalogProductCriticalErrorTest() {
    int senderID = 1;
    CatalogProduct catalogProduct = new CatalogProduct(0, "Shoko", null, null, null, 4, null, null);
    String command = new CommandWrapper(senderID, CommandDescriptor.ADD_PRODUCT_TO_CATALOG, new Gson().toJson(catalogProduct, CatalogProduct.class)).serialize();
    CommandExecuter commandExecuter = new CommandExecuter(command);
    CommandWrapper out;
    try {
        Mockito.doThrow(new CriticalError()).when(sqlDatabaseConnection).addProductToCatalog(senderID, catalogProduct);
    } catch (ProductAlreadyExistInCatalog | IngredientNotExist | ManufacturerNotExist | ClientNotConnected e) {
        fail();
    } catch (CriticalError __) {
    /* Success */
    }
    out = commandExecuter.execute(sqlDatabaseConnection);
    assertEquals(ResultDescriptor.SM_ERR, out.getResultDescriptor());
}
Also used : ManufacturerNotExist(SQLDatabase.SQLDatabaseException.ManufacturerNotExist) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) CatalogProduct(BasicCommonClasses.CatalogProduct) Gson(com.google.gson.Gson) CommandWrapper(ClientServerApi.CommandWrapper) IngredientNotExist(SQLDatabase.SQLDatabaseException.IngredientNotExist) CommandExecuter(CommandHandler.CommandExecuter) ProductAlreadyExistInCatalog(SQLDatabase.SQLDatabaseException.ProductAlreadyExistInCatalog) Test(org.junit.Test)

Example 20 with CatalogProduct

use of BasicCommonClasses.CatalogProduct in project SmartCity-Market by TechnionYP5777.

the class AddProductToCatalogTest method addCatalogProductIngredientNotExistTest.

@Test
public void addCatalogProductIngredientNotExistTest() {
    int senderID = 1;
    CatalogProduct catalogProduct = new CatalogProduct(0, "Shoko", null, null, null, 4, null, null);
    String command = new CommandWrapper(senderID, CommandDescriptor.ADD_PRODUCT_TO_CATALOG, new Gson().toJson(catalogProduct, CatalogProduct.class)).serialize();
    CommandExecuter commandExecuter = new CommandExecuter(command);
    CommandWrapper out;
    try {
        Mockito.doThrow(new IngredientNotExist()).when(sqlDatabaseConnection).addProductToCatalog(senderID, catalogProduct);
    } catch (ProductAlreadyExistInCatalog | ManufacturerNotExist | CriticalError | ClientNotConnected e) {
        fail();
    } catch (IngredientNotExist __) {
    /* Success */
    }
    out = commandExecuter.execute(sqlDatabaseConnection);
    assertEquals(ResultDescriptor.SM_INVALID_PARAMETER, out.getResultDescriptor());
}
Also used : ManufacturerNotExist(SQLDatabase.SQLDatabaseException.ManufacturerNotExist) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) CatalogProduct(BasicCommonClasses.CatalogProduct) Gson(com.google.gson.Gson) CommandWrapper(ClientServerApi.CommandWrapper) IngredientNotExist(SQLDatabase.SQLDatabaseException.IngredientNotExist) CommandExecuter(CommandHandler.CommandExecuter) ProductAlreadyExistInCatalog(SQLDatabase.SQLDatabaseException.ProductAlreadyExistInCatalog) Test(org.junit.Test)

Aggregations

CatalogProduct (BasicCommonClasses.CatalogProduct)26 CriticalError (SQLDatabase.SQLDatabaseException.CriticalError)22 ClientNotConnected (SQLDatabase.SQLDatabaseException.ClientNotConnected)21 Test (org.junit.Test)21 Gson (com.google.gson.Gson)19 CommandWrapper (ClientServerApi.CommandWrapper)18 IngredientNotExist (SQLDatabase.SQLDatabaseException.IngredientNotExist)16 ManufacturerNotExist (SQLDatabase.SQLDatabaseException.ManufacturerNotExist)16 CommandExecuter (CommandHandler.CommandExecuter)14 ProductNotExistInCatalog (SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog)13 ProductAlreadyExistInCatalog (SQLDatabase.SQLDatabaseException.ProductAlreadyExistInCatalog)8 Manufacturer (BasicCommonClasses.Manufacturer)6 Ingredient (BasicCommonClasses.Ingredient)5 Location (BasicCommonClasses.Location)5 SmartCode (BasicCommonClasses.SmartCode)5 SQLDatabaseConnection (SQLDatabase.SQLDatabaseConnection)5 HashSet (java.util.HashSet)4 SQLDatabaseException (SQLDatabase.SQLDatabaseException)3 ProductPackage (BasicCommonClasses.ProductPackage)2 CriticalError (SMExceptions.CommonExceptions.CriticalError)2