use of org.cerberus.util.answer.AnswerList in project cerberus-source by cerberustesting.
the class TestDataLibDAO method readDistinctValuesByCriteria.
@Override
public AnswerList<List<String>> readDistinctValuesByCriteria(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 testdatalib tdl ");
query.append("LEFT OUTER JOIN testdatalibdata tdd ON tdl.TestDataLibID=tdd.TestDataLibID and tdd.SubData='' ");
searchSQL.append("WHERE 1=1");
if (!StringUtil.isNullOrEmpty(searchTerm)) {
searchSQL.append(" and (tdl.`name` like ?");
searchSQL.append(" or tdl.`group` like ?");
searchSQL.append(" or tdl.`type` like ?");
searchSQL.append(" or tdl.`database` like ?");
searchSQL.append(" or tdl.`databaseUrl` like ?");
searchSQL.append(" or tdl.`script` like ?");
searchSQL.append(" or tdl.`servicepath` like ?");
searchSQL.append(" or tdl.`method` like ?");
searchSQL.append(" or tdl.`envelope` like ?");
searchSQL.append(" or tdl.`csvUrl` like ?");
searchSQL.append(" or tdl.`separator` like ?");
searchSQL.append(" or tdl.`description` like ?");
searchSQL.append(" or tdl.`system` like ?");
searchSQL.append(" or tdl.`environment` like ?");
searchSQL.append(" or tdl.`country` 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 (!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 + "%");
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 e) {
LOG.warn("Unable to execute query : " + e.toString());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.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;
}
use of org.cerberus.util.answer.AnswerList in project cerberus-source by cerberustesting.
the class TestDataLibDataDAO method readAll.
@Override
public AnswerList<TestDataLibData> readAll() {
AnswerList answerList = new AnswerList();
List<TestDataLibData> list = new ArrayList<TestDataLibData>();
MessageEvent msg;
final String query = "SELECT * FROM testdatalibdata";
// Debug message on SQL.
if (LOG.isDebugEnabled()) {
LOG.debug("SQL : " + query);
}
Connection connection = this.databaseSpring.connect();
try {
PreparedStatement preStat = connection.prepareStatement(query);
try {
ResultSet resultSet = preStat.executeQuery();
try {
while (resultSet.next()) {
list.add(this.loadFromResultSet(resultSet));
}
if (list.isEmpty()) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).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%", exception.toString()));
list.clear();
} 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()));
list.clear();
} 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()));
list.clear();
} finally {
try {
if (!this.databaseSpring.isOnTransaction()) {
if (connection != null) {
connection.close();
}
}
} catch (SQLException ex) {
LOG.warn("Unable to close connection : " + ex.toString());
}
}
answerList.setDataList(list);
answerList.setTotalRows(list.size());
answerList.setResultMessage(msg);
return answerList;
}
use of org.cerberus.util.answer.AnswerList in project cerberus-source by cerberustesting.
the class BuildRevisionParametersDAO method readByVarious1ByCriteria.
@Override
public AnswerList readByVarious1ByCriteria(String system, String application, String build, String revision, int start, int amount, String column, String dir, String searchTerm, Map<String, List<String>> individualSearch) {
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>();
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 buildrevisionparameters ");
searchSQL.append(" where 1=1 ");
if (!StringUtil.isNullOrEmpty(searchTerm)) {
searchSQL.append(" and (`id` like ?");
searchSQL.append(" or `Build` like ?");
searchSQL.append(" or `Revision` like ?");
searchSQL.append(" or `Release` like ?");
searchSQL.append(" or `Application` like ?");
searchSQL.append(" or `Project` like ?");
searchSQL.append(" or `TicketIDFixed` like ?");
searchSQL.append(" or `BugIDFixed` like ?");
searchSQL.append(" or `Link` like ?");
searchSQL.append(" or `ReleaseOwner` like ?");
searchSQL.append(" or `datecre` like ?");
searchSQL.append(" or `jenkinsbuildid` like ?");
searchSQL.append(" or `mavengroupid` like ?");
searchSQL.append(" or `mavenartifactid` like ?");
searchSQL.append(" or `repositoryurl` like ?");
searchSQL.append(" or `mavenversion` 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(" )");
}
if (!StringUtil.isNullOrEmpty(system)) {
searchSQL.append(" and application in (SELECT application FROM application WHERE `System` = ? )");
}
if (!StringUtil.isNullOrEmpty(application)) {
searchSQL.append(" and (`Application`= ? )");
}
if (!StringUtil.isNullOrEmpty(build)) {
searchSQL.append(" and (`Build`= ? )");
}
if (!StringUtil.isNullOrEmpty(revision)) {
searchSQL.append(" and (`Revision`= ? )");
}
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());
}
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 + "%");
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);
}
if (!StringUtil.isNullOrEmpty(system)) {
preStat.setString(i++, system);
}
if (!StringUtil.isNullOrEmpty(application)) {
preStat.setString(i++, application);
}
if (!StringUtil.isNullOrEmpty(build)) {
preStat.setString(i++, build);
}
if (!StringUtil.isNullOrEmpty(revision)) {
preStat.setString(i++, revision);
}
ResultSet resultSet = preStat.executeQuery();
try {
// gets the data
while (resultSet.next()) {
brpList.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 (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;
}
use of org.cerberus.util.answer.AnswerList in project cerberus-source by cerberustesting.
the class BuildRevisionParametersDAO method readMaxSVNReleasePerApplication.
@Override
public AnswerList readMaxSVNReleasePerApplication(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>();
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 Application, max(`rel1`) rel FROM (");
query.append("SELECT brp.Application, CAST(`Release` AS UNSIGNED) rel1 ");
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 last version is on the same build.
if (StringUtil.isNullOrEmpty(lastRevision)) {
// Same build and revision some 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 needs to be an svn number
query.append(" and `release` REGEXP '^-?[0-9]+$' ");
// We need to have a jenkinsBuildID
query.append(" and jenkinsbuildid is not null and jenkinsbuildid != '' ");
query.append(" ORDER BY brp.Application ) as al1 ");
query.append(" GROUP BY Application ORDER BY Application) as al ");
query.append("JOIN buildrevisionparameters brp ");
query.append(" ON brp.application=al.application and brp.release=al.rel and brp.build = ? ");
query.append(" JOIN application app ");
query.append(" ON app.application=al.application ");
query.append("WHERE app.`system` = ? ;");
// 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++, build);
preStat.setString(i++, system);
ResultSet resultSet = preStat.executeQuery();
try {
// gets the data
while (resultSet.next()) {
BuildRevisionParameters brpItem = this.loadFromResultSet(resultSet);
brpItem.setAppDeployType(resultSet.getString("app.deploytype"));
brpList.add(brpItem);
}
// 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;
}
use of org.cerberus.util.answer.AnswerList in project cerberus-source by cerberustesting.
the class BuildRevisionParametersDAO method readDistinctValuesByCriteria.
@Override
public AnswerList<List<String>> readDistinctValuesByCriteria(String system, 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 buildrevisionparameters ");
searchSQL.append("WHERE 1=1");
if (!StringUtil.isNullOrEmpty(system)) {
searchSQL.append(" and application in (SELECT application FROM application WHERE `System` = ? )");
}
if (!StringUtil.isNullOrEmpty(searchTerm)) {
searchSQL.append(" and (`id` like ?");
searchSQL.append(" or `Build` like ?");
searchSQL.append(" or `Revision` like ?");
searchSQL.append(" or `Release` like ?");
searchSQL.append(" or `Application` like ?");
searchSQL.append(" or `Project` like ?");
searchSQL.append(" or `TicketIDFixed` like ?");
searchSQL.append(" or `BugIDFixed` like ?");
searchSQL.append(" or `Link` like ?");
searchSQL.append(" or `ReleaseOwner` like ?");
searchSQL.append(" or `datecre` like ?");
searchSQL.append(" or `jenkinsbuildid` like ?");
searchSQL.append(" or `mavengroupid` like ?");
searchSQL.append(" or `mavenartifactid` like ?");
searchSQL.append(" or `repositoryurl` like ?");
searchSQL.append(" or `mavenversion` 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(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 + "%");
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"));
}
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;
}
Aggregations