Search in sources :

Example 11 with SQLDatabaseException

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

the class SQLDatabaseConnection method setSecurityQACustomer.

@Override
public void setSecurityQACustomer(String username, ForgotPasswordData d) throws CriticalError, ClientNotExist {
    log.debug("SQL Public setSecurityQACustomer: Customer: " + username + " sets security Q&A.");
    if (!isCustomerExist(username)) {
        log.debug("SQL Public setSecurityQACustomer: no such customer with username: " + username);
        throw new ClientNotExist();
    }
    try {
        // START transaction
        connectionStartTransaction();
        //Write part of transaction
        //updating security question and answer
        assignSecurityQAToRegisteredClient(new CustomersTable(), username, d);
        log.debug("SQL Public setSecurityQACustomer: Success setting ecurity Q&A for username: " + username);
        // END transaction
        connectionCommitTransaction();
    } catch (SQLDatabaseException e) {
        connectionRollbackTransaction();
        throw e;
    } finally {
        connectionEndTransaction();
    }
}
Also used : CustomersTable(SQLDatabase.SQLDatabaseEntities.CustomersTable) SQLDatabaseException(SQLDatabase.SQLDatabaseException)

Example 12 with SQLDatabaseException

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

the class SQLDatabaseConnection method setPasswordCustomer.

@Override
public void setPasswordCustomer(String username, String newPassword) throws CriticalError, ClientNotExist {
    log.debug("SQL Public setPasswordCustomer: Customer: " + username + " sets password.");
    if (!isCustomerExist(username)) {
        log.debug("SQL Public setPasswordCustomer: no such customer with username: " + username);
        throw new ClientNotExist();
    }
    try {
        // START transaction
        connectionStartTransaction();
        //Write part of transaction
        //updating password
        assignPasswordToRegisteredClient(new CustomersTable(), username, newPassword);
        log.debug("SQL Public setPasswordCustomer: Success setting password for username: " + username);
        // END transaction
        connectionCommitTransaction();
    } catch (SQLDatabaseException e) {
        connectionRollbackTransaction();
        throw e;
    } finally {
        connectionEndTransaction();
    }
}
Also used : CustomersTable(SQLDatabase.SQLDatabaseEntities.CustomersTable) SQLDatabaseException(SQLDatabase.SQLDatabaseException)

Example 13 with SQLDatabaseException

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

the class SQLDatabaseConnection method removeWorker.

@Override
public void removeWorker(Integer sessionID, String username) throws CriticalError, ClientNotExist, ClientNotConnected {
    log.debug("SQL Public removeWorker: Remove worker with username: " + username);
    validateSessionEstablished(sessionID);
    if (!isWorkerExist(username)) {
        log.debug("SQL Public removeWorker: no such worker with username: " + username);
        throw new ClientNotExist();
    }
    //validate not deleting the admin
    if (getWorkerTypeByUsername(username) == CLIENT_TYPE.MANAGER) {
        log.debug("SQL Public removeWorker: cant remove the manager!");
        throw new CriticalError();
    }
    try {
        // START transaction
        connectionStartTransaction();
        //Read part of transaction
        Integer workerSessionID = getSessionByUsernameOfRegisteredClient(new WorkersTable(), username);
        //Write part of transaction
        if (workerSessionID != null) {
            log.debug("SQL Public removeWorker: user " + username + " connected! doing logout first");
            logoutAsWorker(workerSessionID, username);
        }
        log.debug("SQL Public removeWorker: remove user " + username + " from workers table");
        removeRegisteredClient(new WorkersTable(), username);
        log.debug("SQL Public removeWorker: Success removing username: " + username);
        // END transaction
        connectionCommitTransaction();
    } catch (SQLDatabaseException e) {
        log.debug("SQL Public removeCustomer: known error occured:" + e.getMessage());
        connectionRollbackTransaction();
        throw e;
    } catch (SQLException e) {
        log.fatal("SQL Public removeCustomer: SQL error occured:" + e.getMessage());
        connectionRollbackTransaction();
        throw new CriticalError();
    } finally {
        connectionEndTransaction();
    }
}
Also used : SQLException(java.sql.SQLException) WorkersTable(SQLDatabase.SQLDatabaseEntities.WorkersTable) SQLDatabaseException(SQLDatabase.SQLDatabaseException)

Example 14 with SQLDatabaseException

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

the class SQLDatabaseConnection method getCustomerProfile.

@Override
public String getCustomerProfile(String username) throws CriticalError, ClientNotExist {
    log.debug("SQL Public getCustomerProfile: Customer get profile: for username: " + username);
    //case of guest login
    boolean isGuest = ClientServerDefs.anonymousCustomerUsername.equals(username);
    if (isGuest)
        return Serialization.serialize(new CustomerProfile(username));
    //case of registered client
    if (!isCustomerExist(username)) {
        log.debug("SQL Public getCustomerProfile: no such customer with username: " + username);
        throw new ClientNotExist();
    }
    PreparedStatement selectCustomerStatement = null, selectCustomerIngredientsStatement = null;
    ResultSet selectCustomerResult = null, selectCustomerIngredientsResult = null;
    try {
        String selectCustomerQuery = generateSelectQuery1Table(CustomersTable.customertable, BinaryCondition.equalTo(CustomersTable.customerusernameCol, PARAM_MARK));
        String selectCustomerIngredientsQuery = generateSelectInnerJoinWithQuery2Tables(CustomersIngredientsTable.table, IngredientsTable.table, CustomersIngredientsTable.ingredientIDCol, CustomersIngredientsTable.customerUsernameCol, BinaryCondition.equalTo(CustomersIngredientsTable.customerUsernameCol, PARAM_MARK));
        selectCustomerStatement = getParameterizedQuery(selectCustomerQuery + "", username);
        selectCustomerIngredientsStatement = getParameterizedQuery(selectCustomerIngredientsQuery + "", username);
        selectCustomerResult = selectCustomerStatement.executeQuery();
        selectCustomerResult.first();
        selectCustomerIngredientsResult = selectCustomerIngredientsStatement.executeQuery();
        String result = SQLJsonGenerator.CostumerProfileToJson(selectCustomerResult, selectCustomerIngredientsResult);
        log.debug("SQL Public setCustomerProfile: Success getting profile for username: " + username);
        return result;
    } catch (SQLDatabaseException e) {
        throw e;
    } catch (SQLException e) {
        throw new CriticalError();
    } finally {
        closeResources(selectCustomerStatement, selectCustomerIngredientsStatement, selectCustomerResult, selectCustomerIngredientsResult);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CustomerProfile(BasicCommonClasses.CustomerProfile) SQLDatabaseException(SQLDatabase.SQLDatabaseException)

Example 15 with SQLDatabaseException

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

the class SQLDatabaseConnection method setPasswordWorker.

@Override
public void setPasswordWorker(String username, String newPassword) throws CriticalError, ClientNotExist {
    log.debug("SQL Public setPasswordWorker: Worker: " + username + " sets password.");
    if (!isWorkerExist(username)) {
        log.debug("SQL Public setPasswordWorker: no such worker with username: " + username);
        throw new ClientNotExist();
    }
    try {
        // START transaction
        connectionStartTransaction();
        //Write part of transaction
        //updating password
        assignPasswordToRegisteredClient(new WorkersTable(), username, newPassword);
        log.debug("SQL Public setPasswordWorker: Success setting password for username: " + username);
        // END transaction
        connectionCommitTransaction();
    } catch (SQLDatabaseException e) {
        connectionRollbackTransaction();
        throw e;
    } finally {
        connectionEndTransaction();
    }
}
Also used : WorkersTable(SQLDatabase.SQLDatabaseEntities.WorkersTable) 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