package com.planarChart; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import com.formaction.Utils; import com.persistence.service.PersistenceFactory; import com.persistence.service.SysPersistence; import com.persistence.service.exception.PersistenceException; import com.planarChart.vo.PlanarChartData; import com.planarChart.vo.QueryPlanarVo; 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; public abstract class PlanarChart{ public final static int PIE = 1; public final static int LINE = 2; public final static int BAR = 3; public final static String[] color = new String[] { "#4572A7", "#AA4643", "#89A54E", "#80699B", "#3D96AE", "#FFCC33", "#FFCC99", "#996633", "#DB843D", "#003333", "#99CC66", "#CCCC99", "#6699CC", "#330000", "#009966", "#00CC99", "#CCCCCC", "#99CC33", "#3399CC", "#66CC33", "#99CC99", "#999933", "#FFCC99", "#CCCC99", "#FF9933", "#6699CC", "#66CCCC", "#33CCCC", "#66CC66", "#6699FF", "#00CCCC", "#66FF66", "#FFCCFF", "#FFCC66", "#CCFF99", "#996666", "#6699CC", "#66CCCC", "#33CCCC", "#0099FF" }; private static SysModel sysmodel = ModelFactory.getSysmodel(); private final static Logger log = Logger.getLogger(PlanarChart.class); /** * 返回给前台的JSON数据,在子类中实现自己的数据格式 * * @return */ public abstract List getJsonData(); public static QueryPlanarVo getQueryVo(String params) { QueryPlanarVo queryVo = new QueryPlanarVo(); String condition = Utils.getParameter("condition", params); String xAxis = Utils.getParameter("xAxis", params); String yAxis = Utils.getParameter("yAxis", params) == null ? "" : Utils.getParameter( "yAxis", params); String type = Utils.getParameter("type", params); String classid = Utils.getParameter("classid", params); queryVo.setClassid(classid); queryVo.setCondition(condition); queryVo.setxAxis(xAxis); queryVo.setyAxis(yAxis); queryVo.setType(type); queryVo = initConstant(queryVo); return queryVo; } private static QueryPlanarVo initConstant(QueryPlanarVo vo) { int classid = Integer.parseInt(vo.getClassid()); MdpClass mdpClass = sysmodel.getMdpClassByClassID(classid); String referenceTable = null; // 设置一维属性值 MdpAttribute atttrbute = mdpClass.getMdpAttributeByName(vo.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.setxConstant(transConstant); /** * 设置二维属性值 二维属性值可能不选,也可能值为no, */ if (!"".equals(vo.getyAxis()) && !"no".equals(vo.getyAxis())) { MdpAttribute yatttrbute = mdpClass.getMdpAttributeByName(vo.getyAxis()); Reference yreference = yatttrbute.getReference();// 取得子集 referenceTable = yreference.getReferenceTable();// 获取相关联的常量表 MdpConstant ymdpConstant = sysmodel.getMdpConstantByName(referenceTable);// 读取某一常量的具体数据 List ynodeList = ymdpConstant.getAllNode();// 这是全部的常量值 Map ytransConstant = new HashMap(); for (String[] ConstantValue : ynodeList) { ytransConstant.put(ConstantValue[0], ConstantValue[1]); } vo.setyConstant(ytransConstant); } else { Map ytransConstant = new HashMap(); vo.setyConstant(ytransConstant); } // 设置饼图属性的颜色 Map colour = new HashMap(); int i = 0; for (String key : vo.getxConstant().keySet()) { if (i >= color.length) { i -= 8;// 这种情况很少出现 } colour.put(key, color[i]); i++; } vo.setColor(colour); return vo; } /** * 根据二维的一个条件值,查询一维的所有数据 * * @param key * : 二维的一个值 * @return */ public String getSQLCondition(String value, QueryPlanarVo queryVo) { int classId = Integer.valueOf(queryVo.getClassid()); SysModel sysmodel = ModelFactory.getSysmodel(); MdpClass mdpClass = sysmodel.getMdpClassByClassID(classId); String tableName = mdpClass.getName(); StringBuffer sql = new StringBuffer(); sql.append("select "); sql.append(queryVo.getxAxis()); sql.append(" from "); sql.append(tableName); sql.append(" where "); sql.append(queryVo.getCondition()); if (!"".equals(value)) { // 二维查询属性不为空 sql.append(" and "); sql.append(queryVo.getyAxis() + " = '" + value + "'"); sql.append(" and "); sql.append(queryVo.getxAxis() + " is not null"); } return sql.toString(); } /** * 取得一维常量中的数据 * * @return */ public ArrayList getXData(QueryPlanarVo queryVo) { ArrayList list = new ArrayList(); int classId = Integer.valueOf(queryVo.getClassid()); SysModel sysmodel = ModelFactory.getSysmodel(); SysPersistence persistence = PersistenceFactory.getInstance(sysmodel); MdpClass mdpClass = sysmodel.getMdpClassByClassID(classId); String tableName = mdpClass.getName(); String sql = "select distinct " + queryVo.getxAxis() + " from " + tableName + " where " + queryVo.getCondition() + " and " + queryVo.getxAxis() + " is not null"; log.info("取得一维常量中的数据sql:" + sql); try { ArrayList result = persistence.getSearchResult(classId, sql); for (String[] str : result) { list.add(str[0]); } } catch (PersistenceException e) { e.printStackTrace(); } Collections.sort(list); return list; } }