Search in sources :

Example 16 with AnswerItem

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

the class DeployTypeDAO method readByKey.

@Override
public AnswerItem readByKey(String deployType) {
    AnswerItem ans = new AnswerItem();
    DeployType result = null;
    final String query = "SELECT * FROM `deploytype` WHERE `deploytype` = ?";
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setString(1, deployType);
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    result = loadFromResultSet(resultSet);
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                    ans.setItem(result);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                }
            } 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 {
                resultSet.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 {
            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());
        }
    }
    // sets the message
    ans.setResultMessage(msg);
    return ans;
}
Also used : FactoryDeployType(org.cerberus.crud.factory.impl.FactoryDeployType) IFactoryDeployType(org.cerberus.crud.factory.IFactoryDeployType) DeployType(org.cerberus.crud.entity.DeployType) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 17 with AnswerItem

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

the class InvariantDAO method readByKey.

@Override
public AnswerItem readByKey(String id, String value) {
    AnswerItem ans = new AnswerItem();
    Invariant result = null;
    final String query = "SELECT * FROM `invariant` WHERE `idname` = ? AND `value` = ?";
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
        LOG.debug("SQL.param.id : " + id);
        LOG.debug("SQL.param.value : " + value);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setString(1, id);
            preStat.setString(2, value);
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    result = loadFromResultSet(resultSet);
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                    ans.setItem(result);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                }
            } 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 {
                resultSet.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 {
            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());
        }
    }
    // sets the message
    ans.setResultMessage(msg);
    return ans;
}
Also used : Invariant(org.cerberus.crud.entity.Invariant) IFactoryInvariant(org.cerberus.crud.factory.IFactoryInvariant) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 18 with AnswerItem

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

the class TestDataLibDAO method readByKey.

@Override
public AnswerItem<TestDataLib> readByKey(int testDataLibID) {
    AnswerItem answer = new AnswerItem();
    MessageEvent msg;
    TestDataLib result;
    final String query = "SELECT * FROM testdatalib tdl " + " LEFT OUTER JOIN testdatalibdata tdd ON tdl.TestDataLibID = tdd.TestDataLibID and tdd.Subdata='' " + " WHERE tdl.`TestDataLibID` = ? ;";
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        preStat.setInt(1, testDataLibID);
        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    result = this.loadFromResultSet(resultSet);
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                    answer.setItem(result);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                }
            } 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 {
                if (resultSet != null) {
                    resultSet.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 {
            if (preStat != null) {
                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 (!this.databaseSpring.isOnTransaction()) {
                if (connection != null) {
                    connection.close();
                }
            }
        } catch (SQLException ex) {
            LOG.warn("Unable to close connection : " + ex.toString());
        }
    }
    // sets the message
    answer.setResultMessage(msg);
    return answer;
}
Also used : TestDataLib(org.cerberus.crud.entity.TestDataLib) IFactoryTestDataLib(org.cerberus.crud.factory.IFactoryTestDataLib) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 19 with AnswerItem

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

the class TestDataLibDAO method uploadFile.

@Override
public Answer uploadFile(int id, FileItem file) {
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", "cerberus_testdatalibcsv_path Parameter not found");
    AnswerItem a = parameterService.readByKey("", "cerberus_testdatalibcsv_path");
    if (a.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        Parameter p = (Parameter) a.getItem();
        String uploadPath = p.getValue();
        File appDir = new File(uploadPath + File.separator + id);
        if (!appDir.exists()) {
            try {
                appDir.mkdirs();
            } catch (SecurityException se) {
                LOG.warn("Unable to create testdatalib csv dir: " + se.getMessage());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", se.toString());
                a.setResultMessage(msg);
            }
        }
        if (a.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
            deleteFolder(appDir, false);
            File picture = new File(uploadPath + File.separator + id + File.separator + file.getName());
            try {
                file.write(picture);
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK).resolveDescription("DESCRIPTION", "TestDataLib CSV file uploaded");
                msg.setDescription(msg.getDescription().replace("%ITEM%", "testDatalib CSV").replace("%OPERATION%", "Upload"));
            } catch (Exception e) {
                LOG.warn("Unable to upload testdatalib csv file: " + e.getMessage());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
            }
        }
    } else {
        LOG.warn("cerberus_testdatalibCSV_path Parameter not found");
    }
    a.setResultMessage(msg);
    return a;
}
Also used : MessageEvent(org.cerberus.engine.entity.MessageEvent) Parameter(org.cerberus.crud.entity.Parameter) AnswerItem(org.cerberus.util.answer.AnswerItem) File(java.io.File) SQLException(java.sql.SQLException)

Example 20 with AnswerItem

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

the class TestDataLibDAO method create.

