BpmExtendJarController.java 5.43 KB
package com.hotent.runtime.controller;

import com.hotent.base.controller.BaseController;
import com.hotent.base.exception.MessagePassingException;
import com.hotent.base.groovy.IScript;
import com.hotent.base.groovy.IUserScript;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.ExtendClassLoader;
import com.hotent.base.util.FileUtil;
import com.hotent.runtime.manager.BpmExtendJarManager;
import com.hotent.runtime.model.BpmExtendClass;
import com.hotent.runtime.model.BpmExtendJar;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;

/**
 * 扩展jar包管理模块 前端控制器
 *
 * @company 广州宏天软件股份有限公司
 * @author 超级管理员
 * @since 2021-01-19
 */
@RestController
@RequestMapping("/bpmExtendJar/v1/")
public class BpmExtendJarController extends BaseController<BpmExtendJarManager, BpmExtendJar> {

	@RequestMapping(value = "resolutionJar", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "解析上传的jar包", httpMethod = "POST", notes = "解析上传的jar包")
	public CommonResult<BpmExtendJar> resolutionJar(MultipartHttpServletRequest request,
						@ApiParam(name="type",value="jar扩展包类型") @RequestParam(required = false) Integer type) throws Exception {
		Map<String, MultipartFile> fileMaps = request.getFileMap();
		Iterator<MultipartFile> it = fileMaps.values().iterator();
		Integer jarType = 2;
		if(request.getParameter("type") != null){
			jarType = Integer.parseInt(request.getParameter("type"));
		}
		BpmExtendJar jarManage = new BpmExtendJar();
		while (it.hasNext()) {
			MultipartFile f = it.next();
			String oriFileName = f.getOriginalFilename();
			jarManage.setJarBytes(FileUtil.readByte(f.getInputStream()));
			jarManage.setSize(Integer.valueOf((int) Math.round((double) f.getSize() / 1024)));
			jarManage.setName(oriFileName);
			jarManage.setType(jarType);
            setJarClassList(jarManage);
		}
		return new CommonResult<BpmExtendJar>(true, "上传成功", jarManage);
	}

	@SuppressWarnings("rawtypes")
	private void setJarClassList(BpmExtendJar jarManage) throws IOException {
		Map<String, Class> resolutionJarClass = null;
        try {
            resolutionJarClass = ExtendClassLoader.resolutionJarClass(jarManage.getJarBytes(), jarManage.getType());
        }catch (NullPointerException e){
            throw new MessagePassingException("文件不合法");
        }
		if (BeanUtils.isNotEmpty(resolutionJarClass)) {
			Set<String> simpleNmaes = new HashSet<>();
			List<BpmExtendClass> addClassList = new ArrayList<>();
			for (Iterator<Entry<String, Class>> iterator = resolutionJarClass.entrySet().iterator(); iterator.hasNext();) {
				Entry<String, Class> next = iterator.next();
				Class cls = next.getValue();
				BpmExtendClass extendClass = new BpmExtendClass();
				extendClass.setPath(cls.getName());
				extendClass.setSimpleName(ExtendClassLoader.getSimpleName(cls.getName()));
				simpleNmaes.add(extendClass.getSimpleName());
				extendClass.setMd5(next.getKey());
				if (IUserScript.class.isAssignableFrom(cls)) {
					extendClass.setType("2");
				} else if (IScript.class.isAssignableFrom(cls)) {
					extendClass.setType("1");
				}
				addClassList.add(extendClass);
			}
			jarManage.setClassList(addClassList);
		}
	}

	@RequestMapping(value = "saveJar", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "附件上传操作", httpMethod = "POST", notes = "附件上传操作")
	public CommonResult<String> saveJar(@ApiParam(name = "jar", value = "jar包管理对象") @RequestBody BpmExtendJar jar)throws Exception {
		return baseService.saveJar(jar);
	}

	@DeleteMapping("deleteById")
	@ApiOperation("根据id删除")
	public CommonResult<String> deleteById(@ApiParam(name = "id", value = "实体id") @RequestParam String id) {
		 baseService.remove(id);
		return new CommonResult<>();
	}
	
	@GetMapping("getJarById")
	@ApiOperation("根据id删除")
	public BpmExtendJar getJarById(@ApiParam(name = "id", value = "实体id") @RequestParam String id) throws IOException {
		 BpmExtendJar byId = baseService.getById(id);
		 setJarClassList(byId);
		return byId;
	}
	
	@RequestMapping(value = "publishJar", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "发布jar包将其中的脚本类注册到服务中", httpMethod = "GET", notes = "发布jar包将其中的脚本类注册到服务中")
	public CommonResult<String> publishJar(@ApiParam(name = "jarId", value = "jar包Id") @RequestParam String jarId,
                                           @ApiParam(name = "iscover", value = "是否覆盖同名class") @RequestParam Optional<Boolean> iscover)throws Exception {
		return baseService.publishJar(jarId, iscover.orElse(false));
	}

	/**
	 * 根据类型查找对应的jar
	 * @param type  类型
	 * @return jar信息列表
	 */
	@RequestMapping(value = "getJarByType", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	public List<BpmExtendJar> getJarByType(@ApiParam(name = "type", value = "jar包类型") @RequestParam(value="type") Integer type) {
		return baseService.getJarByType(type);
	}
}