AppHandler.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. package com.pcitc.imp.bizlog.service.handler;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.regex.Matcher;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import com.pcitc.imp.bizlog.bll.itf.AppService;
  8. import com.pcitc.imp.bizlog.exception.BusiException;
  9. import com.pcitc.imp.bizlog.service.model.App;
  10. import com.pcitc.imp.bizlog.util.CheckPrompt;
  11. import com.pcitc.imp.bizlog.util.CheckUtil;
  12. import com.pcitc.imp.bizlog.util.ErrorCodeEnum;
  13. import com.pcitc.imp.common.handler.BaseHandler;
  14. import com.pcitc.imp.common.model.Pagination;
  15. import io.vertx.core.Vertx;
  16. import io.vertx.ext.web.RoutingContext;
  17. import pcitc.imp.common.ettool.utils.ObjectConverter;
  18. import pcitc.imp.common.ettool.utils.RestfulTool;
  19. /**
  20. * Created by pcitc on 2016/12/27.
  21. */
  22. @Controller
  23. public class AppHandler extends BaseHandler {
  24. @Autowired
  25. private AppService appService;
  26. /**
  27. * 新增应用
  28. *
  29. * @param routingContext
  30. */
  31. @SuppressWarnings({ "unchecked", "rawtypes" })
  32. public void addApp(RoutingContext routingContext) {
  33. try {
  34. List<com.pcitc.imp.bizlog.service.model.App> listApp = RestfulTool.toResourceRep(routingContext.getBodyAsString(),
  35. com.pcitc.imp.bizlog.service.model.App.class);
  36. // 空格校验
  37. List<App> apps = getStringNoBlank(listApp);
  38. // 非空校验
  39. // checkInput(apps);
  40. for (App app : apps) {
  41. app.setHref(routingContext.normalisedPath() + "/" + app.getCode());
  42. }
  43. List appEntitys = ObjectConverter.listConverter(apps, com.pcitc.imp.bizlog.bll.entity.App.class);
  44. // 创建app
  45. Vertx vertx = routingContext.vertx();
  46. vertx.executeBlocking(future -> {
  47. try {
  48. Integer addApp = appService.addApp(appEntitys);
  49. future.complete(addApp.toString());
  50. } catch (Exception e) {
  51. future.fail(e);
  52. }
  53. }, res -> {
  54. String collecion = null;
  55. if (res.failed()) {
  56. collecion = buildErrorCollection(routingContext, (BusiException) res.cause());
  57. } else if (res.succeeded()) {
  58. try {
  59. Object result = res.result();
  60. returnCollection(routingContext, result.toString());
  61. } catch (Exception e) {
  62. collecion = buildErrorCollection(routingContext, e);
  63. returnCollection(routingContext, collecion);
  64. }
  65. }
  66. returnCollection(routingContext, collecion);
  67. });
  68. } catch (BusiException e) {
  69. String collecion = buildErrorCollection(routingContext, e);
  70. returnCollection(routingContext, collecion);
  71. } catch (Exception e) {
  72. String collecion = buildErrorCollection(routingContext, e);
  73. returnCollection(routingContext, collecion);
  74. }
  75. }
  76. /**
  77. * 查询应用
  78. *
  79. * @param routingContext
  80. */
  81. @SuppressWarnings({ "unchecked", "rawtypes" })
  82. public void getApps(RoutingContext routingContext) {
  83. String skip = (routingContext.request().getParam("$skip") == null ? null : routingContext.request().getParam("$skip").trim());
  84. String top = (routingContext.request().getParam("$top") == null ? null : routingContext.request().getParam("$top").trim());
  85. String codeList = (routingContext.request().getParam("$codeList") == null ? null : routingContext.request().getParam("$codeList").trim());
  86. try {
  87. // 输入校验
  88. String codes = checkInput(skip, top, codeList);
  89. Pagination pagination = new Pagination();
  90. // 查询app
  91. Vertx vertx = routingContext.vertx();
  92. vertx.executeBlocking(future -> {
  93. List<com.pcitc.imp.bizlog.bll.entity.App> appList = null;
  94. try {
  95. List<Integer> countList = new ArrayList();
  96. if(routingContext.request().getParam("$skip") != null && routingContext.request().getParam("$top") != null){
  97. appList = appService.getAppsByPage(countList,skip, top, codes);
  98. if (!appList.isEmpty()) {
  99. pagination.setRecordCount(Long.valueOf(countList.get(0).toString()));
  100. }
  101. }else{
  102. appList = appService.getAppsByPage(skip, top, codes);
  103. }
  104. future.complete(appList);
  105. } catch (BusiException e) {
  106. future.fail(e);
  107. }
  108. }, res -> {
  109. String collecion = null;
  110. if (res.failed()) {
  111. collecion = buildErrorCollection(routingContext, (BusiException) res.cause());
  112. returnCollection(routingContext, collecion);
  113. } else if (res.succeeded()) {
  114. try {
  115. List<App> listApp = ObjectConverter.listConverter((List<com.pcitc.imp.bizlog.bll.entity.App>) res.result(), App.class);
  116. collecion = RestfulTool.buildCollection(listApp, pagination, routingContext.request().absoluteURI(), App.class);
  117. returnCollection(routingContext, collecion);
  118. } catch (Exception e) {
  119. collecion = buildErrorCollection(routingContext, e);
  120. returnCollection(routingContext, collecion);
  121. }
  122. }
  123. });
  124. } catch (BusiException e) {
  125. String collecion = buildErrorCollection(routingContext, e);
  126. returnCollection(routingContext, collecion);
  127. }
  128. }
  129. /**
  130. * 根据编码查询应用
  131. *
  132. * @param routingContext
  133. */
  134. @SuppressWarnings("unchecked")
  135. public void getAppByCode(RoutingContext routingContext) {
  136. try {
  137. String codetrim = routingContext.request().getParam("code").trim();
  138. // 输入校验
  139. checkInput(codetrim);
  140. // 查询app
  141. Vertx vertx = routingContext.vertx();
  142. vertx.executeBlocking(future -> {
  143. List<com.pcitc.imp.bizlog.bll.entity.App> appList = null;
  144. try {
  145. appList = appService.getAppByCode(codetrim);
  146. future.complete(appList);
  147. } catch (BusiException e) {
  148. future.fail(e);
  149. }
  150. }, res -> {
  151. String collecion = null;
  152. if (res.failed()) {
  153. collecion = buildErrorCollection(routingContext, (BusiException) res.cause());
  154. returnCollection(routingContext, collecion);
  155. } else if (res.succeeded()) {
  156. try {
  157. List<App> listApp = ObjectConverter.listConverter((List<com.pcitc.imp.bizlog.bll.entity.App>) res.result(), App.class);
  158. collecion = RestfulTool.buildCollection(listApp, routingContext.request().absoluteURI(), App.class);
  159. returnCollection(routingContext, collecion);
  160. } catch (Exception e) {
  161. collecion = buildErrorCollection(routingContext, e);
  162. returnCollection(routingContext, collecion);
  163. }
  164. }
  165. });
  166. } catch (BusiException e) {
  167. String collecion = buildErrorCollection(routingContext, e);
  168. returnCollection(routingContext, collecion);
  169. }
  170. }
  171. /**
  172. * 更新应用的名称
  173. * 不支持批更新
  174. *
  175. * @param routingContext
  176. */
  177. @SuppressWarnings({ "unchecked", "rawtypes" })
  178. public void updateAppName(RoutingContext routingContext) {
  179. try {
  180. List<com.pcitc.imp.bizlog.service.model.App> listApp = RestfulTool.toResourceRep(routingContext.getBodyAsString(),
  181. com.pcitc.imp.bizlog.service.model.App.class);
  182. // 空格校验
  183. listApp = getStringNoBlank(listApp);
  184. // 非空校验
  185. checkInputUp(listApp);
  186. List appEntity = ObjectConverter.listConverter(listApp, com.pcitc.imp.bizlog.bll.entity.App.class);
  187. // 更新app
  188. Vertx vertx = routingContext.vertx();
  189. vertx.executeBlocking(future -> {
  190. try {
  191. appService.updateAppName(routingContext.request().getParam("code"), appEntity);
  192. future.complete();
  193. } catch (BusiException e) {
  194. future.fail(e);
  195. }
  196. }, res -> {
  197. String collecion = null;
  198. if (res.failed()) {
  199. collecion = buildErrorCollection(routingContext, (BusiException) res.cause());
  200. } else if (res.succeeded()) {
  201. // collecion = "ok";
  202. }
  203. returnCollection(routingContext, collecion);
  204. });
  205. } catch (BusiException e) {
  206. String collecion = buildErrorCollection(routingContext, e);
  207. returnCollection(routingContext, collecion);
  208. } catch (Exception e) {
  209. String collecion = buildErrorCollection(routingContext, e);
  210. returnCollection(routingContext, collecion);
  211. }
  212. }
  213. /**
  214. * 删除应用
  215. *
  216. * @param routingContext
  217. */
  218. public void deleteApp(RoutingContext routingContext) {
  219. String code = routingContext.request().getParam("code");
  220. String codeVal = (code == null ? null : code.trim());
  221. try {
  222. checkInput(codeVal);
  223. // 更新app
  224. Vertx vertx = routingContext.vertx();
  225. vertx.executeBlocking(future -> {
  226. try {
  227. appService.deleteApp(codeVal);
  228. future.complete();
  229. } catch (BusiException e) {
  230. future.fail(e);
  231. }
  232. }, res -> {
  233. String collecion = null;
  234. if (res.failed()) {
  235. collecion = buildErrorCollection(routingContext, (BusiException) res.cause());
  236. } else if (res.succeeded()) {
  237. // collecion = "ok";
  238. }
  239. returnCollection(routingContext, collecion);
  240. });
  241. } catch (BusiException e) {
  242. String collecion = buildErrorCollection(routingContext, e);
  243. returnCollection(routingContext, collecion);
  244. }
  245. }
  246. /**
  247. * @param code
  248. * @return void 返回类型
  249. * @throws BusiException
  250. * @Title: checkInput
  251. * @Description: 查询校验code
  252. */
  253. public void checkInput(String code) throws BusiException {
  254. if (CheckUtil.checkStringIsNull(code)) {
  255. throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.APP_CODE_NULL);
  256. }
  257. Matcher checkMatcher = CheckUtil.checkMatcher(code);
  258. if (checkMatcher.find() == false) {
  259. throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.CHECKMATCHER);
  260. }
  261. }
  262. /**
  263. * 新增前数据校验
  264. *
  265. * @param appModels
  266. * @throws BusiException
  267. */
  268. public void checkInput(List<App> appModels) throws BusiException {
  269. for (App appModel : appModels) {
  270. if (CheckUtil.checkStringIsNull(appModel.getCode())) {
  271. throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.APP_CODE_NULL+appModel.getCode());
  272. }
  273. if (CheckUtil.checkStringIsNull(appModel.getName())) {
  274. throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.APP_NAME_NULL+appModel.getName());
  275. }
  276. Matcher checkMatcher = CheckUtil.checkMatcher(appModel.getCode());
  277. if (checkMatcher.find() == false) {
  278. throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.CHECKMATCHER+appModel.getCode());
  279. }
  280. if (CheckUtil.characterFilter(appModel.getName())) {
  281. throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.APP_NAME_CHECKMATCHER+appModel.getName());
  282. }
  283. }
  284. }
  285. /**
  286. * @param appModel
  287. * @return App 返回类型
  288. * @Title: getStringNoBlank
  289. * @Description: 校验前后空格
  290. */
  291. public List<App> getStringNoBlank(List<App> appModel) {
  292. for (App app : appModel) {
  293. String code = null;
  294. String name = null;
  295. if (app.getCode() != null && !"".equals(app.getCode())) {
  296. Matcher m = CheckUtil.checkNoBlank(app.getCode());
  297. code = m.replaceAll("");
  298. }
  299. if (app.getName() != null && !"".equals(app.getName())) {
  300. Matcher m = CheckUtil.checkNoBlank(app.getName());
  301. name = m.replaceAll("");
  302. }
  303. app.setCode(code);
  304. app.setName(name);
  305. }
  306. return appModel;
  307. }
  308. /**
  309. * @param appModel
  310. * @throws BusiException
  311. * @Title: checkInputUp
  312. * @Description: 修改时非空校验
  313. */
  314. public static void checkInputUp(List<App> appModel) throws BusiException {
  315. for (App app : appModel) {
  316. if (CheckUtil.checkStringIsNull(app.getName())) {
  317. throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.APP_NAME_NULL);
  318. }
  319. if (CheckUtil.characterFilter(app.getName())) {
  320. throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.APP_NAME_CHECKMATCHER);
  321. }
  322. }
  323. }
  324. /**
  325. * @return
  326. * @Title: checkCodeList
  327. * @Description: 校验codeList
  328. */
  329. public static String checkCodeList(String codeList) throws BusiException {
  330. String resultCodeList = "";
  331. String[] strs = codeList.split(",");
  332. for (int i = 0; i < strs.length; i++) {
  333. // 空格校验
  334. String codeLists = strs[i].trim();
  335. // 非空校验
  336. boolean checkStringIsNull = CheckUtil.checkStringIsNull(codeLists);
  337. if (checkStringIsNull) {
  338. continue;
  339. }
  340. // 特殊字符判断
  341. if (CheckUtil.characterFilter(codeLists)) {
  342. continue;
  343. }
  344. resultCodeList += codeLists;
  345. if (i < strs.length - 1) {
  346. resultCodeList = resultCodeList + ",";
  347. }
  348. }
  349. return resultCodeList;
  350. }
  351. /**
  352. * @Title: checkInput
  353. */
  354. public static String checkInput(String skip, String top, String codeList) throws BusiException {
  355. String checkCodeList = null;
  356. // 特殊字符判断
  357. if (CheckUtil.checkStringIsNotNull(top) && CheckUtil.characterFilter(top)) {
  358. throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.TOP);
  359. }
  360. if (CheckUtil.checkStringIsNotNull(top) && !CheckUtil.checkDigit(top)) {
  361. throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.TOP_HIGHER);
  362. }
  363. if (CheckUtil.checkStringIsNotNull(top) && Integer.parseInt(top) == 0) {
  364. throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.TOP);
  365. }
  366. // 特殊字符判断
  367. if (CheckUtil.checkStringIsNotNull(skip) && CheckUtil.characterFilter(skip)) {
  368. throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.SKIP);
  369. }
  370. if (CheckUtil.checkStringIsNotNull(skip) && !CheckUtil.checkDigit(skip)) {
  371. throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.SKIP);
  372. }
  373. if (CheckUtil.checkStringIsNotNull(codeList)) {
  374. //校验codeList
  375. checkCodeList = checkCodeList(codeList);
  376. }
  377. return checkCodeList;
  378. }
  379. }