use of org.cerberus.crud.entity.TestDataLib in project cerberus-source by cerberustesting.
the class TestDataLibService method readINTERNALWithSubdataByCriteria.
@Override
public AnswerList<HashMap<String, String>> readINTERNALWithSubdataByCriteria(String dataName, String dataSystem, String dataCountry, String dataEnvironment, int rowLimit, String system) {
AnswerList answer = new AnswerList();
AnswerList answerData = new AnswerList();
MessageEvent msg;
List<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
// We start by calculating the max nb of row we can fetch. Either specified by rowLimit either defined by a parameter.
int maxSecurityFetch = 100;
try {
String maxSecurityFetch1 = parameterService.findParameterByKey("cerberus_testdatalib_fetchmax", system).getValue();
maxSecurityFetch = Integer.valueOf(maxSecurityFetch1);
} catch (CerberusException ex) {
LOG.error(ex);
}
int maxFetch = maxSecurityFetch;
if (rowLimit > 0 && rowLimit < maxSecurityFetch) {
maxFetch = rowLimit;
} else {
maxFetch = maxSecurityFetch;
}
answer = this.readByVariousByCriteria(dataName, dataSystem, dataEnvironment, dataCountry, "INTERNAL", 0, maxFetch, null, null, null, null);
List<TestDataLib> objectList = new ArrayList<TestDataLib>();
objectList = answer.getDataList();
for (TestDataLib tdl : objectList) {
answerData = testDataLibDataService.readByVarious(tdl.getTestDataLibID(), null, null, null);
List<TestDataLibData> objectDataList = new ArrayList<TestDataLibData>();
objectDataList = answerData.getDataList();
HashMap<String, String> row = new HashMap<String, String>();
for (TestDataLibData tdld : objectDataList) {
row.put(tdld.getSubData(), tdld.getValue());
}
row.put("TestDataLibID", String.valueOf(tdl.getTestDataLibID()));
result.add(row);
}
answer.setDataList(result);
return answer;
}
use of org.cerberus.crud.entity.TestDataLib in project cerberus-source by cerberustesting.
the class TestDataLibDAO method readAll.
@Override
public AnswerList readAll() {
AnswerList answer = new AnswerList();
MessageEvent msg;
List<TestDataLib> list = new ArrayList<TestDataLib>();
final String query = "SELECT * FROM testdatalib tdl" + " LEFT OUTER JOIN testdatalibdata tdd ON tdl.TestDataLibID = tdd.TestDataLibID and tdd.Subdata=''; ";
// 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();
list = new ArrayList<TestDataLib>();
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()));
} 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 ex) {
LOG.warn("Unable to close connection : " + ex.toString());
}
}
answer.setResultMessage(msg);
answer.setDataList(list);
answer.setTotalRows(list.size());
return answer;
}
use of org.cerberus.crud.entity.TestDataLib in project cerberus-source by cerberustesting.
the class TestDataLibDAO method readNameListByName.
@Override
public AnswerList readNameListByName(String testDataLibName, int limit, boolean like) {
AnswerList answer = new AnswerList();
MessageEvent msg;
List<TestDataLib> list = new ArrayList<TestDataLib>();
StringBuilder query = new StringBuilder();
query.append("SELECT * ").append("FROM testdatalib tdl ");
if (like) {
query.append(" WHERE `name` like ? ");
} else {
query.append(" WHERE `name` = ? ");
}
query.append(" limit ? ");
if ((limit <= 0) || (limit >= MAX_ROW_SELECTED)) {
limit = MAX_ROW_SELECTED;
}
// Debug message on SQL.
if (LOG.isDebugEnabled()) {
LOG.debug("SQL : " + query);
}
Connection connection = this.databaseSpring.connect();
try {
PreparedStatement preStat = connection.prepareStatement(query.toString());
if (like) {
preStat.setString(1, "%" + testDataLibName + "%");
} else {
preStat.setString(1, testDataLibName);
}
preStat.setInt(2, limit);
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()));
} 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 ex) {
LOG.warn("Unable to close connection : " + ex.toString());
}
}
answer.setDataList(list);
answer.setTotalRows(list.size());
answer.setResultMessage(msg);
return answer;
}
use of org.cerberus.crud.entity.TestDataLib in project cerberus-source by cerberustesting.
the class TestDataLibDAO method readByVariousByCriteria.
@Override
public AnswerList readByVariousByCriteria(String name, String system, String environment, String country, String type, int start, int amount, String column, String dir, String searchTerm, Map<String, List<String>> individualSearch) {
AnswerList answer = new AnswerList();
MessageEvent msg;
int nrTotalRows = 0;
List<TestDataLib> objectList = new ArrayList<>();
List<String> individalColumnSearchValues = new ArrayList<>();
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 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.`service` like ?");
searchSQL.append(" or tdl.`servicepath` like ?");
searchSQL.append(" or tdl.`method` like ?");
searchSQL.append(" or tdl.`envelope` like ?");
searchSQL.append(" or tdl.`databaseCsv` 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(" )");
}
if (name != null) {
searchSQL.append(" and tdl.`name` = ? ");
}
if (system != null) {
searchSQL.append(" and tdl.`system` = ? ");
}
if (environment != null) {
searchSQL.append(" and tdl.`environment` = ? ");
}
if (country != null) {
searchSQL.append(" and tdl.`country` = ? ");
}
if (!StringUtil.isNullOrEmpty(type)) {
searchSQL.append(" and tdl.`type` = ? ");
}
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).append(" ");
}
// Debug message on SQL.
if (LOG.isDebugEnabled()) {
LOG.debug("SQL : " + query);
LOG.debug("SQL.name : " + name);
LOG.debug("SQL.system : " + system);
LOG.debug("SQL.environment : " + environment);
LOG.debug("SQL.country : " + country);
LOG.debug("SQL.type : " + type);
}
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 + "%");
preStat.setString(i++, "%" + searchTerm + "%");
}
for (String individualColumnSearchValue : individalColumnSearchValues) {
preStat.setString(i++, individualColumnSearchValue);
}
if (name != null) {
preStat.setString(i++, name);
}
if (system != null) {
preStat.setString(i++, system);
}
if (environment != null) {
preStat.setString(i++, environment);
}
if (country != null) {
preStat.setString(i++, country);
}
if (!StringUtil.isNullOrEmpty(type)) {
preStat.setString(i++, type);
}
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()");
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));
} else if (objectList.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()));
objectList.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()));
} 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 ex) {
LOG.warn("Unable to close connection : " + ex.toString());
}
}
answer.setTotalRows(nrTotalRows);
answer.setResultMessage(msg);
answer.setDataList(objectList);
return answer;
}
use of org.cerberus.crud.entity.TestDataLib in project cerberus-source by cerberustesting.
the class TestDataLibDAO method readByNameBySystemByEnvironmentByCountry.
@Override
public AnswerItem readByNameBySystemByEnvironmentByCountry(String name, String system, String environment, String country) {
AnswerItem answer = new AnswerItem();
TestDataLib result = null;
MessageEvent msg;
final String query = new StringBuilder("SELECT * FROM testdatalib tdl").append(" LEFT OUTER JOIN testdatalibdata tdd ON tdl.TestDataLibID = tdd.TestDataLibID and tdd.Subdata='' ").append(" WHERE `name` LIKE ?").append(" and (`system` = ? or `system` = '')").append(" and (`environment` = ? or `environment` = '')").append(" and (`country` = ? or `country` = '')").append(" ORDER BY `name` DESC, system DESC, environment DESC, country DESC, tdl.TestDataLibID ASC").append(" LIMIT 1").toString();
// Debug message on SQL.
if (LOG.isDebugEnabled()) {
LOG.debug("SQL : " + query);
LOG.debug("SQL name : " + name);
LOG.debug("SQL system : " + system);
LOG.debug("SQL environment : " + environment);
LOG.debug("SQL country : " + country);
}
Connection connection = this.databaseSpring.connect();
try {
PreparedStatement preStat = connection.prepareStatement(query);
preStat.setString(1, name);
preStat.setString(2, ParameterParserUtil.returnEmptyStringIfNull(system));
preStat.setString(3, ParameterParserUtil.returnEmptyStringIfNull(environment));
preStat.setString(4, ParameterParserUtil.returnEmptyStringIfNull(country));
try {
ResultSet resultSet = preStat.executeQuery();
try {
if (resultSet.first()) {
result = this.loadFromResultSet(resultSet);
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
}
} 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 ex) {
LOG.warn("Unable to close connection : " + ex.toString());
}
}
answer.setItem(result);
answer.setResultMessage(msg);
return answer;
}
Aggregations