Search in sources :

Example 1 with GroceryListIsEmpty

use of CustomerContracts.ACustomerExceptions.GroceryListIsEmpty in project SmartCity-Market by TechnionYP5777.

the class Customer method addProductToCart.

@Override
public void addProductToCart(SmartCode c, int amount) throws CriticalError, CustomerNotConnected, AmountBiggerThanAvailable, ProductPackageDoesNotExist, InvalidParameter {
    String serverResponse;
    log.info("Creating viewProductFromCatalog (in order to addPtoductToCart) command wrapper to customer with id: " + id);
    CatalogProduct catalogProduct;
    try {
        catalogProduct = viewCatalogProduct(c);
    } catch (ProductCatalogDoesNotExist e1) {
        log.fatal("Critical bug: failed to get catalog product from server");
        throw new CriticalError();
    }
    establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
    try {
        serverResponse = sendRequestWithRespondToServer(new CommandWrapper(id, CommandDescriptor.ADD_PRODUCT_TO_GROCERY_LIST, Serialization.serialize(new ProductPackage(c, amount, null))).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 | ProductCatalogDoesNotExist | GroceryListIsEmpty | AuthenticationError | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
        log.fatal("Critical bug: this command result isn't supposed to return here");
        throw new CriticalError();
    }
    ProductPackage productPackage1 = new ProductPackage(c, amount, null), productPackage2 = new ProductPackage(c, amount, null);
    groceryList.addProduct(productPackage1);
    addProductToCacheAndUpdateCartData(productPackage2, catalogProduct);
    log.info("addProductToGroceryList command succeed.");
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) CriticalError(SMExceptions.CommonExceptions.CriticalError) ProductPackage(BasicCommonClasses.ProductPackage) CatalogProduct(BasicCommonClasses.CatalogProduct) CommandWrapper(ClientServerApi.CommandWrapper) GroceryListIsEmpty(CustomerContracts.ACustomerExceptions.GroceryListIsEmpty) SocketTimeoutException(java.net.SocketTimeoutException) InvalidCommandDescriptor(CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor) ForgotPasswordWrongAnswer(CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer) UsernameAlreadyExists(CustomerContracts.ACustomerExceptions.UsernameAlreadyExists) ProductCatalogDoesNotExist(CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)

Example 2 with GroceryListIsEmpty

use of CustomerContracts.ACustomerExceptions.GroceryListIsEmpty in project SmartCity-Market by TechnionYP5777.

the class Customer method login.

@Override
public void login(String username, String password, boolean updateProductPictures) throws CriticalError, AuthenticationError {
    CommandWrapper cmdwrppr = null;
    String serverResponse = null;
    log.info("Creating login command wrapper for customer");
    establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
    try {
        serverResponse = sendRequestWithRespondToServer((new CommandWrapper(CustomerDefs.loginCommandSenderId, CommandDescriptor.LOGIN_CUSTOMER, Serialization.serialize(new Login(username, password)))).serialize());
    } catch (SocketTimeoutException e) {
        log.fatal("Critical bug: failed to get respond from server");
        throw new CriticalError();
    }
    terminateCommunication();
    cmdwrppr = getCommandWrapper(serverResponse);
    try {
        resultDescriptorHandler(cmdwrppr.getResultDescriptor());
    } catch (InvalidCommandDescriptor | InvalidParameter | CustomerNotConnected | ProductCatalogDoesNotExist | AmountBiggerThanAvailable | ProductPackageDoesNotExist | GroceryListIsEmpty | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
        log.fatal("Critical bug: this command result isn't supposed to return here");
        throw new CriticalError();
    }
    id = cmdwrppr.getSenderID();
    if (this instanceof RegisteredCustomer)
        customerProfile = Serialization.deserialize(cmdwrppr.getData(), CustomerProfile.class);
    //check for new product pictures asynchronous
    if (updateProductPictures)
        new UpdateProductPictures().start();
    log.info("Customer Login to server as succeed. Client id is: " + id);
}
Also used : CriticalError(SMExceptions.CommonExceptions.CriticalError) CustomerNotConnected(CustomerContracts.ACustomerExceptions.CustomerNotConnected) CommandWrapper(ClientServerApi.CommandWrapper) GroceryListIsEmpty(CustomerContracts.ACustomerExceptions.GroceryListIsEmpty) Login(BasicCommonClasses.Login) 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)

Example 3 with GroceryListIsEmpty

use of CustomerContracts.ACustomerExceptions.GroceryListIsEmpty in project SmartCity-Market by TechnionYP5777.

the class Customer method viewCatalogProduct.

@Override
public CatalogProduct viewCatalogProduct(SmartCode c) throws CriticalError, CustomerNotConnected, ProductCatalogDoesNotExist {
    String serverResponse;
    log.info("Creating viewProductFromCatalog (in order to addPtoductToCart) command wrapper to customer with id: " + id);
    establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
    /* first: getting the product from the server */
    try {
        serverResponse = sendRequestWithRespondToServer(new CommandWrapper(id, CommandDescriptor.VIEW_PRODUCT_FROM_CATALOG, Serialization.serialize(c)).serialize());
    } catch (SocketTimeoutException e) {
        log.fatal("Critical bug: failed to get respond from server");
        throw new CriticalError();
    }
    terminateCommunication();
    CommandWrapper $ = getCommandWrapper(serverResponse);
    try {
        resultDescriptorHandler($.getResultDescriptor());
    } catch (InvalidCommandDescriptor | InvalidParameter | CriticalError | AmountBiggerThanAvailable | ProductPackageDoesNotExist | GroceryListIsEmpty | AuthenticationError | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
        log.fatal("Critical bug: this command result isn't supposed to return here");
        throw new CriticalError();
    }
    log.info("viewProductFromCatalog command succeed.");
    return Serialization.deserialize($.getData(), CatalogProduct.class);
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) CriticalError(SMExceptions.CommonExceptions.CriticalError) 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)

Example 4 with GroceryListIsEmpty

use of CustomerContracts.ACustomerExceptions.GroceryListIsEmpty in project SmartCity-Market by TechnionYP5777.

the class Customer method logout.

@Override
public void logout() throws CustomerNotConnected, CriticalError {
    String serverResponse;
    log.info("Creating customer logout command wrapper with id: " + id);
    establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
    try {
        serverResponse = sendRequestWithRespondToServer((new CommandWrapper(id, CommandDescriptor.LOGOUT)).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 | ProductCatalogDoesNotExist | AmountBiggerThanAvailable | ProductPackageDoesNotExist | GroceryListIsEmpty | AuthenticationError | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
        log.fatal("Critical bug: this command result isn't supposed to return here");
        throw new CriticalError();
    }
    customerProfile = null;
    log.info("logout from server succeed.");
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) CriticalError(SMExceptions.CommonExceptions.CriticalError) 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)

Example 5 with GroceryListIsEmpty

use of CustomerContracts.ACustomerExceptions.GroceryListIsEmpty 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

CommandWrapper (ClientServerApi.CommandWrapper)9 ForgotPasswordWrongAnswer (CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer)9 GroceryListIsEmpty (CustomerContracts.ACustomerExceptions.GroceryListIsEmpty)9 InvalidCommandDescriptor (CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor)9 CriticalError (SMExceptions.CommonExceptions.CriticalError)9 SocketTimeoutException (java.net.SocketTimeoutException)9 AuthenticationError (CustomerContracts.ACustomerExceptions.AuthenticationError)8 ProductCatalogDoesNotExist (CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)8 UsernameAlreadyExists (CustomerContracts.ACustomerExceptions.UsernameAlreadyExists)8 AmountBiggerThanAvailable (CustomerContracts.ACustomerExceptions.AmountBiggerThanAvailable)7 InvalidParameter (CustomerContracts.ACustomerExceptions.InvalidParameter)7 ProductPackageDoesNotExist (CustomerContracts.ACustomerExceptions.ProductPackageDoesNotExist)7 CustomerNotConnected (CustomerContracts.ACustomerExceptions.CustomerNotConnected)5 ProductPackage (BasicCommonClasses.ProductPackage)2 CartProduct (BasicCommonClasses.CartProduct)1 CatalogProduct (BasicCommonClasses.CatalogProduct)1 GroceryList (BasicCommonClasses.GroceryList)1 Ingredient (BasicCommonClasses.Ingredient)1 Login (BasicCommonClasses.Login)1 AmountIsBiggerThanAvailable (CommonDefs.GroceryListExceptions.AmountIsBiggerThanAvailable)1