FlowManager.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. package com.yihu.hos.system.service;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.yihu.hos.common.constants.Constants;
  4. import com.yihu.hos.core.file.FileUtil;
  5. import com.yihu.hos.core.http.HTTPResponse;
  6. import com.yihu.hos.core.http.HttpClientKit;
  7. import com.yihu.hos.services.ServiceFlowEventService;
  8. import com.yihu.hos.system.dao.FlowClassDao;
  9. import com.yihu.hos.system.dao.FlowTempDao;
  10. import com.yihu.hos.system.dao.intf.IFlowClassDao;
  11. import com.yihu.hos.system.dao.intf.IFlowDao;
  12. import com.yihu.hos.system.dao.intf.IFlowTempDao;
  13. import com.yihu.hos.system.model.SystemServiceFlow;
  14. import com.yihu.hos.system.model.SystemServiceFlowClass;
  15. import com.yihu.hos.system.model.SystemServiceFlowTemp;
  16. import com.yihu.hos.system.service.intf.IFlowManage;
  17. import com.yihu.hos.web.framework.model.ActionResult;
  18. import com.yihu.hos.web.framework.model.DictItem;
  19. import com.yihu.hos.web.framework.model.Result;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.beans.factory.annotation.Value;
  22. import org.springframework.stereotype.Service;
  23. import org.springframework.transaction.annotation.Transactional;
  24. import org.springframework.web.multipart.MultipartFile;
  25. import javax.annotation.Resource;
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.util.*;
  29. /**
  30. * 系统流程管理业务类
  31. *
  32. * @author HZY
  33. * @vsrsion 1.0
  34. * Created at 2016/8/19.
  35. */
  36. @Service("flowManager")
  37. public class FlowManager implements IFlowManage {
  38. public static final String BEAN_ID = "flowManager";
  39. @Value("${esb.genCamelUrl}")
  40. private String genCamelUrl;
  41. @Resource(name = "flowDao")
  42. private IFlowDao flowDao;
  43. @Resource(name = FlowClassDao.BEAN_ID)
  44. private IFlowClassDao flowClassDao;
  45. @Resource(name = FlowTempDao.BEAN_ID)
  46. private IFlowTempDao flowTempDao;
  47. @Autowired
  48. ServiceFlowEventService serviceFlowEventService;
  49. @Override
  50. public Result getFlowList(Map<String, Object> params) throws Exception {
  51. return flowDao.getFlowList(params);
  52. }
  53. @Override
  54. public SystemServiceFlow getFlowById(Integer id) throws Exception {
  55. return flowDao.getEntity(SystemServiceFlow.class, id);
  56. }
  57. @Transactional
  58. public Result addFlow(SystemServiceFlow obj) throws Exception {
  59. obj.setCreateDate(new Date());
  60. flowDao.saveEntity(obj);
  61. if (Constants.CLASS.equals(obj.getFileType())){
  62. List<SystemServiceFlowClass> flowClassList = obj.getFlowClassArray();
  63. for (SystemServiceFlowClass flowClass:flowClassList){
  64. flowClass.setFlowId(obj.getId());
  65. flowDao.saveEntity(flowClass);
  66. //发送消息到MQ对列
  67. sendUpdateMessage(obj.getCode(), flowClass, Constants.FLOW_OP_ADD);
  68. }
  69. }else if (Constants.JAVA.equals(obj.getFileType())){
  70. List<SystemServiceFlowTemp> flowTempList = obj.getFlowTempArray();
  71. for (SystemServiceFlowTemp flowTemp:flowTempList){
  72. flowTemp.setFlowId(obj.getId());
  73. flowDao.saveEntity(flowTemp);
  74. }
  75. }
  76. return Result.success("保存成功");
  77. }
  78. @Transactional
  79. public Result updateFlow(SystemServiceFlow obj) throws Exception {
  80. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, obj.getId());
  81. flow.setCode(obj.getCode());
  82. flow.setName(obj.getName());
  83. flow.setPath(obj.getPath());
  84. flow.setChart(obj.getChart());
  85. flow.setValid(obj.getValid());
  86. flow.setFileType(obj.getFileType());
  87. if (Constants.JAVA.equals(flow.getFileType())){
  88. List<SystemServiceFlowTemp> flowTempList = obj.getFlowTempArray();
  89. boolean succ = flowTempDao.deleteFlowTempByFlowId(obj.getId());
  90. if (succ){
  91. for (SystemServiceFlowTemp flowTemp:flowTempList){
  92. flowTempDao.saveEntity(flowTemp);
  93. }
  94. }
  95. }else if (Constants.CLASS.equals(flow.getFileType())){
  96. List<Integer> classIds = flowClassDao.getFlowClassIds(obj.getId());//原flowclass集合
  97. List<SystemServiceFlowClass> flowClassList = obj.getFlowClassArray();
  98. SystemServiceFlowClass flowClassRoute = null;
  99. String oper = "";
  100. for (SystemServiceFlowClass flowClass:flowClassList){
  101. if (flowClass.getId()!=null) {
  102. classIds.remove(flowClass.getId());
  103. flowClassDao.updateEntity(flowClass);
  104. if (!flowClass.getType().equals(Constants.FLOW_TYPE_ROUTE)) {
  105. sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_UPDATE);
  106. } else {
  107. flowClassRoute = flowClass;
  108. oper = Constants.FLOW_OP_UPDATE;
  109. }
  110. }else {
  111. if (!flowClass.getType().equals(Constants.FLOW_TYPE_ROUTE)) {
  112. flowClassDao.saveEntity(flowClass);
  113. sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_ADD);
  114. } else {
  115. flowClassRoute = flowClass;
  116. oper = Constants.FLOW_OP_ADD;
  117. }
  118. }
  119. }
  120. //删除判断
  121. if (classIds !=null && classIds.size()>0){
  122. for (Integer id:classIds){
  123. SystemServiceFlowClass flowClass = getFlowClassById(id);
  124. flowClassDao.deleteEntity(flowClass);
  125. if (!flowClass.getType().equals(Constants.FLOW_TYPE_ROUTE)) {
  126. sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_DELETE);
  127. } else {
  128. flowClassRoute = flowClass;
  129. flowClassRoute.setIsUpdate("1");
  130. oper = Constants.FLOW_OP_DELETE;
  131. }
  132. }
  133. }
  134. if (flowClassRoute != null) {
  135. sendUpdateMessage(flow.getCode(), flowClassRoute, oper);
  136. }
  137. }
  138. flowDao.updateEntity(flow);
  139. return Result.success("更新成功");
  140. }
  141. @Transactional
  142. public Result deleteFlow(Integer id) throws Exception {
  143. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, id);
  144. List<SystemServiceFlowClass> flowClassList = flowClassDao.getFlowClassByFlowId(id);
  145. SystemServiceFlowClass flowClassRoute = null;
  146. String oper = "";
  147. for (SystemServiceFlowClass flowClass:flowClassList){
  148. flowClassDao.deleteEntity(flowClass);
  149. //发送消息到MQ对列
  150. if (!flowClass.getType().equals(Constants.FLOW_TYPE_ROUTE)) {
  151. sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_DELETE);
  152. } else {
  153. flowClassRoute = flowClass;
  154. flowClassRoute.setIsUpdate("1");
  155. oper = Constants.FLOW_OP_DELETE;
  156. }
  157. }
  158. if (flowClassRoute != null) {
  159. sendUpdateMessage(flow.getCode(), flowClassRoute, oper);
  160. }
  161. if (Constants.JAVA.equals(flow.getFileType())){
  162. flowTempDao.deleteFlowTempByFlowId(id);
  163. }
  164. flowDao.deleteEntity(flow);
  165. return Result.success("删除成功");
  166. }
  167. /* =================================== flowClass 部分================================================*/
  168. @Override
  169. public Result getFlowClassList(Map<String, Object> params) throws Exception {
  170. return null;
  171. }
  172. @Override
  173. public List<SystemServiceFlowClass> getFlowClassByFlowId(Integer flowId) throws Exception {
  174. return flowClassDao.getFlowClassByFlowId(flowId);
  175. }
  176. public SystemServiceFlowClass getFlowClassById(Integer id) throws Exception {
  177. return flowClassDao.getEntity(SystemServiceFlowClass.class, id);
  178. }
  179. @Transactional
  180. public Result addFlowClass(SystemServiceFlowClass obj) throws Exception {
  181. flowDao.saveEntity(obj);
  182. return Result.success("保存成功");
  183. }
  184. @Transactional
  185. public Result updateFlowClass(SystemServiceFlowClass obj) throws Exception {
  186. return null;
  187. }
  188. @Transactional
  189. public boolean deleteFlowClassByFlowId(Integer flowId) {
  190. boolean succ =flowClassDao.deleteFlowClassByFlowId(flowId);
  191. return succ;
  192. }
  193. @Override
  194. public List<SystemServiceFlowTemp> getFlowTempByFlowId(Integer id) throws Exception {
  195. return flowTempDao.getFlowTempByFlowId(id);
  196. }
  197. //TODO
  198. @Override
  199. public boolean genFlewByflowTempId(Integer flowTempId) throws Exception {
  200. SystemServiceFlow flow = getFlowById(flowTempId);
  201. //生成新的route类
  202. //添加新processor记录
  203. return false;
  204. }
  205. @Override
  206. public String uploadFile(MultipartFile file, String baseSavePath) {
  207. String fileName = file.getOriginalFilename();
  208. boolean succ = false;
  209. try {
  210. succ = FileUtil.writeFile(baseSavePath + File.separator + fileName, file.getBytes(), "utf-8");
  211. if (succ){
  212. return fileName;
  213. }
  214. } catch (IOException e) {
  215. e.printStackTrace();
  216. }
  217. return null;
  218. }
  219. /**
  220. * 发送MQ消息-更新路由
  221. * @param flowCode 服务流程Code标识
  222. * @param flowClass
  223. * @param operate
  224. */
  225. public void sendUpdateMessage(String flowCode,SystemServiceFlowClass flowClass,String operate){
  226. //发送消息到MQ对列
  227. if ("1".equals(flowClass.getIsUpdate()) && Constants.FLOW_TYPE_ROUTE.equals(flowClass.getType())) {
  228. //route
  229. switch (operate){
  230. case "add" : serviceFlowEventService.routeDefineAdded(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  231. case "delete" : serviceFlowEventService.routeDefineDelete(flowCode, flowClass.getPackageName(), flowClass.getClassName()); break;
  232. case "update" : serviceFlowEventService.routeDefineChanged(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  233. default : break;
  234. }
  235. } else if ("1".equals(flowClass.getIsUpdate()) && Constants.FLOW_TYPE_PROCESSOR.equals(flowClass.getType())) {
  236. //processor
  237. switch (operate){
  238. case "add" : serviceFlowEventService.processorAdded(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  239. case "delete" : serviceFlowEventService.processorDataDeleted(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  240. case "update" : serviceFlowEventService.processorDataChanged(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  241. default : break;
  242. }
  243. }
  244. }
  245. /**
  246. * 获取流程列表
  247. * @param type 流程的文件类型
  248. * @return
  249. * @throws Exception
  250. */
  251. @Override
  252. public ActionResult getFlowList(String type) throws Exception {
  253. List<SystemServiceFlow> flowList = flowDao.getFlowList(type);
  254. ActionResult re = new ActionResult();
  255. if(flowList!=null&&flowList.size()>0)
  256. {
  257. List<DictItem> dictList = new ArrayList<>();
  258. for(SystemServiceFlow item:flowList){
  259. DictItem dict = new DictItem();
  260. dict.setCode(item.getId().toString());
  261. dict.setValue(item.getName());
  262. dict.setExtend("");
  263. dictList.add(dict);
  264. }
  265. re.setData(dictList);
  266. }
  267. return re;
  268. }
  269. /**
  270. * TODO 调用broker接口生成camel相关文件
  271. * @param flowId
  272. * @param newCron
  273. * @throws Exception
  274. */
  275. public Integer genCamelFile(Integer flowId, String newCron) throws Exception {
  276. Long timestamp = System.currentTimeMillis();
  277. ObjectMapper objectMapper = new ObjectMapper();
  278. List<SystemServiceFlowTemp> flowClassRouters = flowTempDao.getFlowTemps(flowId, Constants.FLOW_TYPE_ROUTE);
  279. List<SystemServiceFlowTemp> flowClassProces = flowTempDao.getFlowTemps(flowId, Constants.FLOW_TYPE_PROCESSOR);
  280. SystemServiceFlow oldFlow = getFlowById(flowId);
  281. //route模板文件记录是否存在。不存在就返回。
  282. if (!flowClassRouters.isEmpty()){
  283. Map<String,String> params = null;
  284. SystemServiceFlowTemp flowTemp =flowClassRouters.get(0);
  285. StringBuilder basePath = new StringBuilder();;
  286. if (flowTemp.getPackageName()!=null){
  287. String packagePath[] = flowTemp.getPackageName().split("\\.");
  288. for (int i=0;i<packagePath.length;i++){
  289. basePath.append(packagePath[i]).append("/");
  290. }
  291. }
  292. //成功生成文件后,添加flow和flowclass记录
  293. //生成新流程
  294. SystemServiceFlow newFlow = new SystemServiceFlow();
  295. newFlow.setName(oldFlow.getName()+timestamp);
  296. newFlow.setCode(oldFlow.getCode()+timestamp);
  297. newFlow.setChart(oldFlow.getChart());
  298. newFlow.setValid(1);
  299. newFlow.setCreateDate(new Date());
  300. newFlow.setFileType(Constants.CLASS);
  301. flowDao.saveEntity(newFlow);
  302. //新增processor记录
  303. for (SystemServiceFlowTemp process:flowClassProces){
  304. // String newProcessName = process.getClassName()+timestamp;
  305. String newProcessPath = null;
  306. StringBuilder proPath = new StringBuilder( );;
  307. if (process.getPackageName()!=null){
  308. String packagePath[] = process.getPackageName().split("\\.");
  309. for (int i=0;i<packagePath.length;i++){
  310. proPath.append(packagePath[i]).append("/");
  311. }
  312. }
  313. params = new HashMap<>();
  314. params.put("type",Constants.FLOW_TYPE_PROCESSOR);
  315. params.put("filePath", process.getClassPath());
  316. params.put("packageName", proPath.toString());
  317. params.put("newClassName",process.getClassName());//原文件名加当前时间戳
  318. params.put("oldClassName", process.getClassName());
  319. params.put("newCron",newCron);
  320. HTTPResponse response = HttpClientKit.post(genCamelUrl, params);
  321. if (response.getStatusCode()==200 ){
  322. Map<String,Object> body = objectMapper.readValue(response.getBody(),Map.class);
  323. boolean succ = (boolean) body.get("successFlg");
  324. if (succ){
  325. newProcessPath = body.get("message").toString();
  326. }else {
  327. return null;
  328. }
  329. }
  330. System.out.println(response.getBody());
  331. SystemServiceFlowClass processClass = new SystemServiceFlowClass();
  332. processClass.setPackageName(process.getPackageName());
  333. processClass.setClassName(process.getClassName());
  334. processClass.setClassPath(newProcessPath);
  335. processClass.setFlowId(newFlow.getId());
  336. processClass.setType(Constants.FLOW_TYPE_PROCESSOR);
  337. flowClassDao.saveEntity(processClass);
  338. processClass.setIsUpdate("1");
  339. sendUpdateMessage(newFlow.getCode(), processClass, Constants.FLOW_OP_ADD);
  340. // copyProcessor(process.getClassPath(),proPath.toString(),process.getClassName());
  341. }
  342. String newClassName = flowTemp.getClassName()+timestamp;
  343. String newRoutePath =null;
  344. params = new HashMap<>();
  345. params.put("type",Constants.FLOW_TYPE_ROUTE);
  346. params.put("filePath", flowTemp.getClassPath());
  347. params.put("packageName", basePath.toString());
  348. params.put("oldClassName", flowTemp.getClassName());
  349. params.put("newClassName",newClassName);//原文件名加当前时间戳
  350. params.put("newCron",newCron);
  351. HTTPResponse response = HttpClientKit.post(genCamelUrl, params);
  352. if (response.getStatusCode()==200 ){
  353. Map<String,Object> body = objectMapper.readValue(response.getBody(),Map.class);
  354. boolean succ = (boolean) body.get("successFlg");
  355. if (succ){
  356. newRoutePath = body.get("message").toString();
  357. }else {
  358. return null;
  359. }
  360. }
  361. System.out.println(response.getBody());
  362. SystemServiceFlowClass newFlowClass = new SystemServiceFlowClass();
  363. newFlowClass.setPackageName(flowTemp.getPackageName());
  364. newFlowClass.setClassName(newClassName);
  365. newFlowClass.setClassPath(newRoutePath);
  366. newFlowClass.setFlowId(newFlow.getId());
  367. newFlowClass.setType(Constants.FLOW_TYPE_ROUTE);
  368. flowClassDao.saveEntity(newFlowClass);
  369. newFlowClass.setIsUpdate("1");
  370. sendUpdateMessage(newFlow.getCode(), newFlowClass, Constants.FLOW_OP_ADD);
  371. // genNewRoutefile(flowTemp.getClassPath(),basePath.toString(),flowTemp.getClassName(),newClassName,newCron);
  372. return newFlow.getId();
  373. }
  374. return null;
  375. }
  376. /**
  377. * 修改camel相关文件
  378. * @param flowId 流程ID
  379. * @param newCron 新cron
  380. * @return
  381. * @throws Exception
  382. */
  383. @Override
  384. public Integer updateCamelFile(Integer flowTempId,Integer flowId, String newCron) throws Exception {
  385. Long timestamp = System.currentTimeMillis();
  386. ObjectMapper objectMapper = new ObjectMapper();
  387. List<SystemServiceFlowTemp> flowTempRouters = flowTempDao.getFlowTemps(flowTempId, Constants.FLOW_TYPE_ROUTE);
  388. List<SystemServiceFlowClass> flowClassRouters = flowClassDao.getFlowClass(flowId, Constants.FLOW_TYPE_ROUTE);
  389. // SystemServiceFlow oldFlow = getFlowById(flowId);
  390. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class,flowId);
  391. //route模板文件记录是否存在。不存在就返回。
  392. if (!flowTempRouters.isEmpty()){
  393. SystemServiceFlowTemp flowTemp =flowTempRouters.get(0);
  394. SystemServiceFlowClass flowClass =flowClassRouters.get(0);
  395. StringBuilder basePath = new StringBuilder();;
  396. if (flowTemp.getPackageName()!=null){
  397. String packagePath[] = flowTemp.getPackageName().split("\\.");
  398. for (int i=0;i<packagePath.length;i++){
  399. basePath.append(packagePath[i]).append("/");
  400. }
  401. }
  402. Map<String,String> params = new HashMap<>();
  403. params.put("type",Constants.FLOW_TYPE_ROUTE);
  404. params.put("filePath", flowTemp.getClassPath());
  405. params.put("packageName", basePath.toString());
  406. params.put("oldClassName", flowTemp.getClassName());
  407. params.put("newClassName", flowClass.getClassName());//原文件名加当前时间戳
  408. params.put("newCron",newCron);
  409. HTTPResponse response = HttpClientKit.post(genCamelUrl, params);
  410. if (response.getStatusCode()==200 ){
  411. Map<String,Object> body = objectMapper.readValue(response.getBody(),Map.class);
  412. boolean succ = (boolean) body.get("successFlg");
  413. if (succ){
  414. //route文件生成成功,发送消息
  415. flowClass.setIsUpdate("1");
  416. sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_UPDATE);
  417. return flowId;
  418. }else {
  419. return null;
  420. }
  421. }else {
  422. return null;
  423. }
  424. // genNewRoutefile(flowTemp.getClassPath(),basePath.toString(),flowTemp.getClassName(),newClassName,newCron);
  425. }
  426. return null;
  427. }
  428. }