Skip to content

Commit fcc51a6

Browse files
committed
feat(alert): refactor UtmAlertResponseActionTemplate and UtmAlertResponseRule for improved relationship management and add merge functionality
1 parent 189f2d3 commit fcc51a6

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

backend/src/main/java/com/park/utmstack/domain/alert_response_rule/UtmAlertResponseActionTemplate.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,7 @@ public class UtmAlertResponseActionTemplate implements Serializable {
4141
@Column(name = "system_owner", nullable = false)
4242
private Boolean systemOwner;
4343

44-
@ManyToMany
45-
@JoinTable(
46-
name = "utm_alert_response_rule_template",
47-
joinColumns = @JoinColumn(name = "rule_id"),
48-
inverseJoinColumns = @JoinColumn(name = "template_id")
49-
)
50-
private List<UtmAlertResponseActionTemplate> utmAlertResponseActionTemplates = new ArrayList<>();
51-
52-
44+
@ManyToMany(mappedBy = "utmAlertResponseActionTemplates")
45+
private List<UtmAlertResponseRule> utmAlertResponseRules = new ArrayList<>();
5346
}
5447

backend/src/main/java/com/park/utmstack/domain/alert_response_rule/UtmAlertResponseRule.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,30 @@ public UtmAlertResponseRule(UtmAlertResponseRuleDTO dto) {
109109
}
110110
}
111111

112+
public void mergeInto(UtmAlertResponseRule target) {
113+
target.setRuleName(this.getRuleName());
114+
target.setRuleDescription(this.getRuleDescription());
115+
target.setRuleCmd(this.getRuleCmd());
116+
target.setRuleActive(this.getRuleActive());
117+
target.setAgentPlatform(this.getAgentPlatform());
118+
target.setDefaultAgent(this.getDefaultAgent());
119+
target.setExcludedAgents(this.getExcludedAgents());
120+
target.setRuleConditions(this.getRuleConditions());
121+
122+
if (!this.getUtmAlertResponseActionTemplates().isEmpty()) {
123+
List<UtmAlertResponseActionTemplate> targetActions = target.getUtmAlertResponseActionTemplates();
124+
targetActions.clear();
125+
targetActions.addAll(this.getUtmAlertResponseActionTemplates().stream().map(templateDto -> {
126+
UtmAlertResponseActionTemplate template = new UtmAlertResponseActionTemplate();
127+
template.setId(templateDto.getId());
128+
template.setTitle(templateDto.getTitle());
129+
template.setDescription(templateDto.getDescription());
130+
template.setCommand(templateDto.getCommand());
131+
return template;
132+
}).collect(Collectors.toList()));
133+
}
134+
135+
}
136+
137+
112138
}

backend/src/main/java/com/park/utmstack/service/alert_response_rule/UtmAlertResponseRuleService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,12 @@ public UtmAlertResponseRule save(UtmAlertResponseRule alertResponseRule) {
9393
final String ctx = CLASSNAME + ".save";
9494
try {
9595
if (alertResponseRule.getId() != null) {
96+
String alertRuleId = String.valueOf(alertResponseRule.getId());
9697
UtmAlertResponseRule current = alertResponseRuleRepository.findById(alertResponseRule.getId())
97-
.orElseThrow(() -> new RuntimeException(String.format("Incident response rule with ID: %1$s not found", alertResponseRule.getId())));
98-
alertResponseRuleHistoryRepository.save(new UtmAlertResponseRuleHistory(new UtmAlertResponseRuleDTO(current)));
98+
.orElseThrow(() -> new RuntimeException(String.format("Incident response rule with ID: %1$s not found", alertRuleId)));
99+
alertResponseRule.mergeInto(current);
100+
alertResponseRuleHistoryRepository.save(new UtmAlertResponseRuleHistory(new UtmAlertResponseRuleDTO(alertResponseRule)));
101+
alertResponseRule = current;
99102
}
100103

101104
if (alertResponseRule.getUtmAlertResponseActionTemplates() != null) {

backend/src/main/java/com/park/utmstack/service/dto/UtmAlertResponseRuleDTO.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import com.google.gson.Gson;
55
import com.google.gson.reflect.TypeToken;
6+
import com.park.utmstack.domain.alert_response_rule.UtmAlertResponseActionTemplate;
67
import com.park.utmstack.domain.alert_response_rule.UtmAlertResponseRule;
78
import com.park.utmstack.domain.chart_builder.types.query.FilterType;
89
import lombok.Data;
910
import lombok.NoArgsConstructor;
11+
import org.springframework.util.CollectionUtils;
1012
import org.springframework.util.StringUtils;
1113

1214
import javax.validation.constraints.*;
@@ -95,4 +97,5 @@ public UtmAlertResponseRuleDTO(UtmAlertResponseRule rule) {
9597
}
9698

9799
}
100+
98101
}

0 commit comments

Comments
 (0)