FlowManager.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. package com.yihu.hos.system.service;
  2. import com.yihu.hos.common.constants.Constants;
  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.services.ServiceFlowEventService;
  8. import com.yihu.hos.system.dao.FlowClassDao;
  9. import com.yihu.hos.system.dao.FlowDao;
  10. import com.yihu.hos.system.dao.FlowTempDao;
  11. import com.yihu.hos.system.model.SystemServiceFlow;
  12. import com.yihu.hos.system.model.SystemServiceFlowClass;
  13. import com.yihu.hos.system.model.SystemServiceFlowTemp;
  14. import com.yihu.hos.system.model.bo.ServiceFlow;
  15. import com.yihu.hos.system.service.intf.IFlowManage;
  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.util.GridFSUtil;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.transaction.annotation.Transactional;
  23. import org.springframework.web.multipart.commons.CommonsMultipartFile;
  24. import javax.annotation.Resource;
  25. import java.io.OutputStream;
  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 implements IFlowManage {
  39. public static final String BEAN_ID = "flowManager";
  40. @Autowired
  41. ServiceFlowEventService serviceFlowEventService;
  42. @Resource(name = "flowDao")
  43. private FlowDao flowDao;
  44. @Resource(name = FlowClassDao.BEAN_ID)
  45. private FlowClassDao flowClassDao;
  46. @Resource(name = FlowTempDao.BEAN_ID)
  47. private FlowTempDao flowTempDao;
  48. @Autowired
  49. private MongoConfig mongoConfig;
  50. @Override
  51. public Result getFlowList(Map<String, Object> params) throws Exception {
  52. return flowDao.getFlowList(params);
  53. }
  54. /**
  55. * @return List<ServiceFlow> 返回所有可运行流程
  56. * @throws Exception ...
  57. */
  58. public List<ServiceFlow> getServiceFlowList() throws Exception {
  59. List<ServiceFlow> serviceFlowList = new ArrayList<>();
  60. List<SystemServiceFlow> classFlowList = flowDao.getFlowList(Constants.CLASS);
  61. for (SystemServiceFlow systemServiceFlow : classFlowList) {
  62. ServiceFlow serviceFlow = new ServiceFlow();
  63. serviceFlow.setRouteCode(systemServiceFlow.getCode());
  64. serviceFlow.setFlowType(systemServiceFlow.getFileType());
  65. List<SystemServiceFlowClass> classList = flowClassDao.getFlowClassByFlowId(systemServiceFlow.getId());
  66. ArrayList<ServiceFlow.HandleFile> handleFileList = new ArrayList<>();
  67. for (SystemServiceFlowClass flowClass : classList) {
  68. ServiceFlow.HandleFile handleFile = serviceFlow.new HandleFile();
  69. handleFile.setFileType(Constants.CLASS);
  70. handleFile.setClassName(flowClass.getClassName());
  71. handleFile.setPackageName(flowClass.getPackageName());
  72. handleFile.setFilePath(flowClass.getClassPath());
  73. handleFile.setUsage(flowClass.getType());
  74. handleFileList.add(handleFile);
  75. }
  76. serviceFlow.setHandleFiles(handleFileList);
  77. serviceFlowList.add(serviceFlow);
  78. }
  79. List<SystemServiceFlow> javaFlowList = flowDao.getFlowList(Constants.JAVA);
  80. for (SystemServiceFlow systemServiceFlow : javaFlowList) {
  81. ServiceFlow serviceFlow = new ServiceFlow();
  82. serviceFlow.setRouteCode(systemServiceFlow.getCode());
  83. List<SystemServiceFlowTemp> tempList = flowTempDao.getFlowTempByFlowId(systemServiceFlow.getId());
  84. ArrayList<ServiceFlow.HandleFile> handleFileList = new ArrayList<>();
  85. for (SystemServiceFlowTemp flowTemp : tempList) {
  86. ServiceFlow.HandleFile handleFile = serviceFlow.new HandleFile();
  87. handleFile.setFileType(Constants.JAVA);
  88. handleFile.setClassName(flowTemp.getClassName());
  89. handleFile.setPackageName(flowTemp.getPackageName());
  90. handleFile.setFilePath(flowTemp.getClassPath());
  91. handleFile.setUsage(flowTemp.getType());
  92. handleFileList.add(handleFile);
  93. }
  94. serviceFlow.setHandleFiles(handleFileList);
  95. serviceFlowList.add(serviceFlow);
  96. }
  97. return serviceFlowList;
  98. }
  99. @Override
  100. public SystemServiceFlow getFlowById(Integer id) throws Exception {
  101. return flowDao.getEntity(SystemServiceFlow.class, id);
  102. }
  103. @Transactional
  104. public Result addFlow(SystemServiceFlow obj) throws Exception {
  105. obj.setCreateDate(new Date());
  106. flowDao.saveEntity(obj);
  107. if (Constants.CLASS.equals(obj.getFileType())) {
  108. List<SystemServiceFlowClass> flowClassList = obj.getFlowClassArray();
  109. for (SystemServiceFlowClass flowClass : flowClassList) {
  110. flowClass.setFlowId(obj.getId());
  111. flowDao.saveEntity(flowClass);
  112. //发送消息到MQ对列
  113. sendUpdateMessage(obj.getCode(), flowClass, Constants.FLOW_OP_ADD);
  114. }
  115. } else if (Constants.JAVA.equals(obj.getFileType())) {
  116. List<SystemServiceFlowTemp> flowTempList = obj.getFlowTempArray();
  117. for (SystemServiceFlowTemp flowTemp : flowTempList) {
  118. flowTemp.setFlowId(obj.getId());
  119. flowDao.saveEntity(flowTemp);
  120. }
  121. }
  122. return Result.success("保存成功");
  123. }
  124. @Transactional
  125. public Result updateFlow(SystemServiceFlow obj) throws Exception {
  126. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, obj.getId());
  127. flow.setCode(obj.getCode());
  128. flow.setName(obj.getName());
  129. flow.setPath(obj.getPath());
  130. flow.setChart(obj.getChart());
  131. flow.setValid(obj.getValid());
  132. flow.setFileType(obj.getFileType());
  133. if (Constants.JAVA.equals(flow.getFileType())) {
  134. List<SystemServiceFlowTemp> flowTempList = obj.getFlowTempArray();
  135. boolean succ = flowTempDao.deleteFlowTempByFlowId(obj.getId());
  136. if (succ) {
  137. for (SystemServiceFlowTemp flowTemp : flowTempList) {
  138. flowTempDao.saveEntity(flowTemp);
  139. }
  140. }
  141. } else if (Constants.CLASS.equals(flow.getFileType())) {
  142. List<Integer> classIds = flowClassDao.getFlowClassIds(obj.getId());//原flowclass集合
  143. List<SystemServiceFlowClass> flowClassList = obj.getFlowClassArray();
  144. SystemServiceFlowClass flowClassRoute = null;
  145. String oper = "";
  146. for (SystemServiceFlowClass flowClass : flowClassList) {
  147. if (flowClass.getId() != null) {
  148. classIds.remove(flowClass.getId());
  149. flowClassDao.updateEntity(flowClass);
  150. if (!flowClass.getType().equals(Constants.FLOW_TYPE_ROUTE)) {
  151. sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_UPDATE);
  152. } else {
  153. flowClassRoute = flowClass;
  154. oper = Constants.FLOW_OP_UPDATE;
  155. }
  156. } else {
  157. if (!flowClass.getType().equals(Constants.FLOW_TYPE_ROUTE)) {
  158. flowClassDao.saveEntity(flowClass);
  159. sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_ADD);
  160. } else {
  161. flowClassRoute = flowClass;
  162. oper = Constants.FLOW_OP_ADD;
  163. }
  164. }
  165. }
  166. //删除判断
  167. if (classIds != null && classIds.size() > 0) {
  168. for (Integer id : classIds) {
  169. SystemServiceFlowClass flowClass = getFlowClassById(id);
  170. flowClassDao.deleteEntity(flowClass);
  171. if (!flowClass.getType().equals(Constants.FLOW_TYPE_ROUTE)) {
  172. sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_DELETE);
  173. } else {
  174. flowClassRoute = flowClass;
  175. flowClassRoute.setIsUpdate("1");
  176. oper = Constants.FLOW_OP_DELETE;
  177. }
  178. }
  179. }
  180. if (flowClassRoute != null) {
  181. sendUpdateMessage(flow.getCode(), flowClassRoute, oper);
  182. }
  183. }
  184. flowDao.updateEntity(flow);
  185. return Result.success("更新成功");
  186. }
  187. @Transactional
  188. public Result deleteFlow(Integer id) throws Exception {
  189. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, id);
  190. List<SystemServiceFlowClass> flowClassList = flowClassDao.getFlowClassByFlowId(id);
  191. List<SystemServiceFlowClass> processorFlowClassList = new ArrayList<>();
  192. if (Constants.JAVA.equals(flow.getFileType())){
  193. flowTempDao.deleteFlowTempByFlowId(id);
  194. } else {
  195. for (SystemServiceFlowClass flowClass:flowClassList){
  196. flowClassDao.deleteEntity(flowClass);
  197. flowClass.setIsUpdate("1");
  198. //发送消息到MQ对列
  199. if (flowClass.getType().equals(Constants.FLOW_TYPE_ROUTE)) {
  200. sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_DELETE);
  201. } else {
  202. processorFlowClassList.add(flowClass);
  203. }
  204. }
  205. for (SystemServiceFlowClass serviceFlowClass : processorFlowClassList) {
  206. sendUpdateMessage(flow.getCode(), serviceFlowClass, Constants.FLOW_OP_DELETE);
  207. }
  208. }
  209. flowDao.deleteEntity(flow);
  210. return Result.success("删除成功");
  211. }
  212. /* =================================== flowClass 部分================================================*/
  213. @Override
  214. public Result getFlowClassList(Map<String, Object> params) throws Exception {
  215. return null;
  216. }
  217. @Override
  218. public List<SystemServiceFlowClass> getFlowClassByFlowId(Integer flowId) throws Exception {
  219. return flowClassDao.getFlowClassByFlowId(flowId);
  220. }
  221. public SystemServiceFlowClass getFlowClassById(Integer id) throws Exception {
  222. return flowClassDao.getEntity(SystemServiceFlowClass.class, id);
  223. }
  224. @Transactional
  225. public Result addFlowClass(SystemServiceFlowClass obj) throws Exception {
  226. flowDao.saveEntity(obj);
  227. return Result.success("保存成功");
  228. }
  229. @Transactional
  230. public Result updateFlowClass(SystemServiceFlowClass obj) throws Exception {
  231. return null;
  232. }
  233. @Transactional
  234. public boolean deleteFlowClassByFlowId(Integer flowId) {
  235. boolean succ = flowClassDao.deleteFlowClassByFlowId(flowId);
  236. return succ;
  237. }
  238. @Override
  239. public List<SystemServiceFlowTemp> getFlowTempByFlowId(Integer id) throws Exception {
  240. return flowTempDao.getFlowTempByFlowId(id);
  241. }
  242. //TODO
  243. @Override
  244. public boolean genFlewByflowTempId(Integer flowTempId) throws Exception {
  245. SystemServiceFlow flow = getFlowById(flowTempId);
  246. //生成新的route类
  247. //添加新processor记录
  248. return false;
  249. }
  250. @Override
  251. public Result uploadFile(CommonsMultipartFile file) {
  252. String dbName = "upload";
  253. String newFileName;
  254. try {
  255. newFileName = GridFSUtil.uploadFile(mongoConfig.mongoClient().getDatabase(dbName), file);
  256. if (!StringUtil.isEmpty(newFileName)) {
  257. return Result.success(DES.encrypt(newFileName, DES.COMMON_PASSWORD));
  258. }
  259. } catch (Exception e) {
  260. e.printStackTrace();
  261. }
  262. return Result.error("上传失败");
  263. }
  264. @Override
  265. public Result readFile(OutputStream os, String fileName) {
  266. String dbName = "upload";
  267. try {
  268. fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
  269. GridFSUtil.readFile(mongoConfig.mongoClient().getDatabase(dbName), os, fileName);
  270. return Result.success("读取成功");
  271. } catch (Exception e) {
  272. e.printStackTrace();
  273. }
  274. return Result.error("读取失败");
  275. }
  276. /**
  277. * 发送MQ消息-更新路由
  278. * @param flowCode 服务流程Code标识
  279. * @param flowClass
  280. * @param operate
  281. */
  282. public void sendUpdateMessage(String flowCode,SystemServiceFlowClass flowClass,String operate){
  283. //发送消息到MQ对列
  284. if ("1".equals(flowClass.getIsUpdate()) && Constants.FLOW_TYPE_ROUTE.equals(flowClass.getType())) {
  285. //route
  286. switch (operate){
  287. case "add" : serviceFlowEventService.routeDefineAdded(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  288. case "delete" : serviceFlowEventService.routeDefineDelete(flowCode, flowClass.getPackageName(), flowClass.getClassName()); break;
  289. case "update" : serviceFlowEventService.routeDefineChanged(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  290. default : break;
  291. }
  292. } else if ("1".equals(flowClass.getIsUpdate()) && Constants.FLOW_TYPE_PROCESSOR.equals(flowClass.getType())) {
  293. //processor
  294. switch (operate){
  295. case "add" : serviceFlowEventService.processorAdded(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  296. case "delete" : serviceFlowEventService.processorDataDeleted(flowCode, flowClass.getPackageName(), flowClass.getClassName()); break;
  297. case "update" : serviceFlowEventService.processorDataChanged(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  298. default : break;
  299. }
  300. }
  301. }
  302. /**
  303. * 获取流程列表
  304. * @param type 流程的文件类型
  305. * @return
  306. * @throws Exception
  307. */
  308. @Override
  309. public ActionResult getFlowList(String type) throws Exception {
  310. List<SystemServiceFlow> flowList = flowDao.getFlowList(type);
  311. ActionResult re = new ActionResult();
  312. if (flowList != null && flowList.size() > 0) {
  313. List<DictItem> dictList = new ArrayList<>();
  314. for (SystemServiceFlow item : flowList) {
  315. DictItem dict = new DictItem();
  316. dict.setCode(item.getId().toString());
  317. dict.setValue(item.getName());
  318. dict.setExtend("");
  319. dictList.add(dict);
  320. }
  321. re.setData(dictList);
  322. }
  323. return re;
  324. }
  325. /**
  326. * 发送消息到broker,生成camel相关文件
  327. * @param flowTempId
  328. * @param newCron
  329. * @throws Exception
  330. */
  331. public Integer genCamelFile(String jobId,Integer flowTempId, String newCron) throws Exception {
  332. Long timestamp = System.currentTimeMillis();
  333. //发送生成processor文件的消息
  334. Integer newFlowId = sendAddProcessor(jobId,flowTempId, timestamp);
  335. if (newFlowId != null){
  336. //发送生成route文件的消息
  337. newFlowId = sendAddRoute(flowTempId, newFlowId, newCron);
  338. if (newFlowId !=null){
  339. return newFlowId;
  340. }else {
  341. System.out.println("生成route文件失败");
  342. return null;
  343. }
  344. }else {
  345. System.out.println("生成processor文件失败");
  346. return null;
  347. }
  348. }
  349. /**
  350. * 修改任务,修改camel相关文件
  351. * @param flowId 流程ID
  352. * @param newCron 新cron
  353. * @return
  354. * @throws Exception
  355. */
  356. @Override
  357. public Integer updateCamelFile(Integer flowTempId,Integer flowId, String newCron) throws Exception {
  358. /* 修改route文件无需重新生成flowClass记录,文件名根据className+routeId 生成;*/
  359. List<SystemServiceFlowTemp> flowTempRouters = flowTempDao.getFlowTemps(flowTempId, Constants.FLOW_TYPE_ROUTE);
  360. List<SystemServiceFlowClass> flowClassRouters = flowClassDao.getFlowClass(flowId, Constants.FLOW_TYPE_ROUTE);
  361. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class,flowId);
  362. //route模板文件记录是否存在。不存在就返回。
  363. if (!flowTempRouters.isEmpty()){
  364. SystemServiceFlowTemp flowTemp =flowTempRouters.get(0);
  365. StringBuilder basePath = new StringBuilder();;
  366. if (flowTemp.getPackageName()!=null){
  367. String packagePath[] = flowTemp.getPackageName().split("\\.");
  368. for (int i=0;i<packagePath.length;i++){
  369. basePath.append(packagePath[i]).append("/");
  370. }
  371. }
  372. serviceFlowEventService.routeClassChanged(flow.getCode(),basePath.toString(), flowTemp.getClassName(), flowTemp.getClassPath(),newCron);
  373. return flowId;
  374. }
  375. return null;
  376. }
  377. /* ********************* 发送消息方式生成文件 ********************************/
  378. public Integer sendAddRoute(Integer tempId, Integer flowId, String newCron) throws Exception {
  379. List<SystemServiceFlowTemp> flowTempRouters = flowTempDao.getFlowTemps(tempId, Constants.FLOW_TYPE_ROUTE);
  380. SystemServiceFlow newFlow = getFlowById(flowId);
  381. //route模板文件记录是否存在。不存在就返回。
  382. if (!flowTempRouters.isEmpty()) {
  383. SystemServiceFlowTemp flowTemp = flowTempRouters.get(0);
  384. StringBuilder basePath = new StringBuilder();
  385. ;
  386. if (flowTemp.getPackageName() != null) {
  387. String packagePath[] = flowTemp.getPackageName().split("\\.");
  388. for (int i = 0; i < packagePath.length; i++) {
  389. basePath.append(packagePath[i]).append("/");
  390. }
  391. }
  392. //新增processor记录
  393. String deName = DES.decrypt(flowTemp.getClassPath(), DES.COMMON_PASSWORD);//吉阿米果的文件名
  394. String enClassName = DES.encrypt(deName.replace(".java", ".class"), DES.COMMON_PASSWORD);//生成机密过的classPath
  395. SystemServiceFlowClass newFlowClass = new SystemServiceFlowClass();
  396. newFlowClass.setPackageName(flowTemp.getPackageName());
  397. newFlowClass.setClassName(flowTemp.getClassName() + newFlow.getCode());
  398. newFlowClass.setClassPath(enClassName);
  399. newFlowClass.setFlowId(newFlow.getId());
  400. newFlowClass.setType(Constants.FLOW_TYPE_ROUTE);
  401. flowClassDao.saveEntity(newFlowClass);
  402. newFlowClass.setIsUpdate("1");
  403. //生成新的route文件
  404. String newFileName = genRouteJavaFile(newFlow.getCode(),flowTemp.getClassName(),deName,newCron);
  405. if (newFileName!=null){
  406. serviceFlowEventService.routeClassAdded(newFlow.getCode(), basePath.toString(), flowTemp.getClassName(), deName,newCron);
  407. return newFlow.getId();
  408. }else {
  409. System.out.println("生成route的java文件过程出错");
  410. return null;
  411. }
  412. }
  413. return null;
  414. }
  415. public Integer sendAddProcessor(String jobId,Integer flowId, Long timestamp) throws Exception {
  416. List<SystemServiceFlowTemp> flowTempRouters = flowTempDao.getFlowTemps(flowId, Constants.FLOW_TYPE_ROUTE);
  417. List<SystemServiceFlowTemp> flowTempProces = flowTempDao.getFlowTemps(flowId, Constants.FLOW_TYPE_PROCESSOR);
  418. SystemServiceFlow oldFlow = getFlowById(flowId);
  419. //route模板文件记录是否存在。不存在就返回。
  420. if (!flowTempRouters.isEmpty()) {
  421. //成功生成文件后,添加flow和flowclass记录
  422. SystemServiceFlow newFlow = new SystemServiceFlow();
  423. newFlow.setName(oldFlow.getName() + timestamp);
  424. newFlow.setCode(oldFlow.getCode() + timestamp);
  425. newFlow.setChart(oldFlow.getChart());
  426. newFlow.setValid(1);
  427. newFlow.setCreateDate(new Date());
  428. newFlow.setFileType(Constants.CLASS);
  429. flowDao.saveEntity(newFlow);
  430. //新增processor记录
  431. for (SystemServiceFlowTemp process : flowTempProces) {
  432. StringBuilder proPath = new StringBuilder();
  433. if (process.getPackageName() != null) {
  434. //生成“/"分割的包名
  435. String packagePath[] = process.getPackageName().split("\\.");
  436. for (int i = 0; i < packagePath.length; i++) {
  437. proPath.append(packagePath[i]).append("/");
  438. }
  439. }
  440. String deName = DES.decrypt(process.getClassPath(), DES.COMMON_PASSWORD);//吉阿米果的文件名
  441. String enClassName = DES.encrypt(deName.replace(".java", ".class"), DES.COMMON_PASSWORD);//生成机密过的classPath
  442. SystemServiceFlowClass processClass = new SystemServiceFlowClass();
  443. processClass.setPackageName(process.getPackageName());
  444. processClass.setClassName(process.getClassName());
  445. processClass.setClassPath(enClassName);
  446. processClass.setFlowId(newFlow.getId());
  447. processClass.setType(Constants.FLOW_TYPE_PROCESSOR);
  448. processClass.setIsUpdate("1");
  449. //生成新的java文件
  450. String newFileName = genProcessorJavaFile(jobId,newFlow.getCode(),deName,processClass.getClassName());
  451. if (newFileName!=null){
  452. //发送消息
  453. serviceFlowEventService.processorClassAdded(newFlow.getCode(),proPath.toString(), processClass.getClassName(), deName);
  454. flowClassDao.saveEntity(processClass);
  455. }else {
  456. System.out.println("生成processor的java文件过程出错");
  457. return null;
  458. }
  459. }
  460. return newFlow.getId();
  461. }
  462. return null;
  463. }
  464. /**
  465. * 生成Route流程的java文件
  466. * @param routeId 流程Id
  467. * @param className 模板类名
  468. * @param tempFilePath 模板文件路径
  469. * @param newCron cron表达式
  470. * @return
  471. */
  472. public static String genRouteJavaFile(String routeId,String className,String tempFilePath,String newCron) {
  473. try {
  474. String newFileName = className+routeId+".java";
  475. String newFilePath = "/temp/"+newFileName;
  476. String text = GridFSUtil.readFile("upload",tempFilePath);
  477. if (text.contains("?cron=")) {
  478. String oldStr = text.substring(text.indexOf("?cron=") + 6);
  479. String cron = oldStr.substring(0, oldStr.indexOf("\""));
  480. text = text.replace(cron,newCron);
  481. }
  482. //修改java类名
  483. if (text.contains(className)) {
  484. text = text.replace(className, className+routeId);//新类名规则=旧类名+routeId
  485. }
  486. //修改routeId;模板规则 routeId("routeId")
  487. text = text.replace("routeId(\"routeId\")", "routeId(\"" + routeId + "\")");
  488. boolean succ = FileUtil.writeFile(newFilePath,text,"UTF-8");
  489. //TODO 上传到GridFS
  490. if (succ){
  491. newFileName = GridFSUtil.uploadFile("upload", newFilePath, newFileName);
  492. }else {
  493. return null;
  494. }
  495. return newFileName;
  496. } catch (Exception e) {
  497. System.out.println("修改Route的java文件操作出错");
  498. e.printStackTrace();
  499. }
  500. return null;
  501. }
  502. /**
  503. * 生成processor的java文件
  504. * @param jobId 任务Id
  505. * @param routeId 流程Code
  506. * @param tempFilePath 模板文件名
  507. * @param className 模板类名
  508. * @return
  509. */
  510. public static String genProcessorJavaFile(String jobId,String routeId ,String tempFilePath, String className) {
  511. try {
  512. String newFileName = className+routeId+".java";
  513. String newFilePath = "/temp/"+className;
  514. String text = GridFSUtil.readFile("upload",tempFilePath);
  515. text = text.replace("jobId=jobId","jobId="+jobId);
  516. boolean succ = FileUtil.writeFile(newFilePath,text,"UTF-8");
  517. //TODO 上传到GridFS
  518. if (succ){
  519. newFileName = GridFSUtil.uploadFile("upload", newFilePath, newFileName);
  520. return newFileName;
  521. }
  522. } catch (Exception e) {
  523. System.out.println("生成processor的java文件操作出错");
  524. e.printStackTrace();
  525. }
  526. return null;
  527. }
  528. }