Search in sources :

Example 16 with AnswerList

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

the class TestCaseExecutionDAO method readDistinctColumnByTag.

@Override
public AnswerList readDistinctColumnByTag(String tag, boolean env, boolean country, boolean browser, boolean app) {
    AnswerList answer = new AnswerList();
    StringBuilder query = new StringBuilder();
    StringBuilder distinct = new StringBuilder();
    int prev = 0;
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
    if (!(!env && !country && !app && !browser)) {
        if (env) {
            distinct.append("Environment");
            prev++;
        }
        if (country) {
            if (prev != 0) {
                prev = 0;
                distinct.append(",");
            }
            distinct.append("Country");
            prev++;
        }
        if (browser) {
            if (prev != 0) {
                prev = 0;
                distinct.append(",");
            }
            distinct.append("Browser");
            prev++;
        }
        if (app) {
            if (prev != 0) {
                prev = 0;
                distinct.append(",");
            }
            distinct.append("Application");
        }
        query.append("SELECT ");
        query.append(distinct.toString());
        query.append(" FROM testcaseexecution exe WHERE exe.tag = ? GROUP BY ");
        query.append(distinct.toString());
    } else {
        // If there is no distinct, select nothing
        query.append("SELECT * FROM testcaseexecution exe WHERE 1 = 0 AND exe.tag = ?");
    }
    Connection connection = this.databaseSpring.connect();
    List<TestCaseExecution> column = new ArrayList<TestCaseExecution>();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        preStat.setString(1, tag);
        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                while (resultSet.next()) {
                    TestCaseExecution tmp = new TestCaseExecution();
                    if (env) {
                        tmp.setEnvironment(resultSet.getString("Environment"));
                    } else {
                        tmp.setEnvironment("");
                    }
                    if (country) {
                        tmp.setCountry(resultSet.getString("Country"));
                    } else {
                        tmp.setCountry("");
                    }
                    if (browser) {
                        tmp.setBrowser(resultSet.getString("Browser"));
                    } else {
                        tmp.setBrowser("");
                    }
                    if (app) {
                        tmp.setApplication(resultSet.getString("Application"));
                    } else {
                        tmp.setApplication("");
                    }
                    column.add(tmp);
                }
                msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseExecution").replace("%OPERATION%", "SELECT"));
                answer = new AnswerList(column, column.size());
            } 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 retrieve the list of entries!"));
                column = null;
            } finally {
                if (resultSet != null) {
                    resultSet.close();
                }
            }
        } catch (SQLException ex) {
            LOG.warn("Unable to execute query : " + ex.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
            column = null;
        } finally {
            if (preStat != null) {
                preStat.close();
            }
        }
    } catch (SQLException ex) {
        LOG.warn(ex.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException ex) {
            LOG.warn("Unable to execute query : " + ex.toString());
        }
    }
    answer.setResultMessage(msg);
    return answer;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) IFactoryTestCaseExecution(org.cerberus.crud.factory.IFactoryTestCaseExecution) TestCaseExecution(org.cerberus.crud.entity.TestCaseExecution) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 17 with AnswerList

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

the class TestCaseExecutionDAO method readByTag.

@Override
public AnswerList readByTag(String tag) throws CerberusException {
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
    AnswerList answer = new AnswerList();
    final StringBuffer query = new StringBuffer();
    query.append("SELECT * FROM testcaseexecution exe ");
    query.append("left join testcase tec on exe.Test = tec.Test and exe.TestCase = tec.TestCase ");
    query.append("left join application as app on tec.application = app.application ");
    query.append("where 1=1 and exe.tag = ? ");
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
        LOG.debug("SQL.param.tag : " + tag);
    }
    List<TestCaseExecution> testCaseExecutionList = new ArrayList<TestCaseExecution>();
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        preStat.setString(1, tag);
        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                while (resultSet.next()) {
                    testCaseExecutionList.add(this.loadWithDependenciesFromResultSet(resultSet));
                }
                msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseExecution").replace("%OPERATION%", "SELECT"));
                answer.setTotalRows(testCaseExecutionList.size());
            } 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 retrieve the list of entries!"));
                testCaseExecutionList = null;
            } finally {
                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 retrieve the list of entries!"));
            testCaseExecutionList = null;
        } finally {
            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 retrieve the list of entries!"));
        testCaseExecutionList = null;
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
        }
    }
    answer.setResultMessage(msg);
    answer.setDataList(testCaseExecutionList);
    return answer;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) IFactoryTestCaseExecution(org.cerberus.crud.factory.IFactoryTestCaseExecution) TestCaseExecution(org.cerberus.crud.entity.TestCaseExecution) 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 18 with AnswerList

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

the class TestCaseExecutionDAO method readBySystemByVarious.

@Override
public AnswerList readBySystemByVarious(String system, List<String> testList, List<String> applicationList, List<String> projectList, List<String> tcstatusList, List<String> groupList, List<String> tcactiveList, List<String> priorityList, List<String> targetsprintList, List<String> targetrevisionList, List<String> creatorList, List<String> implementerList, List<String> buildList, List<String> revisionList, List<String> environmentList, List<String> countryList, List<String> browserList, List<String> tcestatusList, String ip, String port, String tag, String browserversion, String comment, String bugid, String ticket) {
    AnswerList answer = new AnswerList();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
    List<TestCaseExecution> tceList = new ArrayList<TestCaseExecution>();
    List<String> whereClauses = new LinkedList<String>();
    StringBuilder query = new StringBuilder();
    int paramNumber = 0;
    query.append(" select t.ID as statusExecutionID, t.* from ( ");
    query.append(" select exe.*, tec.*, app.* ");
    query.append(" from testcaseexecution exe ");
    query.append(" inner join testcase tec on exe.test = tec.test and exe.testcase = tec.testcase ");
    query.append(" inner join application app on exe.application = app.application ");
    String testClause = SqlUtil.generateInClause("exe.test", testList);
    if (!StringUtil.isNullOrEmpty(testClause)) {
        whereClauses.add(testClause);
    }
    String applicationClause = SqlUtil.generateInClause("exe.application", applicationList);
    if (!StringUtil.isNullOrEmpty(applicationClause)) {
        whereClauses.add(applicationClause);
    }
    String projectClause = SqlUtil.generateInClause("tec.project", projectList);
    if (!StringUtil.isNullOrEmpty(projectClause)) {
        whereClauses.add(projectClause);
    }
    // test case status: working, fully_implemented, ...
    String tcsClause = SqlUtil.generateInClause("exe.status", tcstatusList);
    if (!StringUtil.isNullOrEmpty(tcsClause)) {
        whereClauses.add(tcsClause);
    }
    // group
    String groupClause = SqlUtil.generateInClause("tec.group", groupList);
    if (!StringUtil.isNullOrEmpty(groupClause)) {
        whereClauses.add(groupClause);
    }
    // test case active
    String tcactiveClause = SqlUtil.generateInClause("tec.tcactive", tcactiveList);
    if (!StringUtil.isNullOrEmpty(tcactiveClause)) {
        whereClauses.add(tcactiveClause);
    }
    // test case active
    String priorityClause = SqlUtil.generateInClause("tec.Priority", priorityList);
    if (!StringUtil.isNullOrEmpty(priorityClause)) {
        whereClauses.add(priorityClause);
    }
    // target sprint
    String targetsprintClause = SqlUtil.generateInClause("tec.TargetBuild", targetsprintList);
    if (!StringUtil.isNullOrEmpty(targetsprintClause)) {
        whereClauses.add(targetsprintClause);
    }
    // target revision
    String targetrevisionClause = SqlUtil.generateInClause("tec.TargetRev", targetrevisionList);
    if (!StringUtil.isNullOrEmpty(targetrevisionClause)) {
        whereClauses.add(targetrevisionClause);
    }
    // creator
    String creatorClause = SqlUtil.generateInClause("tec.UsrCreated", creatorList);
    if (!StringUtil.isNullOrEmpty(creatorClause)) {
        whereClauses.add(creatorClause);
    }
    // implementer
    String implementerClause = SqlUtil.generateInClause("tec.Implementer", implementerList);
    if (!StringUtil.isNullOrEmpty(implementerClause)) {
        whereClauses.add(implementerClause);
    }
    // build
    String buildClause = SqlUtil.generateInClause("exe.Build", buildList);
    if (!StringUtil.isNullOrEmpty(buildClause)) {
        whereClauses.add(buildClause);
    }
    // revision
    String revisionClause = SqlUtil.generateInClause("exe.Revision", revisionList);
    if (!StringUtil.isNullOrEmpty(revisionClause)) {
        whereClauses.add(revisionClause);
    }
    // environment
    String environmentClause = SqlUtil.generateInClause("exe.Environment", environmentList);
    if (!StringUtil.isNullOrEmpty(environmentClause)) {
        whereClauses.add(environmentClause);
    }
    // country
    String countryClause = SqlUtil.generateInClause("exe.Country", countryList);
    if (!StringUtil.isNullOrEmpty(countryClause)) {
        whereClauses.add(countryClause);
    }
    // browser
    String browserClause = SqlUtil.generateInClause("exe.Browser", browserList);
    if (!StringUtil.isNullOrEmpty(browserClause)) {
        whereClauses.add(browserClause);
    }
    // test case execution
    String tcestatusClause = SqlUtil.generateInClause("exe.ControlStatus", tcestatusList);
    if (!StringUtil.isNullOrEmpty(tcestatusClause)) {
        whereClauses.add(tcestatusClause);
    }
    if (!StringUtil.isNullOrEmpty(system)) {
        whereClauses.add(" app.system like ? ");
    }
    if (!StringUtil.isNullOrEmpty(ip)) {
        whereClauses.add(" exe.IP like ? ");
    }
    if (!StringUtil.isNullOrEmpty(port)) {
        whereClauses.add(" exe.port like ? ");
    }
    if (!StringUtil.isNullOrEmpty(tag)) {
        whereClauses.add(" exe.tag like ? ");
    }
    if (!StringUtil.isNullOrEmpty(browserversion)) {
        whereClauses.add(" exe.browserfullversion like ? ");
    }
    if (!StringUtil.isNullOrEmpty(comment)) {
        whereClauses.add(" exe.comment like ? ");
    }
    if (!StringUtil.isNullOrEmpty(bugid)) {
        whereClauses.add(" tec.BugID like ? ");
    }
    if (!StringUtil.isNullOrEmpty(ticket)) {
        whereClauses.add(" tec.Ticket like ? ");
    }
    if (whereClauses.size() > 0) {
        query.append("where ");
        String joined = StringUtils.join(whereClauses, " and ");
        query.append(joined);
    }
    query.append(" order by exe.ID desc ");
    query.append(" ) as t group by t.test, t.testcase, t.environment, t.browser, t.country");
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        if (testList != null) {
            for (String param : testList) {
                preStat.setString(++paramNumber, param);
            }
        }
        if (applicationList != null) {
            for (String param : applicationList) {
                preStat.setString(++paramNumber, param);
            }
        }
        if (projectList != null) {
            for (String param : projectList) {
                preStat.setString(++paramNumber, param);
            }
        }
        if (tcstatusList != null) {
            for (String param : tcstatusList) {
                preStat.setString(++paramNumber, param);
            }
        }
        if (groupList != null) {
            for (String param : groupList) {
                preStat.setString(++paramNumber, param);
            }
        }
        if (tcactiveList != null) {
            for (String param : tcactiveList) {
                preStat.setString(++paramNumber, param);
            }
        }
        if (priorityList != null) {
            for (String param : priorityList) {
                preStat.setString(++paramNumber, param);
            }
        }
        if (targetsprintList != null) {
            for (String param : targetsprintList) {
                preStat.setString(++paramNumber, param);
            }
        }
        if (targetrevisionList != null) {
            for (String param : targetrevisionList) {
                preStat.setString(++paramNumber, param);
            }
        }
        if (creatorList != null) {
            for (String param : creatorList) {
                preStat.setString(++paramNumber, param);
            }
        }
        if (implementerList != null) {
            for (String param : implementerList) {
                preStat.setString(++paramNumber, param);
            }
        }
        if (buildList != null) {
            for (String param : buildList) {
                preStat.setString(++paramNumber, param);
            }
        }
        if (revisionList != null) {
            for (String param : revisionList) {
                preStat.setString(++paramNumber, param);
            }
        }
        // environment
        if (environmentList != null) {
            for (String param : environmentList) {
                preStat.setString(++paramNumber, param);
            }
        }
        // country
        if (countryList != null) {
            for (String param : countryList) {
                preStat.setString(++paramNumber, param);
            }
        }
        // browser
        if (browserList != null) {
            for (String param : browserList) {
                preStat.setString(++paramNumber, param);
            }
        }
        // controlstatus
        if (tcestatusList != null) {
            for (String param : tcestatusList) {
                preStat.setString(++paramNumber, param);
            }
        }
        if (!StringUtil.isNullOrEmpty(system)) {
            preStat.setString(++paramNumber, system);
        }
        if (!StringUtil.isNullOrEmpty(ip)) {
            preStat.setString(++paramNumber, "%" + ip + "%");
        }
        if (!StringUtil.isNullOrEmpty(port)) {
            preStat.setString(++paramNumber, "%" + port + "%");
        }
        if (!StringUtil.isNullOrEmpty(tag)) {
            preStat.setString(++paramNumber, "%" + tag + "%");
        }
        if (!StringUtil.isNullOrEmpty(browserversion)) {
            preStat.setString(++paramNumber, "%" + browserversion + "%");
        }
        if (!StringUtil.isNullOrEmpty(comment)) {
            preStat.setString(++paramNumber, "%" + comment + "%");
        }
        if (!StringUtil.isNullOrEmpty(bugid)) {
            preStat.setString(++paramNumber, "%" + bugid + "%");
        }
        if (!StringUtil.isNullOrEmpty(ticket)) {
            preStat.setString(++paramNumber, "%" + ticket + "%");
        }
        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                while (resultSet.next()) {
                    tceList.add(loadWithDependenciesFromResultSet(resultSet));
                }
                if (tceList.isEmpty()) {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                } else {
                    msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseExecution").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 retrieve the list of entries!"));
                tceList.clear();
            } finally {
                if (resultSet != null) {
                    resultSet.close();
                }
            }
        } catch (SQLException ex) {
            LOG.warn("Unable to execute query : " + ex.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
        } finally {
            if (preStat != null) {
                preStat.close();
            }
        }
    } catch (SQLException ex) {
        LOG.warn(ex.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException ex) {
            LOG.warn("Unable to execute query : " + ex.toString());
        }
    }
    answer.setTotalRows(tceList.size());
    answer.setDataList(tceList);
    answer.setResultMessage(msg);
    return answer;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) IFactoryTestCaseExecution(org.cerberus.crud.factory.IFactoryTestCaseExecution) TestCaseExecution(org.cerberus.crud.entity.TestCaseExecution) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) LinkedList(java.util.LinkedList) ResultSet(java.sql.ResultSet)

Example 19 with AnswerList

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

the class TestCaseExecutionDAO method findTagList.

@Override
public AnswerList findTagList(int tagnumber) {
    AnswerList response = new AnswerList();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
    List<String> list = null;
    StringBuilder query = new StringBuilder();
    query.append("SELECT DISTINCT exe.tag FROM testcaseexecution exe WHERE tag != ''");
    if (tagnumber != 0) {
        query.append("ORDER BY id desc LIMIT ");
        query.append(tagnumber);
    }
    query.append(";");
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                list = new ArrayList<String>();
                while (resultSet.next()) {
                    list.add(resultSet.getString("exe.tag"));
                }
                msg.setDescription(msg.getDescription().replace("%ITEM%", "TagList").replace("%OPERATION%", "SELECT"));
            } 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%", "Unable to retrieve the list of entries!"));
            } 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%", "Unable to retrieve the list of entries!"));
        } 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%", "Unable to retrieve the list of entries!"));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    response.setResultMessage(msg);
    response.setDataList(list);
    return response;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 20 with AnswerList

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

the class TestCaseExecutionDAO method readDistinctValuesByCriteria.

@Override
public AnswerList<List<String>> readDistinctValuesByCriteria(String system, String test, String searchParameter, Map<String, List<String>> individualSearch, String columnName) {
    AnswerList answer = new AnswerList();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    List<String> distinctValues = new ArrayList<>();
    List<String> individalColumnSearchValues = new ArrayList<String>();
    final StringBuffer query = new StringBuffer();
    query.append("SELECT distinct ");
    query.append(columnName);
    query.append(" as distinctValues FROM testcaseexecution exe ");
    query.append("where exe.`start`> '").append(DateUtil.getMySQLTimestampTodayDeltaMinutes(-360000)).append("' ");
    if (!StringUtil.isNullOrEmpty(searchParameter)) {
        query.append("and (exe.`id` like ? ");
        query.append(" or exe.`test` like ? ");
        query.append(" or exe.`testCase` like ? ");
        query.append(" or exe.`build` like ? ");
        query.append(" or exe.`revision` like ? ");
        query.append(" or exe.`environment` like ? ");
        query.append(" or exe.`country` like ? ");
        query.append(" or exe.`browser` like ? ");
        query.append(" or exe.`version` like ? ");
        query.append(" or exe.`platform` like ? ");
        query.append(" or exe.`browserfullversion` like ? ");
        query.append(" or exe.`start` like ? ");
        query.append(" or exe.`end` like ? ");
        query.append(" or exe.`controlstatus` like ? ");
        query.append(" or exe.`controlmessage` like ? ");
        query.append(" or exe.`application` like ? ");
        query.append(" or exe.`ip` like ? ");
        query.append(" or exe.`url` like ? ");
        query.append(" or exe.`port` like ? ");
        query.append(" or exe.`tag` like ? ");
        query.append(" or exe.`finished` like ? ");
        query.append(" or exe.`status` like ? ");
        query.append(" or exe.`crbversion` like ? ");
        query.append(" or exe.`executor` like ? ");
        query.append(" or exe.`screensize` like ? )");
    }
    if (individualSearch != null && !individualSearch.isEmpty()) {
        query.append(" and ( 1=1 ");
        for (Map.Entry<String, List<String>> entry : individualSearch.entrySet()) {
            query.append(" and ");
            query.append(SqlUtil.getInSQLClauseForPreparedStatement(entry.getKey(), entry.getValue()));
            individalColumnSearchValues.addAll(entry.getValue());
        }
        query.append(" ) ");
    }
    query.append(" order by ").append(columnName).append(" asc");
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
    }
    try (Connection connection = databaseSpring.connect();
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        Statement stm = connection.createStatement()) {
        int i = 1;
        if (!Strings.isNullOrEmpty(searchParameter)) {
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
            preStat.setString(i++, "%" + searchParameter + "%");
        }
        for (String individualColumnSearchValue : individalColumnSearchValues) {
            preStat.setString(i++, individualColumnSearchValue);
        }
        try (ResultSet resultSet = preStat.executeQuery();
            ResultSet rowSet = stm.executeQuery("SELECT FOUND_ROWS()")) {
            while (resultSet.next()) {
                distinctValues.add(resultSet.getString("distinctValues") == null ? "" : resultSet.getString("distinctValues"));
            }
            // get the total number of rows
            int nrTotalRows = 0;
            if (rowSet != null && rowSet.next()) {
                nrTotalRows = rowSet.getInt(1);
            }
            if (distinctValues.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));
                answer = new AnswerList(distinctValues, nrTotalRows);
            } else if (distinctValues.size() <= 0) {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                answer = new AnswerList(distinctValues, nrTotalRows);
            } else {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                answer = new AnswerList(distinctValues, 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()));
        }
    } catch (Exception e) {
        LOG.warn("Unable to execute query : " + e.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
    } finally {
        // We always set the result message
        answer.setResultMessage(msg);
    }
    answer.setResultMessage(msg);
    answer.setDataList(distinctValues);
    return answer;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) CerberusException(org.cerberus.exception.CerberusException) ResultSet(java.sql.ResultSet) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Map(java.util.Map)

Aggregations

AnswerList (org.cerberus.util.answer.AnswerList)258 ArrayList (java.util.ArrayList)197 MessageEvent (org.cerberus.engine.entity.MessageEvent)152 List (java.util.List)146 Connection (java.sql.Connection)120 PreparedStatement (java.sql.PreparedStatement)120 ResultSet (java.sql.ResultSet)120 SQLException (java.sql.SQLException)120 JSONObject (org.json.JSONObject)90 AnswerItem (org.cerberus.util.answer.AnswerItem)89 Map (java.util.Map)69 HashMap (java.util.HashMap)65 JSONArray (org.json.JSONArray)62 Statement (java.sql.Statement)35 CerberusException (org.cerberus.exception.CerberusException)20 Invariant (org.cerberus.crud.entity.Invariant)18 IInvariantService (org.cerberus.crud.service.IInvariantService)15 TestCase (org.cerberus.crud.entity.TestCase)14 LinkedHashMap (java.util.LinkedHashMap)11 TestCaseExecution (org.cerberus.crud.entity.TestCaseExecution)11