Search in sources :

Example 6 with CountryEnvParam_log

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

the class FactoryCountryEnvParam_log method create.

@Override
public CountryEnvParam_log create(long id, String system, String country, String environment, String build, String revision, int chain, String description, Timestamp datecre, String creator) {
    CountryEnvParam_log newCountryEnvParamLog = new CountryEnvParam_log();
    newCountryEnvParamLog.setId(id);
    newCountryEnvParamLog.setSystem(system);
    newCountryEnvParamLog.setCountry(country);
    newCountryEnvParamLog.setEnvironment(environment);
    newCountryEnvParamLog.setBuild(build);
    newCountryEnvParamLog.setRevision(revision);
    newCountryEnvParamLog.setChain(chain);
    newCountryEnvParamLog.setDescription(description);
    newCountryEnvParamLog.setDatecre(datecre);
    newCountryEnvParamLog.setCreator(creator);
    return newCountryEnvParamLog;
}
Also used : IFactoryCountryEnvParam_log(org.cerberus.crud.factory.IFactoryCountryEnvParam_log) CountryEnvParam_log(org.cerberus.crud.entity.CountryEnvParam_log)

Example 7 with CountryEnvParam_log

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

the class CountryEnvParam_logDAO method readLastChanges.

@Override
public AnswerList readLastChanges(String system, String country, Integer nbdays, String envGp) {
    AnswerList response = new AnswerList();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    List<CountryEnvParam_log> objectList = new ArrayList<CountryEnvParam_log>();
    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 cl.* ");
    query.append("FROM countryenvparam_log cl  ");
    query.append("JOIN invariant i on i.value=cl.Environment and i.idname='ENVIRONMENT'  ");
    query.append("WHERE cl.country = ?  ");
    query.append(" and TO_DAYS(NOW()) - TO_DAYS(cl.datecre) <= ? and cl.build != '' and `System`= ? ");
    if (!(StringUtil.isNullOrEmpty(envGp))) {
        query.append(" and i.gp1 = ? ");
    }
    query.append(" ORDER BY cl.id desc ; ");
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
        LOG.debug("SQL.param : " + country);
        LOG.debug("SQL.param : " + nbdays);
        LOG.debug("SQL.param : " + system);
        LOG.debug("SQL.param : " + envGp);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            int i = 1;
            preStat.setString(i++, country);
            preStat.setInt(i++, nbdays);
            preStat.setString(i++, system);
            if (!(StringUtil.isNullOrEmpty(envGp))) {
                preStat.setString(i++, envGp);
            }
            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) PreparedStatement(java.sql.PreparedStatement) CountryEnvParam_log(org.cerberus.crud.entity.CountryEnvParam_log) IFactoryCountryEnvParam_log(org.cerberus.crud.factory.IFactoryCountryEnvParam_log)

Aggregations

CountryEnvParam_log (org.cerberus.crud.entity.CountryEnvParam_log)7 IFactoryCountryEnvParam_log (org.cerberus.crud.factory.IFactoryCountryEnvParam_log)5 AnswerList (org.cerberus.util.answer.AnswerList)4 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 MessageEvent (org.cerberus.engine.entity.MessageEvent)3 AnswerItem (org.cerberus.util.answer.AnswerItem)3 ICountryEnvParam_logService (org.cerberus.crud.service.ICountryEnvParam_logService)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Invariant (org.cerberus.crud.entity.Invariant)1 IInvariantService (org.cerberus.crud.service.IInvariantService)1