Best Cerberus-source code snippet using org.cerberus.crud.dao.impl.LogEventDAO.loadFromResultSet
Source:LogEventDAO.java
...74 preStat.setLong(1, logEventID);75 ResultSet resultSet = preStat.executeQuery();76 try {77 if (resultSet.first()) {78 result = loadFromResultSet(resultSet);79 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);80 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));81 ans.setItem(result);82 } else {83 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);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 resultSet.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 preStat.close();98 }99 } catch (SQLException exception) {100 LOG.error("Unable to execute query : " + exception.toString());101 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);102 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));103 } finally {104 try {105 if (connection != null) {106 connection.close();107 }108 } catch (SQLException exception) {109 LOG.warn("Unable to close connection : " + exception.toString());110 }111 }112 //sets the message113 ans.setResultMessage(msg);114 return ans;115 }116 @Override117 public AnswerList readByCriteria(int start, int amount, String colName, String dir, String searchTerm, Map<String, List<String>> individualSearch) {118 AnswerList response = new AnswerList();119 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);120 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));121 List<LogEvent> logEventList = new ArrayList<LogEvent>();122 StringBuilder searchSQL = new StringBuilder();123 List<String> individalColumnSearchValues = new ArrayList<String>();124 final StringBuilder query = new StringBuilder();125 //SQL_CALC_FOUND_ROWS allows to retrieve the total number of columns by disrearding the limit clauses that 126 //were applied -- used for pagination p127 query.append("SELECT SQL_CALC_FOUND_ROWS * FROM logevent ");128 searchSQL.append(" where 1=1 ");129 if (!StringUtil.isNullOrEmpty(searchTerm)) {130 searchSQL.append(" and (`time` like ?");131 searchSQL.append(" or `login` like ?");132 searchSQL.append(" or `page` like ?");133 searchSQL.append(" or `action` like ?");134 searchSQL.append(" or `log` like ? )");135 }136 if (individualSearch != null && !individualSearch.isEmpty()) {137 searchSQL.append(" and ( 1=1 ");138 for (Map.Entry<String, List<String>> entry : individualSearch.entrySet()) {139 searchSQL.append(" and ");140 searchSQL.append(SqlUtil.getInSQLClauseForPreparedStatement(entry.getKey(), entry.getValue()));141 individalColumnSearchValues.addAll(entry.getValue());142 }143 searchSQL.append(" )");144 }145 query.append(searchSQL);146 if (!StringUtil.isNullOrEmpty(colName)) {147 query.append("order by `").append(colName).append("` ").append(dir);148 }else{149 query.append("order by `logEventID` desc");150 }151 if ((amount <= 0) || (amount >= MAX_ROW_SELECTED)) {152 query.append(" limit ").append(start).append(" , ").append(MAX_ROW_SELECTED);153 } else {154 query.append(" limit ").append(start).append(" , ").append(amount);155 }156 // Debug message on SQL.157 if (LOG.isDebugEnabled()) {158 LOG.debug("SQL : " + query.toString());159 }160 Connection connection = this.databaseSpring.connect();161 try {162 PreparedStatement preStat = connection.prepareStatement(query.toString());163 try {164 int i = 1;165 if (!StringUtil.isNullOrEmpty(searchTerm)) {166 preStat.setString(i++, "%" + searchTerm + "%");167 preStat.setString(i++, "%" + searchTerm + "%");168 preStat.setString(i++, "%" + searchTerm + "%");169 preStat.setString(i++, "%" + searchTerm + "%");170 preStat.setString(i++, "%" + searchTerm + "%");171 }172 for (String individualColumnSearchValue : individalColumnSearchValues) {173 preStat.setString(i++, individualColumnSearchValue);174 }175 ResultSet resultSet = preStat.executeQuery();176 try {177 //gets the data178 while (resultSet.next()) {179 logEventList.add(this.loadFromResultSet(resultSet));180 }181 //get the total number of rows182 resultSet = preStat.executeQuery("SELECT FOUND_ROWS()");183 int nrTotalRows = 0;184 if (resultSet != null && resultSet.next()) {185 nrTotalRows = resultSet.getInt(1);186 }187 if (logEventList.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.188 LOG.error("Partial Result in the query.");189 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING_PARTIAL_RESULT);190 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Maximum row reached : " + MAX_ROW_SELECTED));191 response = new AnswerList(logEventList, nrTotalRows);192 } else if (logEventList.size() <= 0) {193 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);194 response = new AnswerList(logEventList, nrTotalRows);195 } else {196 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);197 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));198 response = new AnswerList(logEventList, nrTotalRows);199 }200 } catch (SQLException exception) {201 LOG.error("Unable to execute query : " + exception.toString());202 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);203 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));204 } finally {205 if (resultSet != null) {206 resultSet.close();207 }208 }209 } catch (SQLException exception) {210 LOG.error("Unable to execute query : " + exception.toString());211 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);212 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));213 } finally {214 if (preStat != null) {215 preStat.close();216 }217 }218 } catch (SQLException exception) {219 LOG.error("Unable to execute query : " + exception.toString());220 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);221 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));222 } finally {223 try {224 if (!this.databaseSpring.isOnTransaction()) {225 if (connection != null) {226 connection.close();227 }228 }229 } catch (SQLException exception) {230 LOG.warn("Unable to close connection : " + exception.toString());231 }232 }233 response.setResultMessage(msg);234 response.setDataList(logEventList);235 return response;236 }237 @Override238 public Answer create(LogEvent logevent) {239 MessageEvent msg = null;240 StringBuilder query = new StringBuilder();241 query.append("INSERT INTO logevent (userID, Login, Page, Action, Log, remoteIP, localIP) ");242 query.append("VALUES (?, ?, ?, ?, ?, ?, ?)");243 // Debug message on SQL.244 if (LOG.isDebugEnabled()) {245 LOG.debug("SQL : " + query.toString());246 }247 Connection connection = this.databaseSpring.connect();248 try {249 PreparedStatement preStat = connection.prepareStatement(query.toString());250 try {251 preStat.setLong(1, logevent.getUserID());252 preStat.setString(2, logevent.getLogin());253 preStat.setString(3, logevent.getPage());254 preStat.setString(4, logevent.getAction());255 preStat.setString(5, StringUtils.left(logevent.getLog(), 500));256 preStat.setString(6, logevent.getremoteIP());257 preStat.setString(7, logevent.getLocalIP());258 preStat.executeUpdate();259 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);260 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "INSERT"));261 } catch (SQLException exception) {262 LOG.error("Unable to execute query : " + exception.toString());263 if (exception.getSQLState().equals(SQL_DUPLICATED_CODE)) { //23000 is the sql state for duplicate entries264 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_DUPLICATE);265 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "INSERT").replace("%REASON%", exception.toString()));266 } else {267 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);268 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));269 }270 } finally {271 preStat.close();272 }273 } catch (SQLException exception) {274 LOG.error("Unable to execute query : " + exception.toString());275 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);276 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));277 } finally {278 try {279 if (connection != null) {280 connection.close();281 }282 } catch (SQLException exception) {283 LOG.warn("Unable to execute query : " + exception.toString());284 }285 }286 return new Answer(msg);287 }288 @Override289 public LogEvent loadFromResultSet(ResultSet resultSet) throws SQLException {290 long logEventID = resultSet.getLong("logEventID") == 0 ? 0 : resultSet.getLong("logEventID");291 long userID = resultSet.getLong("userID") == 0 ? 0 : resultSet.getLong("userID");292 String login = resultSet.getString("login") == null ? "" : resultSet.getString("login");293 Timestamp time = resultSet.getTimestamp("time");294 String page = resultSet.getString("page") == null ? "" : resultSet.getString("page");295 String action = resultSet.getString("action") == null ? "" : resultSet.getString("action");296 String log = resultSet.getString("log") == null ? "" : resultSet.getString("log");297 String remoteIP = resultSet.getString("remoteIP") == null ? "" : resultSet.getString("remoteIP");298 String localIP = resultSet.getString("localIP") == null ? "" : resultSet.getString("localIP");299 return factoryLogEvent.create(logEventID, userID, login, time, page, action, log, remoteIP, localIP);300 }301 @Override302 public AnswerList<List<String>> readDistinctValuesByCriteria(String searchTerm, Map<String, List<String>> individualSearch, String columnName) {303 AnswerList answer = new AnswerList();...
loadFromResultSet
Using AI Code Generation
1public void loadFromResultSet(ResultSet rs) throws SQLException {2 this.idLogEvent = ParameterParserUtil.parseIntegerParam(rs.getString("idLogEvent"), 0);3 this.logEvent = rs.getString("logEvent");4 this.logEventCode = rs.getString("logEventCode");5 this.logEventDescription = rs.getString("logEventDescription");6 this.logEventDate = rs.getTimestamp("logEventDate");7 this.logEventTime = rs.getTime("logEventTime");8 this.logEventLevel = rs.getString("logEventLevel");9 this.logEventCategory = rs.getString("logEventCategory");10 this.logEventCategoryCode = rs.getString("logEventCategoryCode");11 this.logEventRequest = rs.getString("logEventRequest");12 this.logEventRequestCode = rs.getString("logEventRequestCode");13 this.logEventRequestDescription = rs.getString("logEventRequestDescription");14 this.logEventRequestType = rs.getString("logEventRequestType");15 this.logEventRequestTypeCode = rs.getString("logEventRequestTypeCode");16 this.logEventRequestTypeDescription = rs.getString("logEventRequestTypeDescription");17 this.logEventRequestURL = rs.getString("logEventRequestURL");18 this.logEventRequestURLCode = rs.getString("logEventRequestURLCode");19 this.logEventRequestURLDescription = rs.getString("logEventRequestURLDescription");20 this.logEventRequestDuration = ParameterParserUtil.parseIntegerParam(rs.getString("logEventRequestDuration"), 0);21 this.logEventRequestResult = rs.getString("logEventRequestResult");22 this.logEventRequestResultCode = rs.getString("logEventRequestResultCode");23 this.logEventRequestResultDescription = rs.getString("logEventRequestResultDescription");24 this.logEventRequestResultMessage = rs.getString("logEventRequestResultMessage");25 this.logEventRequestResultSQL = rs.getString("logEventRequestResultSQL");26 this.logEventRequestResultSQLCode = rs.getString("logEventRequestResultSQLCode");27 this.logEventRequestResultSQLDescription = rs.getString("logEventRequestResultSQLDescription");28 this.logEventRequestResultSQLMessage = rs.getString("logEventRequestResultSQLMessage");29 this.logEventRequestResultHTTP = rs.getString("logEventRequestResultHTTP");30 this.logEventRequestResultHTTPCode = rs.getString("logEventRequestResultHTTPCode");31 this.logEventRequestResultHTTPDescription = rs.getString("logEventRequestResultHTTPDescription
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!