PageRenderTime 746ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/jeecgframework/tag/core/easyui/TagUtil.java

https://gitlab.com/spiderworts/ab_weixin_cms
Java | 521 lines | 377 code | 26 blank | 118 comment | 92 complexity | 74020585a21f07bc3c3a833eca25aa2e MD5 | raw file
  1. package org.jeecgframework.tag.core.easyui;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.lang.reflect.Field;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import javax.servlet.http.HttpServletResponse;
  10. import org.codehaus.jackson.JsonGenerationException;
  11. import org.codehaus.jackson.map.JsonMappingException;
  12. import org.codehaus.jackson.map.ObjectMapper;
  13. import org.jeecgframework.core.common.hibernate.qbc.PageList;
  14. import org.jeecgframework.core.common.model.json.ComboBox;
  15. import org.jeecgframework.core.common.model.json.DataGrid;
  16. import org.jeecgframework.core.util.ReflectHelper;
  17. import org.jeecgframework.core.util.StringUtil;
  18. import org.jeecgframework.core.util.oConvertUtils;
  19. import org.jeecgframework.tag.vo.datatable.DataTableReturn;
  20. import org.jeecgframework.tag.vo.easyui.Autocomplete;
  21. import org.jeecgframework.web.system.pojo.base.TSRole;
  22. import com.alibaba.fastjson.JSONObject;
  23. /**
  24. *
  25. * 类描述:标签工具类
  26. *
  27. * @author: 张代浩
  28. * @date: 日期:2012-12-28 时间:上午09:58:00
  29. * @version 1.1
  30. * @author liuht 修改不能输入双引号问题解决
  31. */
  32. public class TagUtil {
  33. /**
  34. * <b>Summary: </b> getFiled(获得实体Bean中所有属性)
  35. *
  36. * @param objClass
  37. * @return
  38. * @throws ClassNotFoundException
  39. */
  40. public static Field[] getFiled(Class<?> objClass) throws ClassNotFoundException {
  41. Field[] field = null;
  42. if (objClass != null) {
  43. Class<?> class1 = Class.forName(objClass.getName());
  44. field = class1.getDeclaredFields();// 这里便是获得实体Bean中所有属性的方法
  45. return field;
  46. } else {
  47. return field;
  48. }
  49. }
  50. /**
  51. *
  52. * 获取对象内对应字段的值
  53. * @param fields
  54. */
  55. public static Object fieldNametoValues(String FiledName, Object o){
  56. Object value = "";
  57. String fieldName = "";
  58. String childFieldName = null;
  59. ReflectHelper reflectHelper=new ReflectHelper(o);
  60. if (FiledName.indexOf("_") == -1) {
  61. if(FiledName.indexOf(".") == -1){
  62. fieldName = FiledName;
  63. }else{
  64. fieldName = FiledName.substring(0, FiledName.indexOf("."));//外键字段引用名
  65. childFieldName = FiledName.substring(FiledName.indexOf(".") + 1);//外键字段名
  66. }
  67. } else {
  68. fieldName = FiledName.substring(0, FiledName.indexOf("_"));//外键字段引用名
  69. childFieldName = FiledName.substring(FiledName.indexOf("_") + 1);//外键字段名
  70. }
  71. value = reflectHelper.getMethodValue(fieldName)==null?"":reflectHelper.getMethodValue(fieldName);
  72. if (value !=""&&value != null && (FiledName.indexOf("_") != -1||FiledName.indexOf(".") != -1)) {
  73. value = fieldNametoValues(childFieldName, value);
  74. }
  75. if(value != "" && value != null) {
  76. value = value.toString().replaceAll("\r\n", "");
  77. }
  78. return value;
  79. }
  80. /**
  81. * 对象转数组
  82. * @param fields
  83. * @param o
  84. * @param stack
  85. * @return
  86. * @throws Exception
  87. */
  88. protected static Object[] field2Values(String[] fields, Object o) throws Exception {
  89. Object[] values = new Object[fields.length];
  90. for (int i = 0; i < fields.length; i++) {
  91. String fieldName = fields[i].toString();
  92. values[i] = fieldNametoValues(fieldName, o);
  93. }
  94. return values;
  95. }
  96. /**
  97. * 循环LIST对象拼接EASYUI格式的JSON数据
  98. * @param fields
  99. * @param total
  100. * @param list
  101. */
  102. private static String listtojson(String[] fields, int total, List<?> list, String[] footers) throws Exception {
  103. Object[] values = new Object[fields.length];
  104. StringBuffer jsonTemp = new StringBuffer();
  105. jsonTemp.append("{\"total\":" + total + ",\"rows\":[");
  106. int i;
  107. String fieldName;
  108. for (int j = 0; j < list.size(); ++j) {
  109. jsonTemp.append("{\"state\":\"closed\",");
  110. for (i = 0; i < fields.length; ++i) {
  111. fieldName = fields[i].toString();
  112. if (list.get(j) instanceof Map)
  113. values[i] = ((Map<?, ?>) list.get(j)).get(fieldName);
  114. else {
  115. values[i] = fieldNametoValues(fieldName, list.get(j));
  116. }
  117. jsonTemp.append("\"" + fieldName + "\"" + ":\"" + String.valueOf(values[i]).replace("\"", "\\\"") + "\"");
  118. if (i != fields.length - 1) {
  119. jsonTemp.append(",");
  120. }
  121. }
  122. if (j != list.size() - 1)
  123. jsonTemp.append("},");
  124. else {
  125. jsonTemp.append("}");
  126. }
  127. }
  128. jsonTemp.append("]");
  129. if (footers != null) {
  130. jsonTemp.append(",");
  131. jsonTemp.append("\"footer\":[");
  132. jsonTemp.append("{");
  133. jsonTemp.append("\"name\":\"合计\",");
  134. for (String footer : footers) {
  135. String footerFiled = footer.split(":")[0];
  136. Object value = null;
  137. if (footer.split(":").length == 2)
  138. value = footer.split(":")[1];
  139. else {
  140. value = getTotalValue(footerFiled, list);
  141. }
  142. jsonTemp.append("\"" + footerFiled + "\":\"" + value + "\",");
  143. }
  144. if (jsonTemp.lastIndexOf(",") == jsonTemp.length()) {
  145. jsonTemp = jsonTemp.deleteCharAt(jsonTemp.length());
  146. }
  147. jsonTemp.append("}");
  148. jsonTemp.append("]");
  149. }
  150. jsonTemp.append("}");
  151. return jsonTemp.toString();
  152. }
  153. /**
  154. * 计算指定列的合计
  155. * @param filed 字段名
  156. * @param list 列表数据
  157. * @return
  158. */
  159. private static Object getTotalValue(String fieldName, List list) {
  160. Double sum = 0D;
  161. try {
  162. for (int j = 0; j < list.size(); j++) {
  163. Double v = 0d;
  164. String vstr = String.valueOf(fieldNametoValues(fieldName, list.get(j)));
  165. if (!StringUtil.isEmpty(vstr)) {
  166. v = Double.valueOf(vstr);
  167. } else {
  168. }
  169. sum += v;
  170. }
  171. } catch (Exception e) {
  172. return "";
  173. }
  174. return sum;
  175. }
  176. /**
  177. * 循环LIST对象拼接自动完成控件数据
  178. * @param fields
  179. * @param total
  180. * @param list
  181. * @throws Exception
  182. */
  183. public static String getAutoList(Autocomplete autocomplete, List list) throws Exception {
  184. String field = autocomplete.getLabelField() + "," + autocomplete.getValueField();
  185. String[] fields = field.split(",");
  186. Object[] values = new Object[fields.length];
  187. StringBuffer jsonTemp = new StringBuffer();
  188. jsonTemp.append("{\"totalResultsCount\":\"1\",\"geonames\":[");
  189. if (list.size() > 0) {
  190. for (int j = 0; j < list.size(); j++) {
  191. jsonTemp.append("{'nodate':'yes',");
  192. for (int i = 0; i < fields.length; i++) {
  193. String fieldName = fields[i].toString();
  194. values[i] = fieldNametoValues(fieldName, list.get(j));
  195. jsonTemp.append("\"").append(fieldName).append("\"").append(":\"").append(values[i]).append("\"");
  196. if (i != fields.length - 1) {
  197. jsonTemp.append(",");
  198. }
  199. }
  200. if (j != list.size() - 1) {
  201. jsonTemp.append("},");
  202. } else {
  203. jsonTemp.append("}");
  204. }
  205. }
  206. } else {
  207. jsonTemp.append("{'nodate':'数据不存在'}");
  208. }
  209. jsonTemp.append("]}");
  210. return JSONObject.toJSONString(jsonTemp).toString();
  211. }
  212. /**
  213. * 循环LIST对象拼接DATATABLE格式的JSON数据
  214. * @param fields
  215. * @param total
  216. * @param list
  217. */
  218. private static String datatable(String field, int total, List list) throws Exception {
  219. String[] fields = field.split(",");
  220. Object[] values = new Object[fields.length];
  221. StringBuffer jsonTemp = new StringBuffer();
  222. jsonTemp.append("{\"iTotalDisplayRecords\":" + total + ",\"iTotalRecords\":" + total + ",\"aaData\":[");
  223. for (int j = 0; j < list.size(); j++) {
  224. jsonTemp.append("{");
  225. for (int i = 0; i < fields.length; i++) {
  226. String fieldName = fields[i].toString();
  227. values[i] = fieldNametoValues(fieldName, list.get(j));
  228. jsonTemp.append("\"" + fieldName + "\"" + ":\"" + values[i] + "\"");
  229. if (i != fields.length - 1) {
  230. jsonTemp.append(",");
  231. }
  232. }
  233. if (j != list.size() - 1) {
  234. jsonTemp.append("},");
  235. } else {
  236. jsonTemp.append("}");
  237. }
  238. }
  239. jsonTemp.append("]}");
  240. return jsonTemp.toString();
  241. }
  242. /**
  243. * 返回列表JSONObject对象
  244. * @param field
  245. * @param dataGrid
  246. * @return
  247. */
  248. private static JSONObject getJson(DataGrid dg) {
  249. JSONObject jObject = null;
  250. try {
  251. if(!StringUtil.isEmpty(dg.getFooter())){
  252. jObject = JSONObject.parseObject(listtojson(dg.getField().split(","), dg.getTotal(), dg.getResults(),dg.getFooter().split(",")));
  253. }else{
  254. jObject = JSONObject.parseObject(listtojson(dg.getField().split(","), dg.getTotal(), dg.getResults(),null));
  255. }
  256. } catch (Exception e) {
  257. e.printStackTrace();
  258. }
  259. return jObject;
  260. }
  261. /**
  262. * 返回列表JSONObject对象
  263. * @param field
  264. * @param dataGrid
  265. * @return
  266. */
  267. private static JSONObject getJson(DataTableReturn dataTable,String field) {
  268. JSONObject jObject = null;
  269. try {
  270. jObject = JSONObject.parseObject(datatable(field, dataTable.getiTotalDisplayRecords(), dataTable.getAaData()));
  271. } catch (Exception e) {
  272. e.printStackTrace();
  273. }
  274. return jObject;
  275. }
  276. /**
  277. * 获取指定字段类型 <b>Summary: </b> getColumnType(请用一句话描述这个方法的作用)
  278. *
  279. * @param fileName
  280. * @param fields
  281. * @return
  282. */
  283. public static String getColumnType(String fileName, Field[] fields) {
  284. String type = "";
  285. if (fields.length > 0) {
  286. for (int i = 0; i < fields.length; i++) {
  287. String name = fields[i].getName(); // 获取属性的名字
  288. String filedType = fields[i].getGenericType().toString(); // 获取属性的类型
  289. if (fileName.equals(name)) {
  290. if (filedType.equals("class java.lang.Integer")) {
  291. filedType = "int";
  292. type = filedType;
  293. }else if (filedType.equals("class java.lang.Short")) {
  294. filedType = "short";
  295. type = filedType;
  296. }else if (filedType.equals("class java.lang.Double")) {
  297. filedType = "double";
  298. type = filedType;
  299. }else if (filedType.equals("class java.util.Date")) {
  300. filedType = "date";
  301. type = filedType;
  302. }else if (filedType.equals("class java.lang.String")) {
  303. filedType = "string";
  304. type = filedType;
  305. }else if (filedType.equals("class java.sql.Timestamp")) {
  306. filedType = "Timestamp";
  307. type = filedType;
  308. }else if (filedType.equals("class java.lang.Character")) {
  309. filedType = "character";
  310. type = filedType;
  311. }else if (filedType.equals("class java.lang.Boolean")) {
  312. filedType = "boolean";
  313. type = filedType;
  314. }else if (filedType.equals("class java.lang.Long")) {
  315. filedType = "long";
  316. type = filedType;
  317. }
  318. }
  319. }
  320. }
  321. return type;
  322. }
  323. /**
  324. *
  325. * <b>Summary: </b> getSortColumnIndex(获取指定字段索引)
  326. *
  327. * @param fileName
  328. * @param fieldString
  329. * @return
  330. */
  331. protected static String getSortColumnIndex(String fileName, String[] fieldString) {
  332. String index = "";
  333. if (fieldString.length > 0) {
  334. for (int i = 0; i < fieldString.length; i++) {
  335. if (fileName.equals(fieldString[i])) {
  336. int j = i + 1;
  337. index = oConvertUtils.getString(j);
  338. }
  339. }
  340. }
  341. return index;
  342. }
  343. // JSON返回页面MAP方式
  344. public static void ListtoView(HttpServletResponse response, PageList pageList) {
  345. response.setContentType("application/json");
  346. response.setHeader("Cache-Control", "no-store");
  347. Map<String, Object> map = new HashMap<String, Object>();
  348. map.put("total", pageList.getCount());
  349. map.put("rows", pageList.getResultList());
  350. ObjectMapper mapper = new ObjectMapper();
  351. // JSONObject jsonObject = JSONObject.fromObject(map);
  352. try {
  353. mapper.writeValue(response.getWriter(), map);
  354. } catch (JsonGenerationException e) {
  355. e.printStackTrace();
  356. } catch (JsonMappingException e) {
  357. e.printStackTrace();
  358. } catch (IOException e) {
  359. e.printStackTrace();
  360. }
  361. }
  362. /**
  363. * 控件类型:easyui
  364. * 返回datagrid JSON数据
  365. * @param response
  366. * @param dataGrid
  367. */
  368. public static void datagrid(HttpServletResponse response,DataGrid dg) {
  369. response.setContentType("application/json");
  370. response.setHeader("Cache-Control", "no-store");
  371. JSONObject object = TagUtil.getJson(dg);
  372. try {
  373. PrintWriter pw=response.getWriter();
  374. pw.write(object.toString());
  375. pw.flush();
  376. } catch (IOException e) {
  377. e.printStackTrace();
  378. }
  379. }
  380. /**
  381. * 控件类型:datatable
  382. * 返回datatable JSON数据
  383. * @param response
  384. * @param datatable
  385. */
  386. public static void datatable(HttpServletResponse response, DataTableReturn dataTableReturn,String field) {
  387. response.setContentType("application/json");
  388. response.setHeader("Cache-Control", "no-store");
  389. JSONObject object = TagUtil.getJson(dataTableReturn,field);
  390. try {
  391. response.getWriter().write(object.toString());
  392. } catch (IOException e) {
  393. e.printStackTrace();
  394. }
  395. }
  396. /**
  397. * 手工拼接JSON
  398. */
  399. public static String getComboBoxJson(List<TSRole> list, List<TSRole> roles) {
  400. StringBuffer buffer = new StringBuffer();
  401. buffer.append("[");
  402. for (TSRole node : list) {
  403. if (roles.size() > 0) {
  404. buffer.append("{\"id\":" + node.getId() + ",\"text\":\"" + node.getRoleName() + "\"");
  405. for (TSRole node1 : roles) {
  406. if (node.getId() == node1.getId()) {
  407. buffer.append(",\"selected\":true");
  408. }
  409. }
  410. buffer.append("},");
  411. } else {
  412. buffer.append("{\"id\":" + node.getId() + ",\"text\":\"" + node.getRoleName() + "\"},");
  413. }
  414. }
  415. buffer.append("]");
  416. // 将,\n]替换成\n]
  417. String tmp = buffer.toString();
  418. tmp = tmp.replaceAll(",]", "]");
  419. return tmp;
  420. }
  421. /**
  422. * 根据模型生成JSON
  423. * @param all 全部对象
  424. * @param in 已拥有的对象
  425. * @param comboBox 模型
  426. * @return
  427. */
  428. public static List<ComboBox> getComboBox(List all, List in, ComboBox comboBox) {
  429. List<ComboBox> comboxBoxs = new ArrayList<ComboBox>();
  430. String[] fields = new String[] { comboBox.getId(), comboBox.getText() };
  431. Object[] values = new Object[fields.length];
  432. for (Object node : all) {
  433. ComboBox box = new ComboBox();
  434. ReflectHelper reflectHelper=new ReflectHelper(node);
  435. for (int i = 0; i < fields.length; i++) {
  436. String fieldName = fields[i].toString();
  437. values[i] = reflectHelper.getMethodValue(fieldName);
  438. }
  439. box.setId(values[0].toString());
  440. box.setText(values[1].toString());
  441. if (in != null) {
  442. for (Object node1 : in) {
  443. ReflectHelper reflectHelper2=new ReflectHelper(node);
  444. if (node1 != null) {
  445. String fieldName = fields[0].toString();
  446. String test = reflectHelper2.getMethodValue(fieldName).toString();
  447. if (values[0].toString().equals(test)) {
  448. box.setSelected(true);
  449. }
  450. }
  451. }
  452. }
  453. comboxBoxs.add(box);
  454. }
  455. return comboxBoxs;
  456. }
  457. /**
  458. * 获取自定义函数名
  459. *
  460. * @param functionname
  461. * @return
  462. */
  463. public static String getFunction(String functionname) {
  464. int index = functionname.indexOf("(");
  465. if (index == -1) {
  466. return functionname;
  467. } else {
  468. return functionname.substring(0, functionname.indexOf("("));
  469. }
  470. }
  471. /**
  472. * 获取自定义函数的参数
  473. *
  474. * @param functionname
  475. * @return
  476. */
  477. public static String getFunParams(String functionname) {
  478. int index = functionname.indexOf("(");
  479. String param = "";
  480. if (index != -1) {
  481. String testparam = functionname.substring(
  482. functionname.indexOf("(") + 1, functionname.length() - 1);
  483. if (StringUtil.isNotEmpty(testparam)) {
  484. String[] params = testparam.split(",");
  485. for (String string : params) {
  486. param += (string.indexOf("{") != -1) ? ("'\"+"
  487. + string.substring(1, string.length() - 1) + "+\"',")
  488. : ("'\"+rec." + string + "+\"',");
  489. }
  490. }
  491. }
  492. param += "'\"+index+\"'";// 传出行索引号参数
  493. return param;
  494. }
  495. }