@Override
public AnswerItem create(TestDataLib testDataLib) {
    MessageEvent msg;
    AnswerItem answer = new AnswerItem();
    StringBuilder query = new StringBuilder();
    TestDataLib createdTestDataLib;
    query.append("INSERT INTO testdatalib (`name`, `system`, `environment`, `country`, `group`, `type`, `database`, `script`, `databaseUrl`, ");
    query.append("`service`, `servicePath`, `method`, `envelope`, `databaseCsv`, `csvUrl`,`separator`, `description`, `creator`) ");
    if ((testDataLib.getService() != null) && (!testDataLib.getService().equals(""))) {
        query.append("VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
    } else {
        query.append("VALUES (?,?,?,?,?,?,?,?,?,null,?,?,?,?,?,?,?,?)");
    }
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
        LOG.debug("SQL.param.name : " + testDataLib.getName());
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString(), PreparedStatement.RETURN_GENERATED_KEYS);
        try {
            int i = 1;
            preStat.setString(i++, testDataLib.getName());
            preStat.setString(i++, ParameterParserUtil.returnEmptyStringIfNull(testDataLib.getSystem()));
            preStat.setString(i++, ParameterParserUtil.returnEmptyStringIfNull(testDataLib.getEnvironment()));
            preStat.setString(i++, ParameterParserUtil.returnEmptyStringIfNull(testDataLib.getCountry()));
            preStat.setString(i++, ParameterParserUtil.returnEmptyStringIfNull(testDataLib.getGroup()));
            preStat.setString(i++, testDataLib.getType());
            preStat.setString(i++, ParameterParserUtil.returnEmptyStringIfNull(testDataLib.getDatabase()));
            preStat.setString(i++, ParameterParserUtil.returnEmptyStringIfNull(testDataLib.getScript()));
            preStat.setString(i++, ParameterParserUtil.returnEmptyStringIfNull(testDataLib.getDatabaseUrl()));
            if ((testDataLib.getService() != null) && (!testDataLib.getService().equals(""))) {
                preStat.setString(i++, ParameterParserUtil.returnEmptyStringIfNull(testDataLib.getService()));
            }
            preStat.setString(i++, ParameterParserUtil.returnEmptyStringIfNull(testDataLib.getServicePath()));
            preStat.setString(i++, ParameterParserUtil.returnEmptyStringIfNull(testDataLib.getMethod()));
            // is the one that allows null values
            preStat.setString(i++, testDataLib.getEnvelope());
            preStat.setString(i++, ParameterParserUtil.returnEmptyStringIfNull(testDataLib.getDatabaseCsv()));
            preStat.setString(i++, ParameterParserUtil.returnEmptyStringIfNull(testDataLib.getCsvUrl()));
            preStat.setString(i++, ParameterParserUtil.returnEmptyStringIfNull(testDataLib.getSeparator()));
            preStat.setString(i++, ParameterParserUtil.returnEmptyStringIfNull(testDataLib.getDescription()));
            preStat.setString(i++, ParameterParserUtil.returnEmptyStringIfNull(testDataLib.getCreator()));
            preStat.executeUpdate();
            ResultSet keys = preStat.getGeneratedKeys();
            try {
                if (keys != null && keys.next()) {
                    testDataLib.setTestDataLibID(keys.getInt(1));
                    // Debug message on SQL.
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("SQL.result.TestDataLibID : " + testDataLib.getTestDataLibID());
                    }
                    answer.setItem(testDataLib);
                }
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "INSERT"));
            } 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 {
                if (keys != null) {
                    keys.close();
                }
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            if (exception.getSQLState().equals(SQL_DUPLICATED_CODE)) {
                // 23000 is the sql state for duplicate entries
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_DUPLICATE);
                msg.setDescription(msg.getDescription().replace("%ITEM%", "Test data lib ").replace("%OPERATION%", "INSERT").replace("%REASON%", exception.toString()));
            } else {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
                msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
            }
        } finally {
            if (preStat != null) {
                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 (!this.databaseSpring.isOnTransaction()) {
                if (connection != null) {
                    connection.close();
                }
            }
        } catch (SQLException ex) {
            LOG.error("Unable to close connection : " + ex.toString());
        }
    }
    answer.setResultMessage(msg);
    return answer;
}
Also used : TestDataLib(org.cerberus.crud.entity.TestDataLib) IFactoryTestDataLib(org.cerberus.crud.factory.IFactoryTestDataLib) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem)

Aggregations

AnswerItem (org.cerberus.util.answer.AnswerItem)322 MessageEvent (org.cerberus.engine.entity.MessageEvent)212 JSONObject (org.json.JSONObject)206 ApplicationContext (org.springframework.context.ApplicationContext)98 AnswerList (org.cerberus.util.answer.AnswerList)90 ArrayList (java.util.ArrayList)78 JSONArray (org.json.JSONArray)74 PolicyFactory (org.owasp.html.PolicyFactory)74 List (java.util.List)72 JSONException (org.json.JSONException)69 HashMap (java.util.HashMap)60 ILogEventService (org.cerberus.crud.service.ILogEventService)58 SQLException (java.sql.SQLException)57 Connection (java.sql.Connection)55 PreparedStatement (java.sql.PreparedStatement)53 Answer (org.cerberus.util.answer.Answer)53 ResultSet (java.sql.ResultSet)52 CerberusException (org.cerberus.exception.CerberusException)44 IOException (java.io.IOException)34 ServletException (javax.servlet.ServletException)24