|
@ -2,14 +2,20 @@ package com.yihu.hos.system.service;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
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;
|
|
|
import com.yihu.hos.system.dao.ProcessorDao;
|
|
|
import com.yihu.hos.system.model.SystemApp;
|
|
|
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;
|
|
@ -18,6 +24,7 @@ import org.springframework.stereotype.Service;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.File;
|
|
|
import java.io.FileWriter;
|
|
|
import java.io.IOException;
|
|
|
import java.util.*;
|
|
|
|
|
|
@Service("ProcessManager")
|
|
@ -29,15 +36,17 @@ public class ProcessManager {
|
|
|
private AppDao appDao;
|
|
|
@Resource(name = AppServiceDao.BEAN_ID)
|
|
|
private AppServiceDao appServiceDao;
|
|
|
@Resource(name = ProcessorDao.BEAN_ID)
|
|
|
private ProcessorDao processorDao;
|
|
|
@Resource(name = FlowProcessDao.BEAN_ID)
|
|
|
private FlowProcessDao processDao;
|
|
|
|
|
|
private ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
public Result getAllApp() throws Exception {
|
|
|
String hql = "select * from SystemServiceEndpoint";
|
|
|
List<SystemServiceEndpoint> serviceEndpointList = appServiceDao.getEntityList(SystemServiceEndpoint.class, hql);
|
|
|
String result = objectMapper.writeValueAsString(serviceEndpointList);
|
|
|
String hql = "select * from SystemApp";
|
|
|
List<SystemApp> appList = appDao.getEntityList(SystemApp.class, hql);
|
|
|
String result = objectMapper.writeValueAsString(appList);
|
|
|
return Result.success(result);
|
|
|
}
|
|
|
|
|
@ -55,6 +64,13 @@ public class ProcessManager {
|
|
|
return Result.success(result);
|
|
|
}
|
|
|
|
|
|
public Result getAllProcessor() throws Exception {
|
|
|
String hql = "select * from SystemServiceFlowProcessor";
|
|
|
List<SystemServiceFlowProcessor> processorList = processorDao.getEntityList(SystemServiceFlowProcessor.class, hql);
|
|
|
String result = objectMapper.writeValueAsString(processorList);
|
|
|
return Result.success(result);
|
|
|
}
|
|
|
|
|
|
public void saveProcess(String code, String name, String fileName, String positionJsonStr) throws Exception {
|
|
|
SystemServiceFlowProcess process = new SystemServiceFlowProcess();
|
|
|
process.setCode(code);
|
|
@ -95,70 +111,61 @@ public class ProcessManager {
|
|
|
" }\n" +
|
|
|
" }\n" +
|
|
|
"}";
|
|
|
|
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
JsonNode flowJson = objectMapper.readValue(flowJsonStr, JsonNode.class);
|
|
|
String code = flowJson.get("code").asText();
|
|
|
String javaName = toUpperCaseFirstOne(code)+"Route";
|
|
|
//sort flow by lines
|
|
|
JsonNode lines = flowJson.get("lines");
|
|
|
Iterator<JsonNode> lineIterator = lines.iterator();
|
|
|
String[] nodeNameArray = new String[lines.size() + 1];
|
|
|
Boolean isFirstLineFlg = true;
|
|
|
while (lineIterator.hasNext()) {
|
|
|
JsonNode line = lineIterator.next();
|
|
|
String nodeNameFrom = line.get("from").asText();
|
|
|
String nodeNameTo = line.get("to").asText();
|
|
|
if (isFirstLineFlg) {
|
|
|
nodeNameArray[0] = nodeNameFrom;
|
|
|
nodeNameArray[1] = nodeNameTo;
|
|
|
isFirstLineFlg = false;
|
|
|
continue;
|
|
|
}
|
|
|
for (int i=0; i<nodeNameArray.length; i++) {
|
|
|
if (nodeNameArray[i].equals(nodeNameFrom)) {
|
|
|
if (StringUtil.isEmpty(nodeNameArray[i + 1])) {
|
|
|
nodeNameArray[i + 1] = nodeNameTo;
|
|
|
} else {
|
|
|
insertArray(nodeNameTo, nodeNameArray, i + 1);
|
|
|
}
|
|
|
break;
|
|
|
} else if (nodeNameArray[i].equals(nodeNameTo)) {
|
|
|
insertArray(nodeNameFrom, nodeNameArray, i);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
JsonNode lines = flowJson.get("lines");
|
|
|
Iterator<Map.Entry<String, JsonNode>> lineIterator = lines.fields();
|
|
|
//get nodeMap by nodes
|
|
|
JsonNode nodes = flowJson.get("nodes");
|
|
|
Map<String, JsonNode> nodeMap = new HashMap<>();
|
|
|
Iterator<Map.Entry<String, JsonNode>> nodeIterator = nodes.fields();
|
|
|
//for the java code import processor class
|
|
|
List<String> processorImport = new ArrayList<>();
|
|
|
Map<String, JsonNode> nodeMap = new HashMap<>();
|
|
|
Map<String, JsonNode> lineMap = new HashMap<>();
|
|
|
DGraph<String> mDG = new BFSGraph<String>();
|
|
|
while (nodeIterator.hasNext()) {
|
|
|
Map.Entry<String, JsonNode> map = nodeIterator.next();
|
|
|
JsonNode node = map.getValue();
|
|
|
String type = node.get("type").asText();
|
|
|
if (type.equals("processor")) {
|
|
|
processorImport.add(node.get("value").asText());
|
|
|
}
|
|
|
nodeMap.put(map.getKey(), map.getValue());
|
|
|
mDG.add(map.getKey());
|
|
|
}
|
|
|
|
|
|
while (lineIterator.hasNext()) {
|
|
|
Map.Entry<String, JsonNode> map = lineIterator.next();
|
|
|
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()));
|
|
|
}
|
|
|
|
|
|
//mosaic the java code
|
|
|
//generate the java code
|
|
|
return generate(code, lineMap, nodeMap, mDG);
|
|
|
}
|
|
|
|
|
|
public String generate(String code, 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";
|
|
|
|
|
|
javaBuilder.append("package "+code+".route;\n\n");
|
|
|
javaBuilder.append("import org.apache.camel.Exchange;\n");
|
|
|
javaBuilder.append("import org.apache.camel.builder.RouteBuilder;\n");
|
|
|
for (String packageName : processorImport) {
|
|
|
javaBuilder.append("import " + packageName + ";\n");
|
|
|
for (String key : nodeMap.keySet()) {
|
|
|
JsonNode node = nodeMap.get(key);
|
|
|
String type = node.get("type").asText();
|
|
|
if (type.equals("processor")) {
|
|
|
javaBuilder.append("import " + node.get("value").asText() + ";\n");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
javaBuilder.append("public class "+javaName+" extends RouteBuilder {\n");
|
|
|
javaBuilder.append("public void configure() throws Exception {\n");
|
|
|
for (String nodeName : nodeNameArray) {
|
|
|
|
|
|
Iterator<String> it = mDG.iterator("1");
|
|
|
while(it.hasNext()) {
|
|
|
String nodeName = it.next();
|
|
|
JsonNode node = nodeMap.get(nodeName);
|
|
|
String type = node.get("type").asText();
|
|
|
String value = node.get("value").asText();
|
|
@ -171,6 +178,8 @@ public class ProcessManager {
|
|
|
} else {
|
|
|
if (type.equals("processor")) {
|
|
|
javaBuilder.append("\n.process(\"new "+name+"())");
|
|
|
} else if (type.equals("judgement")) {
|
|
|
judgement(javaBuilder, value, nodeName, mDG, it, lineMap, nodeMap);
|
|
|
} else {
|
|
|
javaBuilder.append("\n.setHeader(Exchange.HTTP_METHOD, constant(\"POST\"))");
|
|
|
javaBuilder.append("\n.to(\"");
|
|
@ -178,48 +187,70 @@ public class ProcessManager {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
javaBuilder.append("\n}\n}");
|
|
|
|
|
|
System.out.println(javaBuilder.toString());
|
|
|
String packageFilePath = System.getProperty("user.dir");
|
|
|
|
|
|
String filePath = packageFilePath + "/" + javaName + ".java";
|
|
|
File file = new File(filePath);
|
|
|
|
|
|
FileWriter fw = new FileWriter(file);
|
|
|
fw.write(javaBuilder.toString());
|
|
|
fw.flush();
|
|
|
fw.close();//这里只是产生一个JAVA文件,简单的IO操作
|
|
|
|
|
|
//upload to mongo
|
|
|
String dbName = "upload";
|
|
|
String newFileName;
|
|
|
try {
|
|
|
newFileName = GridFSUtil.uploadFile(filePath, file.getName(), null);
|
|
|
if (!StringUtil.isEmpty(newFileName)) {
|
|
|
return newFileName;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
// String packageFilePath = System.getProperty("user.dir");
|
|
|
//
|
|
|
// String filePath = packageFilePath + "/" + javaName + ".java";
|
|
|
// File file = new File(filePath);
|
|
|
//
|
|
|
// FileWriter fw = new FileWriter(file);
|
|
|
// fw.write(javaBuilder.toString());
|
|
|
// fw.flush();
|
|
|
// fw.close();//这里只是产生一个JAVA文件,简单的IO操作
|
|
|
//
|
|
|
// //upload to mongo
|
|
|
// String newFileName;
|
|
|
// try {
|
|
|
// newFileName = GridFSUtil.uploadFile(filePath, file.getName(), null);
|
|
|
// if (!StringUtil.isEmpty(newFileName)) {
|
|
|
// return newFileName;
|
|
|
// }
|
|
|
// } catch (Exception e) {
|
|
|
// e.printStackTrace();
|
|
|
// }
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
public void insertArray(String nodeName, String[] array, int index) {
|
|
|
for (int i=index; i<array.length; i++) {
|
|
|
String nodeNameTemp = array[i];
|
|
|
array[i] = nodeName;
|
|
|
nodeName = nodeNameTemp;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//首字母转大写
|
|
|
public String toUpperCaseFirstOne(String s)
|
|
|
{
|
|
|
public 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) {
|
|
|
|
|
|
javaBuilder.append("\n.when("+value+")");
|
|
|
List<Edge<String>> edgeList = mDG.getEdgeList(nodeName);
|
|
|
String trueNodeName = "";
|
|
|
String falseNodeName = "";
|
|
|
for (Edge<String> edge : edgeList) {
|
|
|
String nextNodeName = edge.getDest();
|
|
|
String nextLineName = edge.getName();
|
|
|
JsonNode nextLine = lineMap.get(nextLineName);
|
|
|
if (nextLine.get("value").asText().equals("right")) {
|
|
|
trueNodeName = nextNodeName;
|
|
|
} else {
|
|
|
falseNodeName = nextNodeName;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
JsonNode node1 = nodeMap.get(trueNodeName);
|
|
|
JsonNode node2 = nodeMap.get(falseNodeName);
|
|
|
String firstValue = node1.get("value").asText();
|
|
|
String secondValue = node2.get("value").asText();
|
|
|
javaBuilder.append("\n.setHeader(Exchange.HTTP_METHOD, constant(\"POST\"))");
|
|
|
javaBuilder.append("\n.to(\"");
|
|
|
javaBuilder.append(firstValue + "\")");
|
|
|
javaBuilder.append(".otherwise()");
|
|
|
javaBuilder.append("\n.setHeader(Exchange.HTTP_METHOD, constant(\"POST\"))");
|
|
|
javaBuilder.append("\n.to(\"");
|
|
|
javaBuilder.append(secondValue + "\")");
|
|
|
it.next();
|
|
|
it.next();
|
|
|
}
|
|
|
}
|