Best Cerberus-source code snippet using org.cerberus.crud.dao.impl.CampaignParameterDAO.loadFromResultSet
Source:CampaignParameterDAO.java
...88 try(Connection connection = this.databaseSpring.connect();89 PreparedStatement preStat = connection.prepareStatement(query);) {90 try(ResultSet resultSet = preStat.executeQuery();) {91 while (resultSet.next()) {92 campaignParameterList.add(this.loadFromResultSet(resultSet));93 }94 } catch (SQLException exception) {95 LOG.warn("Unable to execute query : " + exception.toString());96 campaignParameterList = null;97 } 98 } catch (SQLException exception) {99 LOG.warn("Unable to execute query : " + exception.toString());100 campaignParameterList = null;101 } 102 if (throwEx) {103 throw new CerberusException(new MessageGeneral(MessageGeneralEnum.NO_DATA_FOUND));104 }105 return campaignParameterList;106 }107 @Override108 public CampaignParameter findCampaignParameterByKey(Integer campaignparameterID) throws CerberusException {109 boolean throwEx = false;110 final String query = "SELECT * FROM campaignparameter c WHERE c.campaignparameterID = ?";111 CampaignParameter campaignParameterResult = null;112 try(Connection connection = this.databaseSpring.connect();113 PreparedStatement preStat = connection.prepareStatement(query);) {114 preStat.setInt(1, campaignparameterID);115 try(ResultSet resultSet = preStat.executeQuery();) {116 if (resultSet.first()) {117 campaignParameterResult = this.loadFromResultSet(resultSet);118 }119 } catch (SQLException exception) {120 LOG.warn("Unable to execute query : " + exception.toString());121 } 122 } catch (SQLException exception) {123 LOG.warn("Unable to execute query : " + exception.toString());124 } 125 if (throwEx) {126 throw new CerberusException(new MessageGeneral(MessageGeneralEnum.NO_DATA_FOUND));127 }128 return campaignParameterResult;129}130 @Override131 public List<CampaignParameter> findCampaignParametersByCampaign(String campaign) throws CerberusException {132 boolean throwEx = false;133 final String query = "SELECT * FROM campaignparameter c WHERE c.campaign = ?";134 List<CampaignParameter> campaignParameterList = new ArrayList<CampaignParameter>();135 136 try(Connection connection = this.databaseSpring.connect();137 PreparedStatement preStat = connection.prepareStatement(query);) {138 139 preStat.setString(1, campaign);140 try( ResultSet resultSet = preStat.executeQuery();) {141 while (resultSet.next()) {142 campaignParameterList.add(this.loadFromResultSet(resultSet));143 }144 } catch (SQLException exception) {145 LOG.warn("Unable to execute query : " + exception.toString());146 campaignParameterList = null;147 } 148 } catch (SQLException exception) {149 LOG.warn("Unable to execute query : " + exception.toString());150 campaignParameterList = null;151 } 152 if (throwEx) {153 throw new CerberusException(new MessageGeneral(MessageGeneralEnum.NO_DATA_FOUND));154 }155 return campaignParameterList;156 }157 @Override158 public boolean updateCampaignParameter(CampaignParameter campaignParameter) {159 final StringBuilder query = new StringBuilder("UPDATE `campaignparameter` SET campaign=?, `Parameter`=?, `Value`=? WHERE campaignparameterID=?");160 try (Connection connection = this.databaseSpring.connect();161 PreparedStatement preStat = connection.prepareStatement(query.toString());){162 preStat.setString(1, campaignParameter.getCampaign());163 preStat.setString(2, campaignParameter.getParameter());164 preStat.setString(3, campaignParameter.getValue());165 preStat.setInt(4, campaignParameter.getCampaignparameterID());166 return (preStat.executeUpdate() == 1);167 } catch (SQLException exception) {168 LOG.warn("Unable to execute query : " + exception.toString());169 } 170 return false;171 }172 @Override173 public boolean createCampaignParameter(CampaignParameter campaignParameter) {174 final StringBuilder query = new StringBuilder("INSERT INTO `campaignparameter` (`campaign`, `Parameter`, `Value`) VALUES (?, ?, ?);");175 176 try(Connection connection = this.databaseSpring.connect();177 PreparedStatement preStat = connection.prepareStatement(query.toString());) {178 preStat.setString(1, campaignParameter.getCampaign());179 preStat.setString(2, campaignParameter.getParameter());180 preStat.setString(3, campaignParameter.getValue());181 return (preStat.executeUpdate() == 1);182 } catch (SQLException exception) {183 LOG.warn("Unable to execute query : " + exception.toString());184 }185 return false;186 }187 @Override188 public List<CampaignParameter> findCampaignParameterByCriteria(Integer campaignparameterID, String campaign, String parameter, String value) throws CerberusException {189 boolean throwEx = false;190 final StringBuilder query = new StringBuilder("SELECT * FROM campaignparameter c WHERE 1=1 ");191 if (campaignparameterID != null) {192 query.append(" AND c.campaignparameterID = ?");193 }194 if (campaign != null && !"".equals(campaign.trim())) {195 query.append(" AND c.campaign LIKE ?");196 }197 if (parameter != null && !"".equals(parameter.trim())) {198 query.append(" AND c.parameter LIKE ?");199 }200 if (value != null && !"".equals(value.trim())) {201 query.append(" AND c.value LIKE ?");202 }203 // " c.campaignID = ? AND c.campaign LIKE ? AND c.description LIKE ?";204 List<CampaignParameter> campaignParametersList = new ArrayList<CampaignParameter>();205 206 try(Connection connection = this.databaseSpring.connect();207 PreparedStatement preStat = connection.prepareStatement(query.toString());) {208 209 int index = 1;210 if (campaignparameterID != null) {211 preStat.setInt(index, campaignparameterID);212 index++;213 }214 if (campaign != null && !"".equals(campaign.trim())) {215 preStat.setString(index, "%" + campaign.trim() + "%");216 index++;217 }218 if (parameter != null && !"".equals(parameter.trim())) {219 preStat.setString(index, "%" + parameter.trim() + "%");220 index++;221 }222 if (value != null && !"".equals(value.trim())) {223 preStat.setString(index, "%" + value.trim() + "%");224 index++;225 }226 try(ResultSet resultSet = preStat.executeQuery();) {227 while (resultSet.next()) {228 campaignParametersList.add(this.loadFromResultSet(resultSet));229 }230 } catch (SQLException exception) {231 LOG.warn("Unable to execute query : " + exception.toString());232 campaignParametersList = null;233 } 234 } catch (SQLException exception) {235 LOG.warn("Unable to execute query : " + exception.toString());236 campaignParametersList = null;237 }238 if (throwEx) {239 throw new CerberusException(new MessageGeneral(MessageGeneralEnum.NO_DATA_FOUND));240 }241 return campaignParametersList;242 }243 @Override244 public boolean deleteCampaignParameter(CampaignParameter campaignParameter) {245 final StringBuilder query = new StringBuilder("DELETE FROM `campaignparameter` WHERE campaignparameterID=?");246 try(Connection connection = this.databaseSpring.connect();247 PreparedStatement preStat = connection.prepareStatement(query.toString());) {248 preStat.setInt(1, campaignParameter.getCampaignparameterID());249 return (preStat.executeUpdate() == 1);250 } catch (SQLException exception) {251 LOG.warn("Unable to execute query : " + exception.toString());252 } 253 return false;254 }255 @Override256 public AnswerList readByCampaignByCriteria(String campaign, int start, int amount, String column, String dir, String searchTerm, String individualSearch) {257 AnswerList response = new AnswerList();258 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);259 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));260 List<CampaignParameter> campaignParameterList = new ArrayList<CampaignParameter>();261 StringBuilder searchSQL = new StringBuilder();262 StringBuilder query = new StringBuilder();263 //SQL_CALC_FOUND_ROWS allows to retrieve the total number of columns by disrearding the limit clauses that 264 //were applied -- used for pagination p265 query.append("SELECT SQL_CALC_FOUND_ROWS * FROM campaignparameter ");266 searchSQL.append(" where 1=1 ");267 if (!StringUtil.isNullOrEmpty(searchTerm)) {268 searchSQL.append(" and (`campaignparameterid` like ?");269 searchSQL.append(" or `campaign` like ?");270 searchSQL.append(" or `parameter` like ?");271 searchSQL.append(" or `value` like ?)");272 }273 if (!StringUtil.isNullOrEmpty(individualSearch)) {274 searchSQL.append(" and (`?`)");275 }276 if (!StringUtil.isNullOrEmpty(campaign)) {277 searchSQL.append(" and (`campaign` = ? )");278 }279 query.append(searchSQL);280 if (!StringUtil.isNullOrEmpty(column)) {281 query.append(" order by `").append(column).append("` ").append(dir);282 }283 if ((amount <= 0) || (amount >= MAX_ROW_SELECTED)) {284 query.append(" limit ").append(start).append(" , ").append(MAX_ROW_SELECTED);285 } else {286 query.append(" limit ").append(start).append(" , ").append(amount);287 }288 // Debug message on SQL.289 if (LOG.isDebugEnabled()) {290 LOG.debug("SQL : " + query.toString());291 }292 293 try(Connection connection = this.databaseSpring.connect();294 PreparedStatement preStat = connection.prepareStatement(query.toString());295 Statement stm = connection.createStatement();) {296 297 int i = 1;298 if (!StringUtil.isNullOrEmpty(searchTerm)) {299 preStat.setString(i++, "%" + searchTerm + "%");300 preStat.setString(i++, "%" + searchTerm + "%");301 preStat.setString(i++, "%" + searchTerm + "%");302 }303 if (!StringUtil.isNullOrEmpty(individualSearch)) {304 preStat.setString(i++, individualSearch);305 }306 if (!StringUtil.isNullOrEmpty(campaign)) {307 preStat.setString(i++, campaign);308 }309 310 try(ResultSet resultSet = preStat.executeQuery();311 ResultSet rowSet = stm.executeQuery("SELECT FOUND_ROWS()");) {312 //gets the data313 while (resultSet.next()) {314 campaignParameterList.add(this.loadFromResultSet(resultSet));315 }316 //get the total number of rows317 int nrTotalRows = 0;318 if (resultSet != null && resultSet.next()) {319 nrTotalRows = resultSet.getInt(1);320 }321 if (campaignParameterList.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.322 LOG.error("Partial Result in the query.");323 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING_PARTIAL_RESULT);324 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Maximum row reached : " + MAX_ROW_SELECTED));325 response = new AnswerList(campaignParameterList, nrTotalRows);326 } else if (campaignParameterList.size() <= 0) {327 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);328 response = new AnswerList(campaignParameterList, nrTotalRows);329 } else {330 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);331 msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));332 response = new AnswerList(campaignParameterList, nrTotalRows);333 }334 } catch (SQLException exception) {335 LOG.error("Unable to execute query : " + exception.toString());336 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);337 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));338 } 339 } catch (SQLException exception) {340 LOG.error("Unable to execute query : " + exception.toString());341 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);342 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));343 } 344 response.setResultMessage(msg);345 response.setDataList(campaignParameterList);346 return response;347 }348 @Override349 public AnswerList readByCampaign(String campaign) {350 AnswerList answer = new AnswerList();351 MessageEvent msg;352 List<CampaignParameter> result = new ArrayList<CampaignParameter>();353 final String query = "SELECT * FROM campaignparameter WHERE campaign = ?";354 355 try(Connection connection = this.databaseSpring.connect();356 PreparedStatement preStat = connection.prepareStatement(query);) {357 358 preStat.setString(1, campaign);359 try(ResultSet resultSet = preStat.executeQuery();) {360 while (resultSet.next()) {361 result.add(this.loadFromResultSet(resultSet));362 }363 if (result.isEmpty()) {364 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);365 } else {366 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);367 }368 } catch (SQLException exception) {369 LOG.warn("Unable to execute query : " + exception.toString());370 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);371 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));372 result.clear();373 } 374 } catch (SQLException exception) {375 LOG.warn("Unable to execute query : " + exception.toString());376 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);377 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));378 } 379 answer.setTotalRows(result.size());380 answer.setDataList(result);381 answer.setResultMessage(msg);382 return answer;383 }384 private CampaignParameter loadFromResultSet(ResultSet rs) throws SQLException {385 Integer campaignparameterID = ParameterParserUtil.parseIntegerParam(rs.getString("campaignparameterID"), -1);386 String campaign = ParameterParserUtil.parseStringParam(rs.getString("campaign"), "");387 String parameter = ParameterParserUtil.parseStringParam(rs.getString("Parameter"), "");388 String value = ParameterParserUtil.parseStringParam(rs.getString("Value"), "");389 return factoryCampaignParameter.create(campaignparameterID, campaign, parameter, value);390 }391 @Override392 public Answer deleteByCampaign(String key) {393 Answer ans = new Answer();394 MessageEvent msg = null;395 try (Connection connection = databaseSpring.connect();396 PreparedStatement preStat = connection.prepareStatement(Query.DELETE_BY_CAMPAIGN)) {397 // Prepare and execute query398 preStat.setString(1, key);...
loadFromResultSet
Using AI Code Generation
1 public static void main(String[] args) {2 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");3 ICampaignParameterDAO campaignParameterDAO = context.getBean(ICampaignParameterDAO.class);4 List<CampaignParameter> campaignParameterList = campaignParameterDAO.loadFromResultSet(null, null);5 for(CampaignParameter campaignParameter : campaignParameterList) {6 System.out.println(campaignParameter);7 }8 }9}
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!!