Search in sources :

Example 1 with SQLDatabaseException

use of SQLDatabase.SQLDatabaseException in project SmartCity-Market by TechnionYP5777.

the class SQLDatabaseConnection method getSecurityQuestionWorker.

@Override
public String getSecurityQuestionWorker(String username) throws CriticalError, ClientNotExist {
    log.debug("SQL Public getSecurityQuestionWorker: Worker: " + username + " get security question.");
    if (!isWorkerExist(username)) {
        log.debug("SQL Public getSecurityQuestionWorker: no such worker with username: " + username);
        throw new ClientNotExist();
    }
    try {
        //Read part of transaction
        String result = getSecurityQuestionForRegisteredClient(new WorkersTable(), username);
        log.debug("SQL Public getSecurityQuestionWorker: the security question of user: " + username + " is: \n" + result);
        return result;
    } catch (SQLDatabaseException e) {
        throw e;
    }
}
Also used : WorkersTable(SQLDatabase.SQLDatabaseEntities.WorkersTable) SQLDatabaseException(SQLDatabase.SQLDatabaseException)

Example 2 with SQLDatabaseException

use of SQLDatabase.SQLDatabaseException in project SmartCity-Market by TechnionYP5777.

the class SQLDatabaseConnectionTest method testRemoveCatalogProductStillForSell.

@Test
public void testRemoveCatalogProductStillForSell() {
    SQLDatabaseConnection sqlConnection = new SQLDatabaseConnection();
    CatalogProduct newProduct = createDummyProduct(456L, "testRemoveCatalogProductStillForSell", 1, "תנובה", 3.0);
    ProductPackage newPackage = new ProductPackage(new SmartCode(newProduct.getBarcode(), date232015), 5, locationWarehouse);
    // add catalog-product and add it to warehouse
    try {
        sqlConnection.addProductToCatalog(null, newProduct);
        assertEquals(sqlConnection.getProductFromCatalog(null, newProduct.getBarcode()), new Gson().toJson(newProduct));
        sqlConnection.addProductPackageToWarehouse(null, newPackage);
        assertEquals("5", sqlConnection.getProductPackageAmonutInWarehouse(null, newPackage));
    } catch (SQLDatabaseException e) {
        fail();
    }
    // try to remove
    try {
        sqlConnection.removeProductFromCatalog(null, new SmartCode(newProduct.getBarcode(), null));
        fail();
    } catch (ProductStillForSale e1) {
    } catch (CriticalError | ClientNotConnected | ProductNotExistInCatalog e1) {
        fail();
    }
    // move to shelf
    try {
        sqlConnection.placeProductPackageOnShelves(null, newPackage);
        assertEquals("0", sqlConnection.getProductPackageAmonutInWarehouse(null, newPackage));
        assertEquals("5", sqlConnection.getProductPackageAmonutOnShelves(null, newPackage));
    } catch (SQLDatabaseException e) {
        fail();
    }
    // try to remove
    try {
        sqlConnection.removeProductFromCatalog(null, new SmartCode(newProduct.getBarcode(), null));
        fail();
    } catch (ProductStillForSale e1) {
    } catch (CriticalError | ClientNotConnected | ProductNotExistInCatalog e1) {
        fail();
    }
    // move to shelf
    try {
        sqlConnection.removeProductPackageFromShelves(null, newPackage);
        assertEquals("0", sqlConnection.getProductPackageAmonutInWarehouse(null, newPackage));
        assertEquals("0", sqlConnection.getProductPackageAmonutOnShelves(null, newPackage));
        sqlConnection.removeProductFromCatalog(null, new SmartCode(newProduct.getBarcode(), null));
    } catch (SQLDatabaseException e) {
        fail();
    }
}
Also used : SmartCode(BasicCommonClasses.SmartCode) ProductNotExistInCatalog(SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog) SQLDatabaseConnection(SQLDatabase.SQLDatabaseConnection) ProductStillForSale(SQLDatabase.SQLDatabaseException.ProductStillForSale) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) ProductPackage(BasicCommonClasses.ProductPackage) CatalogProduct(BasicCommonClasses.CatalogProduct) Gson(com.google.gson.Gson) SQLDatabaseException(SQLDatabase.SQLDatabaseException) Test(org.junit.Test)

Example 3 with SQLDatabaseException

use of SQLDatabase.SQLDatabaseException 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 4 with SQLDatabaseException

use of SQLDatabase.SQLDatabaseException in project SmartCity-Market by TechnionYP5777.

