瀏覽代碼

流程编辑器

zhenglingfeng 8 年之前
父節點
當前提交
983f3c5230

+ 6 - 26
src/main/java/com/yihu/hos/system/controller/ProcessController.java

@ -28,41 +28,21 @@ public class ProcessController  extends BaseController {
        return "partView";
    }
    @RequestMapping(value = "/getAllApp", method = RequestMethod.GET)
    @RequestMapping(value = "/getEndpoints", method = RequestMethod.GET)
    @ResponseBody
    public Result getAllApp() {
    public Result getEndpoints(String endpointName) {
        try {
            return processManager.getAllApp();
            return processManager.getEndpoints(endpointName);
        } catch (Exception e) {
            return Result.error("查找所有应用失败");
        }
    }
    @RequestMapping(value = "/getAllAppService", method = RequestMethod.GET)
    @ResponseBody
    public Result getAllAppService() {
        try {
            return processManager.getAllAppService();
        } catch (Exception e) {
            return Result.error("查找所有服务失败");
        }
    }
    @RequestMapping(value = "/getAppService", method = RequestMethod.GET)
    @ResponseBody
    public Result getAppService(String appId) {
        try {
            return processManager.getAppServiceByAppId(appId);
        } catch (Exception e) {
            return Result.error("查找服务失败");
            return Result.error("查找端点失败");
        }
    }
    @RequestMapping(value = "/getAllProcessor", method = RequestMethod.GET)
    @ResponseBody
    public Result getAllProcessor() {
    public Result getAllProcessor(String processorName) {
        try {
            return processManager.getAllProcessor();
            return processManager.getAllProcessor(processorName);
        } catch (Exception e) {
            return Result.error("查找所有转换器失败");
        }

+ 10 - 0
src/main/java/com/yihu/hos/system/dao/AppDao.java

@ -3,6 +3,9 @@ package com.yihu.hos.system.dao;
import com.yihu.hos.system.model.SystemApp;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.model.Result;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
@ -33,4 +36,11 @@ public class AppDao extends SQLGeneralDAO  {
    public List<SystemApp> getAllApplist() throws Exception {
        return (List<SystemApp>) super.hibernateTemplate.find("from SystemApp");
    }
    public List<SystemApp> getAllByIdList(List<String> idList) {
        Session session = getCurrentSession();
        Criteria criteria = session.createCriteria(SystemApp.class);
        criteria.add(Restrictions.in("id", idList));
        return criteria.list();
    }
}

+ 14 - 2
src/main/java/com/yihu/hos/system/dao/AppServiceDao.java

@ -1,10 +1,13 @@
package com.yihu.hos.system.dao;
import com.yihu.hos.core.datatype.StringUtil;
import com.yihu.hos.system.model.SystemServiceEndpoint;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.model.Result;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Map;
/**
@ -32,5 +35,14 @@ public class AppServiceDao extends SQLGeneralDAO {
        }
        sb.append(" order by t.createDate desc");
        return super.getDataGridResult(sb.toString(), Integer.valueOf(params.get("page").toString()), Integer.valueOf(params.get("rows").toString()));    }
}
        return super.getDataGridResult(sb.toString(), Integer.valueOf(params.get("page").toString()), Integer.valueOf(params.get("rows").toString()));
    }
    public List<SystemServiceEndpoint> getListByName(String serviceName) throws Exception {
        String hql = "from SystemServiceEndpoint";
        if (!StringUtil.isEmpty(serviceName)) {
            hql += " where name = "+serviceName+"";
        }
        return getEntityList(SystemServiceEndpoint.class, hql);
    }
}

+ 13 - 1
src/main/java/com/yihu/hos/system/dao/ProcessorDao.java

@ -1,10 +1,13 @@
package com.yihu.hos.system.dao;
import com.yihu.hos.core.datatype.StringUtil;
import com.yihu.hos.system.model.SystemServiceFlowProcessor;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.model.Result;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Map;
/**
@ -22,5 +25,14 @@ public class ProcessorDao extends SQLGeneralDAO {
        if (!StringUtils.isEmpty(name)) {
            sb.append(" and (t.name like '%" + name + "%'')");
        }
        return super.getDataGridResult(sb.toString(), Integer.valueOf(params.get("page").toString()), Integer.valueOf(params.get("rows").toString()));    }
        return super.getDataGridResult(sb.toString(), Integer.valueOf(params.get("page").toString()), Integer.valueOf(params.get("rows").toString()));
    }
    public List<SystemServiceFlowProcessor> getListByName(String processorName) throws Exception {
        String hql = "from SystemServiceFlowProcessor";
        if (!StringUtil.isEmpty(processorName)) {
            hql += " where name = "+processorName+"";
        }
        return getEntityList(SystemServiceFlowProcessor.class, hql);
    }
}

+ 27 - 0
src/main/java/com/yihu/hos/system/model/ProcessResultModel.java

@ -0,0 +1,27 @@
package com.yihu.hos.system.model;
/**
 * SystemApp entity. @author MyEclipse Persistence Tools
 */
public class ProcessResultModel implements java.io.Serializable {
	private String name;
	private String value;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
}

+ 120 - 87
src/main/java/com/yihu/hos/system/service/ProcessManager.java

@ -10,19 +10,15 @@ 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.system.model.*;
import com.yihu.hos.web.framework.model.Result;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.*;
@Service("ProcessManager")
public class ProcessManager {
@ -40,31 +36,54 @@ public class ProcessManager {
    private ObjectMapper objectMapper = new ObjectMapper();
    public Result getAllApp() throws Exception {
        String hql = "from SystemApp";
        List<SystemApp> appList = appDao.getEntityList(SystemApp.class, hql);
        String result = objectMapper.writeValueAsString(appList);
        return Result.success(result);
    }
    public Result getEndpoints(String endpointName) throws Exception {
    public Result getAllAppService() throws Exception {
        String hql = "from SystemServiceEndpoint";
        List<SystemServiceEndpoint> serviceEndpointList = appServiceDao.getEntityList(SystemServiceEndpoint.class, hql);
        String result = objectMapper.writeValueAsString(serviceEndpointList);
        return Result.success(result);
    }
        List<SystemServiceEndpoint> serviceEndpointList = appServiceDao.getListByName(endpointName);
        List<String> appIdList = new ArrayList<>();
        Map<String, List<ProcessResultModel>> appServiceMap = new HashMap<>();
        for (SystemServiceEndpoint endpoint : serviceEndpointList) {
            ProcessResultModel resultModel = new ProcessResultModel();
            resultModel.setName(endpoint.getName());
            resultModel.setValue(endpoint.getEndpoint());
            appIdList.add(endpoint.getAppId());
            List<ProcessResultModel> endpointList;
            if (appServiceMap.get(endpoint.getAppId()) != null) {
                endpointList = appServiceMap.get(endpoint.getAppId());
            } else {
                endpointList = new ArrayList<>();
            }
            endpointList.add(resultModel);
            appServiceMap.put(endpoint.getAppId(), endpointList);
        }
        List<SystemApp> appList = appDao.getAllByIdList(appIdList);
        JSONArray jsonArray = new JSONArray();
        for (SystemApp app : appList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("title", app.getName());
            jsonObject.put("list", appServiceMap.get(app.getId()));
            jsonArray.add(jsonObject);
        }
    public Result getAppServiceByAppId(String appId) throws Exception {
        String hql = "from SystemServiceEndpoint where appId = "+appId+"";
        List<SystemServiceEndpoint> serviceEndpointList = appServiceDao.getEntityList(SystemServiceEndpoint.class, hql);
        String result = objectMapper.writeValueAsString(serviceEndpointList);
        return Result.success(result);
        return Result.success(jsonArray.toString());
    }
    public Result getAllProcessor() throws Exception {
        String hql = "from SystemServiceFlowProcessor";
        List<SystemServiceFlowProcessor> processorList = processorDao.getEntityList(SystemServiceFlowProcessor.class, hql);
        String result = objectMapper.writeValueAsString(processorList);
//    public Result getAppServiceByAppId(String appId) throws Exception {
//        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(String processorName) throws Exception {
        List<SystemServiceFlowProcessor> processorList = processorDao.getListByName(processorName);
        List<ProcessResultModel> resultModelList = new ArrayList<>();
        for (SystemServiceFlowProcessor processor : processorList) {
            ProcessResultModel resultModel = new ProcessResultModel();
            resultModel.setName(processor.getName());
            resultModel.setValue(processor.getClassName());
            resultModelList.add(resultModel);
        }
        String result = objectMapper.writeValueAsString(resultModelList);
        return Result.success(result);
    }
@ -79,7 +98,7 @@ public class ProcessManager {
    public static void main(String[] args) throws Exception {
        String flowJsonStr = "{\n" +
                "    \"code\": \"cralwer\",\n" +
                "    \"code\": \"crawler\",\n" +
                "    \"nodes\": {\n" +
                "        \"node_1\": {\n" +
                "            \"name\": \"quartz\",\n" +
@ -87,23 +106,28 @@ public class ProcessManager {
                "            \"type\": \"service\"\n" +
                "        },\n" +
                "        \"node_2\": {\n" +
                "            \"name\": \"CrawlerProcessor0\",\n" +
                "            \"value\": \"crawler.processor.CrawlerProcessor0\",\n" +
                "            \"name\": \"Processor0\",\n" +
                "            \"value\": \"crawler.processor.Processor0\",\n" +
                "            \"type\": \"processor\"\n" +
                "        },\n" +
                "        \"node_3\": {\n" +
                "            \"name\": \"crawler\",\n" +
                "            \"value\": \"body().isEqualTo(false)\",\n" +
                "            \"type\": \"judgement\"\n" +
                "            \"name\": \"crawler0\",\n" +
                "            \"value\": \"http4://localhost:8088/crawler/patientList\",\n" +
                "            \"type\": \"service\"\n" +
                "        },\n" +
                "        \"node_4\": {\n" +
                "            \"name\": \"crawler\",\n" +
                "            \"value\": \"http4://localhost:8088/crawler/collect\",\n" +
                "            \"type\": \"service\"\n" +
                "            \"name\": \"multicast0\",\n" +
                "            \"value\": \"2\",\n" +
                "            \"type\": \"multicast\"\n" +
                "        },\n" +
                "        \"node_5\": {\n" +
                "            \"name\": \"crawler\",\n" +
                "            \"value\": \"http4://localhost:8088/crawler/patientList\",\n" +
                "            \"name\": \"crawle1r1\",\n" +
                "            \"value\": \"http4://localhost:8088/crawler/cralwer\",\n" +
                "            \"type\": \"service\"\n" +
                "        },\n" +
                "        \"node_6\": {\n" +
                "            \"name\": \"crawler2\",\n" +
                "            \"value\": \"http4://localhost:8088/crawler/collect\",\n" +
                "            \"type\": \"service\"\n" +
                "        }\n" +
                "    },\n" +
@ -116,19 +140,21 @@ public class ProcessManager {
                "            \"from\": \"node_2\",\n" +
                "            \"to\": \"node_3\"\n" +
                "        },\n" +
                "        \"line_3\": {\n" +
                "        \"line3\": {\n" +
                "            \"from\": \"node_3\",\n" +
                "            \"to\": \"node_4\",\n" +
                "            \"value\": \"correct\"\n" +
                "            \"to\": \"node_4\"\n" +
                "        },\n" +
                "        \"line_4\": {\n" +
                "            \"from\": \"node_3\",\n" +
                "            \"from\": \"node_4\",\n" +
                "            \"to\": \"node_5\"\n" +
                "        },\n" +
                "        \"line_5\": {\n" +
                "            \"from\": \"node_4\",\n" +
                "            \"to\": \"node_6\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        formatJson(flowJsonStr);
    }
    public static String formatJson(String flowJsonStr) throws Exception {
@ -171,22 +197,24 @@ public class ProcessManager {
    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();
        StringBuilder bodyBuilder = new StringBuilder();
        StringBuilder packageBuilder = 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");
        javaBuilder.append("import crawler.SplitUtil;\n");
        packageBuilder.append("package "+code+".route;\n\n");
        packageBuilder.append("import org.apache.camel.Exchange;\n");
        packageBuilder.append("import org.apache.camel.builder.RouteBuilder;\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");
                packageBuilder.append("import " + node.get("value").asText() + ";\n");
            }
        }
        javaBuilder.append("public class "+javaName+" extends RouteBuilder {\n");
        javaBuilder.append("public void configure() throws Exception {\n");
        bodyBuilder.append("public class "+javaName+" extends RouteBuilder {\n");
        bodyBuilder.append("public void configure() throws Exception {\n");
        Iterator<String> it = mDG.iterator(root);
        while(it.hasNext()) {
@ -196,15 +224,15 @@ public class ProcessManager {
            String value = node.get("value").asText();
            String name = node.get("name").asText();
            if (isFirstNodeFlg) {
                javaBuilder.append("from(\"");
                javaBuilder.append(value + "\")");
                javaBuilder.append(".routeId(\""+code+"\")");
                bodyBuilder.append("from(\"");
                bodyBuilder.append(value + "\")");
                bodyBuilder.append(".routeId(\""+code+"\")");
                isFirstNodeFlg = false;
            } else {
                if (type.equals("processor")) {
                    JsonNode args = node.get("args");
                    if (args == null) {
                        javaBuilder.append("\n.process(\"new "+name+"())");
                        bodyBuilder.append("\n.process(new "+name+"())");
                    } else {
                        String argStr = "";
                        String[] argArr = args.asText().split(",");
@ -212,26 +240,26 @@ public class ProcessManager {
                            argStr += "\"" + arg + "\",";
                        }
                        argStr = StringUtil.substring(argStr, 0, argStr.length() - 1);
                        javaBuilder.append("\n.process(\"new "+name+"("+argStr+"))");
                        bodyBuilder.append("\n.process(new "+name+"("+argStr+"))");
                    }
                } else if (type.equals("judgement")) {
                    judgement(javaBuilder, value, nodeName, mDG, it, lineMap, nodeMap);
                    judgement(bodyBuilder, value, getEdgeList(nodeName, mDG), it, lineMap, nodeMap);
                } else if (type.equals("circle")) {
                    split(javaBuilder, value);
                    split(bodyBuilder, packageBuilder, value);
                } else if (type.equals("aggregate")) {
                    aggregate(javaBuilder);
                    aggregate(bodyBuilder, packageBuilder, value);
                } else if (type.equals("multicast")) {
                    mulitcast(javaBuilder, value, nodeName, mDG, it, lineMap, nodeMap);
                    mulitcast(bodyBuilder, value, getEdgeList(nodeName, mDG), it, lineMap, nodeMap);
                } else {
                    javaBuilder.append("\n.setHeader(Exchange.HTTP_METHOD, constant(\"POST\"))");
                    javaBuilder.append("\n.to(\"");
                    javaBuilder.append(value + "\")");
//                    bodyBuilder.append("\n.setHeader(Exchange.HTTP_METHOD, constant(\"POST\"))");
                    bodyBuilder.append("\n.to(\"");
                    bodyBuilder.append(value + "\")");
                }
            }
        }
        javaBuilder.append("\n}\n}");
        bodyBuilder.append(";");
        bodyBuilder.append("\n}\n}");
        javaBuilder.append(packageBuilder).append(bodyBuilder);
        System.out.println(javaBuilder.toString());
//        String packageFilePath = System.getProperty("user.dir");
//
@ -239,7 +267,7 @@ public class ProcessManager {
//        File file = new File(filePath);
//
//        FileWriter fw = new FileWriter(file);
//        fw.write(javaBuilder.toString());
//        fw.write(bodyBuilder.toString());
//        fw.flush();
//        fw.close();//这里只是产生一个JAVA文件,简单的IO操作
//
@ -264,10 +292,13 @@ public class ProcessManager {
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
    }
    public static void judgement(StringBuilder javaBuilder, String value, String nodeName, DGraph<String> mDG, Iterator<String> it,  Map<String, JsonNode> lineMap, Map<String, JsonNode> nodeMap) {
    public static List<Edge<String>> getEdgeList(String nodeName, DGraph<String> mDG) {
        return mDG.getEdgeList(nodeName);
    }
        javaBuilder.append("\n.when("+value+")");
        List<Edge<String>> edgeList = mDG.getEdgeList(nodeName);
    public static void judgement(StringBuilder bodyBuilder, String value, List<Edge<String>> edgeList, Iterator<String> it,  Map<String, JsonNode> lineMap, Map<String, JsonNode> nodeMap) {
        bodyBuilder.append("\n.choice()");
        bodyBuilder.append("\n.when("+value+")");
        String trueNodeName = "";
        String falseNodeName = "";
        for (Edge<String> edge : edgeList) {
@ -275,7 +306,7 @@ public class ProcessManager {
            String nextNodeName = edge.getDest();
            String nextLineName = edge.getName();
            JsonNode nextLine = lineMap.get(nextLineName);
            if (nextLine.get("value") != null &&nextLine.get("value").asText().equals("correct")) {
            if (nextLine.get("value") != null && nextLine.get("value").asText().equals("correct")) {
                trueNodeName = nextNodeName;
            } else {
                falseNodeName = nextNodeName;
@ -286,34 +317,36 @@ public class ProcessManager {
        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 + "\")");
        bodyBuilder.append("\n.setHeader(Exchange.HTTP_METHOD, constant(\"POST\"))");
        bodyBuilder.append("\n.to(\"");
        bodyBuilder.append(firstValue + "\")");
        bodyBuilder.append(".otherwise()");
        bodyBuilder.append("\n.setHeader(Exchange.HTTP_METHOD, constant(\"POST\"))");
        bodyBuilder.append("\n.to(\"");
        bodyBuilder.append(secondValue + "\")");
        bodyBuilder.append("\n.end()");
    }
    public static void split(StringBuilder javaBuilder, String value) {
        javaBuilder.append("\n.split().method(Split.class, \""+value+"\")");
    public static void split(StringBuilder bodyBuilder, StringBuilder packageBuilder, String value) {
        packageBuilder.append("import crawler.Split;\n");
        bodyBuilder.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 aggregate(StringBuilder bodyBuilder, StringBuilder packageBuilder, String value) {
        packageBuilder.append("import crawler.Aggregate;\n");
        bodyBuilder.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) {
    public static void mulitcast(StringBuilder bodyBuilder, String value, List<Edge<String>> edgeList, 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") + "\",";
            endpoints += node.get("value") + ",";
            it.next();
        }
        endpoints = StringUtil.substring(endpoints, 0, endpoints.length() - 1);
        javaBuilder.append("\n.multicast().stopOnException().to("+endpoints+").end()");
        bodyBuilder.append("\n.multicast().stopOnException().to("+endpoints+").end()");
    }
}

+ 1 - 1
src/main/resources/application.yml

@ -1,7 +1,7 @@
application:
  message: ESB
server:
  context-path: /
  context-path: /esb
  port: 8080
  session-timeout:  3000
security: