public int registerRule(Rules rules) { int key = 0; try { PreparedStatement ps = con.prepareStatement(INSERT_RULE_QRY, Statement.RETURN_GENERATED_KEYS); ps.setString(1, rules.getRuleName()); ps.setString(2, rules.getSource()); JsonArray ruleArray = new JsonArray(); List<Rule> rule = rules.getRules(); for (Rule rul : rule) { JsonObject ruleObj = new JsonObject(); ruleObj.addProperty("dataField", rul.getDataField()); ruleObj.addProperty("ruleOperator", rul.getRuleOperator()); ruleObj.addProperty("ruleParameters", rul.getRuleParameters()); ruleArray.add(ruleObj); } ps.setString(3, "{\"rules\":" + ruleArray.toString() + "}"); 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); } return key; } catch (SQLException e) { e.printStackTrace(); return 0; } }
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; }