public Rules getRules(int ruleId) { PreparedStatement ps = null; ResultSet rs = null; Rules rules = new Rules(); try { ps = con.prepareStatement(SELECT_RULE_QRY); ps.setInt(1, ruleId); rs = ps.executeQuery(); String sourceID = ""; String ruleString = ""; String sourceFields = ""; String ruleName = ""; int userId = 0; if (rs.next()) { ruleId = rs.getInt("RuleID"); ruleName = rs.getString(RULE_NAME); sourceID = rs.getString(SOURCE_ID); ruleString = rs.getString(RULE_QUERIES); sourceFields = rs.getString(SOURCE_FIELDS); userId = rs.getInt("user_id"); LOGGER.debug(ruleString); } rules.setRuleID(ruleId); rules.setRuleName(ruleName); rules.setSource(sourceID); rules.setUserId(userId); rules.setExtractFields(sourceFields); // objectMapper.convertValue(ruleString, Rule.class); List<Rule> ruless = new ArrayList<Rule>(); JsonObject rulesObj = (new JsonParser()).parse(ruleString).getAsJsonObject(); JsonArray rulesJson = rulesObj.get("rules").getAsJsonArray(); for (int i = 0; i < rulesJson.size(); i++) { JsonObject tempObj = rulesJson.get(i).getAsJsonObject(); Rule rule = new Rule(); rule.setRuleOperator(tempObj.get("ruleOperator").getAsString()); rule.setDataField(tempObj.get("dataField").getAsString()); rule.setRuleParameters(tempObj.get("ruleParameters").getAsString()); ruless.add(rule); } rules.setRules(ruless); // rulesObj.get("") // {"source":"ds59","rules":[{"ruleOperator":"coordinates","dataField":"loc","ruleParameters":"-120.2,40.1,-118.2,32.4"},{"ruleOperator":">","dataField":"value","ruleParameters":"100"}],"extractFields":"loc,value,timestamp"} LOGGER.debug(rules.toString()); } catch (Exception e) { e.printStackTrace(); } finally { } return rules; }
public List<Rules> getAllRules(User user) { PreparedStatement ps = null; ResultSet rs = null; List<Rules> rulesList = new ArrayList<>(); try { ps = con.prepareStatement(SELECT_ALL_RULE_QRY); ps.setInt(1, user.getId()); rs = ps.executeQuery(); while (rs.next()) { Rules rules = new Rules(); int ruleID; String ruleName; String sourceID; String ruleString; String sourceFields; int userId; ruleID = rs.getInt(RULE_ID); ruleName = rs.getString(RULE_NAME); if (ruleName == null) { ruleName = ""; } sourceID = rs.getString(SOURCE_ID); ruleString = rs.getString(RULE_QUERIES); if (ruleString == null) { ruleString = ""; } sourceFields = rs.getString(SOURCE_FIELDS); if (sourceFields == null) { sourceFields = ""; } userId = rs.getInt(USER_ID); rules.setRuleID(ruleID); rules.setRuleName(ruleName); rules.setUserId(userId); rules.setSource(sourceID); rules.setExtractFields(sourceFields); List<Rule> ruless = new ArrayList<Rule>(); if (!ruleString.equals("")) { JsonObject rulesObj = (new JsonParser()).parse(ruleString).getAsJsonObject(); JsonArray rulesJson = rulesObj.get("rules").getAsJsonArray(); for (int i = 0; i < rulesJson.size(); i++) { Rule rule = new Rule(); JsonObject tempObj = rulesJson.get(i).getAsJsonObject(); rule.setRuleOperator(tempObj.get("ruleOperator").getAsString()); rule.setDataField(tempObj.get("dataField").getAsString()); rule.setRuleParameters(tempObj.get("ruleParameters").getAsString()); ruless.add(rule); } } rules.setRules(ruless); rulesList.add(rules); } for (int i = 0; i < rulesList.size(); i++) { LOGGER.debug(rulesList.get(i).toString()); } } catch (Exception e) { e.printStackTrace(); } finally { } return rulesList; }