Chart.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package com.chart;
  2. import java.lang.reflect.Method;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import org.apache.log4j.Logger;
  9. import com.chart.common.ExtendJdbcHelp;
  10. import com.chart.vo.ChartData;
  11. import com.chart.vo.QueryVo;
  12. import com.formaction.vo.RowSet;
  13. import com.sysmodel.datamodel.xmlmodel.ModelFactory;
  14. import com.sysmodel.datamodel.xmlmodel.Reference;
  15. import com.sysmodel.datamodel.xmlmodel.able.MdpAttribute;
  16. import com.sysmodel.datamodel.xmlmodel.able.MdpClass;
  17. import com.sysmodel.datamodel.xmlmodel.able.MdpConstant;
  18. import com.sysmodel.datamodel.xmlmodel.able.SysModel;
  19. /**
  20. * 统计分析图查询的主类
  21. *
  22. * @author peterkong
  23. *
  24. */
  25. public abstract class Chart{
  26. public final static int PIE = 1;
  27. public final static int LINE = 2;
  28. public final static int BAR = 3;
  29. private static SysModel sysmodel = ModelFactory.getSysmodel();
  30. private final static Logger log = Logger.getLogger(ExtendJdbcHelp.class);
  31. private final static String params[] = { "TypeChart", "FieldXAxis", "classid", "StartTime",
  32. "EndTime", "FieldTime" };
  33. /**
  34. * 返回给前台的JSON数据,在子类中实现自己的数据格式
  35. *
  36. * @return
  37. */
  38. public abstract List<ChartData> getJsonData();
  39. /**
  40. * 查询数据同一方法,返回数据格式未定
  41. *
  42. * @param classid
  43. * @param propertyName
  44. * 统计字段的名称
  45. * @return
  46. */
  47. public List<RowSet> queryData(QueryVo extParam) {
  48. int classId = Integer.valueOf(extParam.getClassid());
  49. String typeChart = extParam.getTypeChart();
  50. ExtendJdbcHelp ejh = new ExtendJdbcHelp(classId);
  51. MdpClass mdpClass = sysmodel.getMdpClassByClassID(classId);
  52. String tableName = mdpClass.getName();
  53. List<RowSet> rowSetList = new ArrayList<RowSet>();
  54. String businessWhere = "";
  55. String dateWhere = "";
  56. if (typeChart.equals(String.valueOf(Chart.PIE))
  57. || typeChart.equals(String.valueOf(Chart.BAR))) {
  58. // List<MdpAttributeImpl> attributeList =
  59. // mdpClass.getAllMdpAttributes();
  60. if (extParam.getFieldXAxis() != null) {
  61. businessWhere = ejh.assemblyWhereSql(extParam.getFieldXAxis(),
  62. new ArrayList<String>(extParam.getTranferConstant().keySet()));
  63. }
  64. if (extParam.getFieldTime() != null) {
  65. dateWhere = ejh.assemblyWhereDate(extParam.getFieldTime(), extParam.getStartTime(),
  66. extParam.getEndTime());
  67. }
  68. String executeSql = ejh.assemblyQuerySql(tableName, extParam.getFieldXAxis(),
  69. businessWhere, dateWhere);
  70. log.info("执行统计语句:" + executeSql);
  71. rowSetList.add(ejh.queryData(executeSql));// 执行SQL语句返回具体内容
  72. } else if (typeChart.equals(String.valueOf(Chart.LINE))) {
  73. }
  74. return rowSetList;
  75. }
  76. private static QueryVo initConstant(QueryVo vo) {
  77. int classid = Integer.valueOf(vo.getClassid());
  78. MdpClass mdpClass = sysmodel.getMdpClassByClassID(classid);
  79. String referenceTable = null;
  80. MdpAttribute atttrbute = mdpClass.getMdpAttributeByName(vo.getFieldXAxis());// getxAxis是作为饼图的属性字段
  81. Reference reference = atttrbute.getReference();// 取得子集
  82. referenceTable = reference.getReferenceTable();// 获取相关联的常量表
  83. MdpConstant mdpConstant = sysmodel.getMdpConstantByName(referenceTable);// 读取某一常量的具体数据
  84. List<String[]> nodeList = mdpConstant.getAllNode();// 这是全部的常量值
  85. Map<String, String> transConstant = new HashMap<String, String>();
  86. for (String[] ConstantValue : nodeList) {
  87. transConstant.put(ConstantValue[0], ConstantValue[1]);
  88. }
  89. vo.setTranferConstant(transConstant);
  90. return vo;
  91. }
  92. /**
  93. * 解析前台的参数,并转成QueryVo对象 解析参数需要注意的是,前台的参数名称需要和VO中的名称相等,这样才能利用反射实现动态读取
  94. *
  95. * @param propertyName
  96. * @return
  97. */
  98. @SuppressWarnings({ "unchecked", "rawtypes" })
  99. public static QueryVo paraseParam(String propertyName) {
  100. QueryVo qvReturn = new QueryVo();
  101. String[] splitParam = propertyName.split("&");
  102. int splitValue = -1;
  103. String tmpName = null;
  104. String tmpValue = null;
  105. List<String> p = Arrays.asList(params);
  106. try {
  107. Class c = Class.forName("com.chart.vo.QueryVo");
  108. Object obj = c.newInstance();
  109. Method m = null;
  110. for (String value : splitParam) {
  111. splitValue = value.indexOf("=");
  112. tmpName = value.substring(0, splitValue);
  113. tmpValue = value.substring(splitValue + 1);
  114. if (p.contains(tmpName)) {
  115. m = c.getMethod(getGetMethod(tmpName),
  116. new Class[] { Class.forName("java.lang.String") });
  117. m.invoke(obj, new Object[] { tmpValue });
  118. }
  119. }
  120. qvReturn = (QueryVo) obj;
  121. qvReturn = initConstant(qvReturn);
  122. } catch (Exception e) {
  123. log.error(e);
  124. }
  125. return qvReturn;
  126. }
  127. private static String getGetMethod(String proName) {
  128. proName = "set" + proName.substring(0, 1).toUpperCase() + proName.substring(1);
  129. return proName;
  130. }
  131. }