Search in sources :

Example 1 with Invariant

use of org.cerberus.crud.entity.Invariant in project cerberus-source by cerberustesting.

the class InvariantDAO method readByIdname.

@Override
public AnswerList readByIdname(String idName) {
    AnswerList answer = new AnswerList();
    MessageEvent msg;
    List<Invariant> result = new ArrayList<>();
    final String query = "SELECT * FROM invariant i  WHERE i.idname = ? ORDER BY sort";
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
        LOG.debug("SQL.param.idName : " + idName);
    }
    try (Connection connection = this.databaseSpring.connect();
        PreparedStatement preStat = connection.prepareStatement(query)) {
        preStat.setString(1, idName);
        try (ResultSet resultSet = preStat.executeQuery()) {
            while (resultSet.next()) {
                result.add(this.loadFromResultSet(resultSet));
            }
            if (result.isEmpty()) {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
            } else {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
        }
    } catch (SQLException exception) {
        LOG.warn("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
    }
    answer.setTotalRows(result.size());
    answer.setDataList(result);
    answer.setResultMessage(msg);
    return answer;
}
Also used : Invariant(org.cerberus.crud.entity.Invariant) IFactoryInvariant(org.cerberus.crud.factory.IFactoryInvariant) AnswerList(org.cerberus.util.answer.AnswerList) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 2 with Invariant

use of org.cerberus.crud.entity.Invariant in project cerberus-source by cerberustesting.

the class InvariantDAO method readByIdnameByGp1.

@Override
public AnswerList readByIdnameByGp1(String idName, String gp) {
    AnswerList ansList = new AnswerList();
    MessageEvent msg;
    List<Invariant> invariantList = new ArrayList<Invariant>();
    final String query = "SELECT * FROM invariant i  WHERE i.idname = ? AND i.gp1 = ? ORDER BY sort";
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
        LOG.debug("SQL.param.idName : " + idName);
        LOG.debug("SQL.param.gp : " + gp);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setString(1, idName);
            preStat.setString(2, gp);
            ResultSet resultSet = preStat.executeQuery();
            try {
                while (resultSet.next()) {
                    invariantList.add(this.loadFromResultSet(resultSet));
                }
                if (invariantList.isEmpty()) {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                }
            } catch (SQLException exception) {
                LOG.warn("Unable to execute query : " + exception.toString());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
                msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to execute query : " + exception.toString()));
                invariantList.clear();
            } finally {
                if (resultSet != null) {
                    resultSet.close();
                }
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to execute query : " + exception.toString()));
        } finally {
            if (preStat != null) {
                preStat.close();
            }
        }
    } catch (SQLException exception) {
        LOG.warn("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to execute query : " + exception.toString()));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    ansList.setTotalRows(invariantList.size());
    ansList.setDataList(invariantList);
    ansList.setResultMessage(msg);
    return ansList;
}
Also used : Invariant(org.cerberus.crud.entity.Invariant) IFactoryInvariant(org.cerberus.crud.factory.IFactoryInvariant) AnswerList(org.cerberus.util.answer.AnswerList) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 3 with Invariant

use of org.cerberus.crud.entity.Invariant in project cerberus-source by cerberustesting.

the class InvariantDAO method readCountryListEnvironmentLastChanges.

@Override
public AnswerList readCountryListEnvironmentLastChanges(String system, Integer nbdays) {
    AnswerList response = new AnswerList();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    List<Invariant> objectList = new ArrayList<Invariant>();
    StringBuilder searchSQL = new StringBuilder();
    StringBuilder query = new StringBuilder();
    // SQL_CALC_FOUND_ROWS allows to retrieve the total number of columns by disrearding the limit clauses that
    // were applied -- used for pagination p
    query.append("SELECT distinct i.* FROM countryenvparam_log cl ");
    query.append(" JOIN invariant i on i.value=cl.country and i.idname='COUNTRY' ");
    query.append(" WHERE TO_DAYS(NOW()) - TO_DAYS(cl.datecre) <= ? and build != '' and `System` = ? order by i.sort;");
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
        LOG.debug("SQL.param : " + nbdays);
        LOG.debug("SQL.param : " + system);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            int i = 1;
            preStat.setInt(i++, nbdays);
            preStat.setString(i++, system);
            ResultSet resultSet = preStat.executeQuery();
            try {
                // gets the data
                while (resultSet.next()) {
                    objectList.add(this.loadFromResultSet(resultSet));
                }
                // get the total number of rows
                resultSet = preStat.executeQuery("SELECT FOUND_ROWS()");
                int nrTotalRows = 0;
                if (resultSet != null && resultSet.next()) {
                    nrTotalRows = resultSet.getInt(1);
                }
                if (objectList.size() >= MAX_ROW_SELECTED) {
                    // Result of SQl was limited by MAX_ROW_SELECTED constrain. That means that we may miss some lines in the resultList.
                    LOG.error("Partial Result in the query.");
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING_PARTIAL_RESULT);
                    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Maximum row reached : " + MAX_ROW_SELECTED));
                    response = new AnswerList(objectList, nrTotalRows);
                } else if (objectList.size() <= 0) {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                    response = new AnswerList(objectList, nrTotalRows);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                    response = new AnswerList(objectList, nrTotalRows);
                }
            } 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 exception) {
            LOG.warn("Unable to close connection : " + exception.toString());
        }
    }
    response.setResultMessage(msg);
    response.setDataList(objectList);
    return response;
}
Also used : Invariant(org.cerberus.crud.entity.Invariant) IFactoryInvariant(org.cerberus.crud.factory.IFactoryInvariant) AnswerList(org.cerberus.util.answer.AnswerList) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 4 with Invariant

use of org.cerberus.crud.entity.Invariant 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 5 with Invariant

use of org.cerberus.crud.entity.Invariant in project cerberus-source by cerberustesting.

the class ReadTestCaseExecutionByTag method getCountryList.

private JSONObject getCountryList(HttpServletRequest request, ApplicationContext appContext) {
    JSONObject countryList = new JSONObject();
    try {
        IInvariantService invariantService = appContext.getBean(InvariantService.class);
        // TODO: handle if the response does not turn ok
        AnswerList answer = invariantService.readByIdname("COUNTRY");
        for (Invariant country : (List<Invariant>) answer.getDataList()) {
            countryList.put(country.getValue(), ParameterParserUtil.parseStringParam(request.getParameter(country.getValue()), "off"));
        }
    } catch (JSONException ex) {
        LOG.error("Error on getCountryList : " + ex);
    }
    return countryList;
}
Also used : Invariant(org.cerberus.crud.entity.Invariant) AnswerList(org.cerberus.util.answer.AnswerList) JSONObject(org.json.JSONObject) IInvariantService(org.cerberus.crud.service.IInvariantService) JSONException(org.json.JSONException) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Invariant (org.cerberus.crud.entity.Invariant)28 AnswerList (org.cerberus.util.answer.AnswerList)18 IInvariantService (org.cerberus.crud.service.IInvariantService)15 JSONObject (org.json.JSONObject)15 ArrayList (java.util.ArrayList)13 List (java.util.List)11 MessageEvent (org.cerberus.engine.entity.MessageEvent)10 IFactoryInvariant (org.cerberus.crud.factory.IFactoryInvariant)8 JSONArray (org.json.JSONArray)8 ApplicationContext (org.springframework.context.ApplicationContext)8 Connection (java.sql.Connection)7 PreparedStatement (java.sql.PreparedStatement)7 ResultSet (java.sql.ResultSet)7 SQLException (java.sql.SQLException)7 AnswerItem (org.cerberus.util.answer.AnswerItem)7 JSONException (org.json.JSONException)7 HashMap (java.util.HashMap)6 CerberusException (org.cerberus.exception.CerberusException)5 PolicyFactory (org.owasp.html.PolicyFactory)4 IOException (java.io.IOException)3