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 int registerDefaultRule(DataSource ds) { int key = 0; Rules rules = new Rules(); rules.setRuleName(DEFAULT); rules.setSource("ds" + ds.getSrcID()); rules.setUserId(Integer.parseInt(ds.getUserId())); rules.setExtractFields(DEFAULT_EXTRACT_FIELD); // Sample Rule Insert // {"source":"ds42","extractFields":"loc,theme,value,timestamp,","rules":[{"dataField":"loc","ruleOperator":"coordinates","ruleParameters":"33,33,33,33"}]} try { PreparedStatement ps = con.prepareStatement(INSERT_DEFAULT_RULE_QRY, Statement.RETURN_GENERATED_KEYS); ps.setString(1, ds.getSrcID() + "_" + rules.getRuleName()); String ruleQuery = "{\"rules\":[{\"ruleOperator\":\"coordinates\",\"dataField\":\"loc\",\"ruleParameters\":\"" + ds.getInitParam().getSwLat() + "," + ds.getInitParam().getSwLong() + "," + ds.getInitParam().getNeLat() + "," + ds.getInitParam().getNeLong() + "\"}]}"; ps.setString(2, ruleQuery); ps.setString(3, rules.getSource()); ps.setString(4, rules.getExtractFields()); ps.setInt(5, rules.getUserId()); ps.executeUpdate(); ResultSet rs = ps.getGeneratedKeys(); if (rs.next()) { // Retrieve the auto generated key(s). key = rs.getInt(1); } } catch (Exception ex) { ex.printStackTrace(); } return key; }
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; }