the class SQLDatabaseConnection method getSecurityQuestionCustomer.

@Override
public String getSecurityQuestionCustomer(String username) throws CriticalError, ClientNotExist {
    log.debug("SQL Public getSecurityQuestionCustomer: Customer: " + username + " get security question.");
    if (!isCustomerExist(username)) {
        log.debug("SQL Public getSecurityQuestionCustomer: no such customer with username: " + username);
        throw new ClientNotExist();
    }
    try {
        //Read part of transaction
        String result = getSecurityQuestionForRegisteredClient(new CustomersTable(), username);
        log.debug("SQL Public getSecurityQuestionCustomer: the security question of user: " + username + " is: \n" + result);
        return result;
    } catch (SQLDatabaseException e) {
        throw e;
    }
}
Also used : CustomersTable(SQLDatabase.SQLDatabaseEntities.CustomersTable) SQLDatabaseException(SQLDatabase.SQLDatabaseException)

Example 5 with SQLDatabaseException

use of SQLDatabase.SQLDatabaseException in project SmartCity-Market by TechnionYP5777.

the class SQLDatabaseConnection method addWorker.

@Override
public void addWorker(Integer sessionID, Login l, ForgotPasswordData security) throws CriticalError, ClientAlreadyExist, ClientNotConnected {
    log.debug("SQL Public addWorker: add new worker with username: " + l.getUserName());
    validateSessionEstablished(sessionID);
    if (isWorkerExist(l.getUserName())) {
        log.debug("SQL Public addWorker: already exist worker with username: " + l.getUserName());
        throw new ClientAlreadyExist();
    }
    PreparedStatement statement = null;
    try {
        // START transaction
        connectionStartTransaction();
        //Write part of transaction
        String insertCustomerQuery = new InsertQuery(WorkersTable.workertable).addColumn(WorkersTable.workerusernameCol, PARAM_MARK).addColumn(WorkersTable.workerpasswordCol, PARAM_MARK).addColumn(WorkersTable.workerPrivilegesCol, PARAM_MARK).addColumn(WorkersTable.workersecurityQuestionCol, PARAM_MARK).addColumn(WorkersTable.workersecurityAnswerCol, PARAM_MARK).addColumn(WorkersTable.workerisLoggedInCol, PARAM_MARK).validate() + "";
        statement = getParameterizedQuery(insertCustomerQuery, l.getUserName(), l.getPassword(), WORKERS_TABLE.VALUE_PRIVILEGE_WORKER, security.getQuestion(), security.getAnswer(), 0);
        statement.executeUpdate();
        log.debug("SQL Public addWorker: worker " + l.getUserName() + "added successfuly");
        // END transaction
        connectionCommitTransaction();
    } catch (SQLDatabaseException e) {
        // NOTE: all exceptions flows here - for doing rollback
        connectionRollbackTransaction();
        throw e;
    } catch (SQLException e) {
        connectionRollbackTransaction();
        throw new CriticalError();
    } finally {
        connectionEndTransaction();
        closeResources(statement);
    }
}
Also used : InsertQuery(com.healthmarketscience.sqlbuilder.InsertQuery) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) SQLDatabaseException(SQLDatabase.SQLDatabaseException)

Aggregations

SQLDatabaseException (SQLDatabase.SQLDatabaseException)17 SQLException (java.sql.SQLException)6 CustomersTable (SQLDatabase.SQLDatabaseEntities.CustomersTable)5 WorkersTable (SQLDatabase.SQLDatabaseEntities.WorkersTable)5 PreparedStatement (java.sql.PreparedStatement)4 CatalogProduct (BasicCommonClasses.CatalogProduct)3 SmartCode (BasicCommonClasses.SmartCode)3 SQLDatabaseConnection (SQLDatabase.SQLDatabaseConnection)3 ClientNotConnected (SQLDatabase.SQLDatabaseException.ClientNotConnected)3 CriticalError (SQLDatabase.SQLDatabaseException.CriticalError)3 ProductNotExistInCatalog (SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog)3 Gson (com.google.gson.Gson)3 Test (org.junit.Test)3 Ingredient (BasicCommonClasses.Ingredient)2 Location (BasicCommonClasses.Location)2 Manufacturer (BasicCommonClasses.Manufacturer)2 InsertQuery (com.healthmarketscience.sqlbuilder.InsertQuery)2 HashSet (java.util.HashSet)2 CustomerProfile (BasicCommonClasses.CustomerProfile)1 ProductPackage (BasicCommonClasses.ProductPackage)1