Search in sources :

Example 86 with Answer

use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.

the class UserDAO method delete.

@Override
public Answer delete(User user) {
    MessageEvent msg = null;
    final String query = "DELETE FROM user WHERE userid = ? ";
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setInt(1, user.getUserID());
            preStat.executeUpdate();
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
            msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "DELETE"));
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to close connection : " + exception.toString());
        }
    }
    return new Answer(msg);
}
Also used : Answer(org.cerberus.util.answer.Answer) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 87 with Answer

use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.

the class UserSystemDAO method remove.

@Override
public Answer remove(UserSystem sys) {
    Answer ans = new Answer();
    MessageEvent msg = null;
    try (Connection connection = databaseSpring.connect();
        PreparedStatement preStat = connection.prepareStatement(Query.DELETE)) {
        // Prepare and execute query
        preStat.setString(1, sys.getLogin());
        preStat.setString(2, sys.getSystem());
        preStat.executeUpdate();
        // Set the final message
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK).resolveDescription("ITEM", OBJECT_NAME).resolveDescription("OPERATION", "DELTE");
    } catch (Exception e) {
        LOG.warn("Unable to delete UserSystem: " + e.getMessage());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
    } finally {
        ans.setResultMessage(msg);
    }
    return ans;
}
Also used : Answer(org.cerberus.util.answer.Answer) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) CerberusException(org.cerberus.exception.CerberusException) SQLException(java.sql.SQLException)

Example 88 with Answer

use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.

the class RobotService method delete.

@Override
public Answer delete(Robot robot) {
    // First, delete the robot
    Answer finalAnswer = robotDao.delete(robot);
    if (!finalAnswer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        return finalAnswer;
    }
    // Second, delete its capabilities
    AnswerUtil.agregateAnswer(finalAnswer, robotCapabilityService.delete(robot.getCapabilities()));
    // Finally return aggregated answer
    return finalAnswer;
}
Also used : Answer(org.cerberus.util.answer.Answer)

Example 89 with Answer

use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.

the class RobotService method update.

@Override
public Answer update(Robot robot) {
    // First, update the robot
    Answer finalAnswer = robotDao.update(robot);
    if (!finalAnswer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        return finalAnswer;
    }
    // Second, update its capabilities
    AnswerUtil.agregateAnswer(finalAnswer, robotCapabilityService.compareListAndUpdateInsertDeleteElements(robot.getRobot(), robot.getCapabilities()));
    // Finally return aggregated answer
    return finalAnswer;
}
Also used : Answer(org.cerberus.util.answer.Answer)

Example 90 with Answer

use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.

the class RobotService method create.

@Override
public Answer create(Robot robot) {
    // First, create the robot
    Answer finalAnswer = robotDao.create(robot);
    if (!finalAnswer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        return finalAnswer;
    }
    // Second, create its capabilities
    for (RobotCapability capability : robot.getCapabilities()) {
        Answer robotCapabilityAnswer = robotCapabilityService.create(capability);
        // We try to create as many capabilities as possible, even if an error occurred.
        AnswerUtil.agregateAnswer(finalAnswer, robotCapabilityAnswer);
    }
    return finalAnswer;
}
Also used : Answer(org.cerberus.util.answer.Answer) RobotCapability(org.cerberus.crud.entity.RobotCapability)

Aggregations

Answer (org.cerberus.util.answer.Answer)241 MessageEvent (org.cerberus.engine.entity.MessageEvent)227 Connection (java.sql.Connection)127 PreparedStatement (java.sql.PreparedStatement)127 SQLException (java.sql.SQLException)127 ApplicationContext (org.springframework.context.ApplicationContext)77 JSONObject (org.json.JSONObject)75 ILogEventService (org.cerberus.crud.service.ILogEventService)74 PolicyFactory (org.owasp.html.PolicyFactory)60 AnswerItem (org.cerberus.util.answer.AnswerItem)53 CerberusException (org.cerberus.exception.CerberusException)45 ArrayList (java.util.ArrayList)35 JSONException (org.json.JSONException)26 IOException (java.io.IOException)23 ServletException (javax.servlet.ServletException)19 JSONArray (org.json.JSONArray)15 LogEventService (org.cerberus.crud.service.impl.LogEventService)13 TestCase (org.cerberus.crud.entity.TestCase)10 ITestCaseService (org.cerberus.crud.service.ITestCaseService)10 CountryEnvParam (org.cerberus.crud.entity.CountryEnvParam)8