Search in sources :

Example 1 with GroceryList

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

the class Customer method checkOutGroceryList.

@Override
public Double checkOutGroceryList() throws CriticalError, CustomerNotConnected, GroceryListIsEmpty {
    String serverResponse;
    Double $ = totalSum;
    log.info("Creating CHECKOUT_GROCERY_LIST command wrapper to customer with id: " + id);
    establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
    try {
        serverResponse = sendRequestWithRespondToServer((new CommandWrapper(id, CommandDescriptor.CHECKOUT_GROCERY_LIST)).serialize());
    } catch (SocketTimeoutException e) {
        log.fatal("Critical bug: failed to get respond from server");
        throw new CriticalError();
    }
    terminateCommunication();
    try {
        resultDescriptorHandler(getCommandWrapper(serverResponse).getResultDescriptor());
    } catch (InvalidCommandDescriptor | InvalidParameter | AmountBiggerThanAvailable | ProductPackageDoesNotExist | AuthenticationError | ProductCatalogDoesNotExist | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
        log.fatal("Critical bug: this command result isn't supposed to return here");
        throw new CriticalError();
    }
    /* update customer data: groceryList, cartProductCache, totalSum */
    groceryList = new GroceryList();
    cartProductCache = new HashMap<Long, CartProduct>();
    totalSum = Double.valueOf(0);
    totalProductsAmount = 0;
    log.info("CHECKOUT_GROCERY_LIST command succeed.");
    return $;
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) CriticalError(SMExceptions.CommonExceptions.CriticalError) GroceryList(BasicCommonClasses.GroceryList) CommandWrapper(ClientServerApi.CommandWrapper) InvalidParameter(CustomerContracts.ACustomerExceptions.InvalidParameter) SocketTimeoutException(java.net.SocketTimeoutException) AmountBiggerThanAvailable(CustomerContracts.ACustomerExceptions.AmountBiggerThanAvailable) InvalidCommandDescriptor(CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor) ForgotPasswordWrongAnswer(CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer) CartProduct(BasicCommonClasses.CartProduct) ProductPackageDoesNotExist(CustomerContracts.ACustomerExceptions.ProductPackageDoesNotExist) UsernameAlreadyExists(CustomerContracts.ACustomerExceptions.UsernameAlreadyExists) ProductCatalogDoesNotExist(CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)

Example 2 with GroceryList

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

the class SQLDatabaseConnection method logoutAsCart.

/**
	 * logout cart from system. (remove cartID entry and it grocery list)
	 * 
	 * @param cartID
	 * @throws CriticalError
	 */
private void logoutAsCart(int cartID) throws CriticalError {
    // READ part of transaction
    int listID = getCartListId(cartID);
    PreparedStatement deleteGroceryList = null, deleteCart = null;
    try {
        // WRITE part of transaction
        // get grocery list and returns each product to shelf
        ResultSet groceryListResultSet = getGroceryListResultSetByCartID(cartID);
        groceryListResultSet.first();
        GroceryList groceryList = SQLJsonGenerator.resultSetToGroceryList(groceryListResultSet);
        if (groceryList.getList() != null)
            for (ProductPackage p : groceryList.getList().values()) moveProductPackage(cartID, LOCATIONS_TYPES.CART, LOCATIONS_TYPES.STORE, p, p.getAmount());
        // delete grocery list and cart
        deleteGroceryList = getParameterizedQuery(generateDeleteQuery(GroceriesListsTable.table, BinaryCondition.equalTo(GroceriesListsTable.listIDCol, PARAM_MARK)), listID);
        deleteCart = getParameterizedQuery(generateDeleteQuery(CartsListTable.table, BinaryCondition.equalTo(CartsListTable.listIDCol, PARAM_MARK)), listID);
        log.debug("logoutAsCart: delete groceryList " + listID + ".\n by run query: " + deleteGroceryList);
        deleteGroceryList.executeUpdate();
        log.debug("logoutAsCart: disconnect cart " + cartID + ".\n by run query: " + deleteCart);
        deleteCart.executeUpdate();
    } catch (SQLException e) {
        throw new CriticalError();
    } catch (ProductPackageAmountNotMatch e) {
        log.error("logoutAsCart: trying to return product to shelf but amount not matched to what in the grocery list");
        throw new CriticalError();
    } catch (ProductPackageNotExist e) {
        log.error("logoutAsCart: trying to return product to shelf but product package not exist in grocery list");
        throw new CriticalError();
    } finally {
        closeResources(deleteGroceryList, deleteCart);
    }
}
Also used : ProductPackage(BasicCommonClasses.ProductPackage) SQLException(java.sql.SQLException) GroceryList(BasicCommonClasses.GroceryList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 3 with GroceryList

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

the class Customer method resume.

@Override
public void resume(int _id) throws CriticalError, CustomerNotConnected {
    CommandWrapper $ = null;
    String serverResponse;
    log.info("Creating customer Load grocery list command wrapper with id: " + id);
    establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
    try {
        serverResponse = sendRequestWithRespondToServer((new CommandWrapper(id, CommandDescriptor.LOAD_GROCERY_LIST)).serialize());
    } catch (SocketTimeoutException e) {
        log.fatal("Critical bug: failed to get respond from server");
        throw new CriticalError();
    }
    terminateCommunication();
    try {
        $ = getCommandWrapper(serverResponse);
        resultDescriptorHandler($.getResultDescriptor());
        id = _id;
        groceryList = Serialization.deserialize($.getData(), GroceryList.class);
    } catch (InvalidCommandDescriptor | InvalidParameter | ProductCatalogDoesNotExist | AmountBiggerThanAvailable | ProductPackageDoesNotExist | GroceryListIsEmpty | AuthenticationError | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
        log.fatal("Critical bug: this command result isn't supposed to return here");
        throw new CriticalError();
    }
    /* Restoring Grocery list */
    try {
        loadCartProductCacheAndUpdateCartData();
    } catch (ProductCatalogDoesNotExist | CriticalError | CustomerNotConnected e) {
        log.fatal("Critical bug: Failed to fetch grocery list items from server");
    }
    log.info("load grocery list from server succeed.");
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) CriticalError(SMExceptions.CommonExceptions.CriticalError) GroceryList(BasicCommonClasses.GroceryList) CustomerNotConnected(CustomerContracts.ACustomerExceptions.CustomerNotConnected) CommandWrapper(ClientServerApi.CommandWrapper) GroceryListIsEmpty(CustomerContracts.ACustomerExceptions.GroceryListIsEmpty) InvalidParameter(CustomerContracts.ACustomerExceptions.InvalidParameter) SocketTimeoutException(java.net.SocketTimeoutException) AmountBiggerThanAvailable(CustomerContracts.ACustomerExceptions.AmountBiggerThanAvailable) InvalidCommandDescriptor(CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor) ForgotPasswordWrongAnswer(CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer) ProductPackageDoesNotExist(CustomerContracts.ACustomerExceptions.ProductPackageDoesNotExist) UsernameAlreadyExists(CustomerContracts.ACustomerExceptions.UsernameAlreadyExists) ProductCatalogDoesNotExist(CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)

Aggregations

GroceryList (BasicCommonClasses.GroceryList)3 CommandWrapper (ClientServerApi.CommandWrapper)2 AmountBiggerThanAvailable (CustomerContracts.ACustomerExceptions.AmountBiggerThanAvailable)2 AuthenticationError (CustomerContracts.ACustomerExceptions.AuthenticationError)2 ForgotPasswordWrongAnswer (CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer)2 InvalidCommandDescriptor (CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor)2 InvalidParameter (CustomerContracts.ACustomerExceptions.InvalidParameter)2 ProductCatalogDoesNotExist (CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)2 ProductPackageDoesNotExist (CustomerContracts.ACustomerExceptions.ProductPackageDoesNotExist)2 UsernameAlreadyExists (CustomerContracts.ACustomerExceptions.UsernameAlreadyExists)2 CriticalError (SMExceptions.CommonExceptions.CriticalError)2 SocketTimeoutException (java.net.SocketTimeoutException)2 CartProduct (BasicCommonClasses.CartProduct)1 ProductPackage (BasicCommonClasses.ProductPackage)1 CustomerNotConnected (CustomerContracts.ACustomerExceptions.CustomerNotConnected)1 GroceryListIsEmpty (CustomerContracts.ACustomerExceptions.GroceryListIsEmpty)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1