Search in sources :

Example 1 with CountryEnvironmentParameters

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

the class CountryEnvironmentParametersDAO method findCountryEnvironmentParametersByCriteria.

@Override
public List<CountryEnvironmentParameters> findCountryEnvironmentParametersByCriteria(CountryEnvironmentParameters countryEnvironmentParameter) throws CerberusException {
    List<CountryEnvironmentParameters> result = new ArrayList<CountryEnvironmentParameters>();
    boolean throwex = false;
    StringBuilder query = new StringBuilder();
    query.append("SELECT * FROM countryenvironmentparameters cea ");
    query.append(" WHERE cea.`system` LIKE ? AND cea.country LIKE ? AND cea.environment LIKE ? AND cea.Application LIKE ? ");
    query.append("AND cea.IP LIKE ? AND cea.URL LIKE ? AND cea.URLLOGIN LIKE ? AND cea.domain like ? ");
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
        LOG.debug("SQL.param.system : " + ParameterParserUtil.wildcardIfEmpty(countryEnvironmentParameter.getSystem()));
        LOG.debug("SQL.param.country : " + ParameterParserUtil.wildcardIfEmpty(countryEnvironmentParameter.getCountry()));
        LOG.debug("SQL.param.environment : " + ParameterParserUtil.wildcardIfEmpty(countryEnvironmentParameter.getEnvironment()));
        LOG.debug("SQL.param.application : " + ParameterParserUtil.wildcardIfEmpty(countryEnvironmentParameter.getApplication()));
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            preStat.setString(1, ParameterParserUtil.wildcardIfEmpty(countryEnvironmentParameter.getSystem()));
            preStat.setString(2, ParameterParserUtil.wildcardIfEmpty(countryEnvironmentParameter.getCountry()));
            preStat.setString(3, ParameterParserUtil.wildcardIfEmpty(countryEnvironmentParameter.getEnvironment()));
            preStat.setString(4, ParameterParserUtil.wildcardIfEmpty(countryEnvironmentParameter.getApplication()));
            preStat.setString(5, ParameterParserUtil.wildcardIfEmpty(countryEnvironmentParameter.getIp()));
            preStat.setString(6, ParameterParserUtil.wildcardIfEmpty(countryEnvironmentParameter.getUrl()));
            preStat.setString(7, ParameterParserUtil.wildcardIfEmpty(countryEnvironmentParameter.getUrlLogin()));
            preStat.setString(8, ParameterParserUtil.wildcardIfEmpty(countryEnvironmentParameter.getDomain()));
            ResultSet resultSet = preStat.executeQuery();
            try {
                while (resultSet.next()) {
                    result.add(loadFromResultSet(resultSet));
                }
            } catch (SQLException exception) {
                LOG.warn("Unable to execute query : " + exception.toString());
            } finally {
                resultSet.close();
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to execute query : " + exception.toString());
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.warn("Unable to execute query : " + exception.toString());
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    if (throwex) {
        throw new CerberusException(new MessageGeneral(MessageGeneralEnum.NO_DATA_FOUND));
    }
    return result;
}
Also used : CerberusException(org.cerberus.exception.CerberusException) MessageGeneral(org.cerberus.engine.entity.MessageGeneral) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) IFactoryCountryEnvironmentParameters(org.cerberus.crud.factory.IFactoryCountryEnvironmentParameters) CountryEnvironmentParameters(org.cerberus.crud.entity.CountryEnvironmentParameters) PreparedStatement(java.sql.PreparedStatement)

Example 2 with CountryEnvironmentParameters

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

the class CountryEnvironmentParametersDAO method readByKey.

@Override
public AnswerItem<CountryEnvironmentParameters> readByKey(String system, String country, String environment, String application) {
    AnswerItem ans = new AnswerItem();
    CountryEnvironmentParameters result = null;
    final String query = "SELECT * FROM countryenvironmentparameters cea WHERE cea.`system` = ? AND cea.country = ? AND cea.environment = ? AND cea.application = ?";
    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.system : " + system);
        LOG.debug("SQL.param.country : " + country);
        LOG.debug("SQL.param.environment : " + environment);
        LOG.debug("SQL.param.application : " + application);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setString(1, system);
            preStat.setString(2, country);
            preStat.setString(3, environment);
            preStat.setString(4, application);
            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 : SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) IFactoryCountryEnvironmentParameters(org.cerberus.crud.factory.IFactoryCountryEnvironmentParameters) CountryEnvironmentParameters(org.cerberus.crud.entity.CountryEnvironmentParameters) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 3 with CountryEnvironmentParameters

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

the class CountryEnvironmentParametersDAO method readByVariousByCriteria.

@Override
public AnswerList readByVariousByCriteria(String system, String country, String environment, String application, int start, int amount, String column, String dir, String searchTerm, String individualSearch) {
    AnswerList response = new AnswerList();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    List<CountryEnvironmentParameters> objectList = new ArrayList<CountryEnvironmentParameters>();
    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 SQL_CALC_FOUND_ROWS * FROM countryenvironmentparameters cea");
    searchSQL.append(" where 1=1 ");
    if (!StringUtil.isNullOrEmpty(searchTerm)) {
        searchSQL.append(" and (cea.`system` like ?");
        searchSQL.append(" or cea.`country` like ?");
        searchSQL.append(" or cea.`environment` like ?");
        searchSQL.append(" or cea.`application` like ?");
        searchSQL.append(" or cea.`ip` like ?");
        searchSQL.append(" or cea.`domain` like ?");
        searchSQL.append(" or cea.`URL` like ?");
        searchSQL.append(" or cea.`URLLOGIN` like ?)");
    }
    if (!StringUtil.isNullOrEmpty(individualSearch)) {
        searchSQL.append(" and (`?`)");
    }
    if (!StringUtil.isNullOrEmpty(system)) {
        searchSQL.append(" and (cea.`System` = ? )");
    }
    if (!StringUtil.isNullOrEmpty(country)) {
        searchSQL.append(" and (cea.`country` = ? )");
    }
    if (!StringUtil.isNullOrEmpty(environment)) {
        searchSQL.append(" and (cea.`environment` = ? )");
    }
    if (!StringUtil.isNullOrEmpty(application)) {
        searchSQL.append(" and (cea.`application` = ? )");
    }
    query.append(searchSQL);
    if (!StringUtil.isNullOrEmpty(column)) {
        query.append(" order by `").append(column).append("` ").append(dir);
    }
    if ((amount <= 0) || (amount >= MAX_ROW_SELECTED)) {
        query.append(" limit ").append(start).append(" , ").append(MAX_ROW_SELECTED);
    } else {
        query.append(" limit ").append(start).append(" , ").append(amount);
    }
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
        LOG.debug("SQL.param.system : " + system);
        LOG.debug("SQL.param.country : " + country);
        LOG.debug("SQL.param.environment : " + environment);
        LOG.debug("SQL.param.application : " + application);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            int i = 1;
            if (!StringUtil.isNullOrEmpty(searchTerm)) {
                preStat.setString(i++, "%" + searchTerm + "%");
                preStat.setString(i++, "%" + searchTerm + "%");
                preStat.setString(i++, "%" + searchTerm + "%");
                preStat.setString(i++, "%" + searchTerm + "%");
                preStat.setString(i++, "%" + searchTerm + "%");
                preStat.setString(i++, "%" + searchTerm + "%");
                preStat.setString(i++, "%" + searchTerm + "%");
                preStat.setString(i++, "%" + searchTerm + "%");
            }
            if (!StringUtil.isNullOrEmpty(individualSearch)) {
                preStat.setString(i++, individualSearch);
            }
            if (!StringUtil.isNullOrEmpty(system)) {
                preStat.setString(i++, system);
            }
            if (!StringUtil.isNullOrEmpty(country)) {
                preStat.setString(i++, country);
            }
            if (!StringUtil.isNullOrEmpty(environment)) {
                preStat.setString(i++, environment);
            }
            if (!StringUtil.isNullOrEmpty(application)) {
                preStat.setString(i++, application);
            }
            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 : 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) IFactoryCountryEnvironmentParameters(org.cerberus.crud.factory.IFactoryCountryEnvironmentParameters) CountryEnvironmentParameters(org.cerberus.crud.entity.CountryEnvironmentParameters) PreparedStatement(java.sql.PreparedStatement)

Example 4 with CountryEnvironmentParameters

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

the class CountryEnvParamService method findActiveEnvironmentBySystemCountryApplication.

@Override
public List<JSONObject> findActiveEnvironmentBySystemCountryApplication(String system, String country, String application) throws CerberusException {
    List<JSONObject> result = new ArrayList();
    CountryEnvParam countryEnvParam = countryEnvParamFactory.create(system, country, true);
    CountryEnvironmentParameters countryEnvironmentParameters = countryEnvironmentParametersFactory.create(system, country, null, application, null, null, null, null, null, null, null, null, CountryEnvironmentParameters.DEFAULT_POOLSIZE);
    List<CountryEnvironmentParameters> ceaList = countryEnvironmentParametersService.findCountryEnvironmentParametersByCriteria(countryEnvironmentParameters);
    List<CountryEnvParam> ceList = this.findCountryEnvParamByCriteria(countryEnvParam);
    try {
        for (CountryEnvironmentParameters cea : ceaList) {
            for (CountryEnvParam ce : ceList) {
                if (cea.getEnvironment().equals(ce.getEnvironment())) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("environment", ce.getEnvironment());
                    jsonObject.put("build", ce.getBuild());
                    jsonObject.put("revision", ce.getRevision());
                    jsonObject.put("ip", cea.getIp());
                    jsonObject.put("url", cea.getUrl());
                    result.add(jsonObject);
                }
            }
        }
    } catch (JSONException ex) {
        LOG.warn(ex);
    }
    return result;
}
Also used : JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) CountryEnvironmentParameters(org.cerberus.crud.entity.CountryEnvironmentParameters) IFactoryCountryEnvironmentParameters(org.cerberus.crud.factory.IFactoryCountryEnvironmentParameters) JSONException(org.json.JSONException) IFactoryCountryEnvParam(org.cerberus.crud.factory.IFactoryCountryEnvParam) CountryEnvParam(org.cerberus.crud.entity.CountryEnvParam)

Example 5 with CountryEnvironmentParameters

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

the class CountryEnvironmentParametersService method compareListAndUpdateInsertDeleteElements.

@Override
public Answer compareListAndUpdateInsertDeleteElements(String system, String application, List<CountryEnvironmentParameters> newList) {
    Answer ans = new Answer(null);
    MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);
    Answer finalAnswer = new Answer(msg1);
    List<CountryEnvironmentParameters> oldList = new ArrayList();
    try {
        oldList = this.convert(this.readByVarious(system, null, null, application));
    } catch (CerberusException ex) {
        LOG.error(ex);
    }
    /**
     * Update and Create all objects database Objects from newList
     */
    List<CountryEnvironmentParameters> listToUpdateOrInsert = new ArrayList(newList);
    listToUpdateOrInsert.removeAll(oldList);
    List<CountryEnvironmentParameters> listToUpdateOrInsertToIterate = new ArrayList(listToUpdateOrInsert);
    for (CountryEnvironmentParameters objectDifference : listToUpdateOrInsertToIterate) {
        for (CountryEnvironmentParameters objectInDatabase : oldList) {
            if (objectDifference.hasSameKey(objectInDatabase)) {
                ans = this.update(objectDifference);
                finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
                listToUpdateOrInsert.remove(objectDifference);
            }
        }
    }
    /**
     * Delete all objects database Objects that do not exist from newList
     */
    List<CountryEnvironmentParameters> listToDelete = new ArrayList(oldList);
    listToDelete.removeAll(newList);
    List<CountryEnvironmentParameters> listToDeleteToIterate = new ArrayList(listToDelete);
    for (CountryEnvironmentParameters tcsDifference : listToDeleteToIterate) {
        for (CountryEnvironmentParameters tcsInPage : newList) {
            if (tcsDifference.hasSameKey(tcsInPage)) {
                listToDelete.remove(tcsDifference);
            }
        }
    }
    if (!listToDelete.isEmpty()) {
        ans = this.deleteList(listToDelete);
        finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
    }
    // We insert only at the end (after deletion of all potencial enreg - linked with #1281)
    if (!listToUpdateOrInsert.isEmpty()) {
        ans = this.createList(listToUpdateOrInsert);
        finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
    }
    return finalAnswer;
}
Also used : Answer(org.cerberus.util.answer.Answer) CerberusException(org.cerberus.exception.CerberusException) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) CountryEnvironmentParameters(org.cerberus.crud.entity.CountryEnvironmentParameters)

Aggregations

CountryEnvironmentParameters (org.cerberus.crud.entity.CountryEnvironmentParameters)12 IFactoryCountryEnvironmentParameters (org.cerberus.crud.factory.IFactoryCountryEnvironmentParameters)10 ArrayList (java.util.ArrayList)7 JSONObject (org.json.JSONObject)6 ICountryEnvironmentParametersService (org.cerberus.crud.service.ICountryEnvironmentParametersService)5 MessageEvent (org.cerberus.engine.entity.MessageEvent)5 CerberusException (org.cerberus.exception.CerberusException)4 AnswerItem (org.cerberus.util.answer.AnswerItem)4 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 CountryEnvParam (org.cerberus.crud.entity.CountryEnvParam)3 Answer (org.cerberus.util.answer.Answer)3 JSONArray (org.json.JSONArray)3 PolicyFactory (org.owasp.html.PolicyFactory)3 ILogEventService (org.cerberus.crud.service.ILogEventService)2 MessageGeneral (org.cerberus.engine.entity.MessageGeneral)2 AnswerList (org.cerberus.util.answer.AnswerList)2 JSONException (org.json.JSONException)2