How to use loadFromResultSet method of org.cerberus.crud.dao.impl.TestCaseLabelDAO class

Best Cerberus-source code snippet using org.cerberus.crud.dao.impl.TestCaseLabelDAO.loadFromResultSet

Source:TestCaseLabelDAO.java Github

copy

Full Screen

...81 82 try(ResultSet resultSet = preStat.executeQuery();){83 //parse query84 if (resultSet.first()) {85 result = loadFromResultSet(resultSet, null);86 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);87 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));88 ans.setItem(result);89 } else {90 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);91 }92 }catch (SQLException exception) {93 LOG.error("Unable to execute query : " + exception.toString());94 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);95 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));96 } 97 } catch (Exception e) {98 LOG.warn("Unable to readByKey TestCaseLabel: " + e.getMessage());99 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION",100 e.toString());101 } finally {102 ans.setResultMessage(msg);103 }104 return ans;105 }106 @Override107 public AnswerItem<TestCaseLabel> readByKey(String test, String testCase, Integer labelId) {108 AnswerItem<TestCaseLabel> ans = new AnswerItem();109 TestCaseLabel result = null;110 final String query = "SELECT * FROM `testcaselabel` tel WHERE `labelid` = ? and `test` = ? and `testcase` = ?";111 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);112 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));113 // Debug message on SQL.114 if (LOG.isDebugEnabled()) {115 LOG.debug("SQL : " + query);116 LOG.debug("SQL.param.label : " + labelId);117 LOG.debug("SQL.param.test : " + test);118 LOG.debug("SQL.param.testcase : " + testCase);119 }120 try (Connection connection = databaseSpring.connect();121 PreparedStatement preStat = connection.prepareStatement(query)) {122 //prepare and execute query123 preStat.setInt(1, labelId);124 preStat.setString(2, test);125 preStat.setString(3, testCase);126 127 try(ResultSet resultSet = preStat.executeQuery();){128 //parse query129 if (resultSet.first()) {130 result = loadFromResultSet(resultSet, null);131 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);132 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));133 ans.setItem(result);134 } else {135 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);136 }137 }catch (SQLException exception) {138 LOG.error("Unable to execute query : " + exception.toString());139 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);140 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));141 } 142 } catch (Exception e) {143 LOG.warn("Unable to readByKey TestCaseLabel: " + e.getMessage());144 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION",145 e.toString());146 } finally {147 ans.setResultMessage(msg);148 }149 return ans;150 }151 @Override152 public AnswerList<List<TestCaseLabel>> readByCriteria(int start, int amount, String column, String dir, String searchTerm, Map<String, List<String>> individualSearch) {153 AnswerList response = new AnswerList();154 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);155 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));156 List<TestCaseLabel> objectList = new ArrayList<>();157 StringBuilder searchSQL = new StringBuilder();158 List<String> individalColumnSearchValues = new ArrayList<>();159 StringBuilder query = new StringBuilder();160 //SQL_CALC_FOUND_ROWS allows to retrieve the total number of columns by disrearding the limit clauses that 161 //were applied -- used for pagination p162 query.append("SELECT SQL_CALC_FOUND_ROWS * FROM testcaselabel tel");163 query.append(" LEFT OUTER JOIN label lab on lab.id = tel.labelId ");164 searchSQL.append(" where 1=1 ");165 if (!StringUtil.isNullOrEmpty(searchTerm)) {166 searchSQL.append(" and (tel.`id` like ?");167 searchSQL.append(" or tel.`test` like ?");168 searchSQL.append(" or tel.`testcase` like ?");169 searchSQL.append(" or tel.`labelid` like ?");170 searchSQL.append(" or tel.`usrCreated` like ?");171 searchSQL.append(" or tel.`dateCreated` like ?");172 searchSQL.append(" or tel.`usrModif` like ?");173 searchSQL.append(" or tel.`dateModif` like ?)");174 }175 if (individualSearch != null && !individualSearch.isEmpty()) {176 searchSQL.append(" and ( 1=1 ");177 for (Map.Entry<String, List<String>> entry : individualSearch.entrySet()) {178 searchSQL.append(" and ");179 searchSQL.append(SqlUtil.getInSQLClauseForPreparedStatement(entry.getKey(), entry.getValue()));180 individalColumnSearchValues.addAll(entry.getValue());181 }182 searchSQL.append(" )");183 }184 query.append(searchSQL);185 if (!StringUtil.isNullOrEmpty(column)) {186 query.append(" order by `").append(column).append("` ").append(dir);187 }188 if ((amount <= 0) || (amount >= MAX_ROW_SELECTED)) {189 query.append(" limit ").append(start).append(" , ").append(MAX_ROW_SELECTED);190 } else {191 query.append(" limit ").append(start).append(" , ").append(amount);192 }193 // Debug message on SQL.194 if (LOG.isDebugEnabled()) {195 LOG.debug("SQL : " + query.toString());196 }197 try (Connection connection = databaseSpring.connect();198 PreparedStatement preStat = connection.prepareStatement(query.toString());199 Statement stm = connection.createStatement();) {200 int i = 1;201 if (!StringUtil.isNullOrEmpty(searchTerm)) {202 preStat.setString(i++, "%" + searchTerm + "%");203 preStat.setString(i++, "%" + searchTerm + "%");204 preStat.setString(i++, "%" + searchTerm + "%");205 preStat.setString(i++, "%" + searchTerm + "%");206 preStat.setString(i++, "%" + searchTerm + "%");207 preStat.setString(i++, "%" + searchTerm + "%");208 preStat.setString(i++, "%" + searchTerm + "%");209 preStat.setString(i++, "%" + searchTerm + "%");210 }211 for (String individualColumnSearchValue : individalColumnSearchValues) {212 preStat.setString(i++, individualColumnSearchValue);213 }214 215 try(ResultSet resultSet = preStat.executeQuery();216 ResultSet rowSet = stm.executeQuery("SELECT FOUND_ROWS()");){217 //gets the data218 while (resultSet.next()) {219 Label label = labelDAO.loadFromResultSet(resultSet);220 objectList.add(this.loadFromResultSet(resultSet, label));221 }222 //get the total number of rows223 224 int nrTotalRows = 0;225 if (rowSet != null && rowSet.next()) {226 nrTotalRows = rowSet.getInt(1);227 }228 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.229 LOG.error("Partial Result in the query.");230 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING_PARTIAL_RESULT);231 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Maximum row reached : " + MAX_ROW_SELECTED));232 response = new AnswerList(objectList, nrTotalRows);233 } else if (objectList.size() <= 0) {234 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);235 response = new AnswerList(objectList, nrTotalRows);236 } else {237 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);238 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));239 response = new AnswerList(objectList, nrTotalRows);240 }241 response.setDataList(objectList);242 }catch (SQLException exception) {243 LOG.error("Unable to execute query : " + exception.toString());244 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);245 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));246 } 247 } catch (Exception e) {248 LOG.warn("Unable to readByCriteria TestCaseLabel: " + e.getMessage());249 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION",250 e.toString());251 } finally {252 response.setResultMessage(msg);253 }254 return response;255 }256 @Override257 public Answer create(TestCaseLabel object) {258 Answer response = new Answer();259 MessageEvent msg = null;260 StringBuilder query = new StringBuilder();261 query.append("INSERT INTO testcaselabel ( `test`, `testcase`, `labelId`, `usrCreated`) ");262 query.append("VALUES (?,?,?,?)");263 // Debug message on SQL.264 if (LOG.isDebugEnabled()) {265 LOG.debug("SQL : " + query.toString());266 }267 try (Connection connection = databaseSpring.connect();268 PreparedStatement preStat = connection.prepareStatement(query.toString())) {269 preStat.setString(1, object.getTest());270 preStat.setString(2, object.getTestcase());271 preStat.setInt(3, object.getLabelId());272 preStat.setString(4, object.getUsrCreated());273 preStat.executeUpdate();274 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);275 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "INSERT"));276 } catch (Exception e) {277 LOG.warn("Unable to create TestCaseLabel: " + e.getMessage());278 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION",279 e.toString());280 } finally {281 response.setResultMessage(msg);282 }283 return response;284 }285 @Override286 public Answer delete(TestCaseLabel object) {287 Answer response = new Answer();288 MessageEvent msg = null;289 final String query = "DELETE FROM testcaselabel WHERE id = ? ";290 // Debug message on SQL.291 if (LOG.isDebugEnabled()) {292 LOG.debug("SQL : " + query);293 }294 try (Connection connection = databaseSpring.connect();295 PreparedStatement preStat = connection.prepareStatement(query.toString())) {296 preStat.setInt(1, object.getId());297 preStat.executeUpdate();298 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);299 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "DELETE"));300 } catch (Exception e) {301 LOG.warn("Unable to delete TestCaseLabel: " + e.getMessage());302 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION",303 e.toString());304 } finally {305 response.setResultMessage(msg);306 }307 return response;308 }309 @Override310 public Answer update(TestCaseLabel object) {311 Answer response = new Answer();312 MessageEvent msg = null;313 final String query = "UPDATE testcaselabel SET `test` = ?, `testcase` = ?, `labelId` = ?, `usrModif` = ?, `dateModif` = NOW() WHERE id = ?";314 // Debug message on SQL.315 if (LOG.isDebugEnabled()) {316 LOG.debug("SQL : " + query);317 }318 try (Connection connection = databaseSpring.connect();319 PreparedStatement preStat = connection.prepareStatement(query.toString())) {320 preStat.setString(1, object.getTest());321 preStat.setString(2, object.getTestcase());322 preStat.setInt(3, object.getLabelId());323 preStat.setString(4, object.getUsrModif());324 preStat.setInt(5, object.getId());325 326 preStat.executeUpdate();327 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);328 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "UPDATE"));329 } catch (Exception e) {330 LOG.warn("Unable to update TestCaseLabel: " + e.getMessage());331 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION",332 e.toString());333 } finally {334 response.setResultMessage(msg);335 }336 return response;337 }338 @Override339 public TestCaseLabel loadFromResultSet(ResultSet rs, Label label) throws SQLException {340 Integer id = ParameterParserUtil.parseIntegerParam(rs.getString("tel.id"), 0);341 String test = ParameterParserUtil.parseStringParam(rs.getString("tel.test"), "");342 String testcase = ParameterParserUtil.parseStringParam(rs.getString("tel.testcase"), "");343 Integer labelId = ParameterParserUtil.parseIntegerParam(rs.getString("tel.labelId"), 0);344 String usrCreated = ParameterParserUtil.parseStringParam(rs.getString("tel.usrCreated"), "");345 Timestamp dateCreated = rs.getTimestamp("tel.dateCreated");346 String usrModif = ParameterParserUtil.parseStringParam(rs.getString("tel.usrModif"), "");347 Timestamp dateModif = rs.getTimestamp("tel.dateModif");348 return factoryTestCaseLabel.create(id, test, testcase, labelId, usrCreated, dateCreated, usrModif, dateModif, label);349 }350 @Override351 public AnswerList readByTestTestCase(String test, String testCase) {352 AnswerList response = new AnswerList();353 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);354 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));355 List<TestCaseLabel> objectList = new ArrayList<>();356 StringBuilder query = new StringBuilder();357 //SQL_CALC_FOUND_ROWS allows to retrieve the total number of columns by disrearding the limit clauses that 358 //were applied -- used for pagination p359 query.append("SELECT SQL_CALC_FOUND_ROWS * FROM testcaselabel tel ");360 query.append(" LEFT OUTER JOIN label lab on lab.id = tel.labelId ");361 362 query.append(" WHERE 1=1");363 if (!Strings.isNullOrEmpty(test)) {364 query.append(" AND tel.test = ?");365 }366 if (testCase != null) {367 query.append(" AND tel.testcase = ?");368 }369 // Debug message on SQL.370 if (LOG.isDebugEnabled()) {371 LOG.debug("SQL : " + query.toString());372 }373 try (Connection connection = databaseSpring.connect();374 PreparedStatement preStat = connection.prepareStatement(query.toString());375 Statement stm = connection.createStatement();) {376 int i = 1;377 if (!Strings.isNullOrEmpty(test)) {378 preStat.setString(i++, test);379 }380 if (testCase != null) {381 preStat.setString(i++, testCase);382 }383 384 try( ResultSet resultSet = preStat.executeQuery();385 ResultSet rowSet = stm.executeQuery("SELECT FOUND_ROWS()");){386 //gets the data387 while (resultSet.next()) {388 Label label = labelDAO.loadFromResultSet(resultSet);389 TestCaseLabel tcl = this.loadFromResultSet(resultSet, label);390 objectList.add(tcl);391 }392 //get the total number of rows393 int nrTotalRows = 0;394 if (rowSet != null && rowSet.next()) {395 nrTotalRows = rowSet.getInt(1);396 }397 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.398 LOG.error("Partial Result in the query.");399 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING_PARTIAL_RESULT);400 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Maximum row reached : " + MAX_ROW_SELECTED));401 response = new AnswerList(objectList, nrTotalRows);402 } else if (objectList.size() <= 0) {403 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Cerberus-source automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful