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

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

Source:BatchInvariantDAO.java Github

copy

Full Screen

...67 preStat.setString(1, batch);68 ResultSet resultSet = preStat.executeQuery();69 try {70 if (resultSet.first()) {71 result = loadFromResultSet(resultSet);72 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);73 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));74 ans.setItem(result);75 } else {76 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);77 }78 } catch (SQLException exception) {79 LOG.error("Unable to execute query : " + exception.toString());80 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);81 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));82 } finally {83 resultSet.close();84 }85 } catch (SQLException exception) {86 LOG.error("Unable to execute query : " + exception.toString());87 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);88 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));89 } finally {90 preStat.close();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 } finally {97 try {98 if (connection != null) {99 connection.close();100 }101 } catch (SQLException exception) {102 LOG.warn("Unable to close connection : " + exception.toString());103 }104 }105 //sets the message106 ans.setResultMessage(msg);107 return ans;108 }109 @Override110 public AnswerList<BatchInvariant> readBySystemByCriteria(List<String> system, int start, int amount, String column, String dir, String searchTerm, Map<String, List<String>> individualSearch) {111 AnswerList<BatchInvariant> response = new AnswerList<>();112 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);113 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));114 List<BatchInvariant> objectList = new ArrayList<BatchInvariant>();115 StringBuilder searchSQL = new StringBuilder();116 List<String> individalColumnSearchValues = new ArrayList<String>();117 StringBuilder query = new StringBuilder();118 //SQL_CALC_FOUND_ROWS allows to retrieve the total number of columns by disrearding the limit clauses that 119 //were applied -- used for pagination p120 query.append("SELECT SQL_CALC_FOUND_ROWS * FROM batchinvariant a ");121 searchSQL.append(" where 1=1 ");122 if (!StringUtil.isNullOrEmpty(searchTerm)) {123 searchSQL.append(" and (`system` like ?");124 searchSQL.append(" or `batch` like ?");125 searchSQL.append(" or `description` like ?)");126 }127 if (individualSearch != null && !individualSearch.isEmpty()) {128 searchSQL.append(" and ( 1=1 ");129 for (Map.Entry<String, List<String>> entry : individualSearch.entrySet()) {130 searchSQL.append(" and ");131 searchSQL.append(SqlUtil.getInSQLClauseForPreparedStatement(entry.getKey(), entry.getValue()));132 individalColumnSearchValues.addAll(entry.getValue());133 }134 searchSQL.append(" )");135 }136 if (system != null && !system.isEmpty()) {137 searchSQL.append(" and ");138 searchSQL.append(SqlUtil.generateInClause("`System`", system));139 }140 query.append(searchSQL);141 if (!StringUtil.isNullOrEmpty(column)) {142 query.append(" order by `").append(column).append("` ").append(dir);143 }144 if ((amount <= 0) || (amount >= MAX_ROW_SELECTED)) {145 query.append(" limit ").append(start).append(" , ").append(MAX_ROW_SELECTED);146 } else {147 query.append(" limit ").append(start).append(" , ").append(amount);148 }149 // Debug message on SQL.150 if (LOG.isDebugEnabled()) {151 LOG.debug("SQL : " + query.toString());152 }153 Connection connection = this.databaseSpring.connect();154 try {155 PreparedStatement preStat = connection.prepareStatement(query.toString());156 try {157 int i = 1;158 if (!StringUtil.isNullOrEmpty(searchTerm)) {159 preStat.setString(i++, "%" + searchTerm + "%");160 preStat.setString(i++, "%" + searchTerm + "%");161 preStat.setString(i++, "%" + searchTerm + "%");162 }163 for (String individualColumnSearchValue : individalColumnSearchValues) {164 preStat.setString(i++, individualColumnSearchValue);165 }166 if (system != null && !system.isEmpty()) {167 for (String sys : system) {168 preStat.setString(i++, sys);169 }170 }171 ResultSet resultSet = preStat.executeQuery();172 try {173 //gets the data174 while (resultSet.next()) {175 objectList.add(this.loadFromResultSet(resultSet));176 }177 //get the total number of rows178 resultSet = preStat.executeQuery("SELECT FOUND_ROWS()");179 int nrTotalRows = 0;180 if (resultSet != null && resultSet.next()) {181 nrTotalRows = resultSet.getInt(1);182 }183 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.184 LOG.error("Partial Result in the query.");185 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING_PARTIAL_RESULT);186 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Maximum row reached : " + MAX_ROW_SELECTED));187 response = new AnswerList<>(objectList, nrTotalRows);188 } else if (objectList.size() <= 0) {189 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);190 response = new AnswerList<>(objectList, nrTotalRows);191 } else {192 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);193 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));194 response = new AnswerList<>(objectList, nrTotalRows);195 }196 } catch (SQLException exception) {197 LOG.error("Unable to execute query : " + exception.toString());198 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);199 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));200 } finally {201 if (resultSet != null) {202 resultSet.close();203 }204 }205 } catch (SQLException exception) {206 LOG.error("Unable to execute query : " + exception.toString());207 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);208 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));209 } finally {210 if (preStat != null) {211 preStat.close();212 }213 }214 } catch (SQLException exception) {215 LOG.error("Unable to execute query : " + exception.toString());216 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);217 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));218 } finally {219 try {220 if (!this.databaseSpring.isOnTransaction()) {221 if (connection != null) {222 connection.close();223 }224 }225 } catch (SQLException exception) {226 LOG.warn("Unable to close connection : " + exception.toString());227 }228 }229 response.setResultMessage(msg);230 response.setDataList(objectList);231 return response;232 }233 @Override234 public Answer create(BatchInvariant object) {235 MessageEvent msg = null;236 StringBuilder query = new StringBuilder();237 query.append("INSERT INTO batchinvariant (`system`, `batch`, `description`) ");238 query.append("VALUES (?,?,?)");239 // Debug message on SQL.240 if (LOG.isDebugEnabled()) {241 LOG.debug("SQL : " + query.toString());242 }243 Connection connection = this.databaseSpring.connect();244 try {245 PreparedStatement preStat = connection.prepareStatement(query.toString());246 try {247 preStat.setString(1, object.getSystem());248 preStat.setString(2, object.getBatch());249 preStat.setString(3, object.getDescription());250 preStat.executeUpdate();251 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);252 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "INSERT"));253 } catch (SQLException exception) {254 LOG.error("Unable to execute query : " + exception.toString());255 if (exception.getSQLState().equals(SQL_DUPLICATED_CODE)) { //23000 is the sql state for duplicate entries256 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_DUPLICATE);257 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "INSERT").replace("%REASON%", exception.toString()));258 } else {259 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);260 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));261 }262 } finally {263 preStat.close();264 }265 } catch (SQLException exception) {266 LOG.error("Unable to execute query : " + exception.toString());267 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);268 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));269 } finally {270 try {271 if (connection != null) {272 connection.close();273 }274 } catch (SQLException exception) {275 LOG.error("Unable to close connection : " + exception.toString());276 }277 }278 return new Answer(msg);279 }280 @Override281 public Answer delete(BatchInvariant object) {282 MessageEvent msg = null;283 final String query = "DELETE FROM batchinvariant WHERE batch = ? ";284 // Debug message on SQL.285 if (LOG.isDebugEnabled()) {286 LOG.debug("SQL : " + query);287 }288 Connection connection = this.databaseSpring.connect();289 try {290 PreparedStatement preStat = connection.prepareStatement(query);291 try {292 preStat.setString(1, object.getBatch());293 preStat.executeUpdate();294 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);295 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "DELETE"));296 } catch (SQLException exception) {297 LOG.error("Unable to execute query : " + exception.toString());298 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);299 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));300 } finally {301 preStat.close();302 }303 } catch (SQLException exception) {304 LOG.error("Unable to execute query : " + exception.toString());305 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);306 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));307 } finally {308 try {309 if (connection != null) {310 connection.close();311 }312 } catch (SQLException exception) {313 LOG.warn("Unable to close connection : " + exception.toString());314 }315 }316 return new Answer(msg);317 }318 @Override319 public Answer update(String batch, BatchInvariant object) {320 MessageEvent msg = null;321 final String query = "UPDATE batchinvariant SET batch = ?, description = ?, System = ? WHERE batch = ?";322 // Debug message on SQL.323 if (LOG.isDebugEnabled()) {324 LOG.debug("SQL : " + query);325 }326 Connection connection = this.databaseSpring.connect();327 try {328 PreparedStatement preStat = connection.prepareStatement(query);329 try {330 int i = 1;331 preStat.setString(i++, object.getBatch());332 preStat.setString(i++, object.getDescription());333 preStat.setString(i++, object.getSystem());334 preStat.setString(i++, batch);335 preStat.executeUpdate();336 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);337 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "UPDATE"));338 } catch (SQLException exception) {339 LOG.error("Unable to execute query : " + exception.toString());340 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);341 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));342 } finally {343 preStat.close();344 }345 } catch (SQLException exception) {346 LOG.error("Unable to execute query : " + exception.toString());347 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);348 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));349 } finally {350 try {351 if (connection != null) {352 connection.close();353 }354 } catch (SQLException exception) {355 LOG.warn("Unable to close connection : " + exception.toString());356 }357 }358 return new Answer(msg);359 }360 @Override361 public AnswerList<String> readDistinctValuesByCriteria(List<String> system, String searchTerm, Map<String, List<String>> individualSearch, String columnName) {362 AnswerList<String> answer = new AnswerList<>();363 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);364 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));365 List<String> distinctValues = new ArrayList<>();366 StringBuilder searchSQL = new StringBuilder();367 List<String> individalColumnSearchValues = new ArrayList<String>();368 StringBuilder query = new StringBuilder();369 query.append("SELECT distinct ");370 query.append(columnName);371 query.append(" as distinctValues FROM batchinvariant ");372 searchSQL.append("WHERE 1=1");373 if (system != null && !system.isEmpty()) {374 searchSQL.append(" and ");375 searchSQL.append(SqlUtil.generateInClause("`System`", system));376 }377 if (!StringUtil.isNullOrEmpty(searchTerm)) {378 searchSQL.append(" and (`system` like ?");379 searchSQL.append(" or `batch` like ?");380 searchSQL.append(" or `description` like ?)");381 }382 if (individualSearch != null && !individualSearch.isEmpty()) {383 searchSQL.append(" and ( 1=1 ");384 for (Map.Entry<String, List<String>> entry : individualSearch.entrySet()) {385 searchSQL.append(" and ");386 searchSQL.append(SqlUtil.getInSQLClauseForPreparedStatement(entry.getKey(), entry.getValue()));387 individalColumnSearchValues.addAll(entry.getValue());388 }389 searchSQL.append(" )");390 }391 query.append(searchSQL);392 query.append(" order by ").append(columnName).append(" asc");393 // Debug message on SQL.394 if (LOG.isDebugEnabled()) {395 LOG.debug("SQL : " + query.toString());396 }397 try (Connection connection = databaseSpring.connect();398 PreparedStatement preStat = connection.prepareStatement(query.toString());399 Statement stm = connection.createStatement();) {400 int i = 1;401 if (system != null && !system.isEmpty()) {402 for (String sys : system) {403 preStat.setString(i++, sys);404 }405 }406 if (!StringUtil.isNullOrEmpty(searchTerm)) {407 preStat.setString(i++, "%" + searchTerm + "%");408 preStat.setString(i++, "%" + searchTerm + "%");409 preStat.setString(i++, "%" + searchTerm + "%");410 }411 for (String individualColumnSearchValue : individalColumnSearchValues) {412 preStat.setString(i++, individualColumnSearchValue);413 }414 try (ResultSet resultSet = preStat.executeQuery();415 ResultSet rowSet = stm.executeQuery("SELECT FOUND_ROWS()");) {416 //gets the data417 while (resultSet.next()) {418 distinctValues.add(resultSet.getString("distinctValues") == null ? "" : resultSet.getString("distinctValues"));419 }420 int nrTotalRows = 0;421 if (rowSet != null && rowSet.next()) {422 nrTotalRows = rowSet.getInt(1);423 }424 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.425 LOG.error("Partial Result in the query.");426 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING_PARTIAL_RESULT);427 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Maximum row reached : " + MAX_ROW_SELECTED));428 answer = new AnswerList<>(distinctValues, nrTotalRows);429 } else if (distinctValues.size() <= 0) {430 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);431 answer = new AnswerList<>(distinctValues, nrTotalRows);432 } else {433 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);434 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));435 answer = new AnswerList<>(distinctValues, nrTotalRows);436 }437 } catch (SQLException exception) {438 LOG.error("Unable to execute query : " + exception.toString());439 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);440 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));441 }442 } catch (Exception e) {443 LOG.warn("Unable to execute query : " + e.toString());444 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION",445 e.toString());446 } finally {447 // We always set the result message448 answer.setResultMessage(msg);449 }450 answer.setResultMessage(msg);451 answer.setDataList(distinctValues);452 return answer;453 }454 private BatchInvariant loadFromResultSet(ResultSet rs) throws SQLException {455 String system = ParameterParserUtil.parseStringParam(rs.getString("System"), "");456 String batch = ParameterParserUtil.parseStringParam(rs.getString("Batch"), "");457 String description = ParameterParserUtil.parseStringParam(rs.getString("Description"), "");458 //TODO remove when working in test with mockito and autowired459 factoryBatchInvariant = new FactoryBatchInvariant();460 return factoryBatchInvariant.create(system, batch, description);461 }462}...

Full Screen

Full Screen

loadFromResultSet

Using AI Code Generation

copy

Full Screen

1 public void loadFromResultSet(ResultSet rs) throws SQLException {2 this.setId(rs.getLong("id"));3 this.setSystem(rs.getString("system"));4 this.setApplication(rs.getString("application"));5 this.setCountry(rs.getString("country"));6 this.setEnvironment(rs.getString("environment"));7 this.setBuild(rs.getString("build"));8 this.setRevision(rs.getString("revision"));9 this.setChain(rs.getString("chain"));10 this.setVersion(rs.getString("version"));11 this.setActive(rs.getString("active"));12 this.setDistribList(rs.getString("distribList"));13 this.setBuildChain(rs.getString("buildChain"));14 this.setVersionChain(rs.getString("versionChain"));15 this.setRevisionChain(rs.getString("revisionChain"));16 this.setChainStatus(rs.getString("chainStatus"));17 this.setChainDeployType(rs.getString("chainDeployType"));18 this.setChainComment(rs.getString("chainComment"));19 this.setChainLink(rs.getString("chainLink"));20 this.setChainTarget(rs.getString("chainTarget"));21 this.setChainOpt(rs.getString("chainOpt"));22 this.setChainInfo(rs.getString("chainInfo"));23 this.setChainOptLock(rs.getString("chainOptLock"));24 this.setChainOptLockInfo(rs.getString("chainOptLockInfo"));25 this.setChainOptLockUser(rs.getString("chainOptLockUser"));26 this.setChainOptLockDate(rs.getTimestamp("chainOptLockDate"));27 }28 public void loadFromResultSet(ResultSet rs) throws SQLException {29 this.setId(rs.getLong("id"));30 this.setSystem(rs.getString("system"));31 this.setApplication(rs.getString("application"));32 this.setCountry(rs.getString("country"));33 this.setEnvironment(rs.getString("environment"));34 this.setBuild(rs.getString("build"));35 this.setRevision(rs.getString("revision"));36 this.setChain(rs.getString("chain"));37 this.setVersion(rs.getString("version"));38 this.setActive(rs.getString("active"));39 this.setDistribList(rs.getString("distribList"));40 this.setBuildChain(rs.getString("buildChain"));41 this.setVersionChain(rs.getString("versionChain"));42 this.setRevisionChain(rs.getString("revisionChain"));43 this.setChainStatus(rs.getString("chainStatus"));44 this.setChainDeployType(rs.getString("chainDeployType"));45 this.setChainComment(rs.getString("chainComment"));

Full Screen

Full Screen

loadFromResultSet

Using AI Code Generation

copy

Full Screen

1this.invariantService = new org.cerberus.crud.service.impl.InvariantService();2this.batchInvariantDAO = new org.cerberus.crud.dao.impl.BatchInvariantDAO();3this.invariantService.setInvariantDAO(this.batchInvariantDAO);4this.batchInvariantDAO.loadFromResultSet(this.resultSet);5this.invariantService.convert(this.batchInvariantDAO);6this.invariantService = new org.cerberus.crud.service.impl.InvariantService();7this.invariantDAO = new org.cerberus.crud.dao.impl.InvariantDAO();8this.invariantService.setInvariantDAO(this.invariantDAO);9this.invariantDAO.loadFromResultSet(this.resultSet);10this.invariantService.convert(this.invariantDAO);11this.testCaseService = new org.cerberus.crud.service.impl.TestCaseService();12this.testCaseDAO = new org.cerberus.crud.dao.impl.TestCaseDAO();13this.testCaseService.setTestCaseDAO(this.testCaseDAO);14this.testCaseDAO.loadFromResultSet(this.resultSet);15this.testCaseService.convert(this.testCaseDAO);16this.testCaseStepService = new org.cerberus.crud.service.impl.TestCaseStepService();17this.testCaseStepDAO = new org.cerberus.crud.dao.impl.TestCaseStepDAO();18this.testCaseStepService.setTestCaseStepDAO(this.testCaseStepDAO);19this.testCaseStepDAO.loadFromResultSet(this.resultSet);20this.testCaseStepService.convert(this.testCaseStepDAO);21this.testCaseStepActionService = new org.cerberus.crud.service.impl.TestCaseStepActionService();22this.testCaseStepActionDAO = new org.cerberus.crud.dao.impl.TestCaseStepActionDAO();23this.testCaseStepActionService.setTestCaseStepActionDAO(this.testCaseStepActionDAO);24this.testCaseStepActionDAO.loadFromResultSet(this.resultSet);25this.testCaseStepActionService.convert(this.testCaseStepActionDAO);

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