Search in sources :

Example 16 with BuildRevisionParameters

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

the class BuildRevisionParametersDAO method readNonSVNRelease.

@Override
public AnswerList readNonSVNRelease(String system, String build, String revision, String lastBuild, String lastRevision) {
    AnswerList response = new AnswerList();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    List<BuildRevisionParameters> brpList = new ArrayList<BuildRevisionParameters>();
    BuildRevisionParameters brpItem = null;
    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 * from ( ");
    query.append("SELECT distinct brp.Application, `Release` rel, link, max(id) maxid ");
    query.append(" FROM buildrevisionparameters brp ");
    query.append("JOIN application a on a.application = brp.application ");
    query.append("JOIN buildrevisioninvariant bri ON bri.versionname = brp.revision and bri.`system` = ? and bri.`level` = 2 ");
    query.append(" WHERE 1=1 ");
    query.append(" and a.`system` = ? ");
    query.append(" and brp.build = ? ");
    if (lastBuild.equalsIgnoreCase(build)) {
        if (StringUtil.isNullOrEmpty(lastRevision)) {
            // if lastRevision is not defined, we filter only the current content.
            // revision
            query.append(" and bri.seq = (select seq from buildrevisioninvariant where `system` = ? and `level` = 2 and `versionname` = ? ) ");
        } else {
            // 2 different revisions inside the same build, we take the content between the 2.
            // lastRevision
            query.append(" and bri.seq > (select seq from buildrevisioninvariant where `system` = ? and `level` = 2 and `versionname` = ? ) ");
        }
    }
    // revision
    query.append(" and bri.seq <= (select seq from buildrevisioninvariant where `system` = ? and `level` = 2 and `versionname` = ? )");
    // Release Link for instal instructions needs to exist.
    query.append("  and link is not null and length(trim(link))>0 ");
    query.append("   GROUP BY brp.Application, `Release`, link  ORDER BY Application, `Release`, link ");
    query.append(") as toto ");
    query.append(" JOIN buildrevisionparameters brp   ON brp.id = toto.maxid ");
    query.append(" JOIN buildrevisioninvariant bri1 on bri1.versionname = brp.build  and bri1.`system` = ?  and bri1.`level` = 1 ");
    query.append(" JOIN buildrevisioninvariant bri2 on bri2.versionname = brp.revision  and bri2.`system` = ?  and bri2.`level` = 2 ");
    query.append(" JOIN application app ");
    query.append(" ON app.application=toto.application ");
    query.append(" ORDER BY bri1.seq asc , bri2.seq asc , brp.Application asc;");
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
        LOG.debug("SQL.param.system : " + system);
        LOG.debug("SQL.param.build : " + build);
        LOG.debug("SQL.param.revision : " + revision);
        LOG.debug("SQL.param.lastBuild : " + lastBuild);
        LOG.debug("SQL.param.lastRevision : " + lastRevision);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            int i = 1;
            preStat.setString(i++, system);
            preStat.setString(i++, system);
            preStat.setString(i++, build);
            if (lastBuild.equalsIgnoreCase(build)) {
                if (StringUtil.isNullOrEmpty(lastRevision)) {
                    // if lastRevision is not defined, we filter only the current content.
                    preStat.setString(i++, system);
                    preStat.setString(i++, revision);
                } else {
                    // 2 different revisions inside the same build, we take the content between the 2.
                    preStat.setString(i++, system);
                    preStat.setString(i++, lastRevision);
                }
            }
            preStat.setString(i++, system);
            preStat.setString(i++, revision);
            preStat.setString(i++, system);
            preStat.setString(i++, system);
            ResultSet resultSet = preStat.executeQuery();
            try {
                // gets the data
                while (resultSet.next()) {
                    BuildRevisionParameters newBRP;
                    newBRP = factoryBuildRevisionParameters.create(ParameterParserUtil.parseIntegerParam(resultSet.getString("maxid"), 0), ParameterParserUtil.parseStringParam(resultSet.getString("build"), ""), ParameterParserUtil.parseStringParam(resultSet.getString("revision"), ""), ParameterParserUtil.parseStringParam(resultSet.getString("rel"), ""), ParameterParserUtil.parseStringParam(resultSet.getString("application"), ""), "", "", "", ParameterParserUtil.parseStringParam(resultSet.getString("link"), ""), "", "", null, null, null, null, null, null);
                    newBRP.setAppDeployType(ParameterParserUtil.parseStringParam(resultSet.getString("app.deploytype"), ""));
                    brpList.add(newBRP);
                }
                // 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 (brpList.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(brpList, nrTotalRows);
                } else if (brpList.size() <= 0) {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                    response = new AnswerList(brpList, nrTotalRows);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                    response = new AnswerList(brpList, 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(brpList);
    return response;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) BuildRevisionParameters(org.cerberus.crud.entity.BuildRevisionParameters) FactoryBuildRevisionParameters(org.cerberus.crud.factory.impl.FactoryBuildRevisionParameters) IFactoryBuildRevisionParameters(org.cerberus.crud.factory.IFactoryBuildRevisionParameters) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList)

Aggregations

BuildRevisionParameters (org.cerberus.crud.entity.BuildRevisionParameters)16 AnswerItem (org.cerberus.util.answer.AnswerItem)11 MessageEvent (org.cerberus.engine.entity.MessageEvent)10 IFactoryBuildRevisionParameters (org.cerberus.crud.factory.IFactoryBuildRevisionParameters)9 IBuildRevisionParametersService (org.cerberus.crud.service.IBuildRevisionParametersService)9 JSONObject (org.json.JSONObject)8 ArrayList (java.util.ArrayList)6 FactoryBuildRevisionParameters (org.cerberus.crud.factory.impl.FactoryBuildRevisionParameters)6 AnswerList (org.cerberus.util.answer.AnswerList)6 List (java.util.List)4 ILogEventService (org.cerberus.crud.service.ILogEventService)4 ApplicationContext (org.springframework.context.ApplicationContext)4 IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)3 CerberusException (org.cerberus.exception.CerberusException)3 Answer (org.cerberus.util.answer.Answer)3 JSONArray (org.json.JSONArray)3 PolicyFactory (org.owasp.html.PolicyFactory)3 IApplicationService (org.cerberus.crud.service.IApplicationService)2 BuildRevisionParametersService (org.cerberus.crud.service.impl.BuildRevisionParametersService)2