Forráskód Böngészése

流程添加-java/class文件上传解析

huangzhiyong 8 éve
szülő
commit
6a20246a6d

+ 2 - 1
hos-core/src/main/java/com/yihu/hos/core/datatype/ClassFileUtil.java

@ -292,7 +292,8 @@ public class ClassFileUtil {
            entity = response.getEntity();
            int code = response.getStatusLine().getStatusCode();
            if (code == 200) {
                System.out.println("请求成功,上传成功!entry:" +  response.getEntity().getContent().toString());;
                System.out.println("请求成功,上传成功!entry:" + response.getEntity().getContent().toString());
                ;
                return response.getEntity().getContent().toString();
            } else {
                System.out.println("上传请求失败,status:" + code);

+ 51 - 13
src/main/java/com/yihu/hos/system/controller/FlowController.java

@ -2,11 +2,13 @@ package com.yihu.hos.system.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.hos.config.BeanConfig;
import com.yihu.hos.core.datatype.ClassFileUtil;
import com.yihu.hos.system.model.SystemServiceFlow;
import com.yihu.hos.system.model.SystemServiceFlowClass;
import com.yihu.hos.system.model.SystemServiceFlowTemp;
import com.yihu.hos.system.service.FlowManager;
import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
import com.yihu.hos.web.framework.model.ActionResult;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
@ -15,17 +17,22 @@ import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 *  流程管理
 * 流程管理
 *
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/8/18.
@ -38,8 +45,9 @@ public class FlowController extends BaseController {
    private FlowManager flowManage;
    @Autowired
    private BeanConfig beanConfig;
    /**
     *  流程管理管理界面
     * 流程管理管理界面
     *
     * @param model
     * @return
@ -47,13 +55,13 @@ public class FlowController extends BaseController {
    @RequestMapping("/initial")
    public String appInitial(Model model) {
        model.addAttribute("contentPage", "system/flow/flow");
        model.addAttribute("dfs",beanConfig.getFsUrl());
        model.addAttribute("dfs", beanConfig.getFsUrl());
        return "partView";
    }
    @RequestMapping("/getFlowList")
    @ResponseBody
    public Result getFlowList(HttpServletRequest request,String name,String valid) {
    public Result getFlowList(HttpServletRequest request, String name, String valid) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("name", name);
@ -74,6 +82,7 @@ public class FlowController extends BaseController {
    /**
     * 流程管理-新增/修改页面
     *
     * @param model
     * @param id
     * @return
@ -90,12 +99,12 @@ public class FlowController extends BaseController {
                flowClassList = flowManage.getFlowClassByFlowId(flow.getId());
                flowTempList = flowManage.getFlowTempByFlowId(flow.getId());
                if (ServiceFlowConstant.CLASS.equals(flow.getFileType())){
                if (ServiceFlowConstant.CLASS.equals(flow.getFileType())) {
                    model.addAttribute("flowClass", objectMapper.writeValueAsString(flowClassList));
                }else if (ServiceFlowConstant.JAVA.equals(flow.getFileType())){
                } else if (ServiceFlowConstant.JAVA.equals(flow.getFileType())) {
                    model.addAttribute("flowClass", objectMapper.writeValueAsString(flowTempList));
                }
            }  else {
            } else {
                flow = new SystemServiceFlow();
                flowClassList = new ArrayList<>();
                flowTempList = new ArrayList<>();
@ -103,7 +112,7 @@ public class FlowController extends BaseController {
            flow.setFlowClassArray(flowClassList);
            flow.setFlowTempArray(flowTempList);
            model.addAttribute("model", flow);
            model.addAttribute("dfs",beanConfig.getFsUrl());
            model.addAttribute("dfs", beanConfig.getFsUrl());
            model.addAttribute("contentPage", "/system/flow/editorFlow");
        } catch (Exception e) {
            e.printStackTrace();
@ -114,15 +123,16 @@ public class FlowController extends BaseController {
    /**
     * 新增流程信息
     *
     * @param request
     * @return
     */
    @RequestMapping("addFlow")
    @ResponseBody
    public Result addFlow(HttpServletRequest request,String flowClass) {
    public Result addFlow(HttpServletRequest request, String flowClass) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            SystemServiceFlow flow = objectMapper.readValue(flowClass,SystemServiceFlow.class);
            SystemServiceFlow flow = objectMapper.readValue(flowClass, SystemServiceFlow.class);
            return flowManage.addFlow(flow);
        } catch (Exception ex) {
@ -133,6 +143,7 @@ public class FlowController extends BaseController {
    /**
     * 删除流程信息
     *
     * @param request
     * @return
     */
@ -145,7 +156,7 @@ public class FlowController extends BaseController {
            if (id != null && id.length() > 0) {
                flowManage.deleteFlow(Integer.parseInt(id));
                return Result.success("删除成功!");
            }else {
            } else {
                return Result.error("删除失败,该流程已不存在!");
            }
@ -160,10 +171,10 @@ public class FlowController extends BaseController {
     */
    @RequestMapping("updateFlow")
    @ResponseBody
    public Result updateFlow(HttpServletRequest request,String flowClass) {
    public Result updateFlow(HttpServletRequest request, String flowClass) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            SystemServiceFlow flow = objectMapper.readValue(flowClass,SystemServiceFlow.class);
            SystemServiceFlow flow = objectMapper.readValue(flowClass, SystemServiceFlow.class);
            return flowManage.updateFlow(flow);
        } catch (Exception ex) {
            ex.printStackTrace();
@ -184,5 +195,32 @@ public class FlowController extends BaseController {
        }
    }
    /**
     * 上传文件并解析文件属性:包名,类名等
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "parseFile", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public Result uploadAndParse(@RequestPart MultipartFile file) {
        ActionResult re = new ActionResult();
        File tempFile = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + file.getOriginalFilename());
        try {
            file.transferTo(tempFile);
            // 解析文件并返回类属性
            Map<String, String> map = flowManage.uploadAndParse(tempFile);
            String path = ClassFileUtil.uploadFile(beanConfig.getFsUrl(), tempFile, file.getName());
            map.put("path", path);
            re.setData(map);
            if (!StringUtils.isEmpty(path)) {
                tempFile.delete();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return re;
    }
}

+ 55 - 9
src/main/java/com/yihu/hos/system/service/FlowManager.java

@ -17,6 +17,7 @@ import com.yihu.hos.web.framework.model.ActionResult;
import com.yihu.hos.web.framework.model.DictItem;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.model.bo.ServiceFlow;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@ -24,11 +25,9 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
 * 系统流程管理业务类
@ -63,7 +62,7 @@ public class FlowManager {
     * @param newCron      cron表达式
     * @return
     */
    public String genRouteJavaFile(String jobId,String routeId, String className, String tempFilePath, String newCron) {
    public String genRouteJavaFile(String jobId, String routeId, String className, String tempFilePath, String newCron) {
        try {
            //TODO 临时文件夹
            String newFileName = className + routeId + ".java";
@ -87,7 +86,7 @@ public class FlowManager {
            //修改routeId;模板规则 routeId("routeId")
            text = text.replace("routeId(\"routeId\")", "routeId(\"" + routeId + "\")");
            //修改jobId;模板规则 "{\"jobId\": \"jobId\"}"
            text = text.replace("{\\\"jobId\\\": \\\"jobId\\\"}", "{\\\"jobId\\\": \\\""+ jobId +"\\\"}");
            text = text.replace("{\\\"jobId\\\": \\\"jobId\\\"}", "{\\\"jobId\\\": \\\"" + jobId + "\\\"}");
            boolean succ = FileUtil.writeFile(newFilePath, text, "UTF-8");
            if (succ) {
                newFileName = ClassFileUtil.uploadFile(beanConfig.getFsUrl(), new File(newFilePath), newFileName);
@ -504,7 +503,7 @@ public class FlowManager {
            }
            //生成新的route文件
            String newPath = genRouteJavaFile(jobId,flow.getCode(), flowTemp.getClassName(), flowTemp.getClassPath(), newCron);
            String newPath = genRouteJavaFile(jobId, flow.getCode(), flowTemp.getClassName(), flowTemp.getClassPath(), newCron);
            serviceFlow.setRouteCode(flow.getCode());
            serviceFlow.setFlowType(ServiceFlowConstant.JAVA);
            ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
@ -540,7 +539,7 @@ public class FlowManager {
            }
            //新增processor记录
            String newPath = genRouteJavaFile("" ,newFlow.getCode(), flowTemp.getClassName(), flowTemp.getClassPath(), newCron);
            String newPath = genRouteJavaFile("", newFlow.getCode(), flowTemp.getClassName(), flowTemp.getClassPath(), newCron);
            SystemServiceFlowClass newFlowClass = new SystemServiceFlowClass();
            newFlowClass.setPackageName(flowTemp.getPackageName());
            newFlowClass.setClassName(flowTemp.getClassName() + newFlow.getCode());
@ -681,7 +680,7 @@ public class FlowManager {
                    newPath = genProcessorJavaFile(jobId, newFlow.getCode(), process.getClassPath(), process.getClassName());
                    handleFile.setClassName(processClass.getClassName());
                } else {
                    newPath = genRouteJavaFile(jobId,newFlow.getCode(), process.getClassName(), process.getClassPath(), newCron);
                    newPath = genRouteJavaFile(jobId, newFlow.getCode(), process.getClassName(), process.getClassPath(), newCron);
                    handleFile.setClassName(processClass.getClassName() + newFlow.getCode());
                }
@ -749,5 +748,52 @@ public class FlowManager {
        }
    }
    /**
     * 解析文件 包名和类名
     *
     * @param fileInput java文件流
     * @return
     */
    public Map<String, String> uploadAndParse(File file) {
        Map<String, String> result = null;
        String text = null;
        String packageName = null;
        String className = StringUtils.substringBefore(file.getName(), ".");
        if (file.getName().contains(".class")) {
            try {//class文件解析
                text = parseClassFile(file);
                packageName = StringUtils.substringBefore(StringUtils.substringAfter(text, "public class "), "." + className);//通过javap解析的结构不一样
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {//java文件解析
            text = FileUtil.readFileText(file);
            packageName = StringUtils.substringAfter(StringUtils.substringBefore(text, ";"), "package ");
        }
        result = new HashMap<>();
        result.put("package", packageName);
        result.put("class", className);
        return result;
    }
    /**
     * 解析class文件
     *
     * @param file
     * @return
     * @throws Exception
     */
    public String parseClassFile(File file) throws Exception {
        String path = file.getAbsolutePath();
        String command = "javap " + path;
        Process ps = Runtime.getRuntime().exec(command);
        String result = FileUtil.readFileText(ps.getInputStream());
        System.out.println(result);
        return result;
    }
}

+ 19 - 10
src/main/webapp/WEB-INF/ehr/jsp/system/flow/editorFlowJs.jsp

@ -188,7 +188,7 @@
                var index = newMaxFormIndex();
                index++
                var html = '<div class="flows"  dataIndex="' + index + '" id="paramGroup' + index + '">' +
                        '<form id="form' + index + '" action="${dfs}" method="post" enctype="multipart/form-data">';
                        '<form id="form' + index + '" action="${contextRoot}/flow/parseFile" method="post" enctype="multipart/form-data">';
                html += '<div class="m-form-group" dataIndex="' + index + '"  ><label>类别 : </label><div class="m-form-control ">' +
                        '<div class="l-text"><input type="text" id="type' + index + '" class="l-text-field required" name="type"></div>' +
@ -326,7 +326,7 @@
                var flowJson = $.parseJSON(flowStr);
                $.each(flowJson, function (i, data) {
                    var html = '<div class="flows"  dataIndex="' + index + '" id="paramGroup' + index + '">' +
                            '<form id="form' + index + '" action="${dfs}" method="post" enctype="multipart/form-data">';
                            '<form id="form' + index + '" action="${contextRoot}/flow/parseFile" method="post" enctype="multipart/form-data">';
                    html += '<div class="m-form-group" dataIndex="' + index + '"  ><label>类别 :</label><div class="m-form-control "> ' +
                            '<div class="l-text"><input type="text" id="type' + index + '" value="' + data.type + '" class="l-text-field required" name="type"></div>' +
@ -397,16 +397,25 @@
            if (formObj.closest("div").find("input[type=text]").val().length != 0) {//判断是否选择了文件
                formsay.attr("class", "say true").html("上传中");
                formObj.ajaxForm({
                    dataType: 'text',
                    dataType: 'json',
                    type: "POST",
                    success: function processJson(data) {
                        formsay.attr("class", "say true").html("上传成功");
                        formObj.find("input[type=text]").filter(".btnGrayUp").attr("data-id", data)//设置已上传的文件的ID
                        var inputVal = formObj.closest("div").find("input[type=text]").filter(".btnGrayUp");
                        inputVal.val(data);
                        formObj.closest("div").find("span").html("重新导入<i></i>");
                        var isUpdate = formObj.find(".isUpdate");//是否有上传过
                        isUpdate.val("1");//上传过
                        debugger
                        if(data.successFlg){
                            formsay.attr("class", "say true").html("上传成功");
                            formObj.find("input[type=text]").filter(".btnGrayUp").attr("data-id", data.data.path)//设置已上传的文件的ID
                            var inputVal = formObj.closest("div").find("input[type=text]").filter(".btnGrayUp");
                            inputVal.val(data.data.path);
                            $("#packageName"+index).val(data.data.package);
                            $("#className"+index).val(data.data.class);
                            formObj.closest("div").find("span").html("重新导入<i></i>");
                            var isUpdate = formObj.find(".isUpdate");//是否有上传过
                            isUpdate.val("1");//上传过
                        }else{
                            $.ligerDialog.error("上传格式有问题");
                            Obj.css("background","#ebebeb").html("导入");
                        }
                    },
                    error: function processJson(data) {
                        formsay.attr("class", "say false").html("上传失败");