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

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

Source:RobotDAO.java Github

copy

Full Screen

...78 PreparedStatement preStat = connection.prepareStatement(query);) {79 preStat.setInt(1, robotid);80 try (ResultSet resultSet = preStat.executeQuery()) {81 if (resultSet.first()) {82 result = loadFromResultSet(resultSet);83 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);84 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));85 ans.setItem(result);86 } else {87 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);88 }89 }90 } catch (SQLException exception) {91 LOG.error("Unable to execute query : " + exception.toString());92 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);93 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));94 }95 //sets the message96 ans.setResultMessage(msg);97 return ans;98 }99 @Override100 public Robot readByKey(String robot) throws CerberusException {101 Robot result;102 final String query = "SELECT * FROM `robot` WHERE `robot` = ?";103 result = RequestDbUtils.executeQuery(databaseSpring, query,104 ps -> ps.setString(1, robot),105 rs -> loadFromResultSet(rs)106 );107 //sets the message108 return result;109 }110 @Override111 public AnswerList<Robot> readByRobotList(List<String> robotList) {112 return readByRobotList(robotList, null);113 }114 @Override115 public AnswerList<Robot> readByRobotList(List<String> robotList, String typeRobot) {116 AnswerList<Robot> response = new AnswerList<>();117 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);118 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));119 List<Robot> robotListResult = new ArrayList<Robot>();120 StringBuilder searchSQL = new StringBuilder();121 StringBuilder query = new StringBuilder();122 //SQL_CALC_FOUND_ROWS allows to retrieve the total number of columns by disrearding the limit clauses that 123 //were applied -- used for pagination p124 query.append("SELECT SQL_CALC_FOUND_ROWS * FROM robot rbt ");125 searchSQL.append(" where 1=1 ");126 if ((robotList != null) && (!robotList.isEmpty())) {127 searchSQL.append(" and (" + SqlUtil.generateInClause("rbt.`robot`", robotList) + ")");128 }129 query.append(searchSQL);130 if(! StringUtil.isNullOrEmpty(typeRobot)) {131 query.append(" and type=? ");132 }133 // Debug message on SQL.134 if (LOG.isDebugEnabled()) {135 LOG.debug("SQL : " + query.toString());136 }137 Connection connection = this.databaseSpring.connect();138 try {139 PreparedStatement preStat = connection.prepareStatement(query.toString());140 try {141 int i = 1;142 if ((robotList != null) && (!robotList.isEmpty())) {143 for (String myrobot : robotList) {144 preStat.setString(i++, myrobot);145 }146 }147 if(! StringUtil.isNullOrEmpty(typeRobot)) {148 preStat.setString(i++, typeRobot);149 }150 ResultSet resultSet = preStat.executeQuery();151 try {152 //gets the data153 while (resultSet.next()) {154 robotListResult.add(this.loadFromResultSet(resultSet));155 }156 //get the total number of rows157 resultSet = preStat.executeQuery("SELECT FOUND_ROWS()");158 int nrTotalRows = 0;159 if (resultSet != null && resultSet.next()) {160 nrTotalRows = resultSet.getInt(1);161 }162 if (robotListResult.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.163 LOG.error("Partial Result in the query.");164 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING_PARTIAL_RESULT);165 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Maximum row reached : " + MAX_ROW_SELECTED));166 response = new AnswerList<Robot>(robotListResult, nrTotalRows);167 } else if (robotListResult.size() <= 0) {168 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);169 response = new AnswerList<Robot>(robotListResult, nrTotalRows);170 } else {171 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);172 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));173 response = new AnswerList<Robot>(robotListResult, nrTotalRows);174 }175 } catch (SQLException exception) {176 LOG.error("Unable to execute query : " + exception.toString());177 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);178 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));179 } finally {180 if (resultSet != null) {181 resultSet.close();182 }183 }184 } catch (SQLException exception) {185 LOG.error("Unable to execute query : " + exception.toString());186 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);187 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));188 } finally {189 if (preStat != null) {190 preStat.close();191 }192 }193 } catch (SQLException exception) {194 LOG.error("Unable to execute query : " + exception.toString());195 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);196 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));197 } finally {198 try {199 if (!this.databaseSpring.isOnTransaction()) {200 if (connection != null) {201 connection.close();202 }203 }204 } catch (SQLException exception) {205 LOG.warn("Unable to close connection : " + exception.toString());206 }207 }208 response.setResultMessage(msg);209 response.setDataList(robotListResult);210 return response;211 }212 @Override213 public AnswerList<Robot> readByCriteria(int start, int amount, String column, String dir, String searchTerm, Map<String, List<String>> individualSearch) {214 AnswerList<Robot> response = new AnswerList<>();215 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);216 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));217 List<Robot> robotList = new ArrayList<Robot>();218 StringBuilder searchSQL = new StringBuilder();219 List<String> individalColumnSearchValues = new ArrayList<String>();220 StringBuilder query = new StringBuilder();221 //SQL_CALC_FOUND_ROWS allows to retrieve the total number of columns by disrearding the limit clauses that 222 //were applied -- used for pagination p223 query.append("SELECT SQL_CALC_FOUND_ROWS * FROM robot ");224 searchSQL.append(" where 1=1 ");225 if (!StringUtil.isNullOrEmpty(searchTerm)) {226 searchSQL.append(" and (`platform` like ?");227 searchSQL.append(" or `description` like ?");228 searchSQL.append(" or `robot` like ?");229 searchSQL.append(" or `browser` like ?");230 searchSQL.append(" or `useragent` like ?");231 searchSQL.append(" or `screensize` like ?");232 searchSQL.append(" or `robotdecli` like ?");233 searchSQL.append(" or `version` like ?)");234 }235 if (individualSearch != null && !individualSearch.isEmpty()) {236 searchSQL.append(" and ( 1=1 ");237 for (Map.Entry<String, List<String>> entry : individualSearch.entrySet()) {238 searchSQL.append(" and ");239 searchSQL.append(SqlUtil.getInSQLClauseForPreparedStatement(entry.getKey(), entry.getValue()));240 individalColumnSearchValues.addAll(entry.getValue());241 }242 searchSQL.append(" )");243 }244 query.append(searchSQL);245 if (!StringUtil.isNullOrEmpty(column)) {246 query.append(" order by `").append(column).append("` ").append(dir);247 }248 if ((amount <= 0) || (amount >= MAX_ROW_SELECTED)) {249 query.append(" limit ").append(start).append(" , ").append(MAX_ROW_SELECTED);250 } else {251 query.append(" limit ").append(start).append(" , ").append(amount);252 }253 // Debug message on SQL.254 if (LOG.isDebugEnabled()) {255 LOG.debug("SQL : " + query.toString());256 }257 Connection connection = this.databaseSpring.connect();258 try {259 PreparedStatement preStat = connection.prepareStatement(query.toString());260 try {261 int i = 1;262 if (!Strings.isNullOrEmpty(searchTerm)) {263 preStat.setString(i++, "%" + searchTerm + "%");264 preStat.setString(i++, "%" + searchTerm + "%");265 preStat.setString(i++, "%" + searchTerm + "%");266 preStat.setString(i++, "%" + searchTerm + "%");267 preStat.setString(i++, "%" + searchTerm + "%");268 preStat.setString(i++, "%" + searchTerm + "%");269 preStat.setString(i++, "%" + searchTerm + "%");270 preStat.setString(i++, "%" + searchTerm + "%");271 }272 for (String individualColumnSearchValue : individalColumnSearchValues) {273 preStat.setString(i++, individualColumnSearchValue);274 }275 ResultSet resultSet = preStat.executeQuery();276 try {277 //gets the data278 while (resultSet.next()) {279 robotList.add(this.loadFromResultSet(resultSet));280 }281 //get the total number of rows282 resultSet = preStat.executeQuery("SELECT FOUND_ROWS()");283 int nrTotalRows = 0;284 if (resultSet != null && resultSet.next()) {285 nrTotalRows = resultSet.getInt(1);286 }287 if (robotList.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.288 LOG.error("Partial Result in the query.");289 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING_PARTIAL_RESULT);290 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Maximum row reached : " + MAX_ROW_SELECTED));291 response = new AnswerList<Robot>(robotList, nrTotalRows);292 } else if (robotList.size() <= 0) {293 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);294 response = new AnswerList<Robot>(robotList, nrTotalRows);295 } else {296 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);297 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));298 response = new AnswerList<Robot>(robotList, nrTotalRows);299 }300 } catch (SQLException exception) {301 LOG.error("Unable to execute query : " + exception.toString());302 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);303 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));304 } finally {305 if (resultSet != null) {306 resultSet.close();307 }308 }309 } catch (SQLException exception) {310 LOG.error("Unable to execute query : " + exception.toString());311 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);312 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));313 } finally {314 if (preStat != null) {315 preStat.close();316 }317 }318 } catch (SQLException exception) {319 LOG.error("Unable to execute query : " + exception.toString());320 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);321 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));322 } finally {323 try {324 if (!this.databaseSpring.isOnTransaction()) {325 if (connection != null) {326 connection.close();327 }328 }329 } catch (SQLException exception) {330 LOG.warn("Unable to close connection : " + exception.toString());331 }332 }333 response.setResultMessage(msg);334 response.setDataList(robotList);335 return response;336 }337 @Override338 public Answer create(Robot robot) {339 MessageEvent msg = null;340 StringBuilder query = new StringBuilder();341 query.append("INSERT INTO robot (`robot`, `platform`,`browser`, `version`,`active` , `description`, `useragent`, `screensize`, `robotdecli`, `lbexemethod`, `type`) ");342 query.append("VALUES (?,?,?,?,?,?,?,?,?,?,?)");343 // Debug message on SQL.344 if (LOG.isDebugEnabled()) {345 LOG.debug("SQL : " + query.toString());346 }347 Connection connection = this.databaseSpring.connect();348 try {349 PreparedStatement preStat = connection.prepareStatement(query.toString());350 try {351 int i = 1;352 preStat.setString(i++, robot.getRobot());353 preStat.setString(i++, robot.getPlatform());354 preStat.setString(i++, robot.getBrowser());355 preStat.setString(i++, robot.getVersion());356 preStat.setString(i++, robot.getActive());357 preStat.setString(i++, robot.getDescription());358 preStat.setString(i++, robot.getUserAgent());359 preStat.setString(i++, robot.getScreenSize());360 preStat.setString(i++, robot.getRobotDecli());361 preStat.setString(i++, robot.getLbexemethod());362 preStat.setString(i++, robot.getType());363 preStat.executeUpdate();364 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);365 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "INSERT"));366 } catch (SQLException exception) {367 LOG.error("Unable to execute query : " + exception.toString(), exception);368 if (exception.getSQLState().equals(SQL_DUPLICATED_CODE)) { //23000 is the sql state for duplicate entries369 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_DUPLICATE);370 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "INSERT").replace("%REASON%", exception.toString()));371 } else {372 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);373 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));374 }375 } finally {376 preStat.close();377 }378 } catch (SQLException exception) {379 LOG.error("Unable to execute query : " + exception.toString());380 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);381 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));382 } finally {383 try {384 if (connection != null) {385 connection.close();386 }387 } catch (SQLException exception) {388 LOG.warn("Unable to close connection : " + exception.toString());389 }390 }391 return new Answer(msg);392 }393 @Override394 public Answer delete(Robot application) {395 MessageEvent msg = null;396 final String query = "DELETE FROM robot WHERE robotID = ? ";397 // Debug message on SQL.398 if (LOG.isDebugEnabled()) {399 LOG.debug("SQL : " + query);400 }401 Connection connection = this.databaseSpring.connect();402 try {403 PreparedStatement preStat = connection.prepareStatement(query);404 try {405 preStat.setInt(1, application.getRobotID());406 preStat.executeUpdate();407 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);408 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "DELETE"));409 } catch (SQLException exception) {410 LOG.error("Unable to execute query : " + exception.toString());411 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);412 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));413 } finally {414 preStat.close();415 }416 } catch (SQLException exception) {417 LOG.error("Unable to execute query : " + exception.toString());418 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);419 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));420 } finally {421 try {422 if (connection != null) {423 connection.close();424 }425 } catch (SQLException exception) {426 LOG.warn("Unable to close connection : " + exception.toString());427 }428 }429 return new Answer(msg);430 }431 @Override432 public Answer update(Robot robot) {433 MessageEvent msg = null;434 StringBuilder query = new StringBuilder();435 query.append("UPDATE robot SET robot= ? ,");436 query.append("platform = ?, browser = ? , version = ?, active=?, description = ?, useragent = ?, screensize = ?, robotdecli = ?, lbexemethod = ?, type = ?, dateModif = NOW() ");437 query.append("WHERE robotID = ?");438 // Debug message on SQL.439 if (LOG.isDebugEnabled()) {440 LOG.debug("SQL : " + query.toString());441 }442 Connection connection = this.databaseSpring.connect();443 try {444 PreparedStatement preStat = connection.prepareStatement(query.toString());445 try {446 int cpt = 1;447 preStat.setString(cpt++, robot.getRobot());448 preStat.setString(cpt++, robot.getPlatform());449 preStat.setString(cpt++, robot.getBrowser());450 preStat.setString(cpt++, robot.getVersion());451 preStat.setString(cpt++, robot.getActive());452 preStat.setString(cpt++, robot.getDescription());453 preStat.setString(cpt++, robot.getUserAgent());454 preStat.setString(cpt++, robot.getScreenSize());455 preStat.setString(cpt++, robot.getRobotDecli());456 preStat.setString(cpt++, robot.getLbexemethod());457 preStat.setString(cpt++, robot.getType());458 preStat.setInt(cpt++, robot.getRobotID());459 preStat.executeUpdate();460 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);461 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "UPDATE"));462 } catch (SQLException exception) {463 LOG.error("Unable to execute query : " + exception.toString(), exception);464 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);465 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));466 } finally {467 preStat.close();468 }469 } catch (SQLException exception) {470 LOG.error("Unable to execute query : " + exception.toString(), exception);471 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);472 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));473 } finally {474 try {475 if (connection != null) {476 connection.close();477 }478 } catch (SQLException exception) {479 LOG.warn("Unable to close connection : " + exception.toString());480 }481 }482 return new Answer(msg);483 }484 @Override485 public Robot loadFromResultSet(ResultSet rs) throws SQLException {486 Integer robotID = ParameterParserUtil.parseIntegerParam(rs.getString("robotID"), 0);487 String robot = ParameterParserUtil.parseStringParam(rs.getString("robot"), "");488 String platform = ParameterParserUtil.parseStringParam(rs.getString("platform"), "");489 String browser = ParameterParserUtil.parseStringParam(rs.getString("browser"), "");490 String version = ParameterParserUtil.parseStringParam(rs.getString("version"), "");491 String active = ParameterParserUtil.parseStringParam(rs.getString("active"), "");492 String lbexemethod = ParameterParserUtil.parseStringParam(rs.getString("lbexemethod"), "");493 String description = ParameterParserUtil.parseStringParam(rs.getString("description"), "");494 String userAgent = ParameterParserUtil.parseStringParam(rs.getString("useragent"), "");495 String screenSize = ParameterParserUtil.parseStringParam(rs.getString("screensize"), "");496 String robotDecli = ParameterParserUtil.parseStringParam(rs.getString("robotdecli"), "");497 String type = ParameterParserUtil.parseStringParam(rs.getString("type"), "");498 //TODO remove when working in test with mockito and autowired499 factoryRobot = new FactoryRobot();...

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