FlowManager.java 35 KB

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