package com.chart; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import com.chart.common.ExtendJdbcHelp; import com.chart.vo.ChartData; import com.chart.vo.QueryVo; import com.formaction.vo.RowSet; import com.sysmodel.datamodel.xmlmodel.ModelFactory; import com.sysmodel.datamodel.xmlmodel.Reference; import com.sysmodel.datamodel.xmlmodel.able.MdpAttribute; import com.sysmodel.datamodel.xmlmodel.able.MdpClass; import com.sysmodel.datamodel.xmlmodel.able.MdpConstant; import com.sysmodel.datamodel.xmlmodel.able.SysModel; /** * 统计分析图查询的主类 * * @author peterkong * */ public abstract class Chart{ public final static int PIE = 1; public final static int LINE = 2; public final static int BAR = 3; private static SysModel sysmodel = ModelFactory.getSysmodel(); private final static Logger log = Logger.getLogger(ExtendJdbcHelp.class); private final static String params[] = { "TypeChart", "FieldXAxis", "classid", "StartTime", "EndTime", "FieldTime" }; /** * 返回给前台的JSON数据,在子类中实现自己的数据格式 * * @return */ public abstract List getJsonData(); /** * 查询数据同一方法,返回数据格式未定 * * @param classid * @param propertyName * 统计字段的名称 * @return */ public List queryData(QueryVo extParam) { int classId = Integer.valueOf(extParam.getClassid()); String typeChart = extParam.getTypeChart(); ExtendJdbcHelp ejh = new ExtendJdbcHelp(classId); MdpClass mdpClass = sysmodel.getMdpClassByClassID(classId); String tableName = mdpClass.getName(); List rowSetList = new ArrayList(); String businessWhere = ""; String dateWhere = ""; if (typeChart.equals(String.valueOf(Chart.PIE)) || typeChart.equals(String.valueOf(Chart.BAR))) { // List attributeList = // mdpClass.getAllMdpAttributes(); if (extParam.getFieldXAxis() != null) { businessWhere = ejh.assemblyWhereSql(extParam.getFieldXAxis(), new ArrayList(extParam.getTranferConstant().keySet())); } if (extParam.getFieldTime() != null) { dateWhere = ejh.assemblyWhereDate(extParam.getFieldTime(), extParam.getStartTime(), extParam.getEndTime()); } String executeSql = ejh.assemblyQuerySql(tableName, extParam.getFieldXAxis(), businessWhere, dateWhere); log.info("执行统计语句:" + executeSql); rowSetList.add(ejh.queryData(executeSql));// 执行SQL语句返回具体内容 } else if (typeChart.equals(String.valueOf(Chart.LINE))) { } return rowSetList; } private static QueryVo initConstant(QueryVo vo) { int classid = Integer.valueOf(vo.getClassid()); MdpClass mdpClass = sysmodel.getMdpClassByClassID(classid); String referenceTable = null; MdpAttribute atttrbute = mdpClass.getMdpAttributeByName(vo.getFieldXAxis());// getxAxis是作为饼图的属性字段 Reference reference = atttrbute.getReference();// 取得子集 referenceTable = reference.getReferenceTable();// 获取相关联的常量表 MdpConstant mdpConstant = sysmodel.getMdpConstantByName(referenceTable);// 读取某一常量的具体数据 List nodeList = mdpConstant.getAllNode();// 这是全部的常量值 Map transConstant = new HashMap(); for (String[] ConstantValue : nodeList) { transConstant.put(ConstantValue[0], ConstantValue[1]); } vo.setTranferConstant(transConstant); return vo; } /** * 解析前台的参数,并转成QueryVo对象 解析参数需要注意的是,前台的参数名称需要和VO中的名称相等,这样才能利用反射实现动态读取 * * @param propertyName * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static QueryVo paraseParam(String propertyName) { QueryVo qvReturn = new QueryVo(); String[] splitParam = propertyName.split("&"); int splitValue = -1; String tmpName = null; String tmpValue = null; List p = Arrays.asList(params); try { Class c = Class.forName("com.chart.vo.QueryVo"); Object obj = c.newInstance(); Method m = null; for (String value : splitParam) { splitValue = value.indexOf("="); tmpName = value.substring(0, splitValue); tmpValue = value.substring(splitValue + 1); if (p.contains(tmpName)) { m = c.getMethod(getGetMethod(tmpName), new Class[] { Class.forName("java.lang.String") }); m.invoke(obj, new Object[] { tmpValue }); } } qvReturn = (QueryVo) obj; qvReturn = initConstant(qvReturn); } catch (Exception e) { log.error(e); } return qvReturn; } private static String getGetMethod(String proName) { proName = "set" + proName.substring(0, 1).toUpperCase() + proName.substring(1); return proName; } }