public void add(String project, AutomationRule rule) {
for (AutomationRule.SerializableAction action : rule.actions) {
AutomationAction<?> automationAction = automationActions.stream()
.filter(a -> a.getClass().equals(action.type.getActionClass()))
.findFirst().get();
Type[] genericInterfaces = automationAction.getClass().getGenericInterfaces();
Class optionsClass = null;
for (Type genericInterface : genericInterfaces) {
if (genericInterface instanceof ParameterizedType &&
((ParameterizedType) genericInterface).getRawType().equals(AutomationAction.class)) {
optionsClass = (Class) ((ParameterizedType) genericInterface).getActualTypeArguments()[0];
}
}
if (optionsClass == null) {
throw new IllegalStateException();
}
JsonHelper.convert(action.value, optionsClass);
}
try (Handle handle = dbi.open()) {
handle.createStatement("INSERT INTO automation_rules (project, is_active, event_filters, actions, custom_data) VALUES (:project, true, :event_filters, :actions, :custom_data)")
.bind("project", project)
.bind("event_filters", JsonHelper.encode(rule.scenarios))
.bind("custom_data", rule.customData)
.bind("actions", JsonHelper.encode(rule.actions)).execute();
}
rules.refresh(project);
}