Search in sources :

Example 6 with CustomerNotConnected

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

the class Customer method isFreeUsername.

@Override
public Boolean isFreeUsername(String username) throws CriticalError {
    Boolean isFree = true;
    String serverResponse;
    log.info("Creating isFreeUsername command wrapper");
    establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
    /* first: getting the product from the server */
    try {
        serverResponse = sendRequestWithRespondToServer(new CommandWrapper(id, CommandDescriptor.IS_FREE_CUSTOMER_NAME, Serialization.serialize(username)).serialize());
    } catch (SocketTimeoutException e) {
        log.fatal("Critical bug: failed to get respond from server");
        throw new CriticalError();
    }
    terminateCommunication();
    CommandWrapper commandWrapper = getCommandWrapper(serverResponse);
    try {
        resultDescriptorHandler(commandWrapper.getResultDescriptor());
    } catch (UsernameAlreadyExists e) {
        isFree = false;
    } catch (InvalidCommandDescriptor | CriticalError | AmountBiggerThanAvailable | ProductPackageDoesNotExist | GroceryListIsEmpty | AuthenticationError | CustomerNotConnected | ProductCatalogDoesNotExist | InvalidParameter | ForgotPasswordWrongAnswer ¢) {
        log.fatal("Critical bug: this command result isn't supposed to return here");
        throw new CriticalError();
    }
    log.info("isFreeUsername command succeed.");
    return isFree;
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) CriticalError(SMExceptions.CommonExceptions.CriticalError) 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) UsernameAlreadyExists(CustomerContracts.ACustomerExceptions.UsernameAlreadyExists) ProductPackageDoesNotExist(CustomerContracts.ACustomerExceptions.ProductPackageDoesNotExist) ProductCatalogDoesNotExist(CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)

Example 7 with CustomerNotConnected

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

the class Customer method getAllIngredients.

@Override
public List<Ingredient> getAllIngredients() throws CriticalError {
    String serverResponse;
    log.info("Creating getAllIngredients command wrapper");
    establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
    /* first: getting the product from the server */
    try {
        serverResponse = sendRequestWithRespondToServer(new CommandWrapper(id, CommandDescriptor.GET_ALL_INGREDIENTS).serialize());
    } catch (SocketTimeoutException e) {
        log.fatal("Critical bug: failed to get respond from server");
        throw new CriticalError();
    }
    terminateCommunication();
    CommandWrapper commandWrapper = getCommandWrapper(serverResponse);
    try {
        resultDescriptorHandler(commandWrapper.getResultDescriptor());
    } catch (InvalidCommandDescriptor | CriticalError | AmountBiggerThanAvailable | ProductPackageDoesNotExist | GroceryListIsEmpty | AuthenticationError | CustomerNotConnected | ProductCatalogDoesNotExist | InvalidParameter | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
        log.fatal("Critical bug: this command result isn't supposed to return here");
        throw new CriticalError();
    }
    log.info("getAllIngredients command succeed.");
    return new Gson().fromJson(commandWrapper.getData(), new TypeToken<List<Ingredient>>() {
    }.getType());
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) CriticalError(SMExceptions.CommonExceptions.CriticalError) CustomerNotConnected(CustomerContracts.ACustomerExceptions.CustomerNotConnected) Gson(com.google.gson.Gson) CommandWrapper(ClientServerApi.CommandWrapper) GroceryListIsEmpty(CustomerContracts.ACustomerExceptions.GroceryListIsEmpty) InvalidParameter(CustomerContracts.ACustomerExceptions.InvalidParameter) SocketTimeoutException(java.net.SocketTimeoutException) AmountBiggerThanAvailable(CustomerContracts.ACustomerExceptions.AmountBiggerThanAvailable) Ingredient(BasicCommonClasses.Ingredient) InvalidCommandDescriptor(CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor) TypeToken(com.google.gson.reflect.TypeToken) ForgotPasswordWrongAnswer(CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer) ProductPackageDoesNotExist(CustomerContracts.ACustomerExceptions.ProductPackageDoesNotExist) UsernameAlreadyExists(CustomerContracts.ACustomerExceptions.UsernameAlreadyExists) ProductCatalogDoesNotExist(CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)

Aggregations

CommandWrapper (ClientServerApi.CommandWrapper)7 CustomerNotConnected (CustomerContracts.ACustomerExceptions.CustomerNotConnected)7 CriticalError (SMExceptions.CommonExceptions.CriticalError)7 AmountBiggerThanAvailable (CustomerContracts.ACustomerExceptions.AmountBiggerThanAvailable)6 InvalidParameter (CustomerContracts.ACustomerExceptions.InvalidParameter)6 ProductPackageDoesNotExist (CustomerContracts.ACustomerExceptions.ProductPackageDoesNotExist)6 AuthenticationError (CustomerContracts.ACustomerExceptions.AuthenticationError)5 ForgotPasswordWrongAnswer (CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer)5 GroceryListIsEmpty (CustomerContracts.ACustomerExceptions.GroceryListIsEmpty)5 InvalidCommandDescriptor (CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor)5 ProductCatalogDoesNotExist (CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)5 SocketTimeoutException (java.net.SocketTimeoutException)5 UsernameAlreadyExists (CustomerContracts.ACustomerExceptions.UsernameAlreadyExists)4 IOException (java.io.IOException)2 Before (org.junit.Before)2 GroceryList (BasicCommonClasses.GroceryList)1 Ingredient (BasicCommonClasses.Ingredient)1 Login (BasicCommonClasses.Login)1 ICustomer (CustomerContracts.ICustomer)1 Customer (CustomerImplementations.Customer)1