controller.ftl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <#assign package=table.variable.package>
  2. <#assign class=table.variable.class>
  3. <#assign classVar=table.variable.classVar>
  4. <#assign package=table.variable.package>
  5. <#assign comment=table.tableDesc>
  6. <#assign fieldList=table.fieldList>
  7. <#assign subTableList=table.subTableList>
  8. <#assign hasScript=0>
  9. <#assign hasIdentity=0>
  10. <#list fieldList as field>
  11. <#if field.valueFrom==1|| field.valueFrom==2>
  12. <#assign hasScript=1>
  13. <#elseif field.valueFrom==3>
  14. <#assign hasIdentity=1>
  15. </#if>
  16. </#list>
  17. package com.hotent.${system}.controller.${package};
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import java.util.List;
  21. import javax.annotation.Resource;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import org.springframework.stereotype.Controller;
  25. import org.springframework.web.bind.annotation.RequestMapping;
  26. import com.hotent.platform.annotion.Action;
  27. import org.springframework.web.servlet.ModelAndView;
  28. import com.hotent.core.util.UniqueIdUtil;
  29. import com.hotent.core.web.util.RequestUtil;
  30. import com.hotent.core.web.controller.BaseController;
  31. import com.hotent.core.util.BeanUtils;
  32. import com.hotent.core.web.query.QueryFilter;
  33. import com.hotent.core.page.PageList;
  34. import com.hotent.platform.model.system.SysUser;
  35. import org.apache.commons.lang.exception.ExceptionUtils;
  36. import com.hotent.core.bpm.util.BpmUtil;
  37. import net.sf.json.JSONObject;
  38. import com.hotent.core.util.MapUtil;
  39. import com.hotent.${system}.model.${package}.${class};
  40. import com.hotent.${system}.service.${package}.${class}Service;
  41. <#if subTableList?size != 0>
  42. <#list subTableList as subtable>
  43. import com.hotent.${system}.model.${subtable.variable.package}.${subtable.variable.class};
  44. </#list>
  45. </#if>
  46. import com.hotent.core.web.ResultMessage;
  47. <#if hasScript==1>
  48. import com.hotent.core.engine.GroovyScriptEngine;
  49. </#if>
  50. <#if hasIdentity==1>
  51. import com.hotent.platform.service.system.IdentityService;
  52. </#if>
  53. <#if flowKey?exists>
  54. import com.hotent.core.util.StringUtil;
  55. import com.hotent.core.api.util.ContextUtil;
  56. import com.hotent.core.bpm.model.ProcessCmd;
  57. import com.hotent.platform.model.bpm.ProcessRun;
  58. import com.hotent.platform.service.bpm.ProcessRunService;
  59. </#if>
  60. /**
  61. * 对象功能:${comment} 控制器类
  62. */
  63. @Controller
  64. @RequestMapping("/${system}/${package}/${classVar}/")
  65. public class ${class}Controller extends BaseController
  66. {
  67. @Resource
  68. private ${class}Service ${classVar}Service;
  69. <#if hasScript==1>
  70. @Resource
  71. private GroovyScriptEngine engine;
  72. </#if>
  73. <#if hasIdentity==1>
  74. @Resource
  75. private IdentityService identityService;
  76. </#if>
  77. <#if flowKey?exists>
  78. @Resource
  79. private ProcessRunService processRunService;
  80. </#if>
  81. <#--直接绑定工作流生成-->
  82. <#if table.variable.flowKey?exists>
  83. private final String flowKey = "${model.variables.flowKey}"; //绑定流程定义
  84. </#if>
  85. /**
  86. * 添加或更新${comment}。
  87. * @param request
  88. * @param response
  89. * @param ${classVar} 添加或更新的实体
  90. * @return
  91. * @throws Exception
  92. */
  93. @RequestMapping("save")
  94. @Action(description="添加或更新${comment}")
  95. public void save(HttpServletRequest request, HttpServletResponse response,${class} ${classVar}) throws Exception
  96. {
  97. String resultMsg=null;
  98. try{
  99. if(${classVar}.get<#if table.isExternal==0>Id<#else>${table.pkField?cap_first}</#if>()==null){
  100. <#if subTableList?size != 0>
  101. ${classVar}Service.save(${classVar});
  102. <#else>
  103. ${classVar}Service.save(${classVar});
  104. </#if>
  105. resultMsg=getText("添加","${comment}");
  106. }else{
  107. <#if subTableList?size != 0>
  108. ${classVar}Service.save(${classVar});
  109. <#else>
  110. ${classVar}Service.save(${classVar});
  111. </#if>
  112. resultMsg=getText("更新","${comment}");
  113. }
  114. writeResultMessage(response.getWriter(),resultMsg,ResultMessage.Success);
  115. }catch(Exception e){
  116. writeResultMessage(response.getWriter(),resultMsg+","+e.getMessage(),ResultMessage.Fail);
  117. }
  118. }
  119. /**
  120. * 取得${comment}分页列表
  121. * @param request
  122. * @param response
  123. * @param page
  124. * @return
  125. * @throws Exception
  126. */
  127. @RequestMapping("list")
  128. @Action(description="查看${comment}分页列表")
  129. public ModelAndView list(HttpServletRequest request,HttpServletResponse response) throws Exception
  130. {
  131. List<${class}> list=${classVar}Service.getAll(new QueryFilter(request,"${classVar}Item"));
  132. ModelAndView mv=this.getAutoView().addObject("${classVar}List",list).addObject("defId","${defId}");
  133. return mv;
  134. }
  135. /**
  136. * 删除${comment}
  137. * @param request
  138. * @param response
  139. * @throws Exception
  140. */
  141. @RequestMapping("del")
  142. @Action(description="删除${comment}")
  143. public void del(HttpServletRequest request, HttpServletResponse response) throws Exception
  144. {
  145. String preUrl= RequestUtil.getPrePage(request);
  146. ResultMessage message=null;
  147. try{
  148. <#if table.isExternal==0>
  149. Long[] lAryId=RequestUtil.getLongAryByStr(request,"id");
  150. <#else>
  151. Long[] lAryId=RequestUtil.getLongAryByStr(request,"id");
  152. </#if>
  153. <#if subTableList?size != 0>
  154. ${classVar}Service.delByIds(lAryId);
  155. message=new ResultMessage(ResultMessage.Success,"删除${comment}及其从表成功!");
  156. <#else>
  157. </#if>
  158. ${classVar}Service.delByIds(lAryId);
  159. message=new ResultMessage(ResultMessage.Success, "删除${comment}成功!");
  160. }catch(Exception ex){
  161. message=new ResultMessage(ResultMessage.Fail, "删除失败" + ex.getMessage());
  162. }
  163. addMessage(message, request);
  164. response.sendRedirect(preUrl);
  165. }
  166. /**
  167. * 编辑${comment}
  168. * @param request
  169. * @param response
  170. * @throws Exception
  171. */
  172. @RequestMapping("edit")
  173. @Action(description="编辑${comment}")
  174. public ModelAndView edit(HttpServletRequest request) throws Exception
  175. {
  176. <#if table.isExternal==0>
  177. Long id=RequestUtil.getLong(request,"id");
  178. <#else>
  179. Long ${table.pkField}=RequestUtil.getLong(request,"id");
  180. </#if>
  181. <#if flowKey?exists>
  182. Long runId=0L;
  183. ProcessRun processRun=processRunService.getByBusinessKey(<#if table.isExternal==0>id.toString()<#else>${table.pkField}.toString()</#if>);
  184. if(BeanUtils.isNotEmpty(processRun)){
  185. runId=processRun.getRunId();
  186. }
  187. </#if>
  188. String returnUrl=RequestUtil.getPrePage(request);
  189. <#if table.isExternal==0>
  190. ${class} ${classVar}=${classVar}Service.getById(id);
  191. <#else>
  192. ${class} ${classVar}=${classVar}Service.getById(${table.pkField});
  193. </#if>
  194. <#if hasScript==1||hasIdentity==1>
  195. if(BeanUtils.isEmpty(${classVar})){
  196. ${classVar}=new ${class}();
  197. <#list fieldList as field>
  198. <#if field.valueFrom!=0>
  199. <#if (field.valueFrom==1||field.valueFrom==2)>
  200. String ${field.fieldName}_script="${field.script?trim}";
  201. ${classVar}.set${field.fieldName?cap_first}(engine.executeObject(${field.fieldName}_script, null).toString());
  202. <#elseif (field.valueFrom==3)>
  203. String ${field.fieldName}_id=identityService.nextId("${field.identity}");
  204. ${classVar}.set${(field.fieldName)?cap_first}(${field.fieldName}_id);
  205. </#if>
  206. </#if>
  207. <#if field.fieldType=="date">
  208. <#if field.isCurrentDateStr==1>
  209. ${classVar}.set${field.fieldName?cap_first}(new Date());
  210. </#if>
  211. </#if>
  212. </#list>
  213. }
  214. </#if>
  215. <#if subTableList?size != 0>
  216. <#list subTableList as subtable>
  217. <#assign vars=subtable.variable>
  218. List<${subtable.variable.class}> ${subtable.variable.classVar}List=${classVar}Service.get${vars.classVar?cap_first}List(<#if table.isExternal==0>id<#else>${table.pkField}</#if>);
  219. </#list>
  220. </#if>
  221. return getAutoView().addObject("${classVar}",${classVar})
  222. <#if subTableList?size != 0>
  223. <#list subTableList as subtable>
  224. .addObject("${subtable.variable.classVar}List",${subtable.variable.classVar}List)
  225. </#list>
  226. </#if>
  227. <#if flowKey?exists>
  228. .addObject("runId", runId)
  229. </#if>
  230. .addObject("returnUrl",returnUrl);
  231. }
  232. /**
  233. * 取得${comment}明细
  234. * @param request
  235. * @param response
  236. * @return
  237. * @throws Exception
  238. */
  239. @RequestMapping("get")
  240. @Action(description="查看${comment}明细")
  241. public ModelAndView get(HttpServletRequest request, HttpServletResponse response) throws Exception
  242. {
  243. <#if table.isExternal==0>
  244. Long id=RequestUtil.getLong(request,"id");
  245. ${class} ${classVar}=${classVar}Service.getById(id);
  246. <#else>
  247. Long ${table.pkField}=RequestUtil.getLong(request,"id");
  248. ${class} ${classVar}=${classVar}Service.getById(${table.pkField});
  249. </#if>
  250. <#if flowKey?exists>
  251. Long runId=0L;
  252. ProcessRun processRun=processRunService.getByBusinessKey(<#if table.isExternal==0>id.toString()<#else>${table.pkField}.toString()</#if>);
  253. if(BeanUtils.isNotEmpty(processRun)){
  254. runId=processRun.getRunId();
  255. }
  256. </#if>
  257. <#if subTableList?size != 0>
  258. <#list subTableList as subtable>
  259. <#assign vars=subtable.variable>
  260. List<${vars.class}> ${vars.classVar}List=${classVar}Service.get${vars.classVar?cap_first}List(<#if table.isExternal==0>id<#else>${table.pkField}</#if>);
  261. </#list>
  262. return getAutoView().addObject("${classVar}",${classVar})
  263. <#if flowKey?exists>
  264. .addObject("runId", runId)
  265. </#if>
  266. <#list subTableList as subtable>
  267. .addObject("${subtable.variable.classVar}List",${subtable.variable.classVar}List)<#if !subtable_has_next>;</#if>
  268. </#list>
  269. <#else>
  270. return getAutoView().addObject("${classVar}", ${classVar})<#if flowKey?exists>.addObject("runId", runId)</#if>;
  271. </#if>
  272. }
  273. <#if flowKey?exists>
  274. /**
  275. * 流程url表单 绑定的表单明细
  276. * @param request
  277. * @param response
  278. * @return
  279. * @throws Exception
  280. */
  281. @RequestMapping("detail")
  282. @Action(description="表单明细")
  283. public ModelAndView detail(HttpServletRequest request, HttpServletResponse response) throws Exception
  284. {
  285. <#if table.isExternal==0>
  286. Long id=RequestUtil.getLong(request,"id");
  287. ${class} ${classVar} = ${classVar}Service.getById(id);
  288. <#else>
  289. Long ${table.pkField}=RequestUtil.getLong(request,"id");
  290. ${class} ${classVar}=${classVar}Service.getById(${table.pkField});
  291. </#if>
  292. <#if subTableList?exists && subTableList?size != 0>
  293. <#list subTableList as subtable>
  294. <#assign vars=subtable.variable>
  295. List<${vars.class}> ${vars.classVar}List=${classVar}Service.get${vars.classVar?cap_first}List(<#if table.isExternal==0>id<#else>${table.pkField}</#if>);
  296. </#list>
  297. return getAutoView().addObject("${classVar}",${classVar})
  298. <#list subTableList as subtable>
  299. .addObject("${subtable.variable.classVar}List",${subtable.variable.classVar}List)<#if !subtable_has_next>;</#if>
  300. </#list>
  301. <#else>
  302. return getAutoView().addObject("${classVar}", ${classVar});
  303. </#if>
  304. }
  305. /**
  306. * 流程url表单 绑定的表单编辑页面
  307. * @param request
  308. * @param response
  309. * @return
  310. * @throws Exception
  311. */
  312. @RequestMapping("modify")
  313. public ModelAndView modify(HttpServletRequest request, HttpServletResponse response) throws Exception
  314. {
  315. <#if table.isExternal==0>
  316. Long id=RequestUtil.getLong(request,"id");
  317. ${class} ${classVar} = ${classVar}Service.getById(id);
  318. <#else>
  319. Long ${table.pkField}=RequestUtil.getLong(request,"id");
  320. ${class} ${classVar}=${classVar}Service.getById(${table.pkField});
  321. </#if>
  322. <#if subTableList?exists && subTableList?size != 0>
  323. <#list subTableList as subtable>
  324. <#assign vars=subtable.variable>
  325. List<${vars.class}> ${vars.classVar}List=${classVar}Service.get${vars.classVar?cap_first}List(<#if table.isExternal==0>id<#else>${table.pkField}</#if>);
  326. </#list>
  327. return getAutoView().addObject("${classVar}",${classVar})
  328. <#list subTableList as subtable>
  329. .addObject("${subtable.variable.classVar}List",${subtable.variable.classVar}List)<#if !subtable_has_next>;</#if>
  330. </#list>
  331. <#else>
  332. return getAutoView().addObject("${classVar}", ${classVar});
  333. </#if>
  334. }
  335. /**
  336. * 启动流程
  337. * @param request
  338. * @param response
  339. * @return
  340. * @throws Exception
  341. 由于修改了流程启动方式,不需要这个方法,因此先注释
  342. @RequestMapping("run")
  343. @Action(description="启动流程")
  344. public void run(HttpServletRequest request, HttpServletResponse response,${class} ${classVar}) throws Exception
  345. {
  346. <#if table.isExternal==0>
  347. Long id=RequestUtil.getLong(request,"id",0L);
  348. <#else>
  349. Long ${table.pkField}=RequestUtil.getLong(request,"${table.pkField}",0L);
  350. </#if>
  351. Integer isList=RequestUtil.getInt(request, "isList",0);
  352. ProcessCmd processCmd=new ProcessCmd();
  353. //添加表单数据,当人员为流程变量的时候用到,先注释,因为会引起流程提交报错
  354. //if(isList==0){
  355. // processCmd = BpmUtil.getProcessCmd(request);
  356. //}else{
  357. // ${class} ${classVar}Temd = ${classVar}Service.getById(${classVar}.get<#if table.isExternal==0>Id<#else>${table.pkField?cap_first}</#if>());
  358. // Map<String,Object> map = MapUtil.transBean2Map(${classVar}Temd);
  359. // JSONObject jsonObject = JSONObject.fromObject(map);
  360. // String str = "{'main':{'fields':"+jsonObject+"},'sub':[],'opinion':[]}";
  361. // JSONObject formData = JSONObject.fromObject(str);
  362. // processCmd.setFormData(formData.toString());
  363. //}
  364. processCmd.setFlowKey("${flowKey}");
  365. processCmd.setUserAccount(ContextUtil.getCurrentUser().getAccount());
  366. try {
  367. if(<#if table.isExternal==0>id<#else>${table.pkField}</#if>!=0L){
  368. if(isList==1){
  369. ${classVar}=${classVar}Service.getById(id);
  370. }
  371. processCmd.setBusinessKey(id.toString());
  372. processRunService.startProcess(processCmd);
  373. ${classVar}Service.save(${classVar});
  374. }else{
  375. Long genId=UniqueIdUtil.genId();
  376. processCmd.setBusinessKey(genId.toString());
  377. ${classVar}.set<#if table.isExternal==0>Id<#else>${table.pkField?cap_first}</#if>(genId);
  378. processRunService.startProcess(processCmd);
  379. <#if subTableList?size==0>
  380. ${classVar}Service.save(${classVar});
  381. <#else>
  382. ${classVar}Service.save(${classVar});
  383. </#if>
  384. }
  385. writeResultMessage(response.getWriter(), new ResultMessage(ResultMessage.Success, "启动流程成功"));
  386. } catch (Exception e) {
  387. e.printStackTrace();
  388. writeResultMessage(response.getWriter(), new ResultMessage(ResultMessage.Fail, ExceptionUtils.getRootCauseMessage(e)));
  389. }
  390. }*/
  391. @RequestMapping("getMyTodoTask")
  392. @Action(description="查看${comment}任务分页列表")
  393. public ModelAndView getMyTodoTask(HttpServletRequest request,HttpServletResponse response) throws Exception
  394. {
  395. SysUser sysUser=(SysUser)ContextUtil.getCurrentUser();
  396. Long userId=sysUser.getUserId();
  397. List<${class}> list=${classVar}Service.getMyTodoTask(userId, new QueryFilter(request,"${classVar}Item"));
  398. ModelAndView mv=this.getAutoView().addObject("${classVar}List",list);
  399. return mv;
  400. }
  401. @RequestMapping("getMyDraft")
  402. @Action(description="查看${comment}草稿")
  403. public ModelAndView getMyDraft(HttpServletRequest request,HttpServletResponse response) throws Exception
  404. {
  405. SysUser sysUser=(SysUser)ContextUtil.getCurrentUser();
  406. Long userId=sysUser.getUserId();
  407. List<${class}> list=${classVar}Service.getMyDraft(userId, new QueryFilter(request,"${classVar}Item"));
  408. ModelAndView mv=this.getAutoView().addObject("${classVar}List",list);
  409. return mv;
  410. }
  411. @RequestMapping("getMyEnd")
  412. @Action(description="查看我结束的${comment}实例")
  413. public ModelAndView getMyEnd(HttpServletRequest request,HttpServletResponse response) throws Exception
  414. {
  415. SysUser sysUser=(SysUser)ContextUtil.getCurrentUser();
  416. Long userId=sysUser.getUserId();
  417. List<${class}> list=${classVar}Service.getMyEnd(userId, new QueryFilter(request,"${classVar}Item"));
  418. ModelAndView mv=this.getAutoView().addObject("${classVar}List",list);
  419. return mv;
  420. }
  421. </#if>
  422. }