|
@ -5,9 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.yihu.hos.common.graph.BFSGraph;
|
|
|
import com.yihu.hos.common.graph.DGraph;
|
|
|
import com.yihu.hos.common.graph.Edge;
|
|
|
import com.yihu.hos.config.MongoConfig;
|
|
|
import com.yihu.hos.core.datatype.StringUtil;
|
|
|
import com.yihu.hos.core.encrypt.DES;
|
|
|
import com.yihu.hos.system.dao.AppDao;
|
|
|
import com.yihu.hos.system.dao.AppServiceDao;
|
|
|
import com.yihu.hos.system.dao.FlowProcessDao;
|
|
@ -17,15 +15,14 @@ import com.yihu.hos.system.model.SystemServiceEndpoint;
|
|
|
import com.yihu.hos.system.model.SystemServiceFlowProcess;
|
|
|
import com.yihu.hos.system.model.SystemServiceFlowProcessor;
|
|
|
import com.yihu.hos.web.framework.model.Result;
|
|
|
import com.yihu.hos.web.framework.util.GridFSUtil;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.File;
|
|
|
import java.io.FileWriter;
|
|
|
import java.io.IOException;
|
|
|
import java.util.*;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Service("ProcessManager")
|
|
|
public class ProcessManager {
|
|
@ -44,28 +41,28 @@ public class ProcessManager {
|
|
|
private ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
public Result getAllApp() throws Exception {
|
|
|
String hql = "select * from SystemApp";
|
|
|
String hql = "from SystemApp";
|
|
|
List<SystemApp> appList = appDao.getEntityList(SystemApp.class, hql);
|
|
|
String result = objectMapper.writeValueAsString(appList);
|
|
|
return Result.success(result);
|
|
|
}
|
|
|
|
|
|
public Result getAllAppService() throws Exception {
|
|
|
String hql = "select * from SystemServiceEndpoint";
|
|
|
String hql = "from SystemServiceEndpoint";
|
|
|
List<SystemServiceEndpoint> serviceEndpointList = appServiceDao.getEntityList(SystemServiceEndpoint.class, hql);
|
|
|
String result = objectMapper.writeValueAsString(serviceEndpointList);
|
|
|
return Result.success(result);
|
|
|
}
|
|
|
|
|
|
public Result getAppServiceByAppId(String appId) throws Exception {
|
|
|
String hql = "select * from SystemServiceEndpoint where appId = "+appId+"";
|
|
|
String hql = "from SystemServiceEndpoint where appId = "+appId+"";
|
|
|
List<SystemServiceEndpoint> serviceEndpointList = appServiceDao.getEntityList(SystemServiceEndpoint.class, hql);
|
|
|
String result = objectMapper.writeValueAsString(serviceEndpointList);
|
|
|
return Result.success(result);
|
|
|
}
|
|
|
|
|
|
public Result getAllProcessor() throws Exception {
|
|
|
String hql = "select * from SystemServiceFlowProcessor";
|
|
|
String hql = "from SystemServiceFlowProcessor";
|
|
|
List<SystemServiceFlowProcessor> processorList = processorDao.getEntityList(SystemServiceFlowProcessor.class, hql);
|
|
|
String result = objectMapper.writeValueAsString(processorList);
|
|
|
return Result.success(result);
|
|
@ -80,8 +77,8 @@ public class ProcessManager {
|
|
|
processDao.saveEntity(process);
|
|
|
}
|
|
|
|
|
|
public String formatJson(String flowJsonStr) throws Exception {
|
|
|
flowJsonStr = "{\n" +
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
String flowJsonStr = "{\n" +
|
|
|
" \"code\": \"cralwer\",\n" +
|
|
|
" \"nodes\": {\n" +
|
|
|
" \"node_1\": {\n" +
|
|
@ -96,6 +93,16 @@ public class ProcessManager {
|
|
|
" },\n" +
|
|
|
" \"node_3\": {\n" +
|
|
|
" \"name\": \"crawler\",\n" +
|
|
|
" \"value\": \"body().isEqualTo(false)\",\n" +
|
|
|
" \"type\": \"judgement\"\n" +
|
|
|
" },\n" +
|
|
|
" \"node_4\": {\n" +
|
|
|
" \"name\": \"crawler\",\n" +
|
|
|
" \"value\": \"http4://localhost:8088/crawler/collect\",\n" +
|
|
|
" \"type\": \"service\"\n" +
|
|
|
" },\n" +
|
|
|
" \"node_5\": {\n" +
|
|
|
" \"name\": \"crawler\",\n" +
|
|
|
" \"value\": \"http4://localhost:8088/crawler/patientList\",\n" +
|
|
|
" \"type\": \"service\"\n" +
|
|
|
" }\n" +
|
|
@ -108,11 +115,24 @@ public class ProcessManager {
|
|
|
" \"line_2\": {\n" +
|
|
|
" \"from\": \"node_2\",\n" +
|
|
|
" \"to\": \"node_3\"\n" +
|
|
|
" },\n" +
|
|
|
" \"line_3\": {\n" +
|
|
|
" \"from\": \"node_3\",\n" +
|
|
|
" \"to\": \"node_4\",\n" +
|
|
|
" \"value\": \"correct\"\n" +
|
|
|
" },\n" +
|
|
|
" \"line_4\": {\n" +
|
|
|
" \"from\": \"node_3\",\n" +
|
|
|
" \"to\": \"node_5\"\n" +
|
|
|
" }\n" +
|
|
|
" }\n" +
|
|
|
"}";
|
|
|
formatJson(flowJsonStr);
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String formatJson(String flowJsonStr) throws Exception {
|
|
|
String root = "";
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
JsonNode flowJson = objectMapper.readValue(flowJsonStr, JsonNode.class);
|
|
|
String code = flowJson.get("code").asText();
|
|
@ -130,6 +150,9 @@ public class ProcessManager {
|
|
|
while (nodeIterator.hasNext()) {
|
|
|
Map.Entry<String, JsonNode> map = nodeIterator.next();
|
|
|
nodeMap.put(map.getKey(), map.getValue());
|
|
|
if (StringUtil.isEmpty(mDG.get(0))) {
|
|
|
root = map.getKey();
|
|
|
}
|
|
|
mDG.add(map.getKey());
|
|
|
}
|
|
|
|
|
@ -138,14 +161,14 @@ public class ProcessManager {
|
|
|
lineMap.put(map.getKey(), map.getValue());
|
|
|
String nodeNameFrom = map.getValue().get("from").asText();
|
|
|
String nodeNameTo = map.getValue().get("to").asText();
|
|
|
mDG.add(new Edge<String>(nodeNameFrom, nodeNameTo, map.getKey()));
|
|
|
mDG.add(new Edge<>(nodeNameFrom, nodeNameTo, map.getKey()));
|
|
|
}
|
|
|
|
|
|
//generate the java code
|
|
|
return generate(code, lineMap, nodeMap, mDG);
|
|
|
return generate(code, root, lineMap, nodeMap, mDG);
|
|
|
}
|
|
|
|
|
|
public String generate(String code, Map<String, JsonNode> lineMap, Map<String, JsonNode> nodeMap, DGraph<String> mDG) throws IOException {
|
|
|
public static String generate(String code, String root, Map<String, JsonNode> lineMap, Map<String, JsonNode> nodeMap, DGraph<String> mDG) throws IOException {
|
|
|
Boolean isFirstNodeFlg = true;
|
|
|
StringBuilder javaBuilder = new StringBuilder();
|
|
|
String javaName = toUpperCaseFirstOne(code)+"Route";
|
|
@ -153,6 +176,8 @@ public class ProcessManager {
|
|
|
javaBuilder.append("package "+code+".route;\n\n");
|
|
|
javaBuilder.append("import org.apache.camel.Exchange;\n");
|
|
|
javaBuilder.append("import org.apache.camel.builder.RouteBuilder;\n");
|
|
|
javaBuilder.append("import crawler.SplitUtil;\n");
|
|
|
|
|
|
for (String key : nodeMap.keySet()) {
|
|
|
JsonNode node = nodeMap.get(key);
|
|
|
String type = node.get("type").asText();
|
|
@ -163,7 +188,7 @@ public class ProcessManager {
|
|
|
javaBuilder.append("public class "+javaName+" extends RouteBuilder {\n");
|
|
|
javaBuilder.append("public void configure() throws Exception {\n");
|
|
|
|
|
|
Iterator<String> it = mDG.iterator("1");
|
|
|
Iterator<String> it = mDG.iterator(root);
|
|
|
while(it.hasNext()) {
|
|
|
String nodeName = it.next();
|
|
|
JsonNode node = nodeMap.get(nodeName);
|
|
@ -177,9 +202,26 @@ public class ProcessManager {
|
|
|
isFirstNodeFlg = false;
|
|
|
} else {
|
|
|
if (type.equals("processor")) {
|
|
|
javaBuilder.append("\n.process(\"new "+name+"())");
|
|
|
JsonNode args = node.get("args");
|
|
|
if (args == null) {
|
|
|
javaBuilder.append("\n.process(\"new "+name+"())");
|
|
|
} else {
|
|
|
String argStr = "";
|
|
|
String[] argArr = args.asText().split(",");
|
|
|
for (String arg : argArr) {
|
|
|
argStr += "\"" + arg + "\",";
|
|
|
}
|
|
|
argStr = StringUtil.substring(argStr, 0, argStr.length() - 1);
|
|
|
javaBuilder.append("\n.process(\"new "+name+"("+argStr+"))");
|
|
|
}
|
|
|
} else if (type.equals("judgement")) {
|
|
|
judgement(javaBuilder, value, nodeName, mDG, it, lineMap, nodeMap);
|
|
|
} else if (type.equals("circle")) {
|
|
|
split(javaBuilder, value);
|
|
|
} else if (type.equals("aggregate")) {
|
|
|
aggregate(javaBuilder);
|
|
|
} else if (type.equals("multicast")) {
|
|
|
mulitcast(javaBuilder, value, nodeName, mDG, it, lineMap, nodeMap);
|
|
|
} else {
|
|
|
javaBuilder.append("\n.setHeader(Exchange.HTTP_METHOD, constant(\"POST\"))");
|
|
|
javaBuilder.append("\n.to(\"");
|
|
@ -215,24 +257,25 @@ public class ProcessManager {
|
|
|
}
|
|
|
|
|
|
//首字母转大写
|
|
|
public String toUpperCaseFirstOne(String s) {
|
|
|
public static String toUpperCaseFirstOne(String s) {
|
|
|
if(Character.isUpperCase(s.charAt(0)))
|
|
|
return s;
|
|
|
else
|
|
|
return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
|
|
|
}
|
|
|
|
|
|
public void judgement(StringBuilder javaBuilder, String value, String nodeName, DGraph<String> mDG, Iterator<String> it, Map<String, JsonNode> lineMap, Map<String, JsonNode> nodeMap) {
|
|
|
public static void judgement(StringBuilder javaBuilder, String value, String nodeName, DGraph<String> mDG, Iterator<String> it, Map<String, JsonNode> lineMap, Map<String, JsonNode> nodeMap) {
|
|
|
|
|
|
javaBuilder.append("\n.when("+value+")");
|
|
|
List<Edge<String>> edgeList = mDG.getEdgeList(nodeName);
|
|
|
String trueNodeName = "";
|
|
|
String falseNodeName = "";
|
|
|
for (Edge<String> edge : edgeList) {
|
|
|
it.next();
|
|
|
String nextNodeName = edge.getDest();
|
|
|
String nextLineName = edge.getName();
|
|
|
JsonNode nextLine = lineMap.get(nextLineName);
|
|
|
if (nextLine.get("value").asText().equals("right")) {
|
|
|
if (nextLine.get("value") != null &&nextLine.get("value").asText().equals("correct")) {
|
|
|
trueNodeName = nextNodeName;
|
|
|
} else {
|
|
|
falseNodeName = nextNodeName;
|
|
@ -250,7 +293,27 @@ public class ProcessManager {
|
|
|
javaBuilder.append("\n.setHeader(Exchange.HTTP_METHOD, constant(\"POST\"))");
|
|
|
javaBuilder.append("\n.to(\"");
|
|
|
javaBuilder.append(secondValue + "\")");
|
|
|
it.next();
|
|
|
it.next();
|
|
|
}
|
|
|
|
|
|
public static void split(StringBuilder javaBuilder, String value) {
|
|
|
javaBuilder.append("\n.split().method(Split.class, \""+value+"\")");
|
|
|
}
|
|
|
|
|
|
public static void aggregate(StringBuilder javaBuilder) {
|
|
|
javaBuilder.append("\n.aggregate(header(\"test_correlation_key\"), new Aggregate()).completionSize(3)");
|
|
|
}
|
|
|
|
|
|
public static void mulitcast(StringBuilder javaBuilder, String value, String nodeName, DGraph<String> mDG, Iterator<String> it, Map<String, JsonNode> lineMap, Map<String, JsonNode> nodeMap) {
|
|
|
|
|
|
List<Edge<String>> edgeList = mDG.getEdgeList(nodeName);
|
|
|
String endpoints = "";
|
|
|
for (Edge<String> edge : edgeList) {
|
|
|
String nextNodeName = edge.getDest();
|
|
|
JsonNode node = nodeMap.get(nextNodeName);
|
|
|
endpoints += "\"" + node.get("value") + "\",";
|
|
|
it.next();
|
|
|
}
|
|
|
endpoints = StringUtil.substring(endpoints, 0, endpoints.length() - 1);
|
|
|
javaBuilder.append("\n.multicast().stopOnException().to("+endpoints+").end()");
|
|
|
}
|
|
|
}
|