Skip to content

Commit

Permalink
[improve] support multiple receivers. (#1731)
Browse files Browse the repository at this point in the history
  • Loading branch information
zqr10159 authored Apr 14, 2024
1 parent 828e46c commit 97f27b4
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.apache.hertzbeat.common.entity.manager;

import com.fasterxml.jackson.core.type.TypeReference;
import jakarta.persistence.AttributeConverter;
import org.apache.commons.lang3.StringUtils;
import org.apache.hertzbeat.common.util.JsonUtil;
import java.util.List;


/**
* json str to id list
*/

public class JsonLongListAttributeConverter implements AttributeConverter<List<Long>, String> {
@Override
public String convertToDatabaseColumn(List<Long> attribute) {
return JsonUtil.toJson(attribute);

}

@Override
public List<Long> convertToEntityAttribute(String dbData) {
TypeReference<List<Long>> typeReference = new TypeReference<>() {};
List<Long> longList = JsonUtil.fromJson(dbData, typeReference);
if (longList == null && !dbData.isEmpty()) {
if (StringUtils.isNumeric(dbData)){
return List.of(Long.parseLong(dbData));
}
else throw new NumberFormatException("String convert to Long error");
}else return longList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.apache.hertzbeat.common.entity.manager;

import com.fasterxml.jackson.core.type.TypeReference;
import jakarta.persistence.AttributeConverter;
import org.apache.hertzbeat.common.util.JsonUtil;

import java.util.List;


/**
* Convert the list of strings to a JSON string
*/
public class JsonStringListAttributeConverter implements AttributeConverter<List<String>, String> {
@Override
public String convertToDatabaseColumn(List<String> attribute) {
return JsonUtil.toJson(attribute);

}

@Override
public List<String> convertToEntityAttribute(String dbData) {
TypeReference<List<String>> typeReference = new TypeReference<>() {};
List<String> stringList = JsonUtil.fromJson(dbData, typeReference);
if (stringList == null && !dbData.isEmpty()) {
return List.of(dbData);
}else return stringList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,19 @@ public String convertToDatabaseColumn(List<TagItem> attribute) {

@Override
public List<TagItem> convertToEntityAttribute(String dbData) {
try {
TypeReference<List<TagItem>> typeReference = new TypeReference<>() {};
return JsonUtil.fromJson(dbData, typeReference);
} catch (Exception e) {
// history data handler
TypeReference<Map<String, String>> typeReference = new TypeReference<>() {};
Map<String, String> map = JsonUtil.fromJson(dbData, typeReference);
TypeReference<List<TagItem>> typeReference = new TypeReference<>() {};
List<TagItem> tagItems = JsonUtil.fromJson(dbData, typeReference);
if (tagItems == null) {
TypeReference<Map<String, String>> mapTypeReference = new TypeReference<>() {};
Map<String, String> map = JsonUtil.fromJson(dbData, mapTypeReference);
if (map != null) {
return map.entrySet().stream().map(entry -> new TagItem(entry.getKey(), entry.getValue())).collect(Collectors.toList());
} else {
return null;
}
} else {
return tagItems;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ public class NoticeRule {
description = "接收人ID",
example = "4324324", accessMode = READ_WRITE)
@NotNull
private Long receiverId;
@Convert(converter = JsonLongListAttributeConverter.class)
private List<Long> receiverId;

@Schema(title = "Recipient identification",
description = "接收人标识",
example = "tom", accessMode = READ_WRITE)
@Length(max = 100)
@NotNull
private String receiverName;
@Convert(converter = JsonStringListAttributeConverter.class)
private List<String> receiverName;

@Schema(title = "Template ID",
description = "模板ID",
Expand Down
Loading

0 comments on commit 97f27b4

Please sign in to comment.