FlowManager.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. package com.yihu.hos.system.service;
  2. import com.yihu.hos.common.constants.ContextAttributes;
  3. import com.yihu.hos.config.BeanConfig;
  4. import com.yihu.hos.core.datatype.ClassFileUtil;
  5. import com.yihu.hos.core.encrypt.DES;
  6. import com.yihu.hos.core.file.FileUtil;
  7. import com.yihu.hos.interceptor.LocalContext;
  8. import com.yihu.hos.services.ServiceFlowEventService;
  9. import com.yihu.hos.system.dao.FlowClassDao;
  10. import com.yihu.hos.system.dao.FlowDao;
  11. import com.yihu.hos.system.dao.FlowTempDao;
  12. import com.yihu.hos.system.model.SystemServiceFlow;
  13. import com.yihu.hos.system.model.SystemServiceFlowClass;
  14. import com.yihu.hos.system.model.SystemServiceFlowTemp;
  15. import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
  16. import com.yihu.hos.web.framework.model.ActionResult;
  17. import com.yihu.hos.web.framework.model.DictItem;
  18. import com.yihu.hos.web.framework.model.Result;
  19. import com.yihu.hos.web.framework.model.bo.ServiceFlow;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.transaction.annotation.Transactional;
  23. import javax.annotation.Resource;
  24. import java.io.File;
  25. import java.nio.charset.StandardCharsets;
  26. import java.util.ArrayList;
  27. import java.util.Date;
  28. import java.util.List;
  29. import java.util.Map;
  30. /**
  31. * 系统流程管理业务类
  32. *
  33. * @author HZY
  34. * @vsrsion 1.0
  35. * Created at 2016/8/19.
  36. */
  37. @Service("flowManager")
  38. public class FlowManager {
  39. public static final String BEAN_ID = "flowManager";
  40. @Autowired
  41. ServiceFlowEventService serviceFlowEventService;
  42. @Autowired
  43. private BeanConfig beanConfig;
  44. @Resource(name = "flowDao")
  45. private FlowDao flowDao;
  46. @Resource(name = FlowClassDao.BEAN_ID)
  47. private FlowClassDao flowClassDao;
  48. @Resource(name = FlowTempDao.BEAN_ID)
  49. private FlowTempDao flowTempDao;
  50. /**
  51. * 生成Route流程的java文件
  52. *
  53. * @param routeId 流程Id
  54. * @param className 模板类名
  55. * @param tempFilePath 模板文件路径
  56. * @param newCron cron表达式
  57. * @return
  58. */
  59. public String genRouteJavaFile(String routeId, String className, String tempFilePath, String newCron) {
  60. try {
  61. String newFileName = className + routeId + ".java";
  62. String newFilePath = "/temp/" + newFileName;
  63. byte[] content = ClassFileUtil.down(tempFilePath);
  64. if (content == null) {
  65. return null;
  66. }
  67. String text = new String(content, StandardCharsets.UTF_8);
  68. if (text.contains("?cron=")) {
  69. String oldStr = text.substring(text.indexOf("?cron=") + 6);
  70. String cron = oldStr.substring(0, oldStr.indexOf("\""));
  71. text = text.replace(cron, newCron);
  72. }
  73. //修改java类名
  74. if (text.contains(className)) {
  75. text = text.replace(className, className + routeId);//新类名规则=旧类名+routeId
  76. }
  77. //修改routeId;模板规则 routeId("routeId")
  78. text = text.replace("routeId(\"routeId\")", "routeId(\"" + routeId + "\")");
  79. boolean succ = FileUtil.writeFile(newFilePath, text, "UTF-8");
  80. if (succ) {
  81. newFileName = ClassFileUtil.uploadFile(beanConfig.getFsUrl(), new File(newFilePath + newFileName), newFileName);
  82. } else {
  83. return null;
  84. }
  85. return newFileName;
  86. } catch (Exception e) {
  87. System.out.println("修改Route的java文件操作出错");
  88. e.printStackTrace();
  89. }
  90. return null;
  91. }
  92. /**
  93. * 生成processor的java文件
  94. *
  95. * @param jobId 任务Id
  96. * @param routeId 流程Code
  97. * @param tempFilePath 模板文件名
  98. * @param className 模板类名
  99. * @return
  100. */
  101. public String genProcessorJavaFile(String jobId, String routeId, String tempFilePath, String className) {
  102. try {
  103. String newFileName = className + routeId + ".java";
  104. String newFilePath = "/temp/" + className;
  105. byte[] content = ClassFileUtil.down(tempFilePath);
  106. if (content == null) {
  107. return null;
  108. }
  109. String text = new String(content, StandardCharsets.UTF_8);
  110. text = text.replace("jobId=jobId", "jobId=" + jobId);
  111. boolean succ = FileUtil.writeFile(newFilePath, text, "UTF-8");
  112. //TODO 上传到GridFS
  113. if (succ) {
  114. newFileName = ClassFileUtil.uploadFile(beanConfig.getFsUrl(), new File(newFilePath + newFileName), newFileName);
  115. return newFileName;
  116. }
  117. } catch (Exception e) {
  118. System.out.println("生成processor的java文件操作出错");
  119. e.printStackTrace();
  120. }
  121. return null;
  122. }
  123. public Result getFlowList(Map<String, Object> params) throws Exception {
  124. return flowDao.getFlowList(params);
  125. }
  126. /**
  127. * @return List<ServiceFlow> 返回所有可运行流程
  128. * @throws Exception ...
  129. */
  130. public List<ServiceFlow> getServiceFlowList() throws Exception {
  131. List<ServiceFlow> serviceFlowList = new ArrayList<>();
  132. List<SystemServiceFlow> classFlowList = flowDao.getFlowList(ServiceFlowConstant.CLASS);
  133. for (SystemServiceFlow systemServiceFlow : classFlowList) {
  134. String tenant = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
  135. ServiceFlow serviceFlow = new ServiceFlow();
  136. serviceFlow.setRouteCode(systemServiceFlow.getCode());
  137. serviceFlow.setFlowType(systemServiceFlow.getFileType());
  138. serviceFlow.setTenant(tenant);
  139. List<SystemServiceFlowClass> classList = flowClassDao.getFlowClassByFlowId(systemServiceFlow.getId());
  140. ArrayList<ServiceFlow.HandleFile> handleFileList = new ArrayList<>();
  141. for (SystemServiceFlowClass flowClass : classList) {
  142. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  143. handleFile.setFileType(ServiceFlowConstant.CLASS);
  144. handleFile.setClassName(flowClass.getClassName());
  145. handleFile.setPackageName(flowClass.getPackageName());
  146. handleFile.setFilePath(flowClass.getClassPath());
  147. handleFile.setUsage(flowClass.getType());
  148. handleFile.setRouteCode(serviceFlow.getRouteCode());
  149. handleFileList.add(handleFile);
  150. }
  151. serviceFlow.setHandleFiles(handleFileList);
  152. serviceFlowList.add(serviceFlow);
  153. }
  154. return serviceFlowList;
  155. }
  156. /* =================================== flowClass 部分================================================*/
  157. public SystemServiceFlow getFlowById(Integer id) throws Exception {
  158. return flowDao.getEntity(SystemServiceFlow.class, id);
  159. }
  160. @Transactional
  161. public Result addFlow(SystemServiceFlow obj) throws Exception {
  162. obj.setCreateDate(new Date());
  163. flowDao.saveEntity(obj);
  164. if (ServiceFlowConstant.CLASS.equals(obj.getFileType())) {
  165. List<SystemServiceFlowClass> flowClassList = obj.getFlowClassArray();
  166. ServiceFlow serviceFlow = new ServiceFlow();
  167. serviceFlow.setRouteCode(obj.getCode());
  168. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  169. for (SystemServiceFlowClass flowClass : flowClassList) {
  170. flowClass.setFlowId(obj.getId());
  171. flowDao.saveEntity(flowClass);
  172. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  173. handleFile.setRouteCode(obj.getCode());
  174. handleFile.setFileType(ServiceFlowConstant.CLASS);
  175. handleFile.setPackageName(flowClass.getPackageName());
  176. handleFile.setClassName(flowClass.getClassName());
  177. handleFile.setFilePath(flowClass.getClassPath());
  178. handleFile.setUsage(flowClass.getType());
  179. serviceFlow.addHandleFile(handleFile);
  180. }
  181. serviceFlowEventService.serviceFlowAdded(serviceFlow);
  182. } else if (ServiceFlowConstant.JAVA.equals(obj.getFileType())) {
  183. List<SystemServiceFlowTemp> flowTempList = obj.getFlowTempArray();
  184. for (SystemServiceFlowTemp flowTemp : flowTempList) {
  185. flowTemp.setFlowId(obj.getId());
  186. flowDao.saveEntity(flowTemp);
  187. }
  188. }
  189. return Result.success("保存成功");
  190. }
  191. @Transactional
  192. public Result updateFlow(SystemServiceFlow obj) throws Exception {
  193. ServiceFlow serviceFlow = new ServiceFlow();
  194. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, obj.getId());
  195. flow.setCode(obj.getCode());
  196. flow.setName(obj.getName());
  197. flow.setPath(obj.getPath());
  198. flow.setChart(obj.getChart());
  199. flow.setValid(obj.getValid());
  200. flow.setFileType(obj.getFileType());
  201. if (ServiceFlowConstant.JAVA.equals(flow.getFileType())) {
  202. List<SystemServiceFlowTemp> flowTempList = obj.getFlowTempArray();
  203. boolean succ = flowTempDao.deleteFlowTempByFlowId(obj.getId());
  204. if (succ) {
  205. for (SystemServiceFlowTemp flowTemp : flowTempList) {
  206. flowTempDao.saveEntity(flowTemp);
  207. }
  208. }
  209. } else if (ServiceFlowConstant.CLASS.equals(flow.getFileType())) {
  210. List<Integer> classIds = flowClassDao.getFlowClassIds(obj.getId());//原flowclass集合
  211. List<SystemServiceFlowClass> flowClassList = obj.getFlowClassArray();
  212. SystemServiceFlowClass flowClassRoute = null;
  213. serviceFlow.setRouteCode(obj.getCode());
  214. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  215. for (SystemServiceFlowClass flowClass : flowClassList) {
  216. if (flowClass.getId() != null) {
  217. classIds.remove(flowClass.getId());
  218. flowClassDao.updateEntity(flowClass);
  219. } else {
  220. if (!flowClass.getType().equals(ServiceFlowConstant.FLOW_TYPE_ROUTE)) {
  221. flowClassDao.saveEntity(flowClass);
  222. }
  223. }
  224. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  225. handleFile.setRouteCode(obj.getCode());
  226. handleFile.setFileType(ServiceFlowConstant.CLASS);
  227. handleFile.setPackageName(flowClass.getPackageName());
  228. handleFile.setClassName(flowClass.getClassName());
  229. handleFile.setFilePath(flowClass.getClassPath());
  230. handleFile.setUsage(flowClass.getType());
  231. serviceFlow.addHandleFile(handleFile);
  232. }
  233. serviceFlowEventService.serviceFlowModifiedAdd(serviceFlow);
  234. //删除判断
  235. if (classIds != null && classIds.size() > 0) {
  236. for (Integer id : classIds) {
  237. SystemServiceFlowClass flowClass = getFlowClassById(id);
  238. flowClassDao.deleteEntity(flowClass);
  239. if (flowClass.getType().equals(ServiceFlowConstant.FLOW_TYPE_ROUTE)) {
  240. flowClassRoute = flowClass;
  241. flowClassRoute.setIsUpdate("1");
  242. }
  243. }
  244. }
  245. }
  246. flowDao.updateEntity(flow);
  247. return Result.success("更新成功");
  248. }
  249. @Transactional
  250. public Result deleteFlow(Integer id) throws Exception {
  251. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, id);
  252. if (flow == null) {
  253. return Result.error("删除流程失败");
  254. }
  255. ArrayList<ServiceFlow.HandleFile> handleFiles = new ArrayList<>();
  256. List<SystemServiceFlowClass> flowClassList = flowClassDao.getFlowClassByFlowId(id);
  257. if (ServiceFlowConstant.JAVA.equals(flow.getFileType())) {
  258. flowTempDao.deleteFlowTempByFlowId(id);
  259. } else {
  260. ServiceFlow serviceFlow = new ServiceFlow();
  261. serviceFlow.setRouteCode(flow.getCode());
  262. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  263. for (SystemServiceFlowClass flowClass : flowClassList) {
  264. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  265. handleFile.setRouteCode(flow.getCode());
  266. handleFile.setFileType(ServiceFlowConstant.JAVA);
  267. handleFile.setPackageName(flowClass.getPackageName());
  268. handleFile.setClassName(flowClass.getClassName());
  269. handleFile.setFilePath(flowClass.getClassPath());
  270. handleFile.setUsage(flowClass.getType());
  271. handleFiles.add(handleFile);
  272. flowClassDao.deleteEntity(flowClass);
  273. }
  274. serviceFlow.setHandleFiles(handleFiles);
  275. serviceFlowEventService.serviceFlowDelete(serviceFlow);
  276. }
  277. flowDao.deleteEntity(flow);
  278. return Result.success("删除流程成功");
  279. }
  280. public Result getFlowClassList(Map<String, Object> params) throws Exception {
  281. return null;
  282. }
  283. public List<SystemServiceFlowClass> getFlowClassByFlowId(Integer flowId) throws Exception {
  284. return flowClassDao.getFlowClassByFlowId(flowId);
  285. }
  286. public SystemServiceFlowClass getFlowClassById(Integer id) throws Exception {
  287. return flowClassDao.getEntity(SystemServiceFlowClass.class, id);
  288. }
  289. @Transactional
  290. public Result addFlowClass(SystemServiceFlowClass obj) throws Exception {
  291. flowDao.saveEntity(obj);
  292. return Result.success("保存成功");
  293. }
  294. @Transactional
  295. public Result updateFlowClass(SystemServiceFlowClass obj) throws Exception {
  296. return null;
  297. }
  298. @Transactional
  299. public boolean deleteFlowClassByFlowId(Integer flowId) {
  300. boolean succ = flowClassDao.deleteFlowClassByFlowId(flowId);
  301. return succ;
  302. }
  303. public List<SystemServiceFlowTemp> getFlowTempByFlowId(Integer id) throws Exception {
  304. return flowTempDao.getFlowTempByFlowId(id);
  305. }
  306. //TODO
  307. public boolean genFlewByflowTempId(Integer flowTempId) throws Exception {
  308. SystemServiceFlow flow = getFlowById(flowTempId);
  309. //生成新的route类
  310. //添加新processor记录
  311. return false;
  312. }
  313. /**
  314. * 发送MQ消息-更新路由
  315. *
  316. * @param flowCode 服务流程Code标识
  317. * @param flowClass
  318. * @param operate
  319. */
  320. public void sendUpdateMessage(String flowCode, SystemServiceFlowClass flowClass, String operate) {
  321. if ("1".equals(flowClass.getIsUpdate())) {
  322. ServiceFlow serviceFlow = new ServiceFlow();
  323. serviceFlow.setRouteCode(flowCode);
  324. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  325. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  326. handleFile.setRouteCode(flowCode);
  327. handleFile.setFileType(ServiceFlowConstant.CLASS);
  328. handleFile.setPackageName(flowClass.getPackageName());
  329. handleFile.setClassName(flowClass.getClassName());
  330. handleFile.setFilePath(flowClass.getClassPath());
  331. handleFile.setUsage(flowClass.getType());
  332. serviceFlow.addHandleFile(handleFile);
  333. switch (operate) {
  334. case ServiceFlowConstant.FLOW_OP_ADD:
  335. case ServiceFlowConstant.FLOW_OP_UPDATE:
  336. serviceFlowEventService.serviceFlowModifiedAdd(serviceFlow);
  337. break;
  338. case ServiceFlowConstant.FLOW_OP_DELETE:
  339. serviceFlowEventService.serviceFlowModifiedReduce(serviceFlow);
  340. break;
  341. default:
  342. break;
  343. }
  344. }
  345. }
  346. /**
  347. * 获取流程列表
  348. *
  349. * @param type 流程的文件类型
  350. * @return
  351. * @throws Exception
  352. */
  353. public ActionResult getFlowList(String type) throws Exception {
  354. List<SystemServiceFlow> flowList = flowDao.getFlowList(type);
  355. ActionResult re = new ActionResult();
  356. if (flowList != null && flowList.size() > 0) {
  357. List<DictItem> dictList = new ArrayList<>();
  358. for (SystemServiceFlow item : flowList) {
  359. DictItem dict = new DictItem();
  360. dict.setCode(item.getId().toString());
  361. dict.setValue(item.getName());
  362. dict.setExtend("");
  363. dictList.add(dict);
  364. }
  365. re.setData(dictList);
  366. }
  367. return re;
  368. }
  369. /**
  370. * 发送消息到broker,生成camel相关文件
  371. *
  372. * @param flowTempId
  373. * @param newCron
  374. * @throws Exception
  375. */
  376. public Integer genCamelFile(String jobId, Integer flowTempId, String newCron) throws Exception {
  377. Long timestamp = System.currentTimeMillis();
  378. //发送生成processor文件的消息
  379. Integer newFlowId = sendAddProcessor(jobId, flowTempId, timestamp);
  380. if (newFlowId != null) {
  381. //发送生成route文件的消息
  382. newFlowId = sendAddRoute(flowTempId, newFlowId, newCron);
  383. if (newFlowId != null) {
  384. return newFlowId;
  385. } else {
  386. System.out.println("生成route文件失败");
  387. return null;
  388. }
  389. } else {
  390. System.out.println("生成processor文件失败");
  391. return null;
  392. }
  393. }
  394. /**
  395. * 修改任务,修改camel相关文件
  396. *
  397. * @param flowId 流程ID
  398. * @param newCron 新cron
  399. * @return
  400. * @throws Exception
  401. */
  402. public Integer updateCamelFile(String jobId, Integer flowTempId, Integer flowId, String newCron) throws Exception {
  403. /* 修改route文件无需重新生成flowClass记录,文件名根据className+routeId 生成;*/
  404. List<SystemServiceFlowTemp> flowTempRouters = flowTempDao.getFlowTemps(flowTempId, ServiceFlowConstant.FLOW_TYPE_ROUTE);
  405. List<SystemServiceFlowTemp> flowTempProces = flowTempDao.getFlowTemps(flowTempId, ServiceFlowConstant.FLOW_TYPE_PROCESSOR);
  406. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, flowId);
  407. //route模板文件记录是否存在。不存在就返回。
  408. if (!flowTempRouters.isEmpty()) {
  409. SystemServiceFlowTemp flowTemp = flowTempRouters.get(0);
  410. ArrayList<ServiceFlow.HandleFile> handleFiles = new ArrayList<>();
  411. ServiceFlow serviceFlow = new ServiceFlow();
  412. //新增processor记录
  413. for (SystemServiceFlowTemp process : flowTempProces) {
  414. String newPath = genProcessorJavaFile(jobId, flow.getCode(), process.getClassPath(), process.getClassName());
  415. if (newPath != null) {
  416. //发送消息
  417. serviceFlow.setRouteCode(flow.getCode());
  418. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  419. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  420. handleFile.setRouteCode(flow.getCode());
  421. handleFile.setFileType(ServiceFlowConstant.JAVA);
  422. handleFile.setPackageName(process.getPackageName());
  423. handleFile.setClassName(process.getClassName());
  424. handleFile.setFilePath(newPath);
  425. handleFile.setUsage(ServiceFlowConstant.FLOW_TYPE_PROCESSOR);
  426. handleFiles.add(handleFile);
  427. serviceFlow.setHandleFiles(handleFiles);
  428. } else {
  429. System.out.println("生成processor的java文件过程出错");
  430. return null;
  431. }
  432. }
  433. //生成新的route文件
  434. String newPath = genRouteJavaFile(flow.getCode(), flowTemp.getClassName(), flowTemp.getClassPath(), newCron);
  435. serviceFlow.setRouteCode(flow.getCode());
  436. serviceFlow.setFlowType(ServiceFlowConstant.JAVA);
  437. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  438. handleFile.setRouteCode(flow.getCode());
  439. handleFile.setFileType(ServiceFlowConstant.JAVA);
  440. handleFile.setPackageName(flowTemp.getPackageName());
  441. handleFile.setClassName(flowTemp.getClassName() + flow.getCode());
  442. handleFile.setFilePath(newPath);
  443. handleFile.setUsage(ServiceFlowConstant.FLOW_TYPE_ROUTE);
  444. handleFiles.add(handleFile);
  445. serviceFlow.setHandleFiles(handleFiles);
  446. serviceFlowEventService.serviceFlowModifiedAdd(serviceFlow);
  447. return flowId;
  448. }
  449. return null;
  450. }
  451. /* ********************* 发送消息方式生成文件 ********************************/
  452. public Integer sendAddRoute(Integer tempId, Integer flowId, String newCron) throws Exception {
  453. List<SystemServiceFlowTemp> flowTempRouters = flowTempDao.getFlowTemps(tempId, ServiceFlowConstant.FLOW_TYPE_ROUTE);
  454. SystemServiceFlow newFlow = getFlowById(flowId);
  455. //route模板文件记录是否存在。不存在就返回。
  456. if (!flowTempRouters.isEmpty()) {
  457. SystemServiceFlowTemp flowTemp = flowTempRouters.get(0);
  458. StringBuilder basePath = new StringBuilder();
  459. ;
  460. if (flowTemp.getPackageName() != null) {
  461. String packagePath[] = flowTemp.getPackageName().split("\\.");
  462. for (int i = 0; i < packagePath.length; i++) {
  463. basePath.append(packagePath[i]).append("/");
  464. }
  465. }
  466. //新增processor记录
  467. String newPath = genRouteJavaFile(newFlow.getCode(), flowTemp.getClassName(), flowTemp.getClassPath(), newCron);
  468. SystemServiceFlowClass newFlowClass = new SystemServiceFlowClass();
  469. newFlowClass.setPackageName(flowTemp.getPackageName());
  470. newFlowClass.setClassName(flowTemp.getClassName() + newFlow.getCode());
  471. newFlowClass.setClassPath(flowTemp.getClassPath());
  472. newFlowClass.setFlowId(newFlow.getId());
  473. newFlowClass.setType(ServiceFlowConstant.FLOW_TYPE_ROUTE);
  474. flowClassDao.saveEntity(newFlowClass);
  475. newFlowClass.setIsUpdate("1");
  476. if (newPath != null) {
  477. ServiceFlow serviceFlow = new ServiceFlow();
  478. serviceFlow.setRouteCode(newFlow.getCode());
  479. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  480. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  481. handleFile.setRouteCode(newFlow.getCode());
  482. handleFile.setFileType(ServiceFlowConstant.JAVA);
  483. handleFile.setPackageName(flowTemp.getPackageName());
  484. handleFile.setClassName(flowTemp.getClassName() + newFlow.getCode());
  485. handleFile.setFilePath(newPath);
  486. handleFile.setUsage(ServiceFlowConstant.FLOW_TYPE_ROUTE);
  487. ArrayList<ServiceFlow.HandleFile> handleFiles = new ArrayList<>();
  488. handleFiles.add(handleFile);
  489. serviceFlow.setHandleFiles(handleFiles);
  490. serviceFlowEventService.serviceFlowModifiedAdd(serviceFlow);
  491. return newFlow.getId();
  492. } else {
  493. System.out.println("生成route的java文件过程出错");
  494. return null;
  495. }
  496. }
  497. return null;
  498. }
  499. public Integer sendAddRoute(String code, String name, String javaName, String packageName, String fileInfo) throws Exception {
  500. String fileName = javaName + ".java";
  501. String filePath = this.getClass().getProtectionDomain().getClassLoader().getResource("").getPath() + "temp/" + fileName;
  502. boolean succ = FileUtil.writeFile(filePath, fileInfo, "UTF-8");
  503. String path = "";
  504. if (succ) {
  505. path = ClassFileUtil.uploadFile(beanConfig.getFsUrl(), new File(filePath), fileName);
  506. } else {
  507. return null;
  508. }
  509. if (fileName != null) {
  510. SystemServiceFlow systemServiceFlow = new SystemServiceFlow();
  511. systemServiceFlow.setName(name);
  512. systemServiceFlow.setCode(code);
  513. systemServiceFlow.setCreateDate(new Date());
  514. systemServiceFlow.setValid(1);
  515. systemServiceFlow.setFileType(ServiceFlowConstant.JAVA);
  516. flowDao.saveEntity(systemServiceFlow);
  517. ServiceFlow serviceFlow = new ServiceFlow();
  518. serviceFlow.setRouteCode(code);
  519. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  520. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  521. handleFile.setRouteCode(code);
  522. handleFile.setFileType(ServiceFlowConstant.JAVA);
  523. handleFile.setPackageName(packageName);
  524. handleFile.setClassName(javaName);
  525. handleFile.setFilePath(path);
  526. handleFile.setUsage(ServiceFlowConstant.FLOW_TYPE_ROUTE);
  527. ArrayList<ServiceFlow.HandleFile> handleFiles = new ArrayList<>();
  528. handleFiles.add(handleFile);
  529. serviceFlow.setHandleFiles(handleFiles);
  530. serviceFlowEventService.serviceFlowAdded(serviceFlow);
  531. SystemServiceFlowClass flowClass = new SystemServiceFlowClass();
  532. flowClass.setPackageName(packageName);
  533. flowClass.setClassName(javaName);
  534. flowClass.setClassPath(path);
  535. flowClass.setFlowId(systemServiceFlow.getId());
  536. flowClass.setType(ServiceFlowConstant.FLOW_TYPE_ROUTE);
  537. flowClass.setIsUpdate("1");
  538. flowClassDao.saveEntity(flowClass);
  539. return systemServiceFlow.getId();
  540. } else {
  541. System.out.println("生成route的java文件过程出错");
  542. return null;
  543. }
  544. }
  545. public Integer sendAddProcessor(String jobId, Integer flowId, Long timestamp) throws Exception {
  546. List<SystemServiceFlowTemp> flowTempRouters = flowTempDao.getFlowTemps(flowId, ServiceFlowConstant.FLOW_TYPE_ROUTE);
  547. List<SystemServiceFlowTemp> flowTempProces = flowTempDao.getFlowTemps(flowId, ServiceFlowConstant.FLOW_TYPE_PROCESSOR);
  548. SystemServiceFlow oldFlow = getFlowById(flowId);
  549. //route模板文件记录是否存在。不存在就返回。
  550. if (!flowTempRouters.isEmpty()) {
  551. //成功生成文件后,添加flow和flowclass记录
  552. SystemServiceFlow newFlow = new SystemServiceFlow();
  553. newFlow.setName(oldFlow.getName() + timestamp);
  554. newFlow.setCode(oldFlow.getCode() + timestamp);
  555. newFlow.setChart(oldFlow.getChart());
  556. newFlow.setValid(1);
  557. newFlow.setCreateDate(new Date());
  558. newFlow.setFileType(ServiceFlowConstant.CLASS);
  559. flowDao.saveEntity(newFlow);
  560. ArrayList<ServiceFlow.HandleFile> handleFiles = new ArrayList<>();
  561. //新增processor记录
  562. for (SystemServiceFlowTemp process : flowTempProces) {
  563. StringBuilder proPath = new StringBuilder();
  564. if (process.getPackageName() != null) {
  565. //生成“/"分割的包名
  566. String packagePath[] = process.getPackageName().split("\\.");
  567. for (int i = 0; i < packagePath.length; i++) {
  568. proPath.append(packagePath[i]).append("/");
  569. }
  570. }
  571. String newPath = genProcessorJavaFile(jobId, newFlow.getCode(), process.getClassPath(), process.getClassName());
  572. SystemServiceFlowClass processClass = new SystemServiceFlowClass();
  573. processClass.setPackageName(process.getPackageName());
  574. processClass.setClassName(process.getClassName());
  575. processClass.setClassPath(processClass.getClassPath());
  576. processClass.setFlowId(newFlow.getId());
  577. processClass.setType(ServiceFlowConstant.FLOW_TYPE_PROCESSOR);
  578. processClass.setIsUpdate("1");
  579. if (newPath != null) {
  580. //发送消息
  581. ServiceFlow serviceFlow = new ServiceFlow();
  582. serviceFlow.setRouteCode(newFlow.getCode());
  583. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  584. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  585. handleFile.setRouteCode(newFlow.getCode());
  586. handleFile.setFileType(ServiceFlowConstant.JAVA);
  587. handleFile.setPackageName(processClass.getPackageName());
  588. handleFile.setClassName(processClass.getClassName());
  589. handleFile.setFilePath(newPath);
  590. handleFile.setUsage(ServiceFlowConstant.FLOW_TYPE_PROCESSOR);
  591. handleFiles.add(handleFile);
  592. serviceFlow.setHandleFiles(handleFiles);
  593. serviceFlowEventService.serviceFlowModifiedAdd(serviceFlow);
  594. flowClassDao.saveEntity(processClass);
  595. } else {
  596. System.out.println("生成processor的java文件过程出错");
  597. return null;
  598. }
  599. }
  600. return newFlow.getId();
  601. }
  602. return null;
  603. }
  604. /**
  605. * 服务启动/暂停
  606. *
  607. * @param flowId 流程ID
  608. * @param oper 服务操作;1:启动;0:暂停
  609. * @return
  610. * @throws Exception
  611. */
  612. public boolean serviceOpenOrPause(Integer flowId, Integer oper) throws Exception {
  613. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, flowId);
  614. if (flow != null) {
  615. List<SystemServiceFlowClass> flowClassList = flowClassDao.getFlowClassByFlowId(flowId);
  616. //route模板文件记录是否存在。不存在就返回。
  617. ArrayList<ServiceFlow.HandleFile> handleFiles = new ArrayList<>();
  618. //新增processor记录
  619. ServiceFlow serviceFlow = new ServiceFlow();
  620. serviceFlow.setRouteCode(flow.getCode());
  621. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  622. for (SystemServiceFlowClass flowClass : flowClassList) {
  623. // String deName = DES.decrypt(flowClass.getClassPath(), DES.COMMON_PASSWORD);//吉阿米果的文件名
  624. //发送消息
  625. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  626. handleFile.setRouteCode(flow.getCode());
  627. handleFile.setFileType(ServiceFlowConstant.CLASS);
  628. handleFile.setPackageName(flowClass.getPackageName());
  629. handleFile.setClassName(flowClass.getClassName());
  630. handleFile.setFilePath(flowClass.getClassPath());
  631. handleFile.setUsage(ServiceFlowConstant.FLOW_TYPE_PROCESSOR);
  632. handleFiles.add(handleFile);
  633. }
  634. serviceFlow.setHandleFiles(handleFiles);
  635. if (1 == oper) {
  636. serviceFlowEventService.serviceFlowStarted(serviceFlow);
  637. } else if (0 == oper) {
  638. serviceFlowEventService.serviceFlowStopped(serviceFlow);
  639. } else {
  640. return false;
  641. }
  642. return true;
  643. } else {
  644. return false;
  645. }
  646. }
  647. }