Search in sources :

Example 11 with AnswerList

use of org.cerberus.util.answer.AnswerList 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 12 with AnswerList

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

the class TestCaseCountryPropertiesDAO method findTestCaseCountryPropertiesByValue1.

@Override
public AnswerList findTestCaseCountryPropertiesByValue1(int testDataLib, String name, String country, String propertyType) {
    AnswerList ansList = new AnswerList();
    MessageEvent rs;
    List<TestListDTO> listOfTests = new ArrayList<TestListDTO>();
    StringBuilder query = new StringBuilder();
    query.append("select count(*) as total, tccp.property, t.Test, tc.TestCase, t.Description as testDescription, tc.Description as testCaseDescription, tc.Application, ");
    query.append("tc.TcActive as Active, tc.`Group`, tc.UsrCreated, tc.`Status` ");
    query.append("from testcasecountryproperties tccp    ");
    query.append("inner join test t on t.test = tccp.test ");
    query.append("inner join testcase tc  on t.test = tccp.test  and t.test = tc.test ");
    query.append("inner join testdatalib tdl on tdl.`name` = tccp.value1  and ");
    query.append("(tccp.Country = tdl.Country or tdl.country='') and tccp.test = t.test and tccp.testcase = tc.testcase ");
    query.append("where tccp.`Type` LIKE ? and tdl.TestDataLibID = ? ");
    query.append("and tdl.`Name` LIKE ? and (tdl.Country = ? or tdl.country='') ");
    query.append("group by tccp.test, tccp.testcase, tccp.property ");
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            preStat.setString(1, propertyType);
            preStat.setInt(2, testDataLib);
            preStat.setString(3, name);
            preStat.setString(4, country);
            HashMap<String, TestListDTO> map = new HashMap<String, TestListDTO>();
            // the key is the test + ":" +testcasenumber
            HashMap<String, List<PropertyListDTO>> auxiliaryMap = new HashMap<String, List<PropertyListDTO>>();
            String key, test, testCase;
            ResultSet resultSet = preStat.executeQuery();
            try {
                while (resultSet.next()) {
                    TestListDTO testList;
                    TestCaseListDTO testCaseDTO;
                    List<PropertyListDTO> propertiesList;
                    test = resultSet.getString("Test");
                    testCase = resultSet.getString("TestCase");
                    // gets the info from test cases that match the desired information
                    if (map.containsKey(test)) {
                        testList = map.get(test);
                    } else {
                        testList = new TestListDTO();
                        testList.setDescription(resultSet.getString("testDescription"));
                        testList.setTest(test);
                    }
                    // TESTCASE
                    key = test + ":" + testCase;
                    if (!auxiliaryMap.containsKey(key)) {
                        // means that we must associate a new test case with a test
                        testCaseDTO = new TestCaseListDTO();
                        testCaseDTO.setTestCaseDescription(resultSet.getString("testCaseDescription"));
                        testCaseDTO.setTestCaseNumber(testCase);
                        testCaseDTO.setApplication(resultSet.getString("Application"));
                        testCaseDTO.setCreator(resultSet.getString("tc.UsrCreated"));
                        testCaseDTO.setStatus(resultSet.getString("Status"));
                        testCaseDTO.setGroup(resultSet.getString("Group"));
                        testCaseDTO.setIsActive(resultSet.getString("Active"));
                        testList.getTestCaseList().add(testCaseDTO);
                        map.put(test, testList);
                        propertiesList = new ArrayList<PropertyListDTO>();
                    } else {
                        propertiesList = auxiliaryMap.get(key);
                    }
                    PropertyListDTO prop = new PropertyListDTO();
                    prop.setNrCountries(resultSet.getInt("total"));
                    prop.setPropertyName(resultSet.getString("property"));
                    propertiesList.add(prop);
                    // stores the information about the properties
                    auxiliaryMap.put(key, propertiesList);
                }
                // assigns the list of tests retrieved by the query to the list
                listOfTests = new ArrayList<TestListDTO>(map.values());
                // assigns the list of properties to the correct testcaselist
                for (TestListDTO list : listOfTests) {
                    for (TestCaseListDTO cases : list.getTestCaseList()) {
                        cases.setPropertiesList(auxiliaryMap.get(list.getTest() + ":" + cases.getTestCaseNumber()));
                    }
                }
                if (listOfTests.isEmpty()) {
                    rs = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                } else {
                    rs = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    rs.setDescription(rs.getDescription().replace("%ITEM%", "List of Test Cases").replace("%OPERATION%", "Select"));
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
                rs = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
                rs.setDescription(rs.getDescription().replace("%DESCRIPTION%", "Unable to get the list of test cases."));
            } finally {
                if (resultSet != null) {
                    resultSet.close();
                }
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            rs = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            rs.setDescription(rs.getDescription().replace("%DESCRIPTION%", "Unable to get the list of test cases."));
        } finally {
            if (preStat != null) {
                preStat.close();
            }
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        rs = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        rs.setDescription(rs.getDescription().replace("%DESCRIPTION%", "Unable to get the list of test cases."));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    ansList.setResultMessage(rs);
    ansList.setDataList(listOfTests);
    return ansList;
}
Also used : PropertyListDTO(org.cerberus.dto.PropertyListDTO) AnswerList(org.cerberus.util.answer.AnswerList) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) TestCaseListDTO(org.cerberus.dto.TestCaseListDTO) TestListDTO(org.cerberus.dto.TestListDTO) ResultSet(java.sql.ResultSet) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with AnswerList

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

the class TestCaseDAO method findTestCaseByServiceByDataLib.

@Override
public AnswerList findTestCaseByServiceByDataLib(String service) {
    AnswerList ansList = new AnswerList();
    MessageEvent rs;
    List<TestListDTO> listOfTests = new ArrayList<TestListDTO>();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    List<TestCase> testCaseList = new ArrayList<TestCase>();
    final String sql = " select count(*) as total, t.Test, tc.TestCase, t.Description as testDescription, tc.Description as testCaseDescription, tc.Application," + "tc.TcActive as Active, tc.`Group`, tc.UsrCreated, tc.`Status` " + " from testcase tc INNER JOIN test t ON t.test = tc.test" + " INNER JOIN testcasecountryproperties tccp ON tccp.Test = t.Test AND tccp.TestCase = tc.TestCase" + " INNER JOIN testdatalib td ON td.Name = tccp.Value1 AND (tccp.Country = td.Country or td.country='') and tccp.test = t.test and tccp.testcase = tc.testcase" + " INNER JOIN appservice ser on ser.Service = td.Service" + " WHERE ser.Service = ?" + " group by tc.test, tc.TestCase";
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(sql.toString());
        try {
            preStat.setString(1, service);
            HashMap<String, TestListDTO> map = new HashMap<String, TestListDTO>();
            String key, test, testCase;
            ResultSet resultSet = preStat.executeQuery();
            try {
                while (resultSet.next()) {
                    TestListDTO testList;
                    TestCaseListDTO testCaseDTO;
                    test = resultSet.getString("Test");
                    testCase = resultSet.getString("TestCase");
                    if (map.containsKey(test)) {
                        testList = map.get(test);
                    } else {
                        testList = new TestListDTO();
                        testList.setDescription(resultSet.getString("testDescription"));
                        testList.setTest(test);
                    }
                    testCaseDTO = new TestCaseListDTO();
                    testCaseDTO.setTestCaseDescription(resultSet.getString("testCaseDescription"));
                    testCaseDTO.setTestCaseNumber(testCase);
                    testCaseDTO.setApplication(resultSet.getString("Application"));
                    testCaseDTO.setCreator(resultSet.getString("tc.UsrCreated"));
                    testCaseDTO.setStatus(resultSet.getString("Status"));
                    testCaseDTO.setGroup(resultSet.getString("Group"));
                    testCaseDTO.setIsActive(resultSet.getString("Active"));
                    testList.getTestCaseList().add(testCaseDTO);
                    map.put(test, testList);
                }
                listOfTests = new ArrayList<TestListDTO>(map.values());
                if (listOfTests.isEmpty()) {
                    rs = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                } else {
                    rs = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    rs.setDescription(rs.getDescription().replace("%ITEM%", "List of Test Cases").replace("%OPERATION%", "Select"));
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
                rs = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
                rs.setDescription(rs.getDescription().replace("%DESCRIPTION%", "Unable to get the list of test cases."));
            } finally {
                if (resultSet != null) {
                    resultSet.close();
                }
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            rs = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            rs.setDescription(rs.getDescription().replace("%DESCRIPTION%", "Unable to get the list of test cases."));
        } finally {
            if (preStat != null) {
                preStat.close();
            }
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        rs = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        rs.setDescription(rs.getDescription().replace("%DESCRIPTION%", "Unable to get the list of test cases."));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    ansList.setResultMessage(rs);
    ansList.setDataList(listOfTests);
    return ansList;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) TestCaseListDTO(org.cerberus.dto.TestCaseListDTO) TestListDTO(org.cerberus.dto.TestListDTO) TestCase(org.cerberus.crud.entity.TestCase) IFactoryTestCase(org.cerberus.crud.factory.IFactoryTestCase) ResultSet(java.sql.ResultSet)

Example 14 with AnswerList

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

the class TestCaseDAO method readDistinctValuesByCriteria.

@Override
public AnswerList<List<String>> readDistinctValuesByCriteria(String system, String test, String searchTerm, 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<>();
    StringBuilder searchSQL = new StringBuilder();
    List<String> individalColumnSearchValues = new ArrayList<String>();
    StringBuilder query = new StringBuilder();
    query.append("SELECT distinct ");
    query.append(columnName);
    query.append(" as distinctValues FROM testcase tec ");
    query.append(" LEFT OUTER JOIN testcaselabel tel on tec.test = tel.test AND tec.testcase = tel.testcase ");
    query.append(" LEFT OUTER JOIN label lab on tel.labelId = lab.id ");
    query.append(" LEFT OUTER JOIN application app on app.application = tec.application ");
    searchSQL.append("WHERE 1=1");
    if (!StringUtil.isNullOrEmpty(system)) {
        searchSQL.append(" AND app.`system` = ? ");
    }
    if (!StringUtil.isNullOrEmpty(test)) {
        searchSQL.append(" AND tec.`test` = ?");
    }
    if (!StringUtil.isNullOrEmpty(searchTerm)) {
        searchSQL.append(" and (tec.`testcase` like ?");
        searchSQL.append(" or tec.`test` like ?");
        searchSQL.append(" or tec.`application` like ?");
        searchSQL.append(" or tec.`project` like ?");
        searchSQL.append(" or tec.`usrCreated` like ?");
        searchSQL.append(" or tec.`usrModif` like ?");
        searchSQL.append(" or tec.`tcactive` like ?");
        searchSQL.append(" or tec.`status` like ?");
        searchSQL.append(" or tec.`group` like ?");
        searchSQL.append(" or tec.`priority` like ?");
        searchSQL.append(" or tec.`dateCreated` like ?");
        searchSQL.append(" or lab.`label` like ?");
        searchSQL.append(" or tec.`description` like ?)");
    }
    if (individualSearch != null && !individualSearch.isEmpty()) {
        searchSQL.append(" and ( 1=1 ");
        for (Map.Entry<String, List<String>> entry : individualSearch.entrySet()) {
            searchSQL.append(" and ");
            searchSQL.append(SqlUtil.getInSQLClauseForPreparedStatement(entry.getKey(), entry.getValue()));
            individalColumnSearchValues.addAll(entry.getValue());
        }
        searchSQL.append(" )");
    }
    query.append(searchSQL);
    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 (!StringUtil.isNullOrEmpty(system)) {
            preStat.setString(i++, system);
        }
        if (!StringUtil.isNullOrEmpty(test)) {
            preStat.setString(i++, test);
        }
        if (!Strings.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 + "%");
            preStat.setString(i++, "%" + searchTerm + "%");
            preStat.setString(i++, "%" + searchTerm + "%");
            preStat.setString(i++, "%" + searchTerm + "%");
            preStat.setString(i++, "%" + searchTerm + "%");
        }
        for (String individualColumnSearchValue : individalColumnSearchValues) {
            preStat.setString(i++, individualColumnSearchValue);
        }
        try (ResultSet resultSet = preStat.executeQuery();
            ResultSet rowSet = stm.executeQuery("SELECT FOUND_ROWS()")) {
            // gets the data
            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) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 15 with AnswerList

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

the class TestCaseDAO method readByTestByCriteria.

@Override
public AnswerList readByTestByCriteria(String system, String test, int start, int amount, String sortInformation, String searchTerm, Map<String, List<String>> individualSearch) {
    AnswerList answer = new AnswerList();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    List<TestCase> testCaseList = new ArrayList<TestCase>();
    StringBuilder searchSQL = new StringBuilder();
    List<String> individalColumnSearchValues = new ArrayList<String>();
    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 testcase tec ");
    query.append(" LEFT OUTER JOIN testcaselabel tel on tec.test = tel.test AND tec.testcase = tel.testcase ");
    query.append(" LEFT OUTER JOIN label lab on tel.labelId = lab.id ");
    query.append(" LEFT OUTER JOIN application app on app.application = tec.application ");
    searchSQL.append("WHERE 1=1");
    if (!StringUtil.isNullOrEmpty(system)) {
        searchSQL.append(" AND app.`system` = ? ");
    }
    if (!StringUtil.isNullOrEmpty(test)) {
        searchSQL.append(" AND tec.`test` = ?");
    }
    if (!StringUtil.isNullOrEmpty(searchTerm)) {
        searchSQL.append(" and (tec.`testcase` like ?");
        searchSQL.append(" or tec.`test` like ?");
        searchSQL.append(" or tec.`application` like ?");
        searchSQL.append(" or tec.`project` like ?");
        searchSQL.append(" or tec.`usrCreated` like ?");
        searchSQL.append(" or tec.`usrModif` like ?");
        searchSQL.append(" or tec.`tcactive` like ?");
        searchSQL.append(" or tec.`status` like ?");
        searchSQL.append(" or tec.`group` like ?");
        searchSQL.append(" or tec.`priority` like ?");
        searchSQL.append(" or tec.`dateCreated` like ?");
        searchSQL.append(" or tec.`description` like ?");
        searchSQL.append(" or lab.`label` like ?)");
    }
    if (individualSearch != null && !individualSearch.isEmpty()) {
        searchSQL.append(" and ( 1=1 ");
        for (Map.Entry<String, List<String>> entry : individualSearch.entrySet()) {
            searchSQL.append(" and ");
            searchSQL.append(SqlUtil.getInSQLClauseForPreparedStatement(entry.getKey(), entry.getValue()));
            individalColumnSearchValues.addAll(entry.getValue());
        }
        searchSQL.append(" )");
    }
    query.append(searchSQL);
    query.append(" group by tec.test, tec.testcase ");
    if (!StringUtil.isNullOrEmpty(sortInformation)) {
        query.append(" order by ").append(sortInformation);
    }
    if (amount != 0) {
        query.append(" limit ").append(start).append(" , ").append(amount);
    } else {
        query.append(" limit ").append(start).append(" , ").append(MAX_ROW_SELECTED);
    }
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            int i = 1;
            if (!StringUtil.isNullOrEmpty(system)) {
                preStat.setString(i++, system);
            }
            if (!StringUtil.isNullOrEmpty(test)) {
                preStat.setString(i++, test);
            }
            if (!Strings.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 + "%");
                preStat.setString(i++, "%" + searchTerm + "%");
                preStat.setString(i++, "%" + searchTerm + "%");
                preStat.setString(i++, "%" + searchTerm + "%");
                preStat.setString(i++, "%" + searchTerm + "%");
                preStat.setString(i++, "%" + searchTerm + "%");
            }
            for (String individualColumnSearchValue : individalColumnSearchValues) {
                preStat.setString(i++, individualColumnSearchValue);
            }
            ResultSet resultSet = preStat.executeQuery();
            try {
                // gets the data
                while (resultSet.next()) {
                    testCaseList.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 (testCaseList.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(testCaseList, nrTotalRows);
                } else if (testCaseList.size() <= 0) {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                    answer = new AnswerList(testCaseList, nrTotalRows);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                    answer = new AnswerList(testCaseList, 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());
        }
    }
    answer.setResultMessage(msg);
    answer.setDataList(testCaseList);
    return answer;
}
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) PreparedStatement(java.sql.PreparedStatement) TestCase(org.cerberus.crud.entity.TestCase) IFactoryTestCase(org.cerberus.crud.factory.IFactoryTestCase) ResultSet(java.sql.ResultSet) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) 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