Selaa lähdekoodia

enhancemnet #IANY4U 在NodeComponent中添加getCmpList方法

Xu Qiaolun 8 kuukautta sitten
vanhempi
säilyke
e9b8daff62

+ 9 - 0
liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java

@@ -29,6 +29,7 @@ import com.yomahub.liteflow.util.JsonUtil;
 
 import java.lang.reflect.Method;
 import java.util.Date;
+import java.util.List;
 import java.util.Stack;
 
 /**
@@ -414,6 +415,14 @@ public abstract class NodeComponent{
 		return JsonUtil.parseObject(cmpData, clazz);
 	}
 
+	public <T> List<T> getCmpList(Class<T> clazz) {
+		String cmpData = getRefNode().getCmpData();
+		if (StrUtil.isBlank(cmpData)) {
+			return null;
+		}
+		return JsonUtil.parseList(cmpData, clazz);
+	}
+
 	public Integer getLoopIndex() {
 		return this.getRefNode().getLoopIndex();
 	}

+ 18 - 0
liteflow-core/src/main/java/com/yomahub/liteflow/util/JsonUtil.java

@@ -3,13 +3,17 @@ package com.yomahub.liteflow.util;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.type.CollectionType;
 import com.yomahub.liteflow.exception.JsonProcessException;
 import com.yomahub.liteflow.log.LFLog;
 import com.yomahub.liteflow.log.LFLoggerManager;
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.TimeZone;
 
 /**
@@ -73,4 +77,18 @@ public class JsonUtil {
 			throw new JsonProcessException(errMsg);
 		}
 	}
+
+	public static <T> List<T> parseList(String json, Class<T> clazz) {
+		if (StrUtil.isEmpty(json)) {
+			return null;
+		}
+		try {
+			CollectionType listType = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, clazz);
+			return objectMapper.readValue(json, listType);
+		} catch (IOException e) {
+			String errMsg = StrUtil.format("Error while parsing text [{}],reason: {}", json, e.getMessage());
+			LOG.error(e.getMessage(), e);
+			throw new JsonProcessException(errMsg);
+		}
+	}
 }