Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improvement]Support multiple receivers. #1731

Merged
merged 12 commits into from
Apr 14, 2024
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
Loading