Search in sources :

Example 1 with CommandWrapper

use of ClientServerApi.CommandWrapper in project SmartCity-Market by TechnionYP5777.

the class CommandProcess method process.

@Override
public void process(Socket clientSocket) {
    CommandWrapper outCommandWrapper;
    BufferedReader in = null;
    PrintWriter out = null;
    String command = null;
    try {
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        for (long startTime = System.currentTimeMillis(); command == null && System.currentTimeMillis() - startTime < 1000; ) command = in.readLine();
    } catch (IOException e1) {
        log.fatal("Failed to get string command");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
        out.println(outCommandWrapper.serialize());
        return;
    }
    if (command == null) {
        /* Timeout failure */
        log.fatal("Failed to get string command due to timeout");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
    } else {
        log.info("New command received: " + command);
        outCommandWrapper = new CommandExecuter(command).execute(new SQLDatabaseConnection());
    }
    out.println(outCommandWrapper.serialize());
}
Also used : SQLDatabaseConnection(SQLDatabase.SQLDatabaseConnection) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) CommandWrapper(ClientServerApi.CommandWrapper) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 2 with CommandWrapper

use of ClientServerApi.CommandWrapper in project SmartCity-Market by TechnionYP5777.

the class CommandExecuter method forgetPasswordSendAnswerWithNewPassword.

private void forgetPasswordSendAnswerWithNewPassword(SQLDatabaseConnection c) {
    Login login;
    log.info("Get question for forget password send answer command called with from serderID " + inCommandWrapper.getSenderID() + " command called");
    try {
        login = Serialization.deserialize(inCommandWrapper.getData(), Login.class);
    } catch (java.lang.RuntimeException e) {
        log.fatal("Failed to parse data for Get question for forget password send answer command");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
        return;
    }
    boolean goodAnswer;
    try {
        goodAnswer = inCommandWrapper.getSenderID() == 0 ? /* Command sent from employee */
        c.verifySecurityAnswerWorker(login.getUserName(), login.getForgetPassword().getAnswer()) : /* Command sent from customer */
        c.verifySecurityAnswerCustomer(login.getUserName(), login.getForgetPassword().getAnswer());
        if (!goodAnswer) {
            log.info("the anwser is incorrect.");
            outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_FOROGT_PASSWORD_WRONG_ANSWER, Serialization.serialize(false));
        } else {
            log.info("the anwser is correct");
            if (inCommandWrapper.getSenderID() == 0)
                /* Command sent from employee */
                c.setPasswordWorker(login.getUserName(), login.getPassword());
            else
                /* Command sent from customer */
                c.setPasswordCustomer(login.getUserName(), login.getPassword());
            outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_OK, Serialization.serialize(true));
            log.info("the anwser is correct. password changed succesfully.");
        }
    } catch (CriticalError e) {
        log.fatal("Get question for forget password send answer command failed, critical error occured from SQL Database connection");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
    } catch (ClientNotExist e) {
        log.info("Get question for forget password send answer command failed, client is not exist");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_USERNAME_DOES_NOT_EXIST);
    }
    log.info("Get question for forget password send answer command system finished");
}
Also used : CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) CommandWrapper(ClientServerApi.CommandWrapper) Login(BasicCommonClasses.Login) ClientNotExist(SQLDatabase.SQLDatabaseException.ClientNotExist)

Example 3 with CommandWrapper

use of ClientServerApi.CommandWrapper in project SmartCity-Market by TechnionYP5777.

the class CommandExecuter method registerNewWorker.

private void registerNewWorker(SQLDatabaseConnection c) {
    Login login = null;
    log.info("Register new worker from serderID " + inCommandWrapper.getSenderID() + " command called");
    try {
        login = Serialization.deserialize(inCommandWrapper.getData(), Login.class);
    } catch (java.lang.RuntimeException e) {
        log.fatal("Failed to parse data for Register New Worker command");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
        return;
    }
    log.info("Trying to register new worker " + login.getUserName() + " to system");
    try {
        c.addWorker(inCommandWrapper.getSenderID(), login, login.getForgetPassword());
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_OK);
    } catch (CriticalError e) {
        log.fatal("Register new worker command failed, critical error occured from SQL Database connection");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
    } catch (ClientAlreadyExist e) {
        log.info("Register new worker command failed, worker already exists");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_USERNAME_ALREADY_EXISTS);
    } catch (ClientNotConnected e) {
        log.info("Register new worker command failed, manager is not connected");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_SENDER_IS_NOT_CONNECTED);
    }
    log.info("Register new worker " + login.getUserName() + " to system finished");
}
Also used : CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) ClientAlreadyExist(SQLDatabase.SQLDatabaseException.ClientAlreadyExist) CommandWrapper(ClientServerApi.CommandWrapper) Login(BasicCommonClasses.Login)

Example 4 with CommandWrapper

use of ClientServerApi.CommandWrapper in project SmartCity-Market by TechnionYP5777.

the class CommandExecuter method forceRemoveIngredient.

private void forceRemoveIngredient(SQLDatabaseConnection c) {
    Ingredient ingredient = null;
    log.info("Force remove ingredient from serderID " + inCommandWrapper.getSenderID() + " command called");
    try {
        ingredient = Serialization.deserialize(inCommandWrapper.getData(), Ingredient.class);
    } catch (java.lang.RuntimeException e) {
        log.fatal("Failed to parse data for Force Remove Ingredient command");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
        return;
    }
    log.info("Trying to force remove ingredient " + ingredient + " from system");
    try {
        //TODO noam call here to force remove ingredient
        c.removeIngredient(inCommandWrapper.getSenderID(), ingredient);
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_OK);
    } catch (CriticalError e) {
        log.fatal("Force Remove ingredient command failed, critical error occured from SQL Database connection");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
    } catch (ClientNotConnected e) {
        log.info("Force Remove ingredient customer command failed, client is not connected");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_SENDER_IS_NOT_CONNECTED);
    } catch (IngredientNotExist e) {
        log.info("Force Remove ingredient customer command failed, ingredient does not exist");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.PARAM_ID_IS_NOT_EXIST);
    } catch (IngredientStillUsed e) {
        log.info("Force Remove ingredient customer command failed, ingredient still in use");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_INGREDIENT_STILL_IN_USE);
    }
    log.info("Force Remove ingredient " + ingredient + " from system finished");
}
Also used : CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) Ingredient(BasicCommonClasses.Ingredient) IngredientStillUsed(SQLDatabase.SQLDatabaseException.IngredientStillUsed) CommandWrapper(ClientServerApi.CommandWrapper) IngredientNotExist(SQLDatabase.SQLDatabaseException.IngredientNotExist)

Example 5 with CommandWrapper

use of ClientServerApi.CommandWrapper in project SmartCity-Market by TechnionYP5777.

the class CommandExecuter method editIngredient.

private void editIngredient(SQLDatabaseConnection c) {
    Ingredient ingredient = null;
    log.info("Edit ingredient from serderID " + inCommandWrapper.getSenderID() + " command called");
    try {
        ingredient = Serialization.deserialize(inCommandWrapper.getData(), Ingredient.class);
    } catch (java.lang.RuntimeException e) {
        log.fatal("Failed to parse data for Edit Ingredient command");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
        return;
    }
    log.info("Trying to edit ingredient " + ingredient);
    try {
        c.editIngredient(inCommandWrapper.getSenderID(), ingredient);
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_OK);
    } catch (CriticalError e) {
        log.fatal("Edit ingredient command failed, critical error occured from SQL Database connection");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
    } catch (ClientNotConnected e) {
        log.info("Edit ingredient customer command failed, client is not connected");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_SENDER_IS_NOT_CONNECTED);
    } catch (IngredientNotExist e) {
        log.info("Edit ingredient customer command failed, ingredient does not exist");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.PARAM_ID_IS_NOT_EXIST);
    }
    log.info("Edit ingredient " + ingredient + " from system finished");
}
Also used : CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) Ingredient(BasicCommonClasses.Ingredient) CommandWrapper(ClientServerApi.CommandWrapper) IngredientNotExist(SQLDatabase.SQLDatabaseException.IngredientNotExist)

Aggregations

CommandWrapper (ClientServerApi.CommandWrapper)218 CriticalError (SQLDatabase.SQLDatabaseException.CriticalError)162 Test (org.junit.Test)144 CommandExecuter (CommandHandler.CommandExecuter)135 ClientNotConnected (SQLDatabase.SQLDatabaseException.ClientNotConnected)124 Gson (com.google.gson.Gson)123 ProductNotExistInCatalog (SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog)57 SmartCode (BasicCommonClasses.SmartCode)51 ProductPackage (BasicCommonClasses.ProductPackage)45 CriticalError (SMExceptions.CommonExceptions.CriticalError)39 IngredientNotExist (SQLDatabase.SQLDatabaseException.IngredientNotExist)39 Location (BasicCommonClasses.Location)37 ManufacturerNotExist (SQLDatabase.SQLDatabaseException.ManufacturerNotExist)27 ProductPackageAmountNotMatch (SQLDatabase.SQLDatabaseException.ProductPackageAmountNotMatch)27 ProductPackageNotExist (SQLDatabase.SQLDatabaseException.ProductPackageNotExist)27 ClientNotExist (SQLDatabase.SQLDatabaseException.ClientNotExist)23 InvalidCommandDescriptor (EmployeeDefs.AEmployeeException.InvalidCommandDescriptor)22 AuthenticationError (EmployeeDefs.AEmployeeException.AuthenticationError)21 EmployeeAlreadyConnected (EmployeeDefs.AEmployeeException.EmployeeAlreadyConnected)21 IngredientStillInUse (EmployeeDefs.AEmployeeException.IngredientStillInUse